forked from inngest/inngest-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fresh.ts
62 lines (57 loc) · 1.82 KB
/
fresh.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
import { type SupportedFrameworkName } from "inngest/types";
import {
InngestCommHandler,
type ServeHandler,
} from "../components/InngestCommHandler";
import { headerKeys, queryKeys } from "../helpers/consts";
export const name: SupportedFrameworkName = "deno/fresh";
/**
* With Deno's Fresh framework, 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 as string,
nameOrInngest,
fns,
opts,
(req: Request, env: { [index: string]: string }) => {
const url = new URL(req.url, `https://${req.headers.get("host") || ""}`);
return {
url,
env,
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),
};
}
},
};
},
({ body, status, headers }): Response => {
return new Response(body, { status, headers });
}
).createHandler();
return (req: Request) => handler(req, Deno.env.toObject());
};
declare const Deno: { env: { toObject: () => { [index: string]: string } } };