{"version":3,"file":"fastify.cjs","names":["frameworkName: SupportedFrameworkName","InngestCommHandler","fastifyPlugin: (\n  fastify: FastifyInstance,\n  options: InngestPluginOptions,\n  done: (err?: Error | undefined) => void,\n) => void"],"sources":["../src/fastify.ts"],"sourcesContent":["/**\n * An adapter for Fastify to serve and register any declared functions with\n * Inngest, making them available to be triggered by events.\n *\n * @example Plugin (recommended)\n * ```ts\n * import Fastify from \"fastify\";\n * import inngestFastify from \"inngest/fastify\";\n * import { inngest, fnA } from \"./inngest\";\n *\n * const fastify = Fastify();\n *\n * fastify.register(inngestFastify, {\n *   client: inngest,\n *   functions: [fnA],\n *   options: {},\n * });\n *\n * fastify.listen({ port: 3000 }, function (err, address) {\n *   if (err) {\n *     fastify.log.error(err);\n *     process.exit(1);\n *   }\n * });\n * ```\n *\n * @example Route\n * ```ts\n * import Fastify from \"fastify\";\n * import { serve } from \"inngest/fastify\";\n * import { fnA, inngest } from \"./inngest\";\n *\n * const fastify = Fastify();\n *\n * fastify.route({\n *   method: [\"GET\", \"POST\", \"PUT\"],\n *   handler: serve({ client: inngest, functions: [fnA] }),\n *   url: \"/api/inngest\",\n * });\n *\n * fastify.listen({ port: 3000 }, function (err, address) {\n *   if (err) {\n *     fastify.log.error(err);\n *     process.exit(1);\n *   }\n * });\n * ```\n *\n * @module\n */\n\nimport type {\n  FastifyInstance,\n  FastifyPluginCallback,\n  FastifyReply,\n  FastifyRequest,\n} from \"fastify\";\nimport type { Inngest } from \"./components/Inngest.ts\";\nimport {\n  InngestCommHandler,\n  type ServeHandlerOptions,\n} from \"./components/InngestCommHandler.ts\";\nimport type { InngestFunction } from \"./components/InngestFunction.ts\";\nimport type { RegisterOptions, SupportedFrameworkName } from \"./types.ts\";\n\n/**\n * The name of the framework, used to identify the framework in Inngest\n * dashboards and during testing.\n */\nexport const frameworkName: SupportedFrameworkName = \"fastify\";\n\ntype InngestPluginOptions = {\n  client: Inngest.Like;\n  functions: InngestFunction.Like[];\n  options?: RegisterOptions;\n};\n\n/**\n * Serve and register any declared functions with Inngest, making them available\n * to be triggered by events.\n *\n * It's recommended to use the Fastify plugin to serve your functions with\n * Inngest instead of using this `serve()` function directly.\n *\n * @example\n * ```ts\n * import Fastify from \"fastify\";\n * import { serve } from \"inngest/fastify\";\n * import { fnA, inngest } from \"./inngest\";\n *\n * const fastify = Fastify();\n *\n * fastify.route({\n *   method: [\"GET\", \"POST\", \"PUT\"],\n *   handler: serve({ client: inngest, functions: [fnA] }),\n *   url: \"/api/inngest\",\n * });\n *\n * fastify.listen({ port: 3000 }, function (err, address) {\n *   if (err) {\n *     fastify.log.error(err);\n *     process.exit(1);\n *   }\n * });\n * ```\n *\n * @public\n */\nexport const serve = (\n  options: ServeHandlerOptions,\n): ((\n  req: FastifyRequest<{ Querystring: Record<string, string | undefined> }>,\n  reply: FastifyReply,\n) => Promise<unknown>) => {\n  const handler = new InngestCommHandler({\n    frameworkName,\n    ...options,\n    handler: (\n      req: FastifyRequest<{ Querystring: Record<string, string | undefined> }>,\n      reply: FastifyReply,\n    ) => {\n      return {\n        body: () => req.body,\n        headers: (key) => {\n          const header = req.headers[key];\n          return Array.isArray(header) ? header[0] : header;\n        },\n        method: () => req.method,\n        url: () => {\n          const hostname = req.headers[\"host\"];\n          const protocol = hostname?.includes(\"://\")\n            ? \"\"\n            : `${req.protocol}://`;\n\n          const url = new URL(req.url, `${protocol}${hostname || \"\"}`);\n\n          return url;\n        },\n        queryString: (key) => req.query[key],\n        transformResponse: ({ body, status, headers }) => {\n          for (const [name, value] of Object.entries(headers)) {\n            void reply.header(name, value);\n          }\n          void reply.code(status);\n          return reply.send(body);\n        },\n      };\n    },\n  });\n\n  return handler.createHandler();\n};\n\n/**\n * Serve and register any declared functions with Inngest, making them available\n * to be triggered by events.\n *\n * @example\n * ```ts\n * import Fastify from \"fastify\";\n * import inngestFastify from \"inngest/fastify\";\n * import { inngest, fnA } from \"./inngest\";\n *\n * const fastify = Fastify();\n *\n * fastify.register(inngestFastify, {\n *   client: inngest,\n *   functions: [fnA],\n *   options: {},\n * });\n *\n * fastify.listen({ port: 3000 }, function (err, address) {\n *   if (err) {\n *     fastify.log.error(err);\n *     process.exit(1);\n *   }\n * });\n * ```\n *\n * @public\n */\nexport const fastifyPlugin: (\n  fastify: FastifyInstance,\n  options: InngestPluginOptions,\n  done: (err?: Error | undefined) => void,\n) => void = ((fastify, options, done): void => {\n  if (!options?.client) {\n    throw new Error(\n      \"Inngest `client` is required when serving with Fastify plugin\",\n    );\n  }\n\n  if (!options?.functions) {\n    throw new Error(\n      \"Inngest `functions` are required when serving with Fastify plugin\",\n    );\n  }\n\n  try {\n    const handler = serve({\n      client: options?.client,\n      functions: options?.functions,\n      ...options?.options,\n    });\n\n    fastify.route({\n      method: [\"GET\", \"POST\", \"PUT\"],\n      handler,\n      url: options?.options?.servePath || \"/api/inngest\",\n    });\n\n    done();\n  } catch (err) {\n    done(err as Error);\n  }\n}) satisfies FastifyPluginCallback<InngestPluginOptions>;\n\nexport default fastifyPlugin;\n"],"mappings":";;;;;;;;AAqEA,MAAaA,gBAAwC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCrD,MAAa,SACX,YAIwB;AAqCxB,QApCgB,IAAIC,8CAAmB;EACrC;EACA,GAAG;EACH,UACE,KACA,UACG;AACH,UAAO;IACL,YAAY,IAAI;IAChB,UAAU,QAAQ;KAChB,MAAM,SAAS,IAAI,QAAQ;AAC3B,YAAO,MAAM,QAAQ,OAAO,GAAG,OAAO,KAAK;;IAE7C,cAAc,IAAI;IAClB,WAAW;KACT,MAAM,WAAW,IAAI,QAAQ;KAC7B,MAAM,WAAW,UAAU,SAAS,MAAM,GACtC,KACA,GAAG,IAAI,SAAS;AAIpB,YAFY,IAAI,IAAI,IAAI,KAAK,GAAG,WAAW,YAAY,KAAK;;IAI9D,cAAc,QAAQ,IAAI,MAAM;IAChC,oBAAoB,EAAE,MAAM,QAAQ,cAAc;AAChD,UAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAA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