appflare 0.0.28 → 0.1.0

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.
Files changed (141) hide show
  1. package/cli/commands/index.ts +140 -0
  2. package/cli/generate.ts +149 -0
  3. package/cli/index.ts +56 -447
  4. package/cli/load-config.ts +182 -0
  5. package/cli/schema-compiler.ts +657 -0
  6. package/cli/templates/auth/README.md +156 -0
  7. package/cli/templates/auth/config.ts +61 -0
  8. package/cli/templates/auth/route-config.ts +18 -0
  9. package/cli/templates/auth/route-handler.ts +18 -0
  10. package/cli/templates/auth/route-request-utils.ts +55 -0
  11. package/cli/templates/auth/route.ts +14 -0
  12. package/cli/templates/core/README.md +266 -0
  13. package/cli/templates/core/app-creation.ts +19 -0
  14. package/cli/templates/core/client/appflare.ts +37 -0
  15. package/cli/templates/core/client/index.ts +6 -0
  16. package/cli/templates/core/client/storage.ts +100 -0
  17. package/cli/templates/core/client/types.ts +54 -0
  18. package/cli/templates/core/client-modules/appflare.ts +112 -0
  19. package/cli/templates/core/client-modules/handlers/index.ts +740 -0
  20. package/cli/templates/core/client-modules/handlers.ts +1 -0
  21. package/cli/templates/core/client-modules/index.ts +7 -0
  22. package/cli/templates/core/client-modules/storage.ts +180 -0
  23. package/cli/templates/core/client-modules/types.ts +145 -0
  24. package/cli/templates/core/client.ts +39 -0
  25. package/cli/templates/core/drizzle.ts +15 -0
  26. package/cli/templates/core/export.ts +14 -0
  27. package/cli/templates/core/handlers-route.ts +23 -0
  28. package/cli/templates/core/handlers.ts +1 -0
  29. package/cli/templates/core/imports.ts +8 -0
  30. package/cli/templates/core/server.ts +38 -0
  31. package/cli/templates/core/types.ts +6 -0
  32. package/cli/templates/core/wrangler.ts +109 -0
  33. package/cli/templates/handlers/README.md +265 -0
  34. package/cli/templates/handlers/auth.ts +36 -0
  35. package/cli/templates/handlers/execution.ts +39 -0
  36. package/cli/templates/handlers/generators/context/context-creation.ts +80 -0
  37. package/cli/templates/handlers/generators/context/error-helpers.ts +11 -0
  38. package/cli/templates/handlers/generators/context/scheduler.ts +24 -0
  39. package/cli/templates/handlers/generators/context/storage-api.ts +112 -0
  40. package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -0
  41. package/cli/templates/handlers/generators/context/types.ts +18 -0
  42. package/cli/templates/handlers/generators/context.ts +43 -0
  43. package/cli/templates/handlers/generators/execution.ts +15 -0
  44. package/cli/templates/handlers/generators/handlers.ts +13 -0
  45. package/cli/templates/handlers/index.ts +43 -0
  46. package/cli/templates/handlers/operations.ts +116 -0
  47. package/cli/templates/handlers/registration.ts +1114 -0
  48. package/cli/templates/handlers/types.ts +960 -0
  49. package/cli/templates/handlers/utils.ts +48 -0
  50. package/cli/types.ts +108 -0
  51. package/cli/utils/handler-discovery.ts +366 -0
  52. package/cli/utils/json-utils.ts +24 -0
  53. package/cli/utils/path-utils.ts +19 -0
  54. package/cli/utils/schema-discovery.ts +390 -0
  55. package/index.ts +27 -4
  56. package/package.json +23 -20
  57. package/react/index.ts +5 -3
  58. package/react/use-infinite-query.ts +190 -0
  59. package/react/use-mutation.ts +54 -0
  60. package/react/use-query.ts +158 -0
  61. package/schema.ts +262 -0
  62. package/tsconfig.json +2 -4
  63. package/cli/README.md +0 -108
  64. package/cli/core/build.ts +0 -187
  65. package/cli/core/config.ts +0 -92
  66. package/cli/core/discover-handlers.ts +0 -143
  67. package/cli/core/handlers.ts +0 -7
  68. package/cli/core/index.ts +0 -205
  69. package/cli/generators/generate-api-client/client.ts +0 -163
  70. package/cli/generators/generate-api-client/extract-configuration.ts +0 -121
  71. package/cli/generators/generate-api-client/index.ts +0 -973
  72. package/cli/generators/generate-api-client/types.ts +0 -164
  73. package/cli/generators/generate-api-client/utils.ts +0 -22
  74. package/cli/generators/generate-api-client.ts +0 -1
  75. package/cli/generators/generate-cloudflare-worker/helpers.ts +0 -24
  76. package/cli/generators/generate-cloudflare-worker/index.ts +0 -2
  77. package/cli/generators/generate-cloudflare-worker/worker.ts +0 -148
  78. package/cli/generators/generate-cloudflare-worker/wrangler.ts +0 -108
  79. package/cli/generators/generate-cloudflare-worker.ts +0 -4
  80. package/cli/generators/generate-cron-handlers/cron-handlers-block.ts +0 -2
  81. package/cli/generators/generate-cron-handlers/handler-entries.ts +0 -29
  82. package/cli/generators/generate-cron-handlers/index.ts +0 -61
  83. package/cli/generators/generate-cron-handlers/runtime-block.ts +0 -49
  84. package/cli/generators/generate-cron-handlers/type-helpers-block.ts +0 -60
  85. package/cli/generators/generate-db-handlers/index.ts +0 -33
  86. package/cli/generators/generate-db-handlers/prepare.ts +0 -24
  87. package/cli/generators/generate-db-handlers/templates.ts +0 -189
  88. package/cli/generators/generate-db-handlers.ts +0 -1
  89. package/cli/generators/generate-hono-server/auth.ts +0 -97
  90. package/cli/generators/generate-hono-server/imports.ts +0 -55
  91. package/cli/generators/generate-hono-server/index.ts +0 -52
  92. package/cli/generators/generate-hono-server/routes.ts +0 -115
  93. package/cli/generators/generate-hono-server/template.ts +0 -371
  94. package/cli/generators/generate-hono-server.ts +0 -1
  95. package/cli/generators/generate-scheduler-handlers/constants.ts +0 -8
  96. package/cli/generators/generate-scheduler-handlers/handler-entries.ts +0 -22
  97. package/cli/generators/generate-scheduler-handlers/index.ts +0 -51
  98. package/cli/generators/generate-scheduler-handlers/runtime-block.ts +0 -68
  99. package/cli/generators/generate-scheduler-handlers/scheduler-handlers-block.ts +0 -2
  100. package/cli/generators/generate-scheduler-handlers/type-helpers-block.ts +0 -68
  101. package/cli/generators/generate-scheduler-handlers.ts +0 -1
  102. package/cli/generators/generate-websocket-durable-object/auth.ts +0 -30
  103. package/cli/generators/generate-websocket-durable-object/imports.ts +0 -55
  104. package/cli/generators/generate-websocket-durable-object/index.ts +0 -41
  105. package/cli/generators/generate-websocket-durable-object/query-handlers.ts +0 -18
  106. package/cli/generators/generate-websocket-durable-object/template.ts +0 -714
  107. package/cli/generators/generate-websocket-durable-object.ts +0 -1
  108. package/cli/schema/schema-static-types.ts +0 -702
  109. package/cli/schema/schema.ts +0 -151
  110. package/cli/utils/tsc.ts +0 -54
  111. package/cli/utils/utils.ts +0 -190
  112. package/cli/utils/zod-utils.ts +0 -121
  113. package/lib/README.md +0 -50
  114. package/lib/db.ts +0 -19
  115. package/lib/location.ts +0 -110
  116. package/lib/values.ts +0 -27
  117. package/react/README.md +0 -67
  118. package/react/hooks/useMutation.ts +0 -89
  119. package/react/hooks/usePaginatedQuery.ts +0 -213
  120. package/react/hooks/useQuery.ts +0 -106
  121. package/react/shared/queryShared.ts +0 -174
  122. package/server/README.md +0 -218
  123. package/server/auth.ts +0 -107
  124. package/server/database/builders.ts +0 -83
  125. package/server/database/context.ts +0 -327
  126. package/server/database/populate.ts +0 -234
  127. package/server/database/query-builder.ts +0 -161
  128. package/server/database/query-utils.ts +0 -25
  129. package/server/db.ts +0 -2
  130. package/server/storage/auth.ts +0 -16
  131. package/server/storage/bucket.ts +0 -22
  132. package/server/storage/context.ts +0 -34
  133. package/server/storage/index.ts +0 -38
  134. package/server/storage/operations.ts +0 -149
  135. package/server/storage/route-handler.ts +0 -60
  136. package/server/storage/types.ts +0 -55
  137. package/server/storage/utils.ts +0 -47
  138. package/server/storage.ts +0 -6
  139. package/server/types/schema-refs.ts +0 -66
  140. package/server/types/types.ts +0 -633
  141. package/server/utils/id-utils.ts +0 -230
