appflare 0.0.28 → 0.1.1

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 (151) 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 +747 -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 +353 -0
  34. package/cli/templates/handlers/auth.ts +37 -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/generators/registration/modules/cron.ts +30 -0
  46. package/cli/templates/handlers/generators/registration/modules/realtime.ts +631 -0
  47. package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -0
  48. package/cli/templates/handlers/generators/registration/modules/storage.ts +194 -0
  49. package/cli/templates/handlers/generators/registration/sections.ts +210 -0
  50. package/cli/templates/handlers/generators/types/context.ts +64 -0
  51. package/cli/templates/handlers/generators/types/core.ts +101 -0
  52. package/cli/templates/handlers/generators/types/operations.ts +135 -0
  53. package/cli/templates/handlers/generators/types/query-definitions.ts +1029 -0
  54. package/cli/templates/handlers/generators/types/query-runtime.ts +417 -0
  55. package/cli/templates/handlers/index.ts +43 -0
  56. package/cli/templates/handlers/operations.ts +116 -0
  57. package/cli/templates/handlers/registration.ts +83 -0
  58. package/cli/templates/handlers/types.ts +15 -0
  59. package/cli/templates/handlers/utils.ts +48 -0
  60. package/cli/types.ts +108 -0
  61. package/cli/utils/handler-discovery.ts +366 -0
  62. package/cli/utils/json-utils.ts +24 -0
  63. package/cli/utils/path-utils.ts +19 -0
  64. package/cli/utils/schema-discovery.ts +390 -0
  65. package/index.ts +27 -4
  66. package/package.json +23 -20
  67. package/react/index.ts +5 -3
  68. package/react/use-infinite-query.ts +190 -0
  69. package/react/use-mutation.ts +54 -0
  70. package/react/use-query.ts +158 -0
  71. package/schema.ts +262 -0
  72. package/tsconfig.json +2 -4
  73. package/cli/README.md +0 -108
  74. package/cli/core/build.ts +0 -187
  75. package/cli/core/config.ts +0 -92
  76. package/cli/core/discover-handlers.ts +0 -143
  77. package/cli/core/handlers.ts +0 -7
  78. package/cli/core/index.ts +0 -205
  79. package/cli/generators/generate-api-client/client.ts +0 -163
  80. package/cli/generators/generate-api-client/extract-configuration.ts +0 -121
  81. package/cli/generators/generate-api-client/index.ts +0 -973
  82. package/cli/generators/generate-api-client/types.ts +0 -164
  83. package/cli/generators/generate-api-client/utils.ts +0 -22
  84. package/cli/generators/generate-api-client.ts +0 -1
  85. package/cli/generators/generate-cloudflare-worker/helpers.ts +0 -24
  86. package/cli/generators/generate-cloudflare-worker/index.ts +0 -2
  87. package/cli/generators/generate-cloudflare-worker/worker.ts +0 -148
  88. package/cli/generators/generate-cloudflare-worker/wrangler.ts +0 -108
  89. package/cli/generators/generate-cloudflare-worker.ts +0 -4
  90. package/cli/generators/generate-cron-handlers/cron-handlers-block.ts +0 -2
  91. package/cli/generators/generate-cron-handlers/handler-entries.ts +0 -29
  92. package/cli/generators/generate-cron-handlers/index.ts +0 -61
  93. package/cli/generators/generate-cron-handlers/runtime-block.ts +0 -49
  94. package/cli/generators/generate-cron-handlers/type-helpers-block.ts +0 -60
  95. package/cli/generators/generate-db-handlers/index.ts +0 -33
  96. package/cli/generators/generate-db-handlers/prepare.ts +0 -24
  97. package/cli/generators/generate-db-handlers/templates.ts +0 -189
  98. package/cli/generators/generate-db-handlers.ts +0 -1
  99. package/cli/generators/generate-hono-server/auth.ts +0 -97
  100. package/cli/generators/generate-hono-server/imports.ts +0 -55
  101. package/cli/generators/generate-hono-server/index.ts +0 -52
  102. package/cli/generators/generate-hono-server/routes.ts +0 -115
  103. package/cli/generators/generate-hono-server/template.ts +0 -371
  104. package/cli/generators/generate-hono-server.ts +0 -1
  105. package/cli/generators/generate-scheduler-handlers/constants.ts +0 -8
  106. package/cli/generators/generate-scheduler-handlers/handler-entries.ts +0 -22
  107. package/cli/generators/generate-scheduler-handlers/index.ts +0 -51
  108. package/cli/generators/generate-scheduler-handlers/runtime-block.ts +0 -68
  109. package/cli/generators/generate-scheduler-handlers/scheduler-handlers-block.ts +0 -2
  110. package/cli/generators/generate-scheduler-handlers/type-helpers-block.ts +0 -68
  111. package/cli/generators/generate-scheduler-handlers.ts +0 -1
  112. package/cli/generators/generate-websocket-durable-object/auth.ts +0 -30
  113. package/cli/generators/generate-websocket-durable-object/imports.ts +0 -55
  114. package/cli/generators/generate-websocket-durable-object/index.ts +0 -41
  115. package/cli/generators/generate-websocket-durable-object/query-handlers.ts +0 -18
  116. package/cli/generators/generate-websocket-durable-object/template.ts +0 -714
  117. package/cli/generators/generate-websocket-durable-object.ts +0 -1
  118. package/cli/schema/schema-static-types.ts +0 -702
  119. package/cli/schema/schema.ts +0 -151
  120. package/cli/utils/tsc.ts +0 -54
  121. package/cli/utils/utils.ts +0 -190
  122. package/cli/utils/zod-utils.ts +0 -121
  123. package/lib/README.md +0 -50
  124. package/lib/db.ts +0 -19
  125. package/lib/location.ts +0 -110
  126. package/lib/values.ts +0 -27
  127. package/react/README.md +0 -67
  128. package/react/hooks/useMutation.ts +0 -89
  129. package/react/hooks/usePaginatedQuery.ts +0 -213
  130. package/react/hooks/useQuery.ts +0 -106
  131. package/react/shared/queryShared.ts +0 -174
  132. package/server/README.md +0 -218
  133. package/server/auth.ts +0 -107
  134. package/server/database/builders.ts +0 -83
  135. package/server/database/context.ts +0 -327
  136. package/server/database/populate.ts +0 -234
  137. package/server/database/query-builder.ts +0 -161
  138. package/server/database/query-utils.ts +0 -25
  139. package/server/db.ts +0 -2
  140. package/server/storage/auth.ts +0 -16
  141. package/server/storage/bucket.ts +0 -22
  142. package/server/storage/context.ts +0 -34
  143. package/server/storage/index.ts +0 -38
  144. package/server/storage/operations.ts +0 -149
  145. package/server/storage/route-handler.ts +0 -60
  146. package/server/storage/types.ts +0 -55
  147. package/server/storage/utils.ts +0 -47
  148. package/server/storage.ts +0 -6
  149. package/server/types/schema-refs.ts +0 -66
  150. package/server/types/types.ts +0 -633
  151. package/server/utils/id-utils.ts +0 -230
