chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# InsForge Edge Function Examples
|
||||
|
||||
This folder contains **example serverless (edge) functions** you can deploy to InsForge.
|
||||
|
||||
## Files
|
||||
|
||||
- `demo-hello-world.js`: public function (GET/POST) with CORS + secret example (`HELLO_PREFIX`)
|
||||
- `demo-whoami.js`: authenticated function (GET) that returns the current user
|
||||
|
||||
## Deploy
|
||||
|
||||
Use the InsForge MCP tools:
|
||||
|
||||
- `create-function` with `slug` matching the function name you want (e.g. `demo-hello-world`)
|
||||
- `update-function` to redeploy after edits
|
||||
|
||||
## Invoke from a client app (SDK)
|
||||
|
||||
```js
|
||||
// GET
|
||||
await insforge.functions.invoke('demo-hello-world', { method: 'GET' })
|
||||
|
||||
// POST
|
||||
await insforge.functions.invoke('demo-hello-world', {
|
||||
body: { name: 'Gary' }
|
||||
})
|
||||
|
||||
// Authenticated GET (SDK auto-includes user token if logged in)
|
||||
await insforge.functions.invoke('demo-whoami', { method: 'GET' })
|
||||
```
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Demo: Public "hello world" function
|
||||
*
|
||||
* - Supports CORS (OPTIONS)
|
||||
* - GET: reads from query string (?name=...)
|
||||
* - POST: reads JSON body { name }
|
||||
* - Demonstrates reading secrets via Deno.env.get('KEY')
|
||||
*
|
||||
* Notes:
|
||||
* - `createClient` and `Deno` are injected by InsForge's worker template.
|
||||
* - Keep exports exactly: module.exports = async function(request) { ... }
|
||||
*/
|
||||
|
||||
module.exports = async function (request) {
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
};
|
||||
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, { status: 204, headers: corsHeaders });
|
||||
}
|
||||
|
||||
const url = new URL(request.url);
|
||||
|
||||
let body = null;
|
||||
if (request.method !== 'GET') {
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
body = null;
|
||||
}
|
||||
}
|
||||
|
||||
const name =
|
||||
(typeof body?.name === 'string' && body.name.trim()) ||
|
||||
(url.searchParams.get('name') || '').trim() ||
|
||||
'World';
|
||||
|
||||
// Example secret (configure in InsForge -> Functions -> Secrets):
|
||||
// - HELLO_PREFIX="Hello"
|
||||
const helloPrefix = Deno.env.get('HELLO_PREFIX') || 'Hello';
|
||||
|
||||
return new Response(
|
||||
JSON.stringify(
|
||||
{
|
||||
message: `${helloPrefix}, ${name}!`,
|
||||
timestamp: new Date().toISOString(),
|
||||
method: request.method,
|
||||
path: url.pathname,
|
||||
},
|
||||
null,
|
||||
2
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Demo: Authenticated "whoami" function
|
||||
*
|
||||
* - Requires Authorization: Bearer <user_jwt>
|
||||
* - Uses injected createClient() + edgeFunctionToken to call auth.getCurrentUser()
|
||||
* - Returns 401 if no valid user
|
||||
*/
|
||||
|
||||
module.exports = async function (request) {
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
};
|
||||
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, { status: 204, headers: corsHeaders });
|
||||
}
|
||||
|
||||
if (request.method !== 'GET') {
|
||||
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
||||
status: 405,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
const authHeader = request.headers.get('Authorization') || '';
|
||||
const userToken = authHeader.startsWith('Bearer ')
|
||||
? authHeader.slice('Bearer '.length).trim()
|
||||
: null;
|
||||
|
||||
if (!userToken) {
|
||||
return new Response(JSON.stringify({ error: 'Missing bearer token' }), {
|
||||
status: 401,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
const client = createClient({
|
||||
baseUrl: Deno.env.get('INSFORGE_INTERNAL_URL') || 'http://insforge:7130',
|
||||
edgeFunctionToken: userToken,
|
||||
});
|
||||
|
||||
const { data, error } = await client.auth.getCurrentUser();
|
||||
if (error || !data?.user?.id) {
|
||||
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
||||
status: 401,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify(
|
||||
{
|
||||
user: {
|
||||
id: data.user.id,
|
||||
email: data.user.email,
|
||||
created_at: data.user.created_at,
|
||||
},
|
||||
},
|
||||
null,
|
||||
2
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user