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,100 @@
1
+ export function generateClientStorageSource(): string {
2
+ return `import type { StorageClient, StorageSignedUrlResponse, StorageListResponse } from "./types";
3
+
4
+ export function createStorageClient(endpoint: string, request: typeof fetch = fetch): StorageClient {
5
+ return {
6
+ upload: async (args) => {
7
+ const response = await request(\`\${endpoint}/storage/upload\`, {
8
+ method: "POST",
9
+ headers: {
10
+ "content-type": "application/json",
11
+ },
12
+ body: JSON.stringify(args),
13
+ });
14
+ if (!response.ok) {
15
+ throw new Error(await response.text());
16
+ }
17
+
18
+ return (await response.json()) as StorageSignedUrlResponse;
19
+ },
20
+ download: async (args) => {
21
+ const query = new URLSearchParams({
22
+ path: args.path,
23
+ ...(args.fileName ? { fileName: args.fileName } : {}),
24
+ ...(typeof args.expiresIn === "number"
25
+ ? { expiresIn: String(args.expiresIn) }
26
+ : {}),
27
+ });
28
+ const response = await request(
29
+ \`\${endpoint}/storage/download?\${query.toString()}\`,
30
+ );
31
+ if (!response.ok) {
32
+ throw new Error(await response.text());
33
+ }
34
+
35
+ return (await response.json()) as StorageSignedUrlResponse;
36
+ },
37
+ preview: async (args) => {
38
+ const query = new URLSearchParams({
39
+ path: args.path,
40
+ ...(typeof args.expiresIn === "number"
41
+ ? { expiresIn: String(args.expiresIn) }
42
+ : {}),
43
+ });
44
+ const response = await request(
45
+ \`\${endpoint}/storage/preview?\${query.toString()}\`,
46
+ );
47
+ if (!response.ok) {
48
+ throw new Error(await response.text());
49
+ }
50
+
51
+ return (await response.json()) as StorageSignedUrlResponse;
52
+ },
53
+ delete: async (args) => {
54
+ const query = new URLSearchParams({
55
+ path: args.path,
56
+ });
57
+ const response = await request(
58
+ \`\${endpoint}/storage/object?\${query.toString()}\`,
59
+ { method: "DELETE" },
60
+ );
61
+ if (!response.ok) {
62
+ throw new Error(await response.text());
63
+ }
64
+
65
+ return (await response.json()) as { ok: boolean; path: string };
66
+ },
67
+ list: async (args = {}) => {
68
+ const query = new URLSearchParams();
69
+ if (args.prefix) {
70
+ query.set("prefix", args.prefix);
71
+ }
72
+ if (args.cursor) {
73
+ query.set("cursor", args.cursor);
74
+ }
75
+ if (typeof args.limit === "number") {
76
+ query.set("limit", String(args.limit));
77
+ }
78
+ if (args.delimiter) {
79
+ query.set("delimiter", args.delimiter);
80
+ }
81
+ if (args.method) {
82
+ query.set("method", args.method);
83
+ }
84
+
85
+ const querySuffix = query.toString();
86
+ const response = await request(
87
+ querySuffix.length > 0
88
+ ? \`\${endpoint}/storage/list?\${querySuffix}\`
89
+ : \`\${endpoint}/storage/list\`,
90
+ );
91
+ if (!response.ok) {
92
+ throw new Error(await response.text());
93
+ }
94
+
95
+ return (await response.json()) as StorageListResponse;
96
+ },
97
+ };
98
+ }
99
+ `;
100
+ }
@@ -0,0 +1,54 @@
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 AppflareOptions<Options extends BetterAuthClientOptions = InferredAuthOptions> = {
13
+ endpoint: string;
14
+ authPath?: string;
15
+ authOptions?: Options;
16
+ fetch?: typeof fetch;
17
+ };
18
+
19
+ export type StorageSignedUrlResponse = {
20
+ url: string;
21
+ method: "GET" | "PUT" | "DELETE";
22
+ path: string;
23
+ disposition?: "attachment" | "inline";
24
+ expiresIn?: number;
25
+ };
26
+
27
+ export type StorageListResponse = unknown;
28
+
29
+ export type StorageClient = {
30
+ upload: (args: {
31
+ path: string;
32
+ contentType?: string;
33
+ expiresIn?: number;
34
+ }) => Promise<StorageSignedUrlResponse>;
35
+ download: (args: {
36
+ path: string;
37
+ expiresIn?: number;
38
+ fileName?: string;
39
+ }) => Promise<StorageSignedUrlResponse>;
40
+ preview: (args: {
41
+ path: string;
42
+ expiresIn?: number;
43
+ }) => Promise<StorageSignedUrlResponse>;
44
+ delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
45
+ list: (args?: {
46
+ prefix?: string;
47
+ cursor?: string;
48
+ limit?: number;
49
+ delimiter?: string;
50
+ method?: "download" | "get" | "delete" | "list" | "put" | "preview";
51
+ }) => Promise<StorageListResponse>;
52
+ };
53
+ `;
54
+ }
@@ -0,0 +1,112 @@
1
+ export function generateClientAppflareSource(): string {
2
+ return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
3
+ import type {
4
+ AppflareAuth,
5
+ AppflareAuthTokenResolver,
6
+ AppflareOptions,
7
+ InferredAuthOptions,
8
+ StorageClient,
9
+ } from "./types";
10
+ import type { MutationsClient, QueriesClient } from "./handlers";
11
+ import { createStorageClient } from "./storage";
12
+ import { createMutationsClient, createQueriesClient } from "./handlers";
13
+
14
+ export class Appflare<Options extends BetterAuthClientOptions = InferredAuthOptions> {
15
+ public readonly endpoint: string;
16
+ public readonly wsEndpoint: string;
17
+ public readonly auth: AppflareAuth<Options>;
18
+ public readonly storage: StorageClient;
19
+ public readonly queries: QueriesClient;
20
+ public readonly mutations: MutationsClient;
21
+
22
+ public constructor(options: AppflareOptions<Options>) {
23
+ this.endpoint = options.endpoint.replace(/\\/$/, "");
24
+ this.wsEndpoint = (options.wsEndpoint ?? this.endpoint).replace(/\\/$/, "");
25
+ const authOptions = (options.authOptions ?? {}) as Options;
26
+ const request = options.fetch ?? fetch;
27
+ const onSetAuthToken = options.onSetAuthToken;
28
+ const onGetAuthToken: AppflareAuthTokenResolver =
29
+ options.onGetAuthToken ?? (() => "");
30
+
31
+ const authFetchOptions =
32
+ (authOptions as { fetchOptions?: Record<string, unknown> }).fetchOptions ??
33
+ {};
34
+ const mergedAuthOptions = {
35
+ ...(authOptions as Record<string, unknown>),
36
+ fetchOptions: {
37
+ ...authFetchOptions,
38
+ onSuccess: async (ctx: {
39
+ response: { headers: { get: (name: string) => string | null } };
40
+ }) => {
41
+ if (
42
+ typeof (authFetchOptions as { onSuccess?: unknown }).onSuccess ===
43
+ "function"
44
+ ) {
45
+ await (
46
+ (authFetchOptions as { onSuccess: (ctx: unknown) => unknown }).onSuccess
47
+ )(ctx);
48
+ }
49
+
50
+ const authToken = ctx.response.headers.get("set-auth-token");
51
+ if (authToken && onSetAuthToken) {
52
+ await onSetAuthToken(authToken);
53
+ }
54
+ },
55
+ onRequest: async (ctx: {
56
+ headers: Headers;
57
+ }) => {
58
+ let nextCtx = ctx;
59
+ if (
60
+ typeof (authFetchOptions as { onRequest?: unknown }).onRequest ===
61
+ "function"
62
+ ) {
63
+ const maybeCtx = await (
64
+ (authFetchOptions as { onRequest: (ctx: unknown) => unknown }).onRequest
65
+ )(ctx);
66
+ if (
67
+ typeof maybeCtx === "object" &&
68
+ maybeCtx !== null &&
69
+ "headers" in maybeCtx
70
+ ) {
71
+ nextCtx = maybeCtx as typeof ctx;
72
+ }
73
+ }
74
+
75
+ const authToken = await onGetAuthToken();
76
+ if (typeof authToken === "string" && authToken.trim().length > 0) {
77
+ nextCtx.headers.set("Authorization", \`Bearer \${authToken.trim()}\`);
78
+ }
79
+
80
+ return nextCtx;
81
+ },
82
+ },
83
+ };
84
+
85
+ this.auth = createAuthClient<Options>({
86
+ ...(mergedAuthOptions as Options),
87
+ baseURL: \`\${this.endpoint}\${options.authPath ?? "/api/auth"}\`,
88
+ fetch: request,
89
+ });
90
+
91
+ this.storage = createStorageClient(this.endpoint, request, onGetAuthToken);
92
+ this.queries = createQueriesClient(
93
+ this.endpoint,
94
+ options.requestOptions,
95
+ this.wsEndpoint,
96
+ onGetAuthToken,
97
+ );
98
+ this.mutations = createMutationsClient(
99
+ this.endpoint,
100
+ options.requestOptions,
101
+ onGetAuthToken,
102
+ );
103
+ }
104
+ }
105
+
106
+ export function createAppflare<Options extends BetterAuthClientOptions = InferredAuthOptions>(
107
+ options: AppflareOptions<Options>,
108
+ ): Appflare<Options> {
109
+ return new Appflare(options);
110
+ }
111
+ `;
112
+ }