UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

132 lines (130 loc) 4 kB
import { createWebApiCommHandler } from "./components/createWebApiCommHandler.js"; import { handleDurableEndpointProxyRequest } from "./components/InngestDurableEndpointProxy.js"; import { InngestEndpointAdapter } from "./components/InngestEndpointAdapter.js"; //#region src/edge.ts /** * An adapter for any request that handles standard Web APIs such as `fetch`, * `Request,` and `Response` to serve and register any declared functions with * Inngest, making them available to be triggered by events. * * This is reused by many other adapters, but can be used directly. * * @example * ```ts * import { serve } from "inngest/edge"; * import functions from "~/inngest"; * * export const handler = serve({ id: "my-edge-app", functions }); * ``` * * @module */ /** * The name of the framework, used to identify the framework in Inngest * dashboards and during testing. */ const frameworkName = "edge"; const commHandler = (options, syncOptions) => { return createWebApiCommHandler(frameworkName, options, syncOptions); }; /** * In an edge runtime, serve and register any declared functions with Inngest, * making them available to be triggered by events. * * The edge runtime is a generic term for any serverless runtime that supports * only standard Web APIs such as `fetch`, `Request`, and `Response`, such as * Cloudflare Workers, Vercel Edge Functions, and AWS Lambda@Edge. * * @example * ```ts * import { serve } from "inngest/edge"; * import functions from "~/inngest"; * * export const handler = serve({ id: "my-edge-app", functions }); * ``` * * @public */ const serve = (options) => { return commHandler(options).createHandler(); }; /** * Creates a durable endpoint proxy handler for edge environments. * * This handler extracts `runId` and `token` from query parameters, * fetches the run output from Inngest, decrypts it via middleware * (if configured), and returns it with CORS headers. */ const createDurableEndpointProxyHandler = (options) => { return async (req) => { const url = new URL(req.url); const result = await handleDurableEndpointProxyRequest(options.client, { runId: url.searchParams.get("runId"), token: url.searchParams.get("token"), method: req.method }); return new Response(result.body, { status: result.status, headers: result.headers }); }; }; /** * In an edge runtime, create a function that can wrap any endpoint to be able * to use steps seamlessly within that API. * * The edge runtime is a generic term for any serverless runtime that supports * only standard Web APIs such as `fetch`, `Request`, and `Response`, such as * Cloudflare Workers, Vercel Edge Functions, and AWS Lambda@Edge. * * @example * ```ts * import { Inngest, step } from "inngest"; * import { endpointAdapter } from "inngest/edge"; * * const inngest = new Inngest({ * id: "my-app", * endpointAdapter, * }); * * Bun.serve({ * routes: { * "/": inngest.endpoint(async (req) => { * const foo = await step.run("my-step", () => ({ foo: "bar" })); * * return new Response(`Result: ${JSON.stringify(foo)}`); * }), * }, * }); * ``` * * You can also configure a custom redirect URL and create a proxy endpoint: * * @example * ```ts * import { Inngest } from "inngest"; * import { endpointAdapter } from "inngest/edge"; * * const inngest = new Inngest({ * id: "my-app", * endpointAdapter: endpointAdapter.withOptions({ * asyncRedirectUrl: "/api/inngest/poll", * }), * }); * * // Your durable endpoint * export const GET = inngest.endpoint(async (req) => { * const result = await step.run("work", () => "done"); * return new Response(result); * }); * * // Proxy endpoint at /api/inngest/poll - handles CORS and decryption * export const GET = inngest.endpointProxy(); * ``` */ const endpointAdapter = InngestEndpointAdapter.create((options) => { return commHandler(options, options).createSyncHandler(); }, createDurableEndpointProxyHandler); //#endregion export { endpointAdapter, frameworkName, serve }; //# sourceMappingURL=edge.js.map