forked from inngest/inngest-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuxt.ts
77 lines (73 loc) · 2 KB
/
nuxt.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
import {
getHeader,
getMethod,
getQuery,
readBody,
send,
setHeaders,
type H3Event,
} from "h3";
import {
InngestCommHandler,
type ServeHandler,
} from "./components/InngestCommHandler";
import { headerKeys, queryKeys } from "./helpers/consts";
import { processEnv } from "./helpers/env";
import { type SupportedFrameworkName } from "./types";
export const name: SupportedFrameworkName = "nuxt";
/**
* In Nuxt 3, serve and register any declared functions with Inngest, making
* them available to be triggered by events.
*
* @public
*/
export const serve: ServeHandler = (nameOrInngest, fns, opts) => {
const handler = new InngestCommHandler(
name,
nameOrInngest,
fns,
opts,
(event: H3Event) => {
const host = String(getHeader(event, "host"));
const protocol =
processEnv("NODE_ENV") === "development" ? "http" : "https";
const url = new URL(String(event.path), `${protocol}://${host}`);
const method = getMethod(event);
const query = getQuery(event);
return {
url,
run: async () => {
if (method === "POST") {
return {
fnId: query[queryKeys.FnId]?.toString() ?? "",
stepId: query[queryKeys.StepId]?.toString() ?? "",
signature: getHeader(event, headerKeys.Signature),
data: await readBody(event),
};
}
},
register: () => {
if (method === "PUT") {
return {
deployId: query[queryKeys.DeployId]?.toString(),
};
}
},
view: () => {
if (method === "GET") {
return {
isIntrospection: query && queryKeys.Introspect in query,
};
}
},
};
},
(actionRes, event: H3Event) => {
const { res } = event.node;
res.statusCode = actionRes.status;
setHeaders(event, actionRes.headers);
return send(event, actionRes.body);
}
);
return handler.createHandler();
};