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,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
+ }