appflare 0.0.17 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,7 +39,7 @@ export async function discoverHandlers(params: {
39
39
  const content = await fs.readFile(fileAbs, "utf8");
40
40
  const cronTriggersByHandler = extractCronTriggers(content);
41
41
  const regex =
42
- /export\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*(query|mutation|internalQuery|internalMutation|scheduler|cron)\s*\(/g;
42
+ /export\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*(query|mutation|internalQuery|internalMutation|scheduler|cron|http)\s*\(/g;
43
43
  let match: RegExpExecArray | null;
44
44
  while ((match = regex.exec(content)) !== null) {
45
45
  const kind = match[2] as HandlerKind;
@@ -1,7 +1,7 @@
1
1
  import type { AppflareConfig, DiscoveredHandler } from "../../utils/utils";
2
2
  import { buildImportSection } from "./imports";
3
3
  import { buildAuthSection } from "./auth";
4
- import { buildRouteLines } from "./routes";
4
+ import { buildRouteLines, buildHttpRouteLines } from "./routes";
5
5
  import { renderServerTemplate } from "./template";
6
6
 
7
7
  export type GenerateHonoServerParams = {
@@ -17,6 +17,9 @@ export function generateHonoServer(params: GenerateHonoServerParams): string {
17
17
  const mutations = params.handlers.filter(
18
18
  (handler) => handler.kind === "mutation"
19
19
  );
20
+ const https = params.handlers.filter(
21
+ (handler) => handler.kind === "http"
22
+ );
20
23
 
21
24
  const imports = buildImportSection({
22
25
  handlers: params.handlers,
@@ -30,6 +33,10 @@ export function generateHonoServer(params: GenerateHonoServerParams): string {
30
33
  mutations,
31
34
  localNameFor: imports.localNameFor,
32
35
  });
36
+ const httpLines = buildHttpRouteLines({
37
+ https,
38
+ localNameFor: imports.localNameFor,
39
+ });
33
40
 
34
41
  return renderServerTemplate({
35
42
  schemaImportPath: imports.schemaImportPath,
@@ -40,5 +47,6 @@ export function generateHonoServer(params: GenerateHonoServerParams): string {
40
47
  authMountBlock: auth.mountBlock,
41
48
  authResolverBlock: auth.resolverBlock,
42
49
  routeLines,
50
+ httpLines,
43
51
  });
44
52
  }
@@ -78,3 +78,38 @@ export function buildRouteLines(params: {
78
78
  }
79
79
  return routeLines;
80
80
  }
81
+
82
+ export function buildHttpRouteLines(params: {
83
+ https: DiscoveredHandler[];
84
+ localNameFor: (handler: DiscoveredHandler) => string;
85
+ }): string[] {
86
+ const routeLines: string[] = [];
87
+ for (const hook of params.https) {
88
+ const local = params.localNameFor(hook);
89
+ routeLines.push(
90
+ `app.all(\n` +
91
+ ` ${JSON.stringify(`/http/${hook.routePath}/${hook.name}`)},\n` +
92
+ ` async (c) => {\n` +
93
+ `\t\ttry {\n` +
94
+ `\t\t\tconst ctx = await resolveContext(c);\n` +
95
+ `\t\t\tconst result = await runHandlerWithMiddleware(\n` +
96
+ `\t\t\t\t${local} as any,\n` +
97
+ `\t\t\t\tctx as any,\n` +
98
+ `\t\t\t\tc.req.raw\n` +
99
+ `\t\t\t);\n` +
100
+ `\t\t\tif (isHandlerError(result)) {\n` +
101
+ `\t\t\t\tconst { status, body } = formatHandlerError(result);\n` +
102
+ `\t\t\t\treturn c.json(body as any, status);\n` +
103
+ `\t\t\t}\n` +
104
+ `\t\t\treturn result;\n` +
105
+ `\t\t} catch (err) {\n` +
106
+ `\t\t\tconst { status, body } = formatHandlerError(err);\n` +
107
+ `\t\t\tconsole.error("Appflare http handler error", err);\n` +
108
+ `\t\t\treturn c.json(body as any, status);\n` +
109
+ `\t\t}\n` +
110
+ `\t}\n` +
111
+ `);`
112
+ );
113
+ }
114
+ return routeLines;
115
+ }
@@ -7,6 +7,7 @@ export type ServerTemplateParams = {
7
7
  authMountBlock: string;
8
8
  authResolverBlock: string;
9
9
  routeLines: string[];
10
+ httpLines: string[];
10
11
  };
11
12
 
12
13
  export function renderServerTemplate(params: ServerTemplateParams): string {
@@ -338,13 +339,15 @@ ${params.authSetupBlock}${params.authMountBlock}${params.authResolverBlock}\tcon
338
339
  return merged as AppflareServerContext;
339
340
  };
340
341
 
341
- \t${params.routeLines.join("\n\n\t")}
342
+ ${params.routeLines.join("\n\n\t")}
342
343
 
343
- \treturn app;
344
+ ${params.httpLines.join("\n\n\t")}
345
+
346
+ return app;
344
347
  }
345
348
 
346
349
  function createMutationNotifier(
347
- \toptions?: RealtimeOptions
350
+ options?: RealtimeOptions
348
351
  ): MutationNotifier | undefined {
349
352
  \tif (!options) return undefined;
350
353
  \tif (options.notify) return options.notify;
@@ -676,4 +676,18 @@ export interface InternalMutationDefinition<
676
676
  export const internalMutation = <TArgs extends QueryArgsShape, TResult>(
677
677
  definition: InternalMutationDefinition<TArgs, TResult>
678
678
  ): InternalMutationDefinition<TArgs, TResult> => definition;
679
+
680
+ export interface HttpContext extends AppflareAuthContext {
681
+ db: DatabaseWriter;
682
+ scheduler?: Scheduler;
683
+ error: AppflareErrorFactory;
684
+ }
685
+
686
+ export interface HttpDefinition {
687
+ handler: (ctx: HttpContext, request: Request) => Promise<Response>;
688
+ }
689
+
690
+ export const http = (
691
+ definition: HttpDefinition
692
+ ): HttpDefinition => definition;
679
693
  `;
@@ -50,7 +50,8 @@ export type HandlerKind =
50
50
  | "internalQuery"
51
51
  | "internalMutation"
52
52
  | "scheduler"
53
- | "cron";
53
+ | "cron"
54
+ | "http";
54
55
 
55
56
  export type DiscoveredHandler = {
56
57
  fileName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appflare",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "bin": {
5
5
  "appflare": "./cli/index.ts"
6
6
  },