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.
1 lines • 5.07 kB
Source Map (JSON)
{"version":3,"file":"Fetch.cjs","names":["stepFetch: StepFetch","fetch: Fetch","getAsyncCtx","gatewaySymbol","optionsRef: StepFetch.Options","extras: StepFetch.Extras","fetch"],"sources":["../../src/components/Fetch.ts"],"sourcesContent":["import Debug from \"debug\";\nimport type { Simplify } from \"../helpers/types.ts\";\nimport { getAsyncCtx } from \"./execution/als.ts\";\nimport { gatewaySymbol, type InternalStepTools } from \"./InngestStepTools.ts\";\n\nconst globalFetch = globalThis.fetch;\ntype Fetch = typeof globalFetch;\n\nexport type StepFetch = Fetch &\n Simplify<\n {\n config: (options: StepFetch.Options) => StepFetch;\n } & Readonly<StepFetch.Options>\n >;\n\nexport namespace StepFetch {\n export interface Options {\n fallback?: Fetch | undefined;\n }\n\n export interface Extras extends Options {\n config: (options: Options) => StepFetch;\n }\n}\n\nconst devDebug = Debug(\"inngest:fetch\");\n\nconst createFetchShim = (): StepFetch => {\n // biome-ignore lint/style/useConst: need this to allow fns to be defined\n let stepFetch: StepFetch;\n\n const fetch: Fetch = async (input, init) => {\n const ctx = await getAsyncCtx();\n if (!ctx?.execution) {\n // Not in a function run\n if (!stepFetch.fallback) {\n // TODO Tell the user how to solve\n throw new Error(\n \"step.fetch() called outside of a function and had no fallback set\",\n );\n }\n\n devDebug(\n \"step.fetch() called outside of a function; falling back to global fetch\",\n );\n\n return stepFetch.fallback(input, init);\n }\n\n // In a function run\n if (ctx.execution.executingStep) {\n // Inside a step\n if (!stepFetch.fallback) {\n // TODO Tell the user how to solve\n throw new Error(\n `step.fetch() called inside step \"${ctx.execution.executingStep.id}\" and had no fallback set`,\n );\n }\n\n devDebug(\n `step.fetch() called inside step \"${ctx.execution.executingStep.id}\"; falling back to global fetch`,\n );\n\n return stepFetch.fallback(input, init);\n }\n\n // TODO Do we need to make this better with deferred (global) step tooling?\n // hmmmmm\n\n const targetUrl = new URL(\n input instanceof Request ? input.url : input.toString(),\n );\n\n devDebug(\"step.fetch() shimming request to\", targetUrl.hostname);\n\n // Purposefully do not try/cacth this; if it throws then we treat that as a\n // regular `fetch()` throw, which also would not return a `Response`.\n const jsonRes = await (ctx.execution.ctx.step as InternalStepTools)[\n gatewaySymbol\n ](`step.fetch: ${targetUrl.hostname}`, input, init);\n\n return new Response(jsonRes.body, {\n headers: jsonRes.headers,\n status: jsonRes.status_code,\n });\n };\n\n const optionsRef: StepFetch.Options = {\n fallback: globalFetch,\n };\n\n const extras: StepFetch.Extras = {\n config: (options) => {\n Object.assign(optionsRef, options);\n Object.assign(stepFetch, optionsRef);\n\n return stepFetch;\n },\n ...optionsRef,\n };\n\n stepFetch = Object.assign(fetch, extras);\n\n return stepFetch;\n};\n\n/**\n * `fetch` is a Fetch API-compatible function that can be used to make any HTTP\n * code durable if it's called within an Inngest function.\n *\n * It will gracefully fall back to the global `fetch` if called outside of this\n * context, and a custom fallback can be set using the `config` method.\n *\n * @example Basic usage\n * ```ts\n * import { fetch } from \"inngest\";\n *\n * const api = new MyProductApi({ fetch });\n * ```\n *\n * @example Setting a custom fallback\n * ```ts\n * import { fetch } from \"inngest\";\n *\n * const api = new MyProductApi({\n * fetch: fetch.config({ fallback: myCustomFetch }),\n * });\n * ```\n *\n * @example Do not allow fallback\n * ```ts\n * import { fetch } from \"inngest\";\n *\n * const api = new MyProductApi({\n * fetch: fetch.config({ fallback: undefined }),\n * });\n * ```\n */\nexport const fetch = createFetchShim();\n"],"mappings":";;;;;;;AAKA,MAAM,cAAc,WAAW;AAoB/B,MAAM,8BAAiB,gBAAgB;AAEvC,MAAM,wBAAmC;CAEvC,IAAIA;CAEJ,MAAMC,UAAe,OAAO,OAAO,SAAS;EAC1C,MAAM,MAAM,MAAMC,yBAAa;AAC/B,MAAI,CAAC,KAAK,WAAW;AAEnB,OAAI,CAAC,UAAU,SAEb,OAAM,IAAI,MACR,oEACD;AAGH,YACE,0EACD;AAED,UAAO,UAAU,SAAS,OAAO,KAAK;;AAIxC,MAAI,IAAI,UAAU,eAAe;AAE/B,OAAI,CAAC,UAAU,SAEb,OAAM,IAAI,MACR,oCAAoC,IAAI,UAAU,cAAc,GAAG,2BACpE;AAGH,YACE,oCAAoC,IAAI,UAAU,cAAc,GAAG,iCACpE;AAED,UAAO,UAAU,SAAS,OAAO,KAAK;;EAMxC,MAAM,YAAY,IAAI,IACpB,iBAAiB,UAAU,MAAM,MAAM,MAAM,UAAU,CACxD;AAED,WAAS,oCAAoC,UAAU,SAAS;EAIhE,MAAM,UAAU,MAAO,IAAI,UAAU,IAAI,KACvCC,wCACA,eAAe,UAAU,YAAY,OAAO,KAAK;AAEnD,SAAO,IAAI,SAAS,QAAQ,MAAM;GAChC,SAAS,QAAQ;GACjB,QAAQ,QAAQ;GACjB,CAAC;;CAGJ,MAAMC,aAAgC,EACpC,UAAU,aACX;CAED,MAAMC,SAA2B;EAC/B,SAAS,YAAY;AACnB,UAAO,OAAO,YAAY,QAAQ;AAClC,UAAO,OAAO,WAAW,WAAW;AAEpC,UAAO;;EAET,GAAG;EACJ;AAED,aAAY,OAAO,OAAOC,SAAO,OAAO;AAExC,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCT,MAAa,QAAQ,iBAAiB"}