appflare 0.2.41 → 0.2.43

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 (145) hide show
  1. package/Documentation.md +898 -758
  2. package/cli/commands/index.ts +247 -247
  3. package/cli/generate.ts +360 -360
  4. package/cli/index.ts +120 -120
  5. package/cli/load-config.ts +184 -184
  6. package/cli/schema-compiler.ts +1363 -1361
  7. package/cli/templates/auth/README.md +156 -156
  8. package/cli/templates/auth/config.ts +61 -61
  9. package/cli/templates/auth/route-config.ts +1 -1
  10. package/cli/templates/auth/route-handler.ts +1 -1
  11. package/cli/templates/auth/route-request-utils.ts +5 -5
  12. package/cli/templates/auth/route.config.ts +18 -18
  13. package/cli/templates/auth/route.handler.ts +18 -18
  14. package/cli/templates/auth/route.request-utils.ts +55 -55
  15. package/cli/templates/auth/route.ts +14 -14
  16. package/cli/templates/core/README.md +266 -266
  17. package/cli/templates/core/app-creation.ts +19 -19
  18. package/cli/templates/core/client/appflare.ts +112 -112
  19. package/cli/templates/core/client/handlers/index.ts +763 -755
  20. package/cli/templates/core/client/handlers.ts +1 -1
  21. package/cli/templates/core/client/index.ts +7 -7
  22. package/cli/templates/core/client/storage.ts +195 -195
  23. package/cli/templates/core/client/types.ts +187 -186
  24. package/cli/templates/core/client-modules/appflare.ts +1 -1
  25. package/cli/templates/core/client-modules/handlers.ts +1 -1
  26. package/cli/templates/core/client-modules/index.ts +1 -1
  27. package/cli/templates/core/client-modules/storage.ts +1 -1
  28. package/cli/templates/core/client-modules/types.ts +1 -1
  29. package/cli/templates/core/client.artifacts.ts +39 -39
  30. package/cli/templates/core/client.ts +4 -4
  31. package/cli/templates/core/drizzle.ts +15 -15
  32. package/cli/templates/core/export.ts +14 -14
  33. package/cli/templates/core/handlers.route.ts +24 -24
  34. package/cli/templates/core/handlers.ts +1 -1
  35. package/cli/templates/core/imports.ts +9 -9
  36. package/cli/templates/core/server.ts +38 -38
  37. package/cli/templates/core/types.ts +6 -6
  38. package/cli/templates/core/wrangler.ts +109 -109
  39. package/cli/templates/dashboard/builders/functions/index.ts +17 -17
  40. package/cli/templates/dashboard/builders/functions/render-page/header.ts +20 -20
  41. package/cli/templates/dashboard/builders/functions/render-page/index.ts +33 -33
  42. package/cli/templates/dashboard/builders/functions/render-page/request-panel.ts +171 -171
  43. package/cli/templates/dashboard/builders/functions/render-page/result-panel.ts +85 -85
  44. package/cli/templates/dashboard/builders/functions/render-page/scripts.ts +554 -554
  45. package/cli/templates/dashboard/builders/functions/tree-builder.ts +47 -47
  46. package/cli/templates/dashboard/builders/navigation.ts +155 -155
  47. package/cli/templates/dashboard/builders/storage/index.ts +13 -13
  48. package/cli/templates/dashboard/builders/storage/routes/create-directory-route.ts +29 -29
  49. package/cli/templates/dashboard/builders/storage/routes/delete-route.ts +18 -18
  50. package/cli/templates/dashboard/builders/storage/routes/download-route.ts +23 -23
  51. package/cli/templates/dashboard/builders/storage/routes/index.ts +22 -22
  52. package/cli/templates/dashboard/builders/storage/routes/list-route.ts +25 -25
  53. package/cli/templates/dashboard/builders/storage/routes/preview-route.ts +21 -21
  54. package/cli/templates/dashboard/builders/storage/routes/upload-route.ts +21 -21
  55. package/cli/templates/dashboard/builders/storage/runtime/helpers.ts +72 -72
  56. package/cli/templates/dashboard/builders/storage/runtime/storage-page.ts +130 -130
  57. package/cli/templates/dashboard/builders/table-routes/common/drawer-panel.ts +27 -27
  58. package/cli/templates/dashboard/builders/table-routes/common/pagination.ts +30 -30
  59. package/cli/templates/dashboard/builders/table-routes/common/search-bar.ts +23 -23
  60. package/cli/templates/dashboard/builders/table-routes/fragments.ts +217 -217
  61. package/cli/templates/dashboard/builders/table-routes/helpers.ts +45 -45
  62. package/cli/templates/dashboard/builders/table-routes/index.ts +8 -8
  63. package/cli/templates/dashboard/builders/table-routes/table/actions-cell.ts +71 -71
  64. package/cli/templates/dashboard/builders/table-routes/table/get-route.ts +291 -291
  65. package/cli/templates/dashboard/builders/table-routes/table/index.ts +80 -80
  66. package/cli/templates/dashboard/builders/table-routes/table/post-routes.ts +163 -163
  67. package/cli/templates/dashboard/builders/table-routes/table-route.ts +7 -7
  68. package/cli/templates/dashboard/builders/table-routes/users/get-route.ts +69 -69
  69. package/cli/templates/dashboard/builders/table-routes/users/html/modals.ts +57 -57
  70. package/cli/templates/dashboard/builders/table-routes/users/html/page.ts +27 -27
  71. package/cli/templates/dashboard/builders/table-routes/users/html/table.ts +128 -128
  72. package/cli/templates/dashboard/builders/table-routes/users/index.ts +32 -32
  73. package/cli/templates/dashboard/builders/table-routes/users/post-routes.ts +150 -150
  74. package/cli/templates/dashboard/builders/table-routes/users/redirect.ts +14 -14
  75. package/cli/templates/dashboard/builders/table-routes/users-route.ts +10 -10
  76. package/cli/templates/dashboard/components/dashboard-home.ts +23 -23
  77. package/cli/templates/dashboard/components/layout.ts +420 -420
  78. package/cli/templates/dashboard/components/login-page.ts +65 -65
  79. package/cli/templates/dashboard/index.ts +61 -61
  80. package/cli/templates/dashboard/types.ts +9 -9
  81. package/cli/templates/handlers/README.md +353 -353
  82. package/cli/templates/handlers/auth.ts +37 -37
  83. package/cli/templates/handlers/execution.ts +42 -42
  84. package/cli/templates/handlers/generators/context/context-creation.ts +101 -101
  85. package/cli/templates/handlers/generators/context/error-helpers.ts +11 -11
  86. package/cli/templates/handlers/generators/context/scheduler.ts +24 -24
  87. package/cli/templates/handlers/generators/context/storage-api.ts +82 -82
  88. package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
  89. package/cli/templates/handlers/generators/context/types.ts +40 -40
  90. package/cli/templates/handlers/generators/context.ts +43 -43
  91. package/cli/templates/handlers/generators/execution.ts +15 -15
  92. package/cli/templates/handlers/generators/handlers.ts +14 -14
  93. package/cli/templates/handlers/generators/registration/modules/cron.ts +26 -26
  94. package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -75
  95. package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -144
  96. package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +14 -14
  97. package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +102 -102
  98. package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +164 -164
  99. package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -30
  100. package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +510 -510
  101. package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -56
  102. package/cli/templates/handlers/generators/registration/modules/storage.ts +199 -199
  103. package/cli/templates/handlers/generators/registration/sections.ts +210 -210
  104. package/cli/templates/handlers/generators/types/context.ts +121 -121
  105. package/cli/templates/handlers/generators/types/core.ts +106 -106
  106. package/cli/templates/handlers/generators/types/operations.ts +135 -135
  107. package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +291 -271
  108. package/cli/templates/handlers/generators/types/query-definitions/query-api-types.ts +135 -135
  109. package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +1382 -1382
  110. package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -278
  111. package/cli/templates/handlers/generators/types/query-definitions.ts +13 -13
  112. package/cli/templates/handlers/generators/types/query-runtime/handled-error.ts +13 -13
  113. package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts +174 -174
  114. package/cli/templates/handlers/generators/types/query-runtime/runtime-read.ts +157 -157
  115. package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
  116. package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +958 -697
  117. package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
  118. package/cli/templates/handlers/index.ts +47 -47
  119. package/cli/templates/handlers/operations.ts +116 -116
  120. package/cli/templates/handlers/registration.ts +91 -91
  121. package/cli/templates/handlers/types.ts +17 -17
  122. package/cli/templates/handlers/utils.ts +48 -48
  123. package/cli/types.ts +110 -110
  124. package/cli/utils/handler-discovery.ts +466 -466
  125. package/cli/utils/json-utils.ts +24 -24
  126. package/cli/utils/path-utils.ts +19 -19
  127. package/cli/utils/schema-discovery.ts +399 -399
  128. package/dist/cli/index.js +320 -31
  129. package/dist/cli/index.mjs +320 -31
  130. package/dist/index.js +1 -1
  131. package/dist/index.mjs +1 -1
  132. package/dist/react/index.d.mts +2 -0
  133. package/dist/react/index.d.ts +2 -0
  134. package/dist/react/index.js +1 -1
  135. package/dist/react/index.mjs +1 -1
  136. package/index.ts +18 -18
  137. package/package.json +58 -58
  138. package/react/index.ts +5 -5
  139. package/react/use-infinite-query.ts +255 -252
  140. package/react/use-mutation.ts +89 -89
  141. package/react/use-query.ts +210 -207
  142. package/schema.ts +641 -641
  143. package/test-better-auth-hash.ts +2 -2
  144. package/tsconfig.json +6 -6
  145. package/tsup.config.ts +82 -82
