From 89d1013c455335cbcdfc7f8c61e008b49f69c7ff Mon Sep 17 00:00:00 2001 From: Jack Bridger Date: Thu, 13 Nov 2025 17:35:29 +0000 Subject: [PATCH] add auth session handler for nextjs --- README.md | 14 ++++- src/createAuthorizeSessionHandler.ts | 77 ++++++++++++++++++++++++++++ src/index.ts | 2 + 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/createAuthorizeSessionHandler.ts diff --git a/README.md b/README.md index e64a098..2dd2cb5 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,16 @@ npm install @layercode/node-server-sdk ## Usage -Read the docs: [https://docs.layercode.com/sdk-reference/node_js_sdk](https://docs.layercode.com/sdk-reference/node_js_sdk) +### Next.js authorize helper + +```ts +// app/api/authorize/route.ts +export const dynamic = 'force-dynamic'; +import { createAuthorizeSessionHandler } from '@layercode/node-server-sdk'; + +export const POST = createAuthorizeSessionHandler(); +``` + +The helper reads `LAYERCODE_API_KEY` from the environment and proxies the request to Layercode. You can override the key, endpoint, or transform the outgoing body if needed. + +For other use cases (webhook streaming, signature verification, etc.) read the docs: [https://docs.layercode.com/sdk-reference/node_js_sdk](https://docs.layercode.com/sdk-reference/node_js_sdk) diff --git a/src/createAuthorizeSessionHandler.ts b/src/createAuthorizeSessionHandler.ts new file mode 100644 index 0000000..6373b02 --- /dev/null +++ b/src/createAuthorizeSessionHandler.ts @@ -0,0 +1,77 @@ +const DEFAULT_AUTHORIZE_ENDPOINT = 'https://api.layercode.com/v1/agents/web/authorize_session'; + +export interface CreateAuthorizeSessionHandlerOptions { + /** + * Layercode API key. Defaults to process.env.LAYERCODE_API_KEY. + */ + apiKey?: string; + /** + * Override the Layercode authorize session endpoint (mainly for testing). + */ + endpoint?: string; + /** + * Allows you to transform the incoming body before forwarding it to Layercode. + */ + transformBody?: (body: any) => any; +} + +/** + * Creates a Next.js-compatible route handler that proxies authorize-session requests to Layercode. + * + * ```ts + * import { createAuthorizeSessionHandler } from 'layercode-node-server-sdk'; + * + * export const dynamic = 'force-dynamic'; + * export const POST = createAuthorizeSessionHandler(); + * ``` + */ +export const createAuthorizeSessionHandler = (options: CreateAuthorizeSessionHandlerOptions = {}) => { + const endpoint = options.endpoint ?? DEFAULT_AUTHORIZE_ENDPOINT; + + return async function authorizeSessionRoute(request: Request): Promise { + try { + const apiKey = options.apiKey ?? process.env.LAYERCODE_API_KEY; + if (!apiKey) { + throw new Error('LAYERCODE_API_KEY is not set.'); + } + + const incomingBody = await request.json(); + if (!incomingBody?.agent_id) { + throw new Error('Missing agent_id in request body.'); + } + + const payload = options.transformBody ? options.transformBody(incomingBody) : incomingBody; + + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify(payload), + }); + + if (!response.ok) { + const text = await response.text(); + throw new Error(text || response.statusText); + } + + const json = await response.json(); + return new Response(JSON.stringify(json), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + } catch (error) { + const message = error instanceof Error ? error.message : 'Unknown error'; + console.error('Layercode authorize session response error:', message); + return new Response(JSON.stringify({ error: message }), { + status: 500, + headers: { + 'Content-Type': 'application/json', + }, + }); + } + }; +}; diff --git a/src/index.ts b/src/index.ts index 38af640..6527ebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,4 @@ export { streamResponse } from './streamResponse'; export { verifySignature } from './verifySignature'; +export { createAuthorizeSessionHandler } from './createAuthorizeSessionHandler'; +export type { CreateAuthorizeSessionHandlerOptions } from './createAuthorizeSessionHandler';