forked from inngest/inngest-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.ts
189 lines (172 loc) · 5.54 KB
/
next.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { type NextApiRequest, type NextApiResponse } from "next";
import { type NextRequest } from "next/server";
import {
InngestCommHandler,
type ServeHandler,
} from "./components/InngestCommHandler";
import { headerKeys, queryKeys } from "./helpers/consts";
import { type SupportedFrameworkName } from "./types";
export const name: SupportedFrameworkName = "nextjs";
const isNextEdgeRequest = (
req: NextApiRequest | NextRequest
): req is NextRequest => {
return typeof req?.headers?.get === "function";
};
/**
* In Next.js, serve and register any declared functions with Inngest, making
* them available to be triggered by events.
*
* @example Next.js <=12 can export the handler directly
* ```ts
* export default serve(inngest, [fn1, fn2]);
* ```
*
* @example Next.js >=13 with the `app` dir must export individual methods
* ```ts
* export const { GET, POST, PUT } = serve(inngest, [fn1, fn2]);
* ```
*
* @public
*/
export const serve: ServeHandler = (nameOrInngest, fns, opts) => {
const handler = new InngestCommHandler(
name,
nameOrInngest,
fns,
opts,
(
reqMethod: "GET" | "POST" | "PUT" | undefined,
req: NextApiRequest | NextRequest,
_res: NextApiResponse
) => {
/**
* `req.method`, though types say otherwise, is not available in Next.js
* 13 {@link https://beta.nextjs.org/docs/routing/route-handlers Route Handlers}.
*
* Therefore, we must try to set the method ourselves where we know it.
*/
const method = reqMethod || req.method;
if (!method) {
// TODO PrettyError
throw new Error(
"No method found on request; check that your exports are correct."
);
}
const isEdge = isNextEdgeRequest(req);
let scheme: "http" | "https" = "https";
try {
// eslint-disable-next-line @inngest/process-warn
if (process.env.NODE_ENV === "development") {
scheme = "http";
}
} catch (err) {
// no-op
}
const url = isEdge
? new URL(req.url)
: new URL(req.url as string, `${scheme}://${req.headers.host || ""}`);
const getQueryParam = (key: string): string | undefined => {
return (
(isEdge ? url.searchParams.get(key) : req.query[key]?.toString()) ??
undefined
);
};
const hasQueryParam = (key: string): boolean => {
return (
(isEdge
? url.searchParams.has(key)
: Object.hasOwnProperty.call(req.query, key)) ?? false
);
};
const getHeader = (key: string): string | undefined => {
return (
(isEdge ? req.headers.get(key) : req.headers[key]?.toString()) ??
undefined
);
};
/**
* Vercel Edge Functions do not allow dynamic access to environment
* variables, so we'll manage `isProd` directly here.
*
* We try/catch to avoid situations where Next.js is being used in
* environments where `process.env` is not accessible or polyfilled.
*/
let isProduction: boolean | undefined;
try {
// eslint-disable-next-line @inngest/process-warn
isProduction = process.env.NODE_ENV === "production";
} catch (err) {
// no-op
}
return {
isProduction,
url,
register: () => {
if (method === "PUT") {
return {
deployId: getQueryParam(queryKeys.DeployId)?.toString(),
};
}
},
run: async () => {
if (method === "POST") {
return {
data: isEdge
? ((await req.json()) as Record<string, unknown>)
: (req.body as Record<string, unknown>),
fnId: getQueryParam(queryKeys.FnId) as string,
stepId: getQueryParam(queryKeys.StepId) as string,
signature: getHeader(headerKeys.Signature) as string,
};
}
},
view: () => {
if (method === "GET") {
return {
isIntrospection: hasQueryParam(queryKeys.Introspect),
};
}
},
};
},
({ body, headers, status }, _method, req, res) => {
if (isNextEdgeRequest(req)) {
return new Response(body, { status, headers });
}
for (const [key, value] of Object.entries(headers)) {
res.setHeader(key, value);
}
res.status(status).send(body);
},
({ body, headers, status }) => {
return new Response(body, { status, headers });
}
);
/**
* Next.js 13 uses
* {@link https://beta.nextjs.org/docs/routing/route-handlers Route Handlers}
* to declare API routes instead of a generic catch-all method that was
* available using the `pages/api` directory.
*
* This means that users must now export a function for each method supported
* by the endpoint. For us, this means requiring a user explicitly exports
* `GET`, `POST`, and `PUT` functions.
*
* Because of this, we'll add circular references to those property names of
* the returned handler, meaning we can write some succinct code to export
* them. Thanks, @goodoldneon.
*
* @example
* ```ts
* export const { GET, POST, PUT } = serve(...);
* ```
*
* See {@link https://beta.nextjs.org/docs/routing/route-handlers}
*/
const fn = handler.createHandler();
return Object.defineProperties(fn.bind(null, undefined), {
GET: { value: fn.bind(null, "GET") },
POST: { value: fn.bind(null, "POST") },
PUT: { value: fn.bind(null, "PUT") },
});
};