forked from inngest/inngest-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remix.ts
101 lines (94 loc) · 2.77 KB
/
remix.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
import {
InngestCommHandler,
type ActionResponse,
type ServeHandler,
} from "./components/InngestCommHandler";
import { headerKeys, queryKeys } from "./helpers/consts";
import { type SupportedFrameworkName } from "./types";
export const name: SupportedFrameworkName = "remix";
const createNewResponse = ({
body,
status,
headers,
}: ActionResponse<string | ReadableStream>): Response => {
/**
* If `Response` isn't included in this environment, it's probably a Node
* env that isn't already polyfilling. In this case, we can polyfill it
* here to be safe.
*/
let Res: typeof Response;
if (typeof Response === "undefined") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires
Res = require("cross-fetch").Response;
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Res = Response;
}
return new Res(body, {
status,
headers,
});
};
/**
* In Remix, serve and register any declared functions with Inngest, making them
* available to be triggered by events.
*
* Remix requires that you export both a "loader" for serving `GET` requests,
* and an "action" for serving other requests, therefore exporting both is
* required.
*
* See {@link https://remix.run/docs/en/v1/guides/resource-routes}
*
* @example
* ```ts
* import { serve } from "inngest/remix";
* import fns from "~/inngest";
*
* const handler = serve("My Remix App", fns);
*
* export { handler as loader, handler as action };
* ```
*
* @public
*/
export const serve: ServeHandler = (nameOrInngest, fns, opts): unknown => {
const handler = new InngestCommHandler(
name,
nameOrInngest,
fns,
opts,
({ request: req }: { request: Request }) => {
const url = new URL(req.url, `https://${req.headers.get("host") || ""}`);
return {
url,
register: () => {
if (req.method === "PUT") {
return {
deployId: url.searchParams.get(queryKeys.DeployId),
};
}
},
run: async () => {
if (req.method === "POST") {
return {
data: (await req.json()) as Record<string, unknown>,
fnId: url.searchParams.get(queryKeys.FnId) as string,
stepId: url.searchParams.get(queryKeys.StepId) as string,
signature: req.headers.get(headerKeys.Signature) || undefined,
};
}
},
view: () => {
if (req.method === "GET") {
return {
isIntrospection: url.searchParams.has(queryKeys.Introspect),
};
}
},
};
},
createNewResponse,
createNewResponse
);
return handler.createHandler();
};