@@ -0,0 +1,140 @@
1
+ import chokidar from "chokidar";
2
+ import { resolve } from "node:path";
3
+ import { generateArtifacts } from "../generate";
4
+ import { loadConfig } from "../load-config";
5
+
6
+ type MigrateOptions = {
7
+ local?: boolean;
8
+ remote?: boolean;
9
+ preview?: boolean;
10
+ };
11
+
12
+ export async function runBuild(configPath?: string): Promise<void> {
13
+ const loadedConfig = await loadConfig(configPath);
14
+ await generateArtifacts(loadedConfig);
15
+ if (loadedConfig.wranglerOutDirAbs === loadedConfig.outDirAbs) {
16
+ process.stdout.write(
17
+ `✅ Generated artifacts in ${loadedConfig.outDirAbs}\n`,
18
+ );
19
+ return;
20
+ }
21
+
22
+ process.stdout.write(
23
+ `✅ Generated server/client in ${loadedConfig.outDirAbs} and wrangler.json in ${loadedConfig.wranglerOutDirAbs}\n`,
24
+ );
25
+ }
26
+
27
+ export async function runDev(
28
+ configPath?: string,
29
+ watch = false,
30
+ ): Promise<void> {
31
+ await runBuild(configPath);
32
+
33
+ if (!watch) {
34
+ return;
35
+ }
36
+
37
+ const loadedConfig = await loadConfig(configPath);
38
+ let isRunning = false;
39
+ let shouldRerun = false;
40
+
41
+ const rebuild = async (): Promise<void> => {
42
+ if (isRunning) {
43
+ shouldRerun = true;
44
+ return;
45
+ }
46
+
47
+ isRunning = true;
48
+ try {
49
+ await runBuild(configPath);
50
+ } catch (error) {
51
+ process.stderr.write(`❌ Build failed: ${(error as Error).message}\n`);
52
+ } finally {
53
+ isRunning = false;
54
+ if (shouldRerun) {
55
+ shouldRerun = false;
56
+ await rebuild();
57
+ }
58
+ }
59
+ };
60
+
61
+ const watcher = chokidar.watch(loadedConfig.scanDirAbs, {
62
+ ignoreInitial: true,
63
+ });
64
+
65
+ watcher.on("all", async (_eventName, changedPath) => {
66
+ process.stdout.write(`🔄 Change detected: ${changedPath}\n`);
67
+ await rebuild();
68
+ });
69
+
70
+ process.stdout.write(`👀 Watching ${loadedConfig.scanDirAbs}\n`);
71
+ }
72
+
73
+ export async function runMigrate(
74
+ configPath?: string,
75
+ options: MigrateOptions = {},
76
+ ): Promise<void> {
77
+ const loadedConfig = await loadConfig(configPath);
78
+ const selectedTargetCount = [
79
+ Boolean(options.local),
80
+ Boolean(options.remote),
81
+ Boolean(options.preview),
82
+ ].filter(Boolean).length;
83
+
84
+ if (selectedTargetCount > 1) {
85
+ throw new Error("Only one of --local, --remote, or --preview can be set.");
86
+ }
87
+
88
+ const drizzleConfigPath = resolve(
89
+ loadedConfig.outDirAbs,
90
+ "drizzle.config.ts",
91
+ );
92
+ const drizzleGenerate = Bun.spawn(
93
+ ["npx", "drizzle-kit", "generate", "--config", drizzleConfigPath],
94
+ {
95
+ cwd: loadedConfig.configDir,
96
+ stdin: "inherit",
97
+ stdout: "inherit",
98
+ stderr: "inherit",
99
+ },
100
+ );
101
+
102
+ const drizzleExitCode = await drizzleGenerate.exited;
103
+ if (drizzleExitCode !== 0) {
104
+ throw new Error(
105
+ `drizzle-kit generate failed with exit code ${drizzleExitCode}`,
106
+ );
107
+ }
108
+
109
+ const databaseName = loadedConfig.config.database[0].databaseName;
110
+ const wranglerArgs = [
111
+ "npx",
112
+ "wrangler",
113
+ "d1",
114
+ "migrations",
115
+ "apply",
116
+ databaseName,
117
+ ];
118
+
119
+ if (options.local) {
120
+ wranglerArgs.push("--local");
121
+ } else if (options.remote) {
122
+ wranglerArgs.push("--remote");
123
+ } else if (options.preview) {
124
+ wranglerArgs.push("--preview");
125
+ }
126
+
127
+ const wranglerApply = Bun.spawn(wranglerArgs, {
128
+ cwd: loadedConfig.configDir,
129
+ stdin: "inherit",
130
+ stdout: "inherit",
131
+ stderr: "inherit",
132
+ });
133
+
134
+ const wranglerExitCode = await wranglerApply.exited;
135
+ if (wranglerExitCode !== 0) {
136
+ throw new Error(
137
+ `wrangler d1 migrations apply failed with exit code ${wranglerExitCode}`,
138
+ );
139
+ }
140
+ }
@@ -0,0 +1,149 @@
1
+ import { mkdir } from "node:fs/promises";
2
+ import { relative, resolve } from "node:path";
3
+ import { generateAuthConfigSource } from "./templates/auth/config";
4
+ import { generateClientArtifacts } from "./templates/core/client";
5
+ import { generateDrizzleConfigSource } from "./templates/core/drizzle";
6
+ import { generateHandlersArtifacts } from "./templates/core/handlers";
7
+ import { generateServerSource } from "./templates/core/server";
8
+ import { generateWranglerJson } from "./templates/core/wrangler";
9
+ import { compileSchemaDsl } from "./schema-compiler";
10
+ import type { LoadedAppflareConfig } from "./types";
11
+ import { discoverHandlerOperations } from "./utils/handler-discovery";
12
+ import { discoverSchema } from "./utils/schema-discovery";
13
+ import { ensureRelativeImportPath } from "./utils/path-utils";
14
+
15
+ function toConfigRelativePath(configDir: string, absolutePath: string): string {
16
+ const relativePath = relative(configDir, absolutePath).replace(/\\/g, "/");
17
+ return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
18
+ }
19
+
20
+ export async function generateArtifacts(
21
+ loadedConfig: LoadedAppflareConfig,
22
+ ): Promise<void> {
23
+ const { outDirAbs, wranglerOutDirAbs, config, configPath, configDir } =
24
+ loadedConfig;
25
+ const configImport = ensureRelativeImportPath(outDirAbs, configPath);
26
+ const clientOutDirAbs = resolve(outDirAbs, "client");
27
+ const clientConfigImport = ensureRelativeImportPath(
28
+ clientOutDirAbs,
29
+ configPath,
30
+ );
31
+
32
+ await Promise.all([
33
+ mkdir(outDirAbs, { recursive: true }),
34
+ mkdir(clientOutDirAbs, { recursive: true }),
35
+ mkdir(wranglerOutDirAbs, { recursive: true }),
36
+ ]);
37
+
38
+ const serverPath = resolve(outDirAbs, "server.ts");
39
+ const clientPath = resolve(outDirAbs, "client.ts");
40
+ const authConfigPath = resolve(outDirAbs, "auth.config.ts");
41
+ const authSchemaPath = resolve(outDirAbs, "auth.schema.ts");
42
+ const drizzleConfigPath = resolve(outDirAbs, "drizzle.config.ts");
43
+ const wranglerPath = resolve(wranglerOutDirAbs, "wrangler.json");
44
+ const compiledSchema = await compileSchemaDsl(loadedConfig);
45
+
46
+ const discoveredSchema = await discoverSchema(
47
+ loadedConfig,
48
+ compiledSchema ? [compiledSchema.schemaPath] : [],
49
+ );
50
+ const schemaImport = ensureRelativeImportPath(
51
+ outDirAbs,
52
+ discoveredSchema.schemaPath,
53
+ );
54
+ const discoveredHandlers = await discoverHandlerOperations(loadedConfig);
55
+
56
+ const serverSource = generateServerSource(
57
+ config.auth.basePath,
58
+ config.database[0].binding,
59
+ config.kv[0]?.binding,
60
+ config.scheduler.binding,
61
+ config.r2[0]?.binding,
62
+ config.realtime.binding,
63
+ config.realtime.objectName,
64
+ config.realtime.subscribePath,
65
+ config.realtime.websocketPath,
66
+ config.realtime.protocol,
67
+ );
68
+ const clientArtifacts = generateClientArtifacts(
69
+ clientConfigImport,
70
+ discoveredHandlers,
71
+ );
72
+ const handlerArtifacts = generateHandlersArtifacts(
73
+ schemaImport,
74
+ discoveredHandlers,
75
+ config.r2[0]?.binding,
76
+ );
77
+ const authConfigSource = generateAuthConfigSource(configImport);
78
+ const drizzleSchemaPaths = compiledSchema
79
+ ? [
80
+ toConfigRelativePath(configDir, compiledSchema.schemaPath),
81
+ ...config.schema.filter((schemaPath) => {
82
+ return !/(^|\/)schema\.ts$/.test(schemaPath);
83
+ }),
84
+ ]
85
+ : config.schema;
86
+ const drizzleConfigSource = generateDrizzleConfigSource(drizzleSchemaPaths);
87
+ const wranglerJson = generateWranglerJson(loadedConfig, discoveredHandlers);
88
+ const handlerWriteOperations = handlerArtifacts.map((artifact) => {
89
+ return Bun.write(
90
+ resolve(outDirAbs, artifact.relativePath),
91
+ artifact.source,
92
+ );
93
+ });
94
+ const clientWriteOperations = clientArtifacts.map((artifact) => {
95
+ return Bun.write(
96
+ resolve(outDirAbs, artifact.relativePath),
97
+ artifact.source,
98
+ );
99
+ });
100
+
101
+ await Promise.all([
102
+ Bun.write(serverPath, serverSource),
103
+ Bun.write(clientPath, 'export * from "./client/index";\n'),
104
+ ...clientWriteOperations,
105
+ ...handlerWriteOperations,
106
+ Bun.write(authConfigPath, authConfigSource),
107
+ Bun.write(authSchemaPath, ""),
108
+ Bun.write(drizzleConfigPath, drizzleConfigSource),
109
+ Bun.write(wranglerPath, `${JSON.stringify(wranglerJson, null, 2)}\n`),
110
+ ]);
111
+
112
+ const authConfigPathFromConfigDir = relative(
113
+ configDir,
114
+ authConfigPath,
115
+ ).replace(/\\/g, "/");
116
+ const authConfigCliPath = authConfigPathFromConfigDir.startsWith(".")
117
+ ? authConfigPathFromConfigDir
118
+ : `./${authConfigPathFromConfigDir}`;
119
+ const authSchemaPathFromConfigDir = relative(
120
+ configDir,
121
+ authSchemaPath,
122
+ ).replace(/\\/g, "/");
123
+ const authSchemaCliPath = authSchemaPathFromConfigDir.startsWith(".")
124
+ ? authSchemaPathFromConfigDir
125
+ : `./${authSchemaPathFromConfigDir}`;
126
+
127
+ const betterAuthGenerate = Bun.spawn(
128
+ [
129
+ "npx",
130
+ "@better-auth/cli",
131
+ "generate",
132
+ "--config",
133
+ authConfigCliPath,
134
+ "--output",
135
+ authSchemaCliPath,
136
+ "--yes",
137
+ ],
138
+ {
139
+ cwd: configDir,
140
+ stdout: "inherit",
141
+ stderr: "inherit",
142
+ },
143
+ );
144
+
145
+ const exitCode = await betterAuthGenerate.exited;
146
+ if (exitCode !== 0) {
147
+ throw new Error(`better-auth generation failed with exit code ${exitCode}`);
148
+ }
149
+ }