@@ -0,0 +1 @@
1
+ export { generateClientHandlersSource } from "./handlers/index";
@@ -0,0 +1,7 @@
1
+ export function generateClientIndexSource(): string {
2
+ return `export * from "./types";
3
+ export * from "./appflare";
4
+ export * from "./storage";
5
+ export * from "./handlers";
6
+ `;
7
+ }
@@ -0,0 +1,180 @@
1
+ export function generateClientStorageSource(): string {
2
+ return `import type { StorageClient, StorageSignedUrlResponse, StorageListResponse } from "./types";
3
+
4
+ type AuthTokenResolver = (() => string | Promise<string>) | undefined;
5
+
6
+ function toHeaderRecord(headers: HeadersInit | undefined): Record<string, string> {
7
+ const result: Record<string, string> = {};
8
+ if (!headers) {
9
+ return result;
10
+ }
11
+
12
+ if (Array.isArray(headers)) {
13
+ for (const entry of headers) {
14
+ if (!Array.isArray(entry) || entry.length < 2) {
15
+ continue;
16
+ }
17
+ result[String(entry[0])] = String(entry[1]);
18
+ }
19
+ return result;
20
+ }
21
+
22
+ if (typeof (headers as { forEach?: unknown }).forEach === "function") {
23
+ (headers as Headers).forEach((value, key) => {
24
+ result[key] = String(value);
25
+ });
26
+ return result;
27
+ }
28
+
29
+ for (const [key, value] of Object.entries(headers as Record<string, unknown>)) {
30
+ result[key] = String(value ?? "");
31
+ }
32
+
33
+ return result;
34
+ }
35
+
36
+ function hasAuthorizationHeader(headers: Record<string, string>): boolean {
37
+ for (const key of Object.keys(headers)) {
38
+ if (key.toLowerCase() === "authorization") {
39
+ return true;
40
+ }
41
+ }
42
+
43
+ return false;
44
+ }
45
+
46
+ async function createAuthorizedHeaders(
47
+ headers: HeadersInit | undefined,
48
+ onGetAuthToken: AuthTokenResolver,
49
+ ): Promise<HeadersInit | undefined> {
50
+ const resolvedHeaders = toHeaderRecord(headers);
51
+ if (onGetAuthToken) {
52
+ const authToken = await onGetAuthToken();
53
+ if (typeof authToken === "string" && authToken.trim().length > 0) {
54
+ if (!hasAuthorizationHeader(resolvedHeaders)) {
55
+ resolvedHeaders.authorization = \`Bearer \${authToken.trim()}\`;
56
+ }
57
+ }
58
+ }
59
+
60
+ return resolvedHeaders;
61
+ }
62
+
63
+ export function createStorageClient(
64
+ endpoint: string,
65
+ request: typeof fetch = fetch,
66
+ onGetAuthToken?: () => string | Promise<string>,
67
+ ): StorageClient {
68
+ return {
69
+ upload: async (args) => {
70
+ const headers = await createAuthorizedHeaders(
71
+ {
72
+ "content-type": "application/json",
73
+ },
74
+ onGetAuthToken,
75
+ );
76
+
77
+ const response = await request(\`\${endpoint}/storage/upload\`, {
78
+ method: "POST",
79
+ headers,
80
+ body: JSON.stringify(args),
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error(await response.text());
84
+ }
85
+
86
+ return (await response.json()) as StorageSignedUrlResponse;
87
+ },
88
+ download: async (args) => {
89
+ const query = new URLSearchParams({
90
+ path: args.path,
91
+ ...(args.fileName ? { fileName: args.fileName } : {}),
92
+ ...(typeof args.expiresIn === "number"
93
+ ? { expiresIn: String(args.expiresIn) }
94
+ : {}),
95
+ });
96
+ const response = await request(
97
+ \`\${endpoint}/storage/download?\${query.toString()}\`,
98
+ {
99
+ headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
100
+ },
101
+ );
102
+ if (!response.ok) {
103
+ throw new Error(await response.text());
104
+ }
105
+
106
+ return (await response.json()) as StorageSignedUrlResponse;
107
+ },
108
+ preview: async (args) => {
109
+ const query = new URLSearchParams({
110
+ path: args.path,
111
+ ...(typeof args.expiresIn === "number"
112
+ ? { expiresIn: String(args.expiresIn) }
113
+ : {}),
114
+ });
115
+ const response = await request(
116
+ \`\${endpoint}/storage/preview?\${query.toString()}\`,
117
+ {
118
+ headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
119
+ },
120
+ );
121
+ if (!response.ok) {
122
+ throw new Error(await response.text());
123
+ }
124
+
125
+ return (await response.json()) as StorageSignedUrlResponse;
126
+ },
127
+ delete: async (args) => {
128
+ const query = new URLSearchParams({
129
+ path: args.path,
130
+ });
131
+ const response = await request(
132
+ \`\${endpoint}/storage/object?\${query.toString()}\`,
133
+ {
134
+ method: "DELETE",
135
+ headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
136
+ },
137
+ );
138
+ if (!response.ok) {
139
+ throw new Error(await response.text());
140
+ }
141
+
142
+ return (await response.json()) as { ok: boolean; path: string };
143
+ },
144
+ list: async (args = {}) => {
145
+ const query = new URLSearchParams();
146
+ if (args.prefix) {
147
+ query.set("prefix", args.prefix);
148
+ }
149
+ if (args.cursor) {
150
+ query.set("cursor", args.cursor);
151
+ }
152
+ if (typeof args.limit === "number") {
153
+ query.set("limit", String(args.limit));
154
+ }
155
+ if (args.delimiter) {
156
+ query.set("delimiter", args.delimiter);
157
+ }
158
+ if (args.method) {
159
+ query.set("method", args.method);
160
+ }
161
+
162
+ const querySuffix = query.toString();
163
+ const response = await request(
164
+ querySuffix.length > 0
165
+ ? \`\${endpoint}/storage/list?\${querySuffix}\`
166
+ : \`\${endpoint}/storage/list\`,
167
+ {
168
+ headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
169
+ },
170
+ );
171
+ if (!response.ok) {
172
+ throw new Error(await response.text());
173
+ }
174
+
175
+ return (await response.json()) as StorageListResponse;
176
+ },
177
+ };
178
+ }
179
+ `;
180
+ }
@@ -0,0 +1,145 @@
1
+ export function generateClientTypesSource(configPathImport: string): string {
2
+ return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
3
+ import type appflareConfig from "${configPathImport}";
4
+
5
+ export type AppflareConfig = typeof appflareConfig;
6
+ export type InferredAuthOptions = AppflareConfig["auth"]["clientOptions"];
7
+
8
+ export type AppflareAuth<Options extends BetterAuthClientOptions = InferredAuthOptions> = ReturnType<
9
+ typeof createAuthClient<Options>
10
+ >;
11
+
12
+ export type AppflareAuthTokenResolver = () => string | Promise<string>;
13
+
14
+ export type AppflareOptions<Options extends BetterAuthClientOptions = InferredAuthOptions> = {
15
+ endpoint: string;
16
+ wsEndpoint?: string;
17
+ authPath?: string;
18
+ authOptions?: Options;
19
+ fetch?: typeof fetch;
20
+ requestOptions?: AppflareResultRouteCallOptions;
21
+ onSetAuthToken?: (token: string) => void | Promise<void>;
22
+ onGetAuthToken?: AppflareAuthTokenResolver;
23
+ };
24
+
25
+ export type AppflareErrorMode = "throw" | "return";
26
+
27
+ export type AppflareRequestError = {
28
+ route: string;
29
+ method: "GET" | "POST";
30
+ status: number;
31
+ message: string;
32
+ body?: unknown;
33
+ responseText?: string;
34
+ };
35
+
36
+ export type AppflareRequestResult<TData> = {
37
+ data: TData | null;
38
+ error: AppflareRequestError | null;
39
+ };
40
+
41
+ export type AppflareRouteCallOptions<
42
+ Mode extends AppflareErrorMode = "throw",
43
+ > = {
44
+ errorMode?: Mode;
45
+ headers?: HeadersInit;
46
+ signal?: AbortSignal;
47
+ onError?: (error: AppflareRequestError) => void;
48
+ };
49
+
50
+ export type AppflareResultRouteCallOptions = Omit<
51
+ AppflareRouteCallOptions<"return">,
52
+ "errorMode"
53
+ >;
54
+
55
+ export type AppflareRouteClient<
56
+ TSchema extends import("zod").ZodObject<import("zod").ZodRawShape>,
57
+ TOutput,
58
+ > = {
59
+ schema: TSchema;
60
+ run(
61
+ args: import("zod").input<TSchema>,
62
+ options?: AppflareResultRouteCallOptions,
63
+ ): Promise<AppflareRequestResult<TOutput>>;
64
+ };
65
+
66
+ export type AppflareRealtimeSubscription = {
67
+ remove: () => void;
68
+ };
69
+
70
+ export type AppflareRealtimeQueryUpdate<TOutput> = {
71
+ event: "query:update";
72
+ payload: {
73
+ queryName: string;
74
+ signature: string;
75
+ data: TOutput;
76
+ };
77
+ };
78
+
79
+ export type AppflareQuerySubscribeOptions<TInput, TOutput> = {
80
+ args?: TInput;
81
+ authToken?: string;
82
+ onChange: (data: TOutput, update: AppflareRealtimeQueryUpdate<TOutput>) => void;
83
+ onError?: (error: unknown) => void;
84
+ requestOptions?: AppflareRouteCallOptions;
85
+ signal?: AbortSignal;
86
+ };
87
+
88
+ export type AppflareQueryRouteClient<
89
+ TSchema extends import("zod").ZodObject<import("zod").ZodRawShape>,
90
+ TOutput,
91
+ > = AppflareRouteClient<TSchema, TOutput> & {
92
+ subscribe: (
93
+ options: AppflareQuerySubscribeOptions<import("zod").input<TSchema>, TOutput>,
94
+ ) => AppflareRealtimeSubscription;
95
+ };
96
+
97
+ export type StorageSignedUrlResponse = {
98
+ url: string;
99
+ method: "GET" | "PUT" | "DELETE";
100
+ path: string;
101
+ disposition?: "attachment" | "inline";
102
+ expiresIn?: number;
103
+ };
104
+
105
+ export type StorageListResponse = unknown;
106
+
107
+ export type StorageClient = {
108
+ upload: (args: {
109
+ path: string;
110
+ contentType?: string;
111
+ expiresIn?: number;
112
+ }) => Promise<StorageSignedUrlResponse>;
113
+ download: (args: {
114
+ path: string;
115
+ expiresIn?: number;
116
+ fileName?: string;
117
+ }) => Promise<StorageSignedUrlResponse>;
118
+ preview: (args: {
119
+ path: string;
120
+ expiresIn?: number;
121
+ }) => Promise<StorageSignedUrlResponse>;
122
+ delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
123
+ list: (args?: {
124
+ prefix?: string;
125
+ cursor?: string;
126
+ limit?: number;
127
+ delimiter?: string;
128
+ method?: "download" | "get" | "delete" | "list" | "put" | "preview";
129
+ }) => Promise<StorageListResponse>;
130
+ };
131
+
132
+ export type RealtimeSubscriptionResponse = {
133
+ token: string;
134
+ signature: string;
135
+ websocket: {
136
+ url: string;
137
+ protocol: string;
138
+ params: {
139
+ tokenParam: "token";
140
+ authTokenParam: "authToken";
141
+ };
142
+ };
143
+ };
144
+ `;
145
+ }
@@ -0,0 +1,39 @@
1
+ import { generateClientAppflareSource } from "./client-modules/appflare";
2
+ import { generateClientHandlersSource } from "./client-modules/handlers";
3
+ import { generateClientIndexSource } from "./client-modules/index";
4
+ import { generateClientStorageSource } from "./client-modules/storage";
5
+ import { generateClientTypesSource } from "./client-modules/types";
6
+ import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
7
+
8
+ export type GeneratedClientArtifact = {
9
+ relativePath: string;
10
+ source: string;
11
+ };
12
+
13
+ export function generateClientArtifacts(
14
+ configPathImport: string,
15
+ operations: DiscoveredHandlerOperation[],
16
+ ): GeneratedClientArtifact[] {
17
+ return [
18
+ {
19
+ relativePath: "client/index.ts",
20
+ source: generateClientIndexSource(),
21
+ },
22
+ {
23
+ relativePath: "client/types.ts",
24
+ source: generateClientTypesSource(configPathImport),
25
+ },
26
+ {
27
+ relativePath: "client/storage.ts",
28
+ source: generateClientStorageSource(),
29
+ },
30
+ {
31
+ relativePath: "client/handlers.ts",
32
+ source: generateClientHandlersSource(operations),
33
+ },
34
+ {
35
+ relativePath: "client/appflare.ts",
36
+ source: generateClientAppflareSource(),
37
+ },
38
+ ];
39
+ }
@@ -0,0 +1,15 @@
1
+ export function generateDrizzleConfigSource(schema: string[]): string {
2
+ return `import { defineConfig } from "drizzle-kit";
3
+
4
+ export default defineConfig({
5
+ dialect: "sqlite",
6
+ schema: ${JSON.stringify(schema)},
7
+ driver: "d1-http",
8
+ dbCredentials: {
9
+ accountId: "accountId",
10
+ databaseId: "databaseId",
11
+ token: "token",
12
+ },
13
+ });
14
+ `;
15
+ }
@@ -0,0 +1,14 @@
1
+ export function generateExport(): string {
2
+ return `export { AppflareRealtimeDurableObject };
3
+
4
+ export default {
5
+ \tfetch: app.fetch,
6
+ queue: async (batch, env) => {
7
+ await executeScheduledBatch(batch, env, generatedHandlerOptions);
8
+ },
9
+ scheduled: async (controller, env) => {
10
+ await executeCronTriggers(controller, env, generatedHandlerOptions);
11
+ },
12
+ };
13
+ `;
14
+ }
@@ -0,0 +1,23 @@
1
+ export function generateHandlersRoute(
2
+ databaseBinding: string,
3
+ kvBinding?: string,
4
+ schedulerBinding = "APPFLARE_SCHEDULER_QUEUE",
5
+ r2Binding?: string,
6
+ realtimeBinding = "APPFLARE_REALTIME",
7
+ realtimeObjectName = "global",
8
+ realtimeSubscribePath = "/realtime/subscribe",
9
+ realtimeWebsocketPath = "/realtime/ws",
10
+ realtimeProtocol = "appflare.realtime.v1",
11
+ ): string {
12
+ const kvLine = kvBinding ? `\n\tkvBinding: "${kvBinding}",` : "";
13
+ const storageLine = r2Binding ? `\n\tr2Binding: "${r2Binding}",` : "";
14
+ const realtimeLines = `\n\trealtimeBinding: "${realtimeBinding}",\n\trealtimeObjectName: "${realtimeObjectName}",\n\trealtimeSubscribePath: "${realtimeSubscribePath}",\n\trealtimeWebsocketPath: "${realtimeWebsocketPath}",\n\trealtimeProtocol: "${realtimeProtocol}",`;
15
+
16
+ return `const generatedHandlerOptions = {
17
+ databaseBinding: "${databaseBinding}",${kvLine}
18
+ schedulerBinding: "${schedulerBinding}",${storageLine}${realtimeLines}
19
+ };
20
+ registerGeneratedHandlers(app, generatedHandlerOptions);
21
+ registerGeneratedStorageRoutes(app, generatedHandlerOptions);
22
+ `;
23
+ }
@@ -0,0 +1 @@
1
+ export { generateHandlersArtifacts } from "../handlers/index";
@@ -0,0 +1,8 @@
1
+ export function generateImports(): string {
2
+ return `import { createAuth } from "./auth.config";
3
+ import { AppflareRealtimeDurableObject, executeCronTriggers, executeScheduledBatch, registerGeneratedHandlers, registerGeneratedStorageRoutes } from "./handlers.routes";
4
+ import { Hono } from "hono";
5
+ import { cors } from "hono/cors";
6
+ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
7
+ `;
8
+ }
@@ -0,0 +1,38 @@
1
+ import { generateAuthRoute } from "../auth/route";
2
+ import { generateAppCreation } from "./app-creation";
3
+ import { generateExport } from "./export";
4
+ import { generateHandlersRoute } from "./handlers-route";
5
+ import { generateImports } from "./imports";
6
+ import { generateTypes } from "./types";
7
+
8
+ export function generateServerSource(
9
+ authBasePath: string,
10
+ databaseBinding: string,
11
+ kvBinding?: string,
12
+ schedulerBinding?: string,
13
+ r2Binding?: string,
14
+ realtimeBinding?: string,
15
+ realtimeObjectName?: string,
16
+ realtimeSubscribePath?: string,
17
+ realtimeWebsocketPath?: string,
18
+ realtimeProtocol?: string,
19
+ ): string {
20
+ return (
21
+ generateImports() +
22
+ generateTypes() +
23
+ generateAppCreation() +
24
+ generateHandlersRoute(
25
+ databaseBinding,
26
+ kvBinding,
27
+ schedulerBinding,
28
+ r2Binding,
29
+ realtimeBinding,
30
+ realtimeObjectName,
31
+ realtimeSubscribePath,
32
+ realtimeWebsocketPath,
33
+ realtimeProtocol,
34
+ ) +
35
+ generateAuthRoute(authBasePath, databaseBinding, kvBinding) +
36
+ generateExport()
37
+ );
38
+ }
@@ -0,0 +1,6 @@
1
+ export function generateTypes(): string {
2
+ return `type WorkerEnv = {
3
+ \tBindings: Record<string, unknown>;
4
+ };
5
+ `;
6
+ }
@@ -0,0 +1,109 @@
1
+ import type { JsonObject, LoadedAppflareConfig } from "../../types";
2
+ import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
3
+ import { deepMergeJson } from "../../utils/json-utils";
4
+
5
+ function unique(values: string[]): string[] {
6
+ return Array.from(new Set(values.filter((value) => value.length > 0)));
7
+ }
8
+
9
+ export function generateWranglerJson(
10
+ config: LoadedAppflareConfig,
11
+ operations: DiscoveredHandlerOperation[],
12
+ ): JsonObject {
13
+ const schedulerOperations = operations.filter(
14
+ (operation) => operation.kind === "scheduler" || operation.kind === "cron",
15
+ );
16
+ const cronTriggers = unique(
17
+ operations
18
+ .filter((operation) => operation.kind === "cron")
19
+ .flatMap((operation) => operation.cronTriggers ?? []),
20
+ );
21
+ const schedulerEnabled =
22
+ config.config.scheduler.enabled && schedulerOperations.length > 0;
23
+ const realtimeEnabled = config.config.realtime.enabled;
24
+ const workerNameOverride =
25
+ typeof config.config.wranglerOverrides?.name === "string"
26
+ ? config.config.wranglerOverrides.name
27
+ : undefined;
28
+ const workerName = workerNameOverride ?? "appflare-worker";
29
+ const schedulerQueueName =
30
+ config.config.scheduler.queue ?? `${workerName}-scheduler`;
31
+
32
+ const wranglerBase: JsonObject = {
33
+ name: workerName,
34
+ main: "./src/index.ts",
35
+ d1_databases: config.config.database.map((database) => ({
36
+ binding: database.binding,
37
+ database_name: database.databaseName,
38
+ database_id: database.databaseId,
39
+ preview_database_id: database.previewDatabaseId ?? database.databaseId,
40
+ ...(database.migrationsDir
41
+ ? { migrations_dir: database.migrationsDir }
42
+ : {}),
43
+ })),
44
+ kv_namespaces: config.config.kv.map((namespace) => ({
45
+ binding: namespace.binding,
46
+ id: namespace.id,
47
+ ...(namespace.previewId ? { preview_id: namespace.previewId } : {}),
48
+ })),
49
+ r2_buckets: config.config.r2.map((bucket) => ({
50
+ binding: bucket.binding,
51
+ bucket_name: bucket.bucketName,
52
+ ...(bucket.previewBucketName
53
+ ? { preview_bucket_name: bucket.previewBucketName }
54
+ : {}),
55
+ ...(bucket.jurisdiction ? { jurisdiction: bucket.jurisdiction } : {}),
56
+ })),
57
+ ...(schedulerEnabled
58
+ ? {
59
+ queues: {
60
+ producers: [
61
+ {
62
+ binding: config.config.scheduler.binding,
63
+ queue: schedulerQueueName,
64
+ },
65
+ ],
66
+ consumers: [
67
+ {
68
+ queue: schedulerQueueName,
69
+ },
70
+ ],
71
+ },
72
+ }
73
+ : {}),
74
+ ...(cronTriggers.length > 0
75
+ ? {
76
+ triggers: {
77
+ crons: cronTriggers,
78
+ },
79
+ }
80
+ : {}),
81
+ ...(realtimeEnabled
82
+ ? {
83
+ durable_objects: {
84
+ bindings: [
85
+ {
86
+ name: config.config.realtime.binding,
87
+ class_name: config.config.realtime.className,
88
+ },
89
+ ],
90
+ },
91
+ migrations: [
92
+ {
93
+ tag: "appflare-realtime-v1",
94
+ new_classes: [config.config.realtime.className],
95
+ },
96
+ ],
97
+ }
98
+ : {}),
99
+ };
100
+
101
+ if (!config.config.wranglerOverrides) {
102
+ return wranglerBase;
103
+ }
104
+
105
+ const { scheduler: _ignoredSchedulerOverride, ...wranglerOverrides } =
106
+ config.config.wranglerOverrides;
107
+
108
+ return deepMergeJson(wranglerBase, wranglerOverrides);
109
+ }