@@ -1,466 +1,466 @@
1
- import { readdir } from "node:fs/promises";
2
- import { extname, relative, resolve } from "node:path";
3
- import * as ts from "typescript";
4
- import type { LoadedAppflareConfig } from "../types";
5
- import { ensureRelativeImportPath } from "./path-utils";
6
-
7
- export type HandlerKind =
8
- | "query"
9
- | "mutation"
10
- | "scheduler"
11
- | "cron"
12
- | "storage";
13
-
14
- export type DiscoveredArgField = {
15
- name: string;
16
- type: "string" | "number" | "boolean" | "unknown";
17
- optional: boolean;
18
- defaultValue?: string;
19
- };
20
-
21
- export type DiscoveredHandlerOperation = {
22
- kind: HandlerKind;
23
- exportName: string;
24
- filePath: string;
25
- importPath: string;
26
- clientImportPath: string;
27
- routePath: string;
28
- handlerName?: string;
29
- clientSegments?: string[];
30
- taskName?: string;
31
- cronTriggers?: string[];
32
- args?: DiscoveredArgField[];
33
- };
34
-
35
- const supportedExtensions = new Set([".ts", ".tsx", ".mts", ".cts"]);
36
-
37
- async function discoverFiles(dirPath: string): Promise<string[]> {
38
- const entries = await readdir(dirPath, { withFileTypes: true });
39
- const files: string[] = [];
40
-
41
- for (const entry of entries) {
42
- if (entry.name.startsWith(".")) {
43
- continue;
44
- }
45
- if (entry.name === "node_modules" || entry.name === "_generated") {
46
- continue;
47
- }
48
-
49
- const entryPath = resolve(dirPath, entry.name);
50
- if (entry.isDirectory()) {
51
- files.push(...(await discoverFiles(entryPath)));
52
- continue;
53
- }
54
-
55
- if (entry.isFile() && supportedExtensions.has(extname(entry.name))) {
56
- files.push(entryPath);
57
- }
58
- }
59
-
60
- return files;
61
- }
62
-
63
- function withoutExtension(filePath: string): string {
64
- return filePath.replace(/\.[cm]?tsx?$/, "");
65
- }
66
-
67
- type DiscoveredExport = {
68
- exportName: string;
69
- kind: HandlerKind;
70
- cronTriggers: string[];
71
- args: DiscoveredArgField[];
72
- };
73
-
74
- /**
75
- * Walk a Zod call chain like z.string().optional().default("x")
76
- * and return the base type name + whether optional/default are applied.
77
- */
78
- function readZodArgField(
79
- expression: ts.Expression,
80
- name: string,
81
- ): DiscoveredArgField {
82
- let node: ts.Expression = expression;
83
- let optional = false;
84
- let defaultValue: string | undefined;
85
- let baseType: DiscoveredArgField["type"] = "unknown";
86
-
87
- // Walk the call chain: z.string().optional().default("x")
88
- // Each iteration processes one layer (e.g. .default, .optional, .string)
89
- while (ts.isCallExpression(node)) {
90
- const expr = node.expression;
91
- if (!ts.isPropertyAccessExpression(expr)) break;
92
-
93
- const prop = expr.name.text;
94
-
95
- if (prop === "optional" || prop === "nullable") {
96
- optional = true;
97
- node = expr.expression;
98
- } else if (prop === "default") {
99
- optional = true;
100
- const arg = node.arguments[0];
101
- if (arg) {
102
- if (ts.isStringLiteral(arg)) defaultValue = arg.text;
103
- else if (ts.isNumericLiteral(arg)) defaultValue = arg.text;
104
- else if (arg.kind === ts.SyntaxKind.TrueKeyword) defaultValue = "true";
105
- else if (arg.kind === ts.SyntaxKind.FalseKeyword)
106
- defaultValue = "false";
107
- }
108
- node = expr.expression;
109
- } else if (
110
- prop === "string" ||
111
- prop === "uuid" ||
112
- prop === "email" ||
113
- prop === "url"
114
- ) {
115
- baseType = "string";
116
- break;
117
- } else if (prop === "number" || prop === "int" || prop === "float") {
118
- baseType = "number";
119
- break;
120
- } else if (prop === "boolean") {
121
- baseType = "boolean";
122
- break;
123
- } else {
124
- // unknown modifier (.min, .max, .trim, etc.) — keep walking inward
125
- node = expr.expression;
126
- }
127
- }
128
-
129
- return { name, type: baseType, optional, defaultValue };
130
- }
131
-
132
- function readArgsFields(
133
- definitionArg: ts.Expression | undefined,
134
- ): DiscoveredArgField[] {
135
- if (!definitionArg || !ts.isObjectLiteralExpression(definitionArg)) {
136
- return [];
137
- }
138
-
139
- const argsProp = definitionArg.properties.find(
140
- (p): p is ts.PropertyAssignment =>
141
- ts.isPropertyAssignment(p) &&
142
- ts.isIdentifier(p.name) &&
143
- p.name.text === "args",
144
- );
145
-
146
- if (!argsProp || !ts.isObjectLiteralExpression(argsProp.initializer)) {
147
- return [];
148
- }
149
-
150
- const fields: DiscoveredArgField[] = [];
151
- for (const prop of argsProp.initializer.properties) {
152
- if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name)) {
153
- continue;
154
- }
155
- fields.push(readZodArgField(prop.initializer, prop.name.text));
156
- }
157
- return fields;
158
- }
159
-
160
- function isExportedConst(
161
- statement: ts.Statement,
162
- ): statement is ts.VariableStatement {
163
- if (!ts.isVariableStatement(statement)) {
164
- return false;
165
- }
166
-
167
- return (
168
- statement.modifiers?.some(
169
- (modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
170
- ) ?? false
171
- );
172
- }
173
-
174
- function readIdentifierText(expression: ts.Expression): string | null {
175
- if (ts.isIdentifier(expression)) {
176
- return expression.text;
177
- }
178
-
179
- if (ts.isParenthesizedExpression(expression)) {
180
- return readIdentifierText(expression.expression);
181
- }
182
-
183
- return null;
184
- }
185
-
186
- function readCronTriggers(argument: ts.Expression | undefined): string[] {
187
- if (!argument || !ts.isObjectLiteralExpression(argument)) {
188
- return [];
189
- }
190
-
191
- const cronProperty = argument.properties.find((property) => {
192
- if (!ts.isPropertyAssignment(property)) {
193
- return false;
194
- }
195
-
196
- if (!ts.isIdentifier(property.name)) {
197
- return false;
198
- }
199
-
200
- return property.name.text === "cronTrigger";
201
- });
202
-
203
- if (!cronProperty || !ts.isPropertyAssignment(cronProperty)) {
204
- return [];
205
- }
206
-
207
- const initializer = cronProperty.initializer;
208
- if (
209
- ts.isStringLiteral(initializer) ||
210
- ts.isNoSubstitutionTemplateLiteral(initializer)
211
- ) {
212
- return [initializer.text.trim()].filter((value) => value.length > 0);
213
- }
214
-
215
- if (ts.isArrayLiteralExpression(initializer)) {
216
- return initializer.elements
217
- .map((element) => {
218
- if (
219
- ts.isStringLiteral(element) ||
220
- ts.isNoSubstitutionTemplateLiteral(element)
221
- ) {
222
- return element.text.trim();
223
- }
224
- return "";
225
- })
226
- .filter((value) => value.length > 0);
227
- }
228
-
229
- return [];
230
- }
231
-
232
- function discoverExports(source: string, filePath: string): DiscoveredExport[] {
233
- const sourceFile = ts.createSourceFile(
234
- filePath,
235
- source,
236
- ts.ScriptTarget.Latest,
237
- true,
238
- ts.ScriptKind.TS,
239
- );
240
- const exports: DiscoveredExport[] = [];
241
-
242
- for (const statement of sourceFile.statements) {
243
- if (!isExportedConst(statement)) {
244
- continue;
245
- }
246
-
247
- for (const declaration of statement.declarationList.declarations) {
248
- if (!ts.isIdentifier(declaration.name) || !declaration.initializer) {
249
- continue;
250
- }
251
-
252
- if (!ts.isCallExpression(declaration.initializer)) {
253
- continue;
254
- }
255
-
256
- const callee = readIdentifierText(declaration.initializer.expression);
257
- if (
258
- callee !== "query" &&
259
- callee !== "mutation" &&
260
- callee !== "scheduler" &&
261
- callee !== "cron" &&
262
- callee !== "storageManager"
263
- ) {
264
- continue;
265
- }
266
-
267
- exports.push({
268
- exportName: declaration.name.text,
269
- kind: callee === "storageManager" ? "storage" : callee,
270
- cronTriggers:
271
- callee === "cron"
272
- ? readCronTriggers(declaration.initializer.arguments[0])
273
- : [],
274
- args:
275
- callee === "query" || callee === "mutation"
276
- ? readArgsFields(declaration.initializer.arguments[0])
277
- : [],
278
- });
279
- }
280
- }
281
-
282
- return exports;
283
- }
284
-
285
- function buildRoutePath(
286
- kindDirectory: "queries" | "mutations",
287
- relativeFilePath: string,
288
- exportName: string,
289
- ): string {
290
- const normalizedRelativePath = relativeFilePath.replace(/\\/g, "/");
291
- const relativeWithoutExtension = withoutExtension(normalizedRelativePath);
292
- const segments = relativeWithoutExtension.split("/").filter(Boolean);
293
- const routeSegments = [kindDirectory, ...segments, exportName]
294
- .filter(Boolean)
295
- .map((segment) => segment.trim())
296
- .filter((segment) => segment.length > 0);
297
-
298
- return `/${routeSegments.join("/")}`;
299
- }
300
-
301
- function relativePathForKind(
302
- relativeToScanDir: string,
303
- kindDirectory: "queries" | "mutations" | "schedulers" | "crons",
304
- ): string {
305
- const normalized = relativeToScanDir.replace(/\\/g, "/");
306
- const marker = `${kindDirectory}/`;
307
- const markerIndex = normalized.indexOf(marker);
308
-
309
- if (markerIndex >= 0) {
310
- return normalized.slice(markerIndex + marker.length);
311
- }
312
-
313
- if (normalized === kindDirectory) {
314
- return "index.ts";
315
- }
316
-
317
- return normalized;
318
- }
319
-
320
- function buildTaskName(relativeFilePath: string, exportName: string): string {
321
- const normalizedRelativePath = relativeFilePath.replace(/\\/g, "/");
322
- const relativeWithoutExtension = withoutExtension(normalizedRelativePath);
323
- const segments = relativeWithoutExtension.split("/").filter(Boolean);
324
- const fileName = segments[segments.length - 1] ?? "index";
325
- const dirName = segments.length > 1 ? segments[segments.length - 2] : "root";
326
-
327
- return [dirName, fileName, exportName]
328
- .map((segment) => segment.trim())
329
- .filter((segment) => segment.length > 0)
330
- .join("/");
331
- }
332
-
333
- export async function discoverHandlerOperations(
334
- loadedConfig: LoadedAppflareConfig,
335
- ): Promise<DiscoveredHandlerOperation[]> {
336
- const operations: DiscoveredHandlerOperation[] = [];
337
-
338
- const files = await discoverFiles(loadedConfig.scanDirAbs).catch(() => []);
339
- for (const filePath of files) {
340
- const sourceFile = Bun.file(filePath);
341
- if (!(await sourceFile.exists())) {
342
- continue;
343
- }
344
-
345
- const source = await sourceFile.text();
346
- const relativeToScanDir = relative(loadedConfig.scanDirAbs, filePath);
347
- const discoveredExports = discoverExports(source, filePath);
348
-
349
- const discoveredKinds: Array<{
350
- kind: HandlerKind;
351
- kindDirectory: "queries" | "mutations" | "schedulers" | "crons";
352
- exports: DiscoveredExport[];
353
- }> = [
354
- {
355
- kind: "query",
356
- kindDirectory: "queries",
357
- exports: discoveredExports.filter((entry) => entry.kind === "query"),
358
- },
359
- {
360
- kind: "mutation",
361
- kindDirectory: "mutations",
362
- exports: discoveredExports.filter((entry) => entry.kind === "mutation"),
363
- },
364
- {
365
- kind: "scheduler",
366
- kindDirectory: "schedulers",
367
- exports: discoveredExports.filter(
368
- (entry) => entry.kind === "scheduler",
369
- ),
370
- },
371
- {
372
- kind: "cron",
373
- kindDirectory: "crons",
374
- exports: discoveredExports.filter((entry) => entry.kind === "cron"),
375
- },
376
- {
377
- kind: "storage",
378
- kindDirectory: "queries",
379
- exports: discoveredExports.filter((entry) => entry.kind === "storage"),
380
- },
381
- ];
382
-
383
- for (const discovered of discoveredKinds) {
384
- if (discovered.exports.length === 0) {
385
- continue;
386
- }
387
-
388
- const relativeForKind = relativePathForKind(
389
- relativeToScanDir,
390
- discovered.kindDirectory,
391
- );
392
-
393
- for (const discoveredExport of discovered.exports) {
394
- const handlerName =
395
- discovered.kind === "query" || discovered.kind === "mutation"
396
- ? buildTaskName(relativeForKind, discoveredExport.exportName)
397
- : undefined;
398
- const taskName =
399
- discovered.kind === "scheduler" || discovered.kind === "cron"
400
- ? buildTaskName(relativeForKind, discoveredExport.exportName)
401
- : undefined;
402
- const clientSegments =
403
- discovered.kind === "query" || discovered.kind === "mutation"
404
- ? [
405
- ...withoutExtension(relativeForKind).split("/").filter(Boolean),
406
- discoveredExport.exportName,
407
- ]
408
- : undefined;
409
- const routePath =
410
- discovered.kind === "query"
411
- ? buildRoutePath(
412
- "queries",
413
- relativeForKind,
414
- discoveredExport.exportName,
415
- )
416
- : discovered.kind === "mutation"
417
- ? buildRoutePath(
418
- "mutations",
419
- relativeForKind,
420
- discoveredExport.exportName,
421
- )
422
- : discovered.kind === "storage"
423
- ? `/storage/managers/${discoveredExport.exportName}`
424
- : `/${discovered.kindDirectory}/${taskName}`;
425
-
426
- operations.push({
427
- kind: discovered.kind,
428
- exportName: discoveredExport.exportName,
429
- filePath,
430
- importPath: ensureRelativeImportPath(
431
- loadedConfig.outDirAbs,
432
- filePath,
433
- ),
434
- clientImportPath: ensureRelativeImportPath(
435
- resolve(loadedConfig.outDirAbs, "client"),
436
- filePath,
437
- ),
438
- routePath,
439
- handlerName,
440
- clientSegments,
441
- taskName,
442
- cronTriggers: discoveredExport.cronTriggers,
443
- args: discoveredExport.args,
444
- });
445
- }
446
- }
447
- }
448
-
449
- operations.sort((a, b) => a.routePath.localeCompare(b.routePath));
450
-
451
- const seen = new Map<string, string>();
452
- for (const operation of operations) {
453
- const uniqueKey = operation.taskName
454
- ? `task:${operation.taskName}`
455
- : `route:${operation.routePath}`;
456
- const previous = seen.get(uniqueKey);
457
- if (previous) {
458
- throw new Error(
459
- `Duplicate handler operation discovered: ${operation.taskName ?? operation.routePath} (${previous} and ${operation.filePath}#${operation.exportName}).`,
460
- );
461
- }
462
- seen.set(uniqueKey, `${operation.filePath}#${operation.exportName}`);
463
- }
464
-
465
- return operations;
466
- }
1
+ import { readdir } from "node:fs/promises";
2
+ import { extname, relative, resolve } from "node:path";
3
+ import * as ts from "typescript";
4
+ import type { LoadedAppflareConfig } from "../types";
5
+ import { ensureRelativeImportPath } from "./path-utils";
6
+
7
+ export type HandlerKind =
8
+ | "query"
9
+ | "mutation"
10
+ | "scheduler"
11
+ | "cron"
12
+ | "storage";
13
+
14
+ export type DiscoveredArgField = {
15
+ name: string;
16
+ type: "string" | "number" | "boolean" | "unknown";
17
+ optional: boolean;
18
+ defaultValue?: string;
19
+ };
20
+
21
+ export type DiscoveredHandlerOperation = {
22
+ kind: HandlerKind;
23
+ exportName: string;
24
+ filePath: string;
25
+ importPath: string;
26
+ clientImportPath: string;
27
+ routePath: string;
28
+ handlerName?: string;
29
+ clientSegments?: string[];
30
+ taskName?: string;
31
+ cronTriggers?: string[];
32
+ args?: DiscoveredArgField[];
33
+ };
34
+
35
+ const supportedExtensions = new Set([".ts", ".tsx", ".mts", ".cts"]);
36
+
37
+ async function discoverFiles(dirPath: string): Promise<string[]> {
38
+ const entries = await readdir(dirPath, { withFileTypes: true });
39
+ const files: string[] = [];
40
+
41
+ for (const entry of entries) {
42
+ if (entry.name.startsWith(".")) {
43
+ continue;
44
+ }
45
+ if (entry.name === "node_modules" || entry.name === "_generated") {
46
+ continue;
47
+ }
48
+
49
+ const entryPath = resolve(dirPath, entry.name);
50
+ if (entry.isDirectory()) {
51
+ files.push(...(await discoverFiles(entryPath)));
52
+ continue;
53
+ }
54
+
55
+ if (entry.isFile() && supportedExtensions.has(extname(entry.name))) {
56
+ files.push(entryPath);
57
+ }
58
+ }
59
+
60
+ return files;
61
+ }
62
+
63
+ function withoutExtension(filePath: string): string {
64
+ return filePath.replace(/\.[cm]?tsx?$/, "");
65
+ }
66
+
67
+ type DiscoveredExport = {
68
+ exportName: string;
69
+ kind: HandlerKind;
70
+ cronTriggers: string[];
71
+ args: DiscoveredArgField[];
72
+ };
73
+
74
+ /**
75
+ * Walk a Zod call chain like z.string().optional().default("x")
76
+ * and return the base type name + whether optional/default are applied.
77
+ */
78
+ function readZodArgField(
79
+ expression: ts.Expression,
80
+ name: string,
81
+ ): DiscoveredArgField {
82
+ let node: ts.Expression = expression;
83
+ let optional = false;
84
+ let defaultValue: string | undefined;
85
+ let baseType: DiscoveredArgField["type"] = "unknown";
86
+
87
+ // Walk the call chain: z.string().optional().default("x")
88
+ // Each iteration processes one layer (e.g. .default, .optional, .string)
89
+ while (ts.isCallExpression(node)) {
90
+ const expr = node.expression;
91
+ if (!ts.isPropertyAccessExpression(expr)) break;
92
+
93
+ const prop = expr.name.text;
94
+
95
+ if (prop === "optional" || prop === "nullable") {
96
+ optional = true;
97
+ node = expr.expression;
98
+ } else if (prop === "default") {
99
+ optional = true;
100
+ const arg = node.arguments[0];
101
+ if (arg) {
102
+ if (ts.isStringLiteral(arg)) defaultValue = arg.text;
103
+ else if (ts.isNumericLiteral(arg)) defaultValue = arg.text;
104
+ else if (arg.kind === ts.SyntaxKind.TrueKeyword) defaultValue = "true";
105
+ else if (arg.kind === ts.SyntaxKind.FalseKeyword)
106
+ defaultValue = "false";
107
+ }
108
+ node = expr.expression;
109
+ } else if (
110
+ prop === "string" ||
111
+ prop === "uuid" ||
112
+ prop === "email" ||
113
+ prop === "url"
114
+ ) {
115
+ baseType = "string";
116
+ break;
117
+ } else if (prop === "number" || prop === "int" || prop === "float") {
118
+ baseType = "number";
119
+ break;
120
+ } else if (prop === "boolean") {
121
+ baseType = "boolean";
122
+ break;
123
+ } else {
124
+ // unknown modifier (.min, .max, .trim, etc.) — keep walking inward
125
+ node = expr.expression;
126
+ }
127
+ }
128
+
129
+ return { name, type: baseType, optional, defaultValue };
130
+ }
131
+
132
+ function readArgsFields(
133
+ definitionArg: ts.Expression | undefined,
134
+ ): DiscoveredArgField[] {
135
+ if (!definitionArg || !ts.isObjectLiteralExpression(definitionArg)) {
136
+ return [];
137
+ }
138
+
139
+ const argsProp = definitionArg.properties.find(
140
+ (p): p is ts.PropertyAssignment =>
141
+ ts.isPropertyAssignment(p) &&
142
+ ts.isIdentifier(p.name) &&
143
+ p.name.text === "args",
144
+ );
145
+
146
+ if (!argsProp || !ts.isObjectLiteralExpression(argsProp.initializer)) {
147
+ return [];
148
+ }
149
+
150
+ const fields: DiscoveredArgField[] = [];
151
+ for (const prop of argsProp.initializer.properties) {
152
+ if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name)) {
153
+ continue;
154
+ }
155
+ fields.push(readZodArgField(prop.initializer, prop.name.text));
156
+ }
157
+ return fields;
158
+ }
159
+
160
+ function isExportedConst(
161
+ statement: ts.Statement,
162
+ ): statement is ts.VariableStatement {
163
+ if (!ts.isVariableStatement(statement)) {
164
+ return false;
165
+ }
166
+
167
+ return (
168
+ statement.modifiers?.some(
169
+ (modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
170
+ ) ?? false
171
+ );
172
+ }
173
+
174
+ function readIdentifierText(expression: ts.Expression): string | null {
175
+ if (ts.isIdentifier(expression)) {
176
+ return expression.text;
177
+ }
178
+
179
+ if (ts.isParenthesizedExpression(expression)) {
180
+ return readIdentifierText(expression.expression);
181
+ }
182
+
183
+ return null;
184
+ }
185
+
186
+ function readCronTriggers(argument: ts.Expression | undefined): string[] {
187
+ if (!argument || !ts.isObjectLiteralExpression(argument)) {
188
+ return [];
189
+ }
190
+
191
+ const cronProperty = argument.properties.find((property) => {
192
+ if (!ts.isPropertyAssignment(property)) {
193
+ return false;
194
+ }
195
+
196
+ if (!ts.isIdentifier(property.name)) {
197
+ return false;
198
+ }
199
+
200
+ return property.name.text === "cronTrigger";
201
+ });
202
+
203
+ if (!cronProperty || !ts.isPropertyAssignment(cronProperty)) {
204
+ return [];
205
+ }
206
+
207
+ const initializer = cronProperty.initializer;
208
+ if (
209
+ ts.isStringLiteral(initializer) ||
210
+ ts.isNoSubstitutionTemplateLiteral(initializer)
211
+ ) {
212
+ return [initializer.text.trim()].filter((value) => value.length > 0);
213
+ }
214
+
215
+ if (ts.isArrayLiteralExpression(initializer)) {
216
+ return initializer.elements
217
+ .map((element) => {
218
+ if (
219
+ ts.isStringLiteral(element) ||
220
+ ts.isNoSubstitutionTemplateLiteral(element)
221
+ ) {
222
+ return element.text.trim();
223
+ }
224
+ return "";
225
+ })
226
+ .filter((value) => value.length > 0);
227
+ }
228
+
229
+ return [];
230
+ }
231
+
232
+ function discoverExports(source: string, filePath: string): DiscoveredExport[] {
233
+ const sourceFile = ts.createSourceFile(
234
+ filePath,
235
+ source,
236
+ ts.ScriptTarget.Latest,
237
+ true,
238
+ ts.ScriptKind.TS,
239
+ );
240
+ const exports: DiscoveredExport[] = [];
241
+
242
+ for (const statement of sourceFile.statements) {
243
+ if (!isExportedConst(statement)) {
244
+ continue;
245
+ }
246
+
247
+ for (const declaration of statement.declarationList.declarations) {
248
+ if (!ts.isIdentifier(declaration.name) || !declaration.initializer) {
249
+ continue;
250
+ }
251
+
252
+ if (!ts.isCallExpression(declaration.initializer)) {
253
+ continue;
254
+ }
255
+
256
+ const callee = readIdentifierText(declaration.initializer.expression);
257
+ if (
258
+ callee !== "query" &&
259
+ callee !== "mutation" &&
260
+ callee !== "scheduler" &&
261
+ callee !== "cron" &&
262
+ callee !== "storageManager"
263
+ ) {
264
+ continue;
265
+ }
266
+
267
+ exports.push({
268
+ exportName: declaration.name.text,
269
+ kind: callee === "storageManager" ? "storage" : callee,
270
+ cronTriggers:
271
+ callee === "cron"
272
+ ? readCronTriggers(declaration.initializer.arguments[0])
273
+ : [],
274
+ args:
275
+ callee === "query" || callee === "mutation"
276
+ ? readArgsFields(declaration.initializer.arguments[0])
277
+ : [],
278
+ });
279
+ }
280
+ }
281
+
282
+ return exports;
283
+ }
284
+
285
+ function buildRoutePath(
286
+ kindDirectory: "queries" | "mutations",
287
+ relativeFilePath: string,
288
+ exportName: string,
289
+ ): string {
290
+ const normalizedRelativePath = relativeFilePath.replace(/\\/g, "/");
291
+ const relativeWithoutExtension = withoutExtension(normalizedRelativePath);
292
+ const segments = relativeWithoutExtension.split("/").filter(Boolean);
293
+ const routeSegments = [kindDirectory, ...segments, exportName]
294
+ .filter(Boolean)
295
+ .map((segment) => segment.trim())
296
+ .filter((segment) => segment.length > 0);
297
+
298
+ return `/${routeSegments.join("/")}`;
299
+ }
300
+
301
+ function relativePathForKind(
302
+ relativeToScanDir: string,
303
+ kindDirectory: "queries" | "mutations" | "schedulers" | "crons",
304
+ ): string {
305
+ const normalized = relativeToScanDir.replace(/\\/g, "/");
306
+ const marker = `${kindDirectory}/`;
307
+ const markerIndex = normalized.indexOf(marker);
308
+
309
+ if (markerIndex >= 0) {
310
+ return normalized.slice(markerIndex + marker.length);
311
+ }
312
+
313
+ if (normalized === kindDirectory) {
314
+ return "index.ts";
315
+ }
316
+
317
+ return normalized;
318
+ }
319
+
320
+ function buildTaskName(relativeFilePath: string, exportName: string): string {
321
+ const normalizedRelativePath = relativeFilePath.replace(/\\/g, "/");
322
+ const relativeWithoutExtension = withoutExtension(normalizedRelativePath);
323
+ const segments = relativeWithoutExtension.split("/").filter(Boolean);
324
+ const fileName = segments[segments.length - 1] ?? "index";
325
+ const dirName = segments.length > 1 ? segments[segments.length - 2] : "root";
326
+
327
+ return [dirName, fileName, exportName]
328
+ .map((segment) => segment.trim())
329
+ .filter((segment) => segment.length > 0)
330
+ .join("/");
331
+ }
332
+
333
+ export async function discoverHandlerOperations(
334
+ loadedConfig: LoadedAppflareConfig,
335
+ ): Promise<DiscoveredHandlerOperation[]> {
336
+ const operations: DiscoveredHandlerOperation[] = [];
337
+
338
+ const files = await discoverFiles(loadedConfig.scanDirAbs).catch(() => []);
339
+ for (const filePath of files) {
340
+ const sourceFile = Bun.file(filePath);
341
+ if (!(await sourceFile.exists())) {
342
+ continue;
343
+ }
344
+
345
+ const source = await sourceFile.text();
346
+ const relativeToScanDir = relative(loadedConfig.scanDirAbs, filePath);
347
+ const discoveredExports = discoverExports(source, filePath);
348
+
349
+ const discoveredKinds: Array<{
350
+ kind: HandlerKind;
351
+ kindDirectory: "queries" | "mutations" | "schedulers" | "crons";
352
+ exports: DiscoveredExport[];
353
+ }> = [
354
+ {
355
+ kind: "query",
356
+ kindDirectory: "queries",
357
+ exports: discoveredExports.filter((entry) => entry.kind === "query"),
358
+ },
359
+ {
360
+ kind: "mutation",
361
+ kindDirectory: "mutations",
362
+ exports: discoveredExports.filter((entry) => entry.kind === "mutation"),
363
+ },
364
+ {
365
+ kind: "scheduler",
366
+ kindDirectory: "schedulers",
367
+ exports: discoveredExports.filter(
368
+ (entry) => entry.kind === "scheduler",
369
+ ),
370
+ },
371
+ {
372
+ kind: "cron",
373
+ kindDirectory: "crons",
374
+ exports: discoveredExports.filter((entry) => entry.kind === "cron"),
375
+ },
376
+ {
377
+ kind: "storage",
378
+ kindDirectory: "queries",
379
+ exports: discoveredExports.filter((entry) => entry.kind === "storage"),
380
+ },
381
+ ];
382
+
383
+ for (const discovered of discoveredKinds) {
384
+ if (discovered.exports.length === 0) {
385
+ continue;
386
+ }
387
+
388
+ const relativeForKind = relativePathForKind(
389
+ relativeToScanDir,
390
+ discovered.kindDirectory,
391
+ );
392
+
393
+ for (const discoveredExport of discovered.exports) {
394
+ const handlerName =
395
+ discovered.kind === "query" || discovered.kind === "mutation"
396
+ ? buildTaskName(relativeForKind, discoveredExport.exportName)
397
+ : undefined;
398
+ const taskName =
399
+ discovered.kind === "scheduler" || discovered.kind === "cron"
400
+ ? buildTaskName(relativeForKind, discoveredExport.exportName)
401
+ : undefined;
402
+ const clientSegments =
403
+ discovered.kind === "query" || discovered.kind === "mutation"
404
+ ? [
405
+ ...withoutExtension(relativeForKind).split("/").filter(Boolean),
406
+ discoveredExport.exportName,
407
+ ]
408
+ : undefined;
409
+ const routePath =
410
+ discovered.kind === "query"
411
+ ? buildRoutePath(
412
+ "queries",
413
+ relativeForKind,
414
+ discoveredExport.exportName,
415
+ )
416
+ : discovered.kind === "mutation"
417
+ ? buildRoutePath(
418
+ "mutations",
419
+ relativeForKind,
420
+ discoveredExport.exportName,
421
+ )
422
+ : discovered.kind === "storage"
423
+ ? `/storage/managers/${discoveredExport.exportName}`
424
+ : `/${discovered.kindDirectory}/${taskName}`;
425
+
426
+ operations.push({
427
+ kind: discovered.kind,
428
+ exportName: discoveredExport.exportName,
429
+ filePath,
430
+ importPath: ensureRelativeImportPath(
431
+ loadedConfig.outDirAbs,
432
+ filePath,
433
+ ),
434
+ clientImportPath: ensureRelativeImportPath(
435
+ resolve(loadedConfig.outDirAbs, "client"),
436
+ filePath,
437
+ ),
438
+ routePath,
439
+ handlerName,
440
+ clientSegments,
441
+ taskName,
442
+ cronTriggers: discoveredExport.cronTriggers,
443
+ args: discoveredExport.args,
444
+ });
445
+ }
446
+ }
447
+ }
448
+
449
+ operations.sort((a, b) => a.routePath.localeCompare(b.routePath));
450
+
451
+ const seen = new Map<string, string>();
452
+ for (const operation of operations) {
453
+ const uniqueKey = operation.taskName
454
+ ? `task:${operation.taskName}`
455
+ : `route:${operation.routePath}`;
456
+ const previous = seen.get(uniqueKey);
457
+ if (previous) {
458
+ throw new Error(
459
+ `Duplicate handler operation discovered: ${operation.taskName ?? operation.routePath} (${previous} and ${operation.filePath}#${operation.exportName}).`,
460
+ );
461
+ }
462
+ seen.set(uniqueKey, `${operation.filePath}#${operation.exportName}`);
463
+ }
464
+
465
+ return operations;
466
+ }