appflare 0.2.27 → 0.2.29
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.
- package/cli/templates/core/client/storage.ts +2 -12
- package/cli/templates/core/client/types.ts +1 -1
- package/cli/templates/handlers/generators/context/types.ts +23 -1
- package/cli/templates/handlers/generators/registration/modules/storage.ts +15 -8
- package/cli/templates/handlers/generators/types/context.ts +26 -1
- package/dist/cli/index.js +79 -35
- package/dist/cli/index.mjs +79 -35
- package/package.json +1 -1
|
@@ -130,24 +130,14 @@ export function createStorageClient(
|
|
|
130
130
|
|
|
131
131
|
return (await response.json()) as StorageSignedUrlResponse;
|
|
132
132
|
},
|
|
133
|
-
preview:
|
|
133
|
+
preview: (args) => {
|
|
134
134
|
const query = new URLSearchParams({
|
|
135
135
|
path: args.path,
|
|
136
136
|
...(typeof args.expiresIn === "number"
|
|
137
137
|
? { expiresIn: String(args.expiresIn) }
|
|
138
138
|
: {}),
|
|
139
139
|
});
|
|
140
|
-
|
|
141
|
-
\`\${endpoint}/storage/preview?\${query.toString()}\`,
|
|
142
|
-
{
|
|
143
|
-
headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
|
|
144
|
-
},
|
|
145
|
-
);
|
|
146
|
-
if (!response.ok) {
|
|
147
|
-
throw new Error(await response.text());
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return (await response.json()) as StorageSignedUrlResponse;
|
|
140
|
+
return \`\${endpoint}/storage/download?\${query.toString()}\`;
|
|
151
141
|
},
|
|
152
142
|
delete: async (args) => {
|
|
153
143
|
const query = new URLSearchParams({
|
|
@@ -159,7 +159,7 @@ export type StorageClient = {
|
|
|
159
159
|
preview: (args: {
|
|
160
160
|
path: string;
|
|
161
161
|
expiresIn?: number;
|
|
162
|
-
}) =>
|
|
162
|
+
}) => string;
|
|
163
163
|
delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
|
|
164
164
|
list: (args?: {
|
|
165
165
|
prefix?: string;
|
|
@@ -4,9 +4,31 @@ type SchedulerQueueBinding = {
|
|
|
4
4
|
send: (body: unknown, options?: SchedulerEnqueueOptions) => Promise<void>;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
+
type R2ObjectBody = {
|
|
8
|
+
body: ReadableStream;
|
|
9
|
+
bodyUsed: boolean;
|
|
10
|
+
writeHttpResponse: (response: Response) => void;
|
|
11
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
12
|
+
customMetadata?: Record<string, string>;
|
|
13
|
+
range?: { offset: number; length: number };
|
|
14
|
+
size: number;
|
|
15
|
+
etag: string;
|
|
16
|
+
httpEtag: string;
|
|
17
|
+
checksums: {
|
|
18
|
+
md5?: ArrayBuffer;
|
|
19
|
+
sha1?: ArrayBuffer;
|
|
20
|
+
sha256?: ArrayBuffer;
|
|
21
|
+
sha384?: ArrayBuffer;
|
|
22
|
+
sha512?: ArrayBuffer;
|
|
23
|
+
};
|
|
24
|
+
uploaded: Date;
|
|
25
|
+
key: string;
|
|
26
|
+
contentType?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
7
29
|
type R2BucketBinding = {
|
|
8
30
|
put: (key: string, value: unknown, options?: Record<string, unknown>) => Promise<unknown>;
|
|
9
|
-
get: (key: string, options?: Record<string, unknown>) => Promise<
|
|
31
|
+
get: (key: string, options?: Record<string, unknown>) => Promise<R2ObjectBody | null>;
|
|
10
32
|
delete: (key: string | string[]) => Promise<void>;
|
|
11
33
|
list: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
12
34
|
createPresignedUrl?: (
|
|
@@ -83,21 +83,28 @@ export function registerGeneratedStorageRoutes(
|
|
|
83
83
|
try {
|
|
84
84
|
const path = readStoragePath(c);
|
|
85
85
|
const fileName = c.req.query("fileName") ?? undefined;
|
|
86
|
-
const expiresIn = parseExpiresIn(c.req.query("expiresIn"));
|
|
87
86
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
87
|
+
const file = await ctx.storage.get({ path });
|
|
88
|
+
if (!file) {
|
|
89
|
+
return c.json({ message: "File not found" }, 404);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const headers: Record<string, string> = {};
|
|
93
|
+
if (file.contentType) {
|
|
94
|
+
headers["content-type"] = file.contentType;
|
|
95
|
+
}
|
|
96
|
+
if (fileName) {
|
|
97
|
+
headers["content-disposition"] = \`attachment; filename="\${fileName}"\`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return c.body(file.body, 200, headers);
|
|
94
101
|
} catch (error) {
|
|
95
102
|
if (error instanceof AppflareHandledError) {
|
|
96
103
|
return c.json(error.payload, error.status as any);
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
return c.json(
|
|
100
|
-
{ message: (error as Error).message ?? "Unable to
|
|
107
|
+
{ message: (error as Error).message ?? "Unable to download file" },
|
|
101
108
|
400,
|
|
102
109
|
);
|
|
103
110
|
}
|
|
@@ -22,6 +22,31 @@ export type StorageGetArgs = {
|
|
|
22
22
|
includeBody?: boolean;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
export type StorageGetResult = {
|
|
26
|
+
body: ReadableStream;
|
|
27
|
+
bodyUsed: boolean;
|
|
28
|
+
writeHttpResponse: (response: Response) => void;
|
|
29
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
30
|
+
customMetadata?: Record<string, string>;
|
|
31
|
+
range?: {
|
|
32
|
+
offset: number;
|
|
33
|
+
length: number;
|
|
34
|
+
};
|
|
35
|
+
size: number;
|
|
36
|
+
etag: string;
|
|
37
|
+
httpEtag: string;
|
|
38
|
+
checksums: {
|
|
39
|
+
md5?: ArrayBuffer;
|
|
40
|
+
sha1?: ArrayBuffer;
|
|
41
|
+
sha256?: ArrayBuffer;
|
|
42
|
+
sha384?: ArrayBuffer;
|
|
43
|
+
sha512?: ArrayBuffer;
|
|
44
|
+
};
|
|
45
|
+
uploaded: Date;
|
|
46
|
+
key: string;
|
|
47
|
+
contentType?: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
25
50
|
export type StorageDeleteArgs = {
|
|
26
51
|
path: string;
|
|
27
52
|
};
|
|
@@ -46,7 +71,7 @@ export type StorageSignedUrlArgs = {
|
|
|
46
71
|
|
|
47
72
|
export type AppflareStorage = {
|
|
48
73
|
put: (args: StoragePutArgs) => Promise<unknown>;
|
|
49
|
-
get: (args: StorageGetArgs) => Promise<
|
|
74
|
+
get: (args: StorageGetArgs) => Promise<StorageGetResult | null>;
|
|
50
75
|
delete: (args: StorageDeleteArgs) => Promise<void>;
|
|
51
76
|
list: (args?: StorageListArgs) => Promise<unknown>;
|
|
52
77
|
};
|
package/dist/cli/index.js
CHANGED
|
@@ -166,8 +166,8 @@ export function createAppflare<Options extends BetterAuthClientOptions = Inferre
|
|
|
166
166
|
): Appflare<Options> {
|
|
167
167
|
return new Appflare(options);
|
|
168
168
|
}
|
|
169
|
-
`}function
|
|
170
|
-
`)}function En(e,t){if(e.kind!=="query"&&e.kind!=="mutation")return null;let n=e.clientSegments&&e.clientSegments.length>0?e.clientSegments:e.routePath.replace(/^\//,"").split("/").filter(Boolean).slice(1);if(n.length===0)return null;let r=
|
|
169
|
+
`}function Dn(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t||"_route"}function ae(e){return e.split(/[^A-Za-z0-9]+/).filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}function Fn(e){return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)?e:JSON.stringify(e)}function oe(e){let t={children:new Map};for(let n of e){let r=t;for(let a of n.segments){let o=r.children.get(a);o||(o={children:new Map},r.children.set(a,o)),r=o;}r.operation=n;}return t}function V(e,t=1){let n=" ".repeat(t),r=" ".repeat(t+1),a=Array.from(e.children.entries()).sort(([i],[s])=>i.localeCompare(s));if(a.length===0)return e.operation?`${e.operation.alias}Route(runtime)`:"{}";let o=["{"];for(let[i,s]of a)o.push(`${r}${Fn(i)}: ${V(s,t+1)},`);return e.operation&&(o.push(`${r}run: ${e.operation.alias}Route(runtime).run,`),o.push(`${r}schema: ${e.operation.alias}Route(runtime).schema,`),e.operation.kind==="query"&&o.push(`${r}subscribe: ${e.operation.alias}Route(runtime).subscribe,`)),o.push(`${n}}`),o.join(`
|
|
170
|
+
`)}function En(e,t){if(e.kind!=="query"&&e.kind!=="mutation")return null;let n=e.clientSegments&&e.clientSegments.length>0?e.clientSegments:e.routePath.replace(/^\//,"").split("/").filter(Boolean).slice(1);if(n.length===0)return null;let r=Dn(`op_${t}_${e.kind}_${n.join("_")}`);return {kind:e.kind,routePath:e.routePath,queryName:e.handlerName??n.join("/"),segments:n,importPath:e.clientImportPath,exportName:e.exportName,alias:r,schemaConst:`${r}Schema`,typeBase:`${ae(e.kind)}${ae(n.join("_"))}`}}function On(e){let t=`${e.typeBase}Input`,n=`${e.typeBase}Output`,r=`${e.typeBase}Schema`,a=e.kind==="query"?"GET":"POST";return e.kind==="query"?`const ${e.alias}Route = (
|
|
171
171
|
runtime: RequestRuntime,
|
|
172
172
|
): AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}> => {
|
|
173
173
|
const run: AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}>["run"] = async (
|
|
@@ -872,24 +872,14 @@ export function createStorageClient(
|
|
|
872
872
|
|
|
873
873
|
return (await response.json()) as StorageSignedUrlResponse;
|
|
874
874
|
},
|
|
875
|
-
preview:
|
|
875
|
+
preview: (args) => {
|
|
876
876
|
const query = new URLSearchParams({
|
|
877
877
|
path: args.path,
|
|
878
878
|
...(typeof args.expiresIn === "number"
|
|
879
879
|
? { expiresIn: String(args.expiresIn) }
|
|
880
880
|
: {}),
|
|
881
881
|
});
|
|
882
|
-
|
|
883
|
-
\`\${endpoint}/storage/preview?\${query.toString()}\`,
|
|
884
|
-
{
|
|
885
|
-
headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
|
|
886
|
-
},
|
|
887
|
-
);
|
|
888
|
-
if (!response.ok) {
|
|
889
|
-
throw new Error(await response.text());
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
return (await response.json()) as StorageSignedUrlResponse;
|
|
882
|
+
return \`\${endpoint}/storage/download?\${query.toString()}\`;
|
|
893
883
|
},
|
|
894
884
|
delete: async (args) => {
|
|
895
885
|
const query = new URLSearchParams({
|
|
@@ -1103,7 +1093,7 @@ export type StorageClient = {
|
|
|
1103
1093
|
preview: (args: {
|
|
1104
1094
|
path: string;
|
|
1105
1095
|
expiresIn?: number;
|
|
1106
|
-
}) =>
|
|
1096
|
+
}) => string;
|
|
1107
1097
|
delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
|
|
1108
1098
|
list: (args?: {
|
|
1109
1099
|
prefix?: string;
|
|
@@ -1177,9 +1167,31 @@ type SchedulerQueueBinding = {
|
|
|
1177
1167
|
send: (body: unknown, options?: SchedulerEnqueueOptions) => Promise<void>;
|
|
1178
1168
|
};
|
|
1179
1169
|
|
|
1170
|
+
type R2ObjectBody = {
|
|
1171
|
+
body: ReadableStream;
|
|
1172
|
+
bodyUsed: boolean;
|
|
1173
|
+
writeHttpResponse: (response: Response) => void;
|
|
1174
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
1175
|
+
customMetadata?: Record<string, string>;
|
|
1176
|
+
range?: { offset: number; length: number };
|
|
1177
|
+
size: number;
|
|
1178
|
+
etag: string;
|
|
1179
|
+
httpEtag: string;
|
|
1180
|
+
checksums: {
|
|
1181
|
+
md5?: ArrayBuffer;
|
|
1182
|
+
sha1?: ArrayBuffer;
|
|
1183
|
+
sha256?: ArrayBuffer;
|
|
1184
|
+
sha384?: ArrayBuffer;
|
|
1185
|
+
sha512?: ArrayBuffer;
|
|
1186
|
+
};
|
|
1187
|
+
uploaded: Date;
|
|
1188
|
+
key: string;
|
|
1189
|
+
contentType?: string;
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1180
1192
|
type R2BucketBinding = {
|
|
1181
1193
|
put: (key: string, value: unknown, options?: Record<string, unknown>) => Promise<unknown>;
|
|
1182
|
-
get: (key: string, options?: Record<string, unknown>) => Promise<
|
|
1194
|
+
get: (key: string, options?: Record<string, unknown>) => Promise<R2ObjectBody | null>;
|
|
1183
1195
|
delete: (key: string | string[]) => Promise<void>;
|
|
1184
1196
|
list: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
1185
1197
|
createPresignedUrl?: (
|
|
@@ -4333,6 +4345,31 @@ export type StorageGetArgs = {
|
|
|
4333
4345
|
includeBody?: boolean;
|
|
4334
4346
|
};
|
|
4335
4347
|
|
|
4348
|
+
export type StorageGetResult = {
|
|
4349
|
+
body: ReadableStream;
|
|
4350
|
+
bodyUsed: boolean;
|
|
4351
|
+
writeHttpResponse: (response: Response) => void;
|
|
4352
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
4353
|
+
customMetadata?: Record<string, string>;
|
|
4354
|
+
range?: {
|
|
4355
|
+
offset: number;
|
|
4356
|
+
length: number;
|
|
4357
|
+
};
|
|
4358
|
+
size: number;
|
|
4359
|
+
etag: string;
|
|
4360
|
+
httpEtag: string;
|
|
4361
|
+
checksums: {
|
|
4362
|
+
md5?: ArrayBuffer;
|
|
4363
|
+
sha1?: ArrayBuffer;
|
|
4364
|
+
sha256?: ArrayBuffer;
|
|
4365
|
+
sha384?: ArrayBuffer;
|
|
4366
|
+
sha512?: ArrayBuffer;
|
|
4367
|
+
};
|
|
4368
|
+
uploaded: Date;
|
|
4369
|
+
key: string;
|
|
4370
|
+
contentType?: string;
|
|
4371
|
+
};
|
|
4372
|
+
|
|
4336
4373
|
export type StorageDeleteArgs = {
|
|
4337
4374
|
path: string;
|
|
4338
4375
|
};
|
|
@@ -4357,7 +4394,7 @@ export type StorageSignedUrlArgs = {
|
|
|
4357
4394
|
|
|
4358
4395
|
export type AppflareStorage = {
|
|
4359
4396
|
put: (args: StoragePutArgs) => Promise<unknown>;
|
|
4360
|
-
get: (args: StorageGetArgs) => Promise<
|
|
4397
|
+
get: (args: StorageGetArgs) => Promise<StorageGetResult | null>;
|
|
4361
4398
|
delete: (args: StorageDeleteArgs) => Promise<void>;
|
|
4362
4399
|
list: (args?: StorageListArgs) => Promise<unknown>;
|
|
4363
4400
|
};
|
|
@@ -4374,7 +4411,7 @@ export type AppflareContext = {
|
|
|
4374
4411
|
storage: AppflareStorage;
|
|
4375
4412
|
error: (status: number, message: string, details?: unknown) => never;
|
|
4376
4413
|
};
|
|
4377
|
-
`}function
|
|
4414
|
+
`}function De(){return `type InferOperationArgs<TShape extends ZodRawShape> = z.output<z.ZodObject<TShape>>;
|
|
4378
4415
|
|
|
4379
4416
|
export type SchedulerEnqueueOptions = {
|
|
4380
4417
|
delaySeconds?: number;
|
|
@@ -4506,7 +4543,7 @@ export function cron(definition: CronDefinition): RegisteredCron {
|
|
|
4506
4543
|
definition,
|
|
4507
4544
|
};
|
|
4508
4545
|
}
|
|
4509
|
-
`}function
|
|
4546
|
+
`}function Fe(){return [ve(),Ae(),Me(),Pe(),De()].join(`
|
|
4510
4547
|
|
|
4511
4548
|
`)}function Ee(e){return `import type { Context } from "hono";
|
|
4512
4549
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
@@ -4515,7 +4552,7 @@ import { z, type ZodRawShape } from "zod";
|
|
|
4515
4552
|
import * as authSchema from "./auth.schema";
|
|
4516
4553
|
import * as schema from "${e}";
|
|
4517
4554
|
|
|
4518
|
-
${
|
|
4555
|
+
${Fe()}
|
|
4519
4556
|
`}function jn(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t}function Vn(e,t){let n=e.routePath.replace(/^\//,"").replace(/\//g,"_");return jn(`op_${t}_${n}`)}function Bn(e){return e.map((t,n)=>({operation:t,index:n,alias:Vn(t,n)}))}function Hn(e){return e.map(({operation:t,alias:n})=>`import { ${t.exportName} as ${n} } from "${t.importPath}";`).join(`
|
|
4520
4557
|
`)}function Wn(e){return e.filter(({operation:t})=>t.kind==="query"||t.kind==="mutation").map(({alias:t})=>`const ${`${t}Schema`} = z.object(${t}.definition.args);`).join(`
|
|
4521
4558
|
`)}function Ln(e){return e.filter(({operation:t})=>t.kind==="scheduler").map(({alias:t})=>`const ${`${t}SchedulerSchema`} = ${t}.definition.args ? z.object(${t}.definition.args) : z.undefined();`).join(`
|
|
@@ -5678,21 +5715,28 @@ export function registerGeneratedStorageRoutes(
|
|
|
5678
5715
|
try {
|
|
5679
5716
|
const path = readStoragePath(c);
|
|
5680
5717
|
const fileName = c.req.query("fileName") ?? undefined;
|
|
5681
|
-
const expiresIn = parseExpiresIn(c.req.query("expiresIn"));
|
|
5682
5718
|
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
|
|
5686
|
-
|
|
5687
|
-
|
|
5688
|
-
|
|
5719
|
+
const file = await ctx.storage.get({ path });
|
|
5720
|
+
if (!file) {
|
|
5721
|
+
return c.json({ message: "File not found" }, 404);
|
|
5722
|
+
}
|
|
5723
|
+
|
|
5724
|
+
const headers: Record<string, string> = {};
|
|
5725
|
+
if (file.contentType) {
|
|
5726
|
+
headers["content-type"] = file.contentType;
|
|
5727
|
+
}
|
|
5728
|
+
if (fileName) {
|
|
5729
|
+
headers["content-disposition"] = \`attachment; filename="\${fileName}"\`;
|
|
5730
|
+
}
|
|
5731
|
+
|
|
5732
|
+
return c.body(file.body, 200, headers);
|
|
5689
5733
|
} catch (error) {
|
|
5690
5734
|
if (error instanceof AppflareHandledError) {
|
|
5691
5735
|
return c.json(error.payload, error.status as any);
|
|
5692
5736
|
}
|
|
5693
5737
|
|
|
5694
5738
|
return c.json(
|
|
5695
|
-
{ message: (error as Error).message ?? "Unable to
|
|
5739
|
+
{ message: (error as Error).message ?? "Unable to download file" },
|
|
5696
5740
|
400,
|
|
5697
5741
|
);
|
|
5698
5742
|
}
|
|
@@ -7827,7 +7871,7 @@ ${Q()}`}function $t(e){return `
|
|
|
7827
7871
|
adminApp.get('/functions${n.routePath}', (c) => {
|
|
7828
7872
|
${Mt(n)}
|
|
7829
7873
|
});`).join(`
|
|
7830
|
-
`)}function
|
|
7874
|
+
`)}function Dt(){return `
|
|
7831
7875
|
const getStorageBucket = (c: any): R2Bucket | null => {
|
|
7832
7876
|
const r2Binding = (options as any).r2Binding;
|
|
7833
7877
|
if (!r2Binding || !c.env[r2Binding]) return null;
|
|
@@ -7896,7 +7940,7 @@ ${Q()}`}function $t(e){return `
|
|
|
7896
7940
|
const parts = prefix.split('/').filter(Boolean);
|
|
7897
7941
|
return parts.slice(0, -1).join('/') + (parts.length > 1 ? '/' : '');
|
|
7898
7942
|
};
|
|
7899
|
-
`}function
|
|
7943
|
+
`}function Ft(){return `
|
|
7900
7944
|
const buildStorageListingContent = (listed: any, prefix: string) => {
|
|
7901
7945
|
const parts = prefix.split('/').filter(Boolean);
|
|
7902
7946
|
const breadcrumbs: any[] = [];
|
|
@@ -8155,10 +8199,10 @@ ${Q()}`}function $t(e){return `
|
|
|
8155
8199
|
|
|
8156
8200
|
${Et()}
|
|
8157
8201
|
`}function Lt(){return `
|
|
8158
|
-
${Ft()}
|
|
8159
|
-
|
|
8160
8202
|
${Dt()}
|
|
8161
8203
|
|
|
8204
|
+
${Ft()}
|
|
8205
|
+
|
|
8162
8206
|
${Wt()}
|
|
8163
8207
|
`}function zt(e,t){let n=ct(t);return `
|
|
8164
8208
|
function Layout(props: { children: any; title: string; hideSidebar?: boolean }) {
|
|
@@ -8640,7 +8684,7 @@ ${f}
|
|
|
8640
8684
|
app.route('/admin', adminApp);
|
|
8641
8685
|
app.get('/admin/', (c) => c.redirect('/admin'));
|
|
8642
8686
|
}
|
|
8643
|
-
`}function _(e){if(typeof e!="object"||e===null)return false;let t=e;return t.kind==="schema"&&typeof t.tables=="object"}function M(e){return e.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()}function
|
|
8687
|
+
`}function _(e){if(typeof e!="object"||e===null)return false;let t=e;return t.kind==="schema"&&typeof t.tables=="object"}function M(e){return e.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()}function D(e){return e.replace(/[_-]+/g," ").replace(/\s+(.)/g,(t,n)=>n.toUpperCase()).replace(/\s/g,"").replace(/^(.)/,(t,n)=>n.toUpperCase())}function J(e){return e.endsWith("ies")?`${e.slice(0,-3)}y`:e.endsWith("ses")?e.slice(0,-2):e.endsWith("s")&&e.length>1?e.slice(0,-1):e}function h(e){return JSON.stringify(e)}function dr(e){if(typeof e=="string")return h(e);if(typeof e=="number"||typeof e=="boolean")return String(e);if(e===null)return "null";if(e instanceof Date)return h(e.toISOString());throw new Error(`Unsupported SQL default value '${String(e)}'. Use string, number, boolean, null, or Date.`)}function pr(e){return {kind:"schema",tables:Object.fromEntries(Object.entries(e.tables).map(([t,n])=>[t,{kind:"table",sqlName:n.sqlName,columns:Object.fromEntries(Object.entries(n.columns).map(([r,a])=>[r,{...a}])),relations:Object.fromEntries(Object.entries(n.relations).map(([r,a])=>[r,{...a}]))}]))}}function P(e,t,n){let r=e.tables[t];return r?r.columns[n]?.type:void 0}function Kt(e,t,n,r,a,o,i){let s=t.columns[n];if(s){if(s.references&&(s.references.table!==r||s.references.column!==a))throw new Error(`Inferred relation '${e}.${n}' conflicts with explicit references(${s.references.table}.${s.references.column}).`);t.columns[n]={...s,notNull:s.notNull??(s.nullable===true?false:o.notNull),nullable:s.nullable??(o.notNull===false?true:void 0),references:{table:r,column:a,onDelete:s.references?.onDelete??o.onDelete,onUpdate:s.references?.onUpdate??o.onUpdate}};return}t.columns[n]={kind:"column",type:o.fkType??i,sqlName:o.sqlName,notNull:o.notNull??true,nullable:o.notNull===false?true:void 0,references:{table:r,column:a,onDelete:o.onDelete,onUpdate:o.onUpdate}};}function Gt(e,t,n){if(t.notNull===true&&t.nullable===true)throw new Error(`Invalid nullable configuration on '${e}': cannot set both notNull and nullable to true.`);return t.notNull===true?true:t.nullable===true||t.notNull===false?false:n}function Jt(e,t){return `${e}:${t}`}function mr(e,t,n,r){let a=Jt(e,t),o=Jt(n,r);return a<=o?{key:`${a}|${o}`,leftTable:e,leftReferenceField:t,rightTable:n,rightReferenceField:r,sourceIsLeft:true}:{key:`${o}|${a}`,leftTable:n,leftReferenceField:r,rightTable:e,rightReferenceField:t,sourceIsLeft:false}}function gr(e,t){return `${e}${D(t)}Links`}function Zt(e,t,n){let r=J(e),a=J(t);return r===a?`${n}${D(r)}Id`:`${r}Id`}function fr(e,t,n){if(e.junctionTable!==t.junctionTable)throw new Error(`manyToMany pair '${n}' has conflicting junctionTable values ('${e.junctionTable}' vs '${t.junctionTable}').`);if(e.leftField!==t.leftField)throw new Error(`manyToMany pair '${n}' has conflicting left field values ('${e.leftField}' vs '${t.leftField}').`);if(e.rightField!==t.rightField)throw new Error(`manyToMany pair '${n}' has conflicting right field values ('${e.rightField}' vs '${t.rightField}').`);if(e.leftSqlName!==t.leftSqlName)throw new Error(`manyToMany pair '${n}' has conflicting left sql name values ('${e.leftSqlName}' vs '${t.leftSqlName}').`);if(e.rightSqlName!==t.rightSqlName)throw new Error(`manyToMany pair '${n}' has conflicting right sql name values ('${e.rightSqlName}' vs '${t.rightSqlName}').`);if(e.onDelete!==t.onDelete)throw new Error(`manyToMany pair '${n}' has conflicting onDelete values ('${e.onDelete}' vs '${t.onDelete}').`);if(e.onUpdate!==t.onUpdate)throw new Error(`manyToMany pair '${n}' has conflicting onUpdate values ('${e.onUpdate}' vs '${t.onUpdate}').`);return e}function hr(e){let t=new Map;for(let[n,r]of Object.entries(e.tables))for(let a of Object.values(r.relations)){if(a.relation!=="manyToMany")continue;let o=a.referenceField??"id",i=a.targetReferenceField??"id",s=mr(n,o,a.targetTable,i),l=Zt(s.leftTable,s.rightTable,"source"),u=Zt(s.rightTable,s.leftTable,"target"),c={leftTable:s.leftTable,leftReferenceField:s.leftReferenceField,rightTable:s.rightTable,rightReferenceField:s.rightReferenceField,junctionTable:a.junctionTable??gr(s.leftTable,s.rightTable),leftField:s.sourceIsLeft?a.sourceField??l:a.targetField??l,rightField:s.sourceIsLeft?a.targetField??u:a.sourceField??u,leftSqlName:s.sourceIsLeft?a.sourceSqlName:a.targetSqlName,rightSqlName:s.sourceIsLeft?a.targetSqlName:a.sourceSqlName,onDelete:a.onDelete,onUpdate:a.onUpdate};if(c.leftField===c.rightField)throw new Error(`manyToMany pair '${s.key}' resolves to duplicate junction fields '${c.leftField}'. Set sourceField/targetField explicitly.`);let f=t.get(s.key);f?fr(f,c,s.key):t.set(s.key,c),a.referenceField=o,a.targetReferenceField=i,a.junctionTable=c.junctionTable,a.sourceField=s.sourceIsLeft?c.leftField:c.rightField,a.targetField=s.sourceIsLeft?c.rightField:c.leftField,a.sourceSqlName=s.sourceIsLeft?c.leftSqlName:c.rightSqlName,a.targetSqlName=s.sourceIsLeft?c.rightSqlName:c.leftSqlName;}for(let n of t.values()){if(n.junctionTable in e.tables)throw new Error(`manyToMany auto junction table '${n.junctionTable}' conflicts with an existing table. Set a different junctionTable name.`);let r=P(e,n.leftTable,n.leftReferenceField)??"string",a=P(e,n.rightTable,n.rightReferenceField)??"string";e.tables[n.junctionTable]={kind:"table",columns:{[n.leftField]:{kind:"column",type:r,sqlName:n.leftSqlName,notNull:true,nullable:false,references:{table:n.leftTable,column:n.leftReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true},[n.rightField]:{kind:"column",type:a,sqlName:n.rightSqlName,notNull:true,nullable:false,references:{table:n.rightTable,column:n.rightReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true}},relations:{[n.leftTable]:{kind:"relation",relation:"one",targetTable:n.leftTable,field:n.leftField,referenceField:n.leftReferenceField},[n.rightTable]:{kind:"relation",relation:"one",targetTable:n.rightTable,field:n.rightField,referenceField:n.rightReferenceField}}};}}function br(e){for(let[t,n]of Object.entries(e.tables)){for(let[r,a]of Object.entries(n.columns))if(a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`);for(let[r,a]of Object.entries(n.relations))if(a.relation!=="manyToMany"&&a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`)}}function yr(e){br(e);let t=pr(e);for(let[n,r]of Object.entries(t.tables))for(let[a,o]of Object.entries(r.relations)){if(o.relation!=="one")continue;let i=o.referenceField??"id",s=o.field??`${a}Id`;o.field=s;let l=P(t,o.targetTable,i)??o.fkType??"string";Kt(n,r,s,o.targetTable,i,{fkType:o.fkType,sqlName:o.sqlName,notNull:Gt(`${n}.${a}`,o,true),onDelete:o.onDelete,onUpdate:o.onUpdate},l);}for(let[n,r]of Object.entries(t.tables))for(let a of Object.values(r.relations)){if(a.relation!=="many")continue;let o=t.tables[a.targetTable];if(!o)continue;let i=a.referenceField??"id",s=a.field??`${J(n)}Id`;a.field=s;let l=P(t,n,i)??a.fkType??"string";Kt(a.targetTable,o,s,n,i,{fkType:a.fkType,sqlName:a.sqlName,notNull:Gt(`${n}.${a.targetTable}`,a,true),onDelete:a.onDelete,onUpdate:a.onUpdate},l);}return hr(t),t}function Xt(e){return e.primaryKey===true||e.notNull!==true||e.autoIncrement===true||e.sqlDefault!==void 0||e.runtimeDefaultFn!==void 0}function $(e){return e.notNull!==true}function wr(e,t,n){let r=t.sqlName??M(e),a=r!==e;return t.type==="int"?a?`t.int(${h(r)})`:"t.int()":t.type==="string"?t.length!==void 0?a?`t.text(${h(r)}, { length: ${t.length} })`:`t.text({ length: ${t.length} })`:a?`t.text(${h(r)})`:"t.text()":t.type==="boolean"?a?`t.int(${h(r)}, { mode: "boolean" })`:'t.int({ mode: "boolean" })':t.type==="date"?a?`t.int(${h(r)}, { mode: "timestamp_ms" })`:'t.int({ mode: "timestamp_ms" })':n==="camelToSnake"&&a?`t.text(${h(r)})`:"t.text()"}function xr(e,t,n){let r=n.field,a=n.referenceField??"id";if(!r)throw new Error(`Relation on '${e}' targeting '${n.targetTable}' is missing a local field.`);if(!(r in t.columns))throw new Error(`Relation '${e}.${r}' references missing local field '${r}'.`);return {sourceField:r,targetField:a}}function vr(e){return e.size===0?"":`import { ${Array.from(e).sort().join(", ")} } from "./auth.schema";
|
|
8644
8688
|
`}function Tr(e){return Object.values(e.relations).filter(t=>t.relation==="one").map(t=>t.targetTable)}function Rr(e,t,n){if(t.references)return {tableName:t.references.table,fieldName:t.references.column};let r=Object.values(n.relations).find(a=>a.relation==="one"&&a.field===e);if(r)return {tableName:r.targetTable,fieldName:r.referenceField??"id"}}function kr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=[];for(let[o,i]of Object.entries(r.relations))i.relation==="manyToMany"&&i.junctionTable&&a.push(`${h(o)}: {
|
|
8645
8689
|
targetTable: ${h(i.targetTable)},
|
|
8646
8690
|
junctionTable: ${h(i.junctionTable)},
|
|
@@ -8714,7 +8758,7 @@ ${o.join(`
|
|
|
8714
8758
|
${kr(e)}
|
|
8715
8759
|
|
|
8716
8760
|
${Sr(e)}
|
|
8717
|
-
`}function Yt(e,t,n){let r="z.unknown()";return e.type==="int"?r="z.number().int()":e.type==="string"?(r="z.string()",e.length!==void 0&&(r+=`.max(${e.length})`)):e.type==="boolean"?r="z.boolean()":e.type==="date"&&(r="z.date()"),t&&(r+=".optional()"),n&&(r+=".nullable()"),r}function Nr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=
|
|
8761
|
+
`}function Yt(e,t,n){let r="z.unknown()";return e.type==="int"?r="z.number().int()":e.type==="string"?(r="z.string()",e.length!==void 0&&(r+=`.max(${e.length})`)):e.type==="boolean"?r="z.boolean()":e.type==="date"&&(r="z.date()"),t&&(r+=".optional()"),n&&(r+=".nullable()"),r}function Nr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=D(n),o=[],i=[];for(let[s,l]of Object.entries(r.columns))o.push(` ${s}: ${Yt(l,Xt(l),$(l))},`),i.push(` ${s}: ${Yt(l,$(l),$(l))},`);t.push(`export const ${n}InsertSchema = z.object({
|
|
8718
8762
|
${o.join(`
|
|
8719
8763
|
`)}
|
|
8720
8764
|
});
|
|
@@ -8728,7 +8772,7 @@ export type ${a}Select = z.infer<typeof ${n}SelectSchema>;
|
|
|
8728
8772
|
`);}return `import { z } from "zod";
|
|
8729
8773
|
|
|
8730
8774
|
${t.join(`
|
|
8731
|
-
`)}`}function $r(e){return e.type==="int"?"number":e.type==="string"?"string":e.type==="boolean"?"boolean":e.type==="date"?"Date":"unknown"}function qr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=
|
|
8775
|
+
`)}`}function $r(e){return e.type==="int"?"number":e.type==="string"?"string":e.type==="boolean"?"boolean":e.type==="date"?"Date":"unknown"}function qr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=D(n),o=[],i=[];for(let[s,l]of Object.entries(r.columns)){let u=$r(l),c=$(l)?" | null":"";o.push(` ${s}${$(l)?"?":""}: ${u}${c};`),i.push(` ${s}${Xt(l)?"?":""}: ${u}${c};`);}t.push(`export type ${a} = {
|
|
8732
8776
|
${o.join(`
|
|
8733
8777
|
`)}
|
|
8734
8778
|
};
|
package/dist/cli/index.mjs
CHANGED
|
@@ -166,8 +166,8 @@ export function createAppflare<Options extends BetterAuthClientOptions = Inferre
|
|
|
166
166
|
): Appflare<Options> {
|
|
167
167
|
return new Appflare(options);
|
|
168
168
|
}
|
|
169
|
-
`}function
|
|
170
|
-
`)}function En(e,t){if(e.kind!=="query"&&e.kind!=="mutation")return null;let n=e.clientSegments&&e.clientSegments.length>0?e.clientSegments:e.routePath.replace(/^\//,"").split("/").filter(Boolean).slice(1);if(n.length===0)return null;let r=
|
|
169
|
+
`}function Dn(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t||"_route"}function ae(e){return e.split(/[^A-Za-z0-9]+/).filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}function Fn(e){return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)?e:JSON.stringify(e)}function oe(e){let t={children:new Map};for(let n of e){let r=t;for(let a of n.segments){let o=r.children.get(a);o||(o={children:new Map},r.children.set(a,o)),r=o;}r.operation=n;}return t}function V(e,t=1){let n=" ".repeat(t),r=" ".repeat(t+1),a=Array.from(e.children.entries()).sort(([i],[s])=>i.localeCompare(s));if(a.length===0)return e.operation?`${e.operation.alias}Route(runtime)`:"{}";let o=["{"];for(let[i,s]of a)o.push(`${r}${Fn(i)}: ${V(s,t+1)},`);return e.operation&&(o.push(`${r}run: ${e.operation.alias}Route(runtime).run,`),o.push(`${r}schema: ${e.operation.alias}Route(runtime).schema,`),e.operation.kind==="query"&&o.push(`${r}subscribe: ${e.operation.alias}Route(runtime).subscribe,`)),o.push(`${n}}`),o.join(`
|
|
170
|
+
`)}function En(e,t){if(e.kind!=="query"&&e.kind!=="mutation")return null;let n=e.clientSegments&&e.clientSegments.length>0?e.clientSegments:e.routePath.replace(/^\//,"").split("/").filter(Boolean).slice(1);if(n.length===0)return null;let r=Dn(`op_${t}_${e.kind}_${n.join("_")}`);return {kind:e.kind,routePath:e.routePath,queryName:e.handlerName??n.join("/"),segments:n,importPath:e.clientImportPath,exportName:e.exportName,alias:r,schemaConst:`${r}Schema`,typeBase:`${ae(e.kind)}${ae(n.join("_"))}`}}function On(e){let t=`${e.typeBase}Input`,n=`${e.typeBase}Output`,r=`${e.typeBase}Schema`,a=e.kind==="query"?"GET":"POST";return e.kind==="query"?`const ${e.alias}Route = (
|
|
171
171
|
runtime: RequestRuntime,
|
|
172
172
|
): AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}> => {
|
|
173
173
|
const run: AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}>["run"] = async (
|
|
@@ -872,24 +872,14 @@ export function createStorageClient(
|
|
|
872
872
|
|
|
873
873
|
return (await response.json()) as StorageSignedUrlResponse;
|
|
874
874
|
},
|
|
875
|
-
preview:
|
|
875
|
+
preview: (args) => {
|
|
876
876
|
const query = new URLSearchParams({
|
|
877
877
|
path: args.path,
|
|
878
878
|
...(typeof args.expiresIn === "number"
|
|
879
879
|
? { expiresIn: String(args.expiresIn) }
|
|
880
880
|
: {}),
|
|
881
881
|
});
|
|
882
|
-
|
|
883
|
-
\`\${endpoint}/storage/preview?\${query.toString()}\`,
|
|
884
|
-
{
|
|
885
|
-
headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
|
|
886
|
-
},
|
|
887
|
-
);
|
|
888
|
-
if (!response.ok) {
|
|
889
|
-
throw new Error(await response.text());
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
return (await response.json()) as StorageSignedUrlResponse;
|
|
882
|
+
return \`\${endpoint}/storage/download?\${query.toString()}\`;
|
|
893
883
|
},
|
|
894
884
|
delete: async (args) => {
|
|
895
885
|
const query = new URLSearchParams({
|
|
@@ -1103,7 +1093,7 @@ export type StorageClient = {
|
|
|
1103
1093
|
preview: (args: {
|
|
1104
1094
|
path: string;
|
|
1105
1095
|
expiresIn?: number;
|
|
1106
|
-
}) =>
|
|
1096
|
+
}) => string;
|
|
1107
1097
|
delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
|
|
1108
1098
|
list: (args?: {
|
|
1109
1099
|
prefix?: string;
|
|
@@ -1177,9 +1167,31 @@ type SchedulerQueueBinding = {
|
|
|
1177
1167
|
send: (body: unknown, options?: SchedulerEnqueueOptions) => Promise<void>;
|
|
1178
1168
|
};
|
|
1179
1169
|
|
|
1170
|
+
type R2ObjectBody = {
|
|
1171
|
+
body: ReadableStream;
|
|
1172
|
+
bodyUsed: boolean;
|
|
1173
|
+
writeHttpResponse: (response: Response) => void;
|
|
1174
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
1175
|
+
customMetadata?: Record<string, string>;
|
|
1176
|
+
range?: { offset: number; length: number };
|
|
1177
|
+
size: number;
|
|
1178
|
+
etag: string;
|
|
1179
|
+
httpEtag: string;
|
|
1180
|
+
checksums: {
|
|
1181
|
+
md5?: ArrayBuffer;
|
|
1182
|
+
sha1?: ArrayBuffer;
|
|
1183
|
+
sha256?: ArrayBuffer;
|
|
1184
|
+
sha384?: ArrayBuffer;
|
|
1185
|
+
sha512?: ArrayBuffer;
|
|
1186
|
+
};
|
|
1187
|
+
uploaded: Date;
|
|
1188
|
+
key: string;
|
|
1189
|
+
contentType?: string;
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1180
1192
|
type R2BucketBinding = {
|
|
1181
1193
|
put: (key: string, value: unknown, options?: Record<string, unknown>) => Promise<unknown>;
|
|
1182
|
-
get: (key: string, options?: Record<string, unknown>) => Promise<
|
|
1194
|
+
get: (key: string, options?: Record<string, unknown>) => Promise<R2ObjectBody | null>;
|
|
1183
1195
|
delete: (key: string | string[]) => Promise<void>;
|
|
1184
1196
|
list: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
1185
1197
|
createPresignedUrl?: (
|
|
@@ -4333,6 +4345,31 @@ export type StorageGetArgs = {
|
|
|
4333
4345
|
includeBody?: boolean;
|
|
4334
4346
|
};
|
|
4335
4347
|
|
|
4348
|
+
export type StorageGetResult = {
|
|
4349
|
+
body: ReadableStream;
|
|
4350
|
+
bodyUsed: boolean;
|
|
4351
|
+
writeHttpResponse: (response: Response) => void;
|
|
4352
|
+
httpMetadata?: Record<string, string | undefined>;
|
|
4353
|
+
customMetadata?: Record<string, string>;
|
|
4354
|
+
range?: {
|
|
4355
|
+
offset: number;
|
|
4356
|
+
length: number;
|
|
4357
|
+
};
|
|
4358
|
+
size: number;
|
|
4359
|
+
etag: string;
|
|
4360
|
+
httpEtag: string;
|
|
4361
|
+
checksums: {
|
|
4362
|
+
md5?: ArrayBuffer;
|
|
4363
|
+
sha1?: ArrayBuffer;
|
|
4364
|
+
sha256?: ArrayBuffer;
|
|
4365
|
+
sha384?: ArrayBuffer;
|
|
4366
|
+
sha512?: ArrayBuffer;
|
|
4367
|
+
};
|
|
4368
|
+
uploaded: Date;
|
|
4369
|
+
key: string;
|
|
4370
|
+
contentType?: string;
|
|
4371
|
+
};
|
|
4372
|
+
|
|
4336
4373
|
export type StorageDeleteArgs = {
|
|
4337
4374
|
path: string;
|
|
4338
4375
|
};
|
|
@@ -4357,7 +4394,7 @@ export type StorageSignedUrlArgs = {
|
|
|
4357
4394
|
|
|
4358
4395
|
export type AppflareStorage = {
|
|
4359
4396
|
put: (args: StoragePutArgs) => Promise<unknown>;
|
|
4360
|
-
get: (args: StorageGetArgs) => Promise<
|
|
4397
|
+
get: (args: StorageGetArgs) => Promise<StorageGetResult | null>;
|
|
4361
4398
|
delete: (args: StorageDeleteArgs) => Promise<void>;
|
|
4362
4399
|
list: (args?: StorageListArgs) => Promise<unknown>;
|
|
4363
4400
|
};
|
|
@@ -4374,7 +4411,7 @@ export type AppflareContext = {
|
|
|
4374
4411
|
storage: AppflareStorage;
|
|
4375
4412
|
error: (status: number, message: string, details?: unknown) => never;
|
|
4376
4413
|
};
|
|
4377
|
-
`}function
|
|
4414
|
+
`}function De(){return `type InferOperationArgs<TShape extends ZodRawShape> = z.output<z.ZodObject<TShape>>;
|
|
4378
4415
|
|
|
4379
4416
|
export type SchedulerEnqueueOptions = {
|
|
4380
4417
|
delaySeconds?: number;
|
|
@@ -4506,7 +4543,7 @@ export function cron(definition: CronDefinition): RegisteredCron {
|
|
|
4506
4543
|
definition,
|
|
4507
4544
|
};
|
|
4508
4545
|
}
|
|
4509
|
-
`}function
|
|
4546
|
+
`}function Fe(){return [ve(),Ae(),Me(),Pe(),De()].join(`
|
|
4510
4547
|
|
|
4511
4548
|
`)}function Ee(e){return `import type { Context } from "hono";
|
|
4512
4549
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
@@ -4515,7 +4552,7 @@ import { z, type ZodRawShape } from "zod";
|
|
|
4515
4552
|
import * as authSchema from "./auth.schema";
|
|
4516
4553
|
import * as schema from "${e}";
|
|
4517
4554
|
|
|
4518
|
-
${
|
|
4555
|
+
${Fe()}
|
|
4519
4556
|
`}function jn(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t}function Vn(e,t){let n=e.routePath.replace(/^\//,"").replace(/\//g,"_");return jn(`op_${t}_${n}`)}function Bn(e){return e.map((t,n)=>({operation:t,index:n,alias:Vn(t,n)}))}function Hn(e){return e.map(({operation:t,alias:n})=>`import { ${t.exportName} as ${n} } from "${t.importPath}";`).join(`
|
|
4520
4557
|
`)}function Wn(e){return e.filter(({operation:t})=>t.kind==="query"||t.kind==="mutation").map(({alias:t})=>`const ${`${t}Schema`} = z.object(${t}.definition.args);`).join(`
|
|
4521
4558
|
`)}function Ln(e){return e.filter(({operation:t})=>t.kind==="scheduler").map(({alias:t})=>`const ${`${t}SchedulerSchema`} = ${t}.definition.args ? z.object(${t}.definition.args) : z.undefined();`).join(`
|
|
@@ -5678,21 +5715,28 @@ export function registerGeneratedStorageRoutes(
|
|
|
5678
5715
|
try {
|
|
5679
5716
|
const path = readStoragePath(c);
|
|
5680
5717
|
const fileName = c.req.query("fileName") ?? undefined;
|
|
5681
|
-
const expiresIn = parseExpiresIn(c.req.query("expiresIn"));
|
|
5682
5718
|
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
|
|
5686
|
-
|
|
5687
|
-
|
|
5688
|
-
|
|
5719
|
+
const file = await ctx.storage.get({ path });
|
|
5720
|
+
if (!file) {
|
|
5721
|
+
return c.json({ message: "File not found" }, 404);
|
|
5722
|
+
}
|
|
5723
|
+
|
|
5724
|
+
const headers: Record<string, string> = {};
|
|
5725
|
+
if (file.contentType) {
|
|
5726
|
+
headers["content-type"] = file.contentType;
|
|
5727
|
+
}
|
|
5728
|
+
if (fileName) {
|
|
5729
|
+
headers["content-disposition"] = \`attachment; filename="\${fileName}"\`;
|
|
5730
|
+
}
|
|
5731
|
+
|
|
5732
|
+
return c.body(file.body, 200, headers);
|
|
5689
5733
|
} catch (error) {
|
|
5690
5734
|
if (error instanceof AppflareHandledError) {
|
|
5691
5735
|
return c.json(error.payload, error.status as any);
|
|
5692
5736
|
}
|
|
5693
5737
|
|
|
5694
5738
|
return c.json(
|
|
5695
|
-
{ message: (error as Error).message ?? "Unable to
|
|
5739
|
+
{ message: (error as Error).message ?? "Unable to download file" },
|
|
5696
5740
|
400,
|
|
5697
5741
|
);
|
|
5698
5742
|
}
|
|
@@ -7827,7 +7871,7 @@ ${Q()}`}function $t(e){return `
|
|
|
7827
7871
|
adminApp.get('/functions${n.routePath}', (c) => {
|
|
7828
7872
|
${Mt(n)}
|
|
7829
7873
|
});`).join(`
|
|
7830
|
-
`)}function
|
|
7874
|
+
`)}function Dt(){return `
|
|
7831
7875
|
const getStorageBucket = (c: any): R2Bucket | null => {
|
|
7832
7876
|
const r2Binding = (options as any).r2Binding;
|
|
7833
7877
|
if (!r2Binding || !c.env[r2Binding]) return null;
|
|
@@ -7896,7 +7940,7 @@ ${Q()}`}function $t(e){return `
|
|
|
7896
7940
|
const parts = prefix.split('/').filter(Boolean);
|
|
7897
7941
|
return parts.slice(0, -1).join('/') + (parts.length > 1 ? '/' : '');
|
|
7898
7942
|
};
|
|
7899
|
-
`}function
|
|
7943
|
+
`}function Ft(){return `
|
|
7900
7944
|
const buildStorageListingContent = (listed: any, prefix: string) => {
|
|
7901
7945
|
const parts = prefix.split('/').filter(Boolean);
|
|
7902
7946
|
const breadcrumbs: any[] = [];
|
|
@@ -8155,10 +8199,10 @@ ${Q()}`}function $t(e){return `
|
|
|
8155
8199
|
|
|
8156
8200
|
${Et()}
|
|
8157
8201
|
`}function Lt(){return `
|
|
8158
|
-
${Ft()}
|
|
8159
|
-
|
|
8160
8202
|
${Dt()}
|
|
8161
8203
|
|
|
8204
|
+
${Ft()}
|
|
8205
|
+
|
|
8162
8206
|
${Wt()}
|
|
8163
8207
|
`}function zt(e,t){let n=ct(t);return `
|
|
8164
8208
|
function Layout(props: { children: any; title: string; hideSidebar?: boolean }) {
|
|
@@ -8640,7 +8684,7 @@ ${f}
|
|
|
8640
8684
|
app.route('/admin', adminApp);
|
|
8641
8685
|
app.get('/admin/', (c) => c.redirect('/admin'));
|
|
8642
8686
|
}
|
|
8643
|
-
`}function _(e){if(typeof e!="object"||e===null)return false;let t=e;return t.kind==="schema"&&typeof t.tables=="object"}function M(e){return e.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()}function
|
|
8687
|
+
`}function _(e){if(typeof e!="object"||e===null)return false;let t=e;return t.kind==="schema"&&typeof t.tables=="object"}function M(e){return e.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()}function D(e){return e.replace(/[_-]+/g," ").replace(/\s+(.)/g,(t,n)=>n.toUpperCase()).replace(/\s/g,"").replace(/^(.)/,(t,n)=>n.toUpperCase())}function J(e){return e.endsWith("ies")?`${e.slice(0,-3)}y`:e.endsWith("ses")?e.slice(0,-2):e.endsWith("s")&&e.length>1?e.slice(0,-1):e}function h(e){return JSON.stringify(e)}function dr(e){if(typeof e=="string")return h(e);if(typeof e=="number"||typeof e=="boolean")return String(e);if(e===null)return "null";if(e instanceof Date)return h(e.toISOString());throw new Error(`Unsupported SQL default value '${String(e)}'. Use string, number, boolean, null, or Date.`)}function pr(e){return {kind:"schema",tables:Object.fromEntries(Object.entries(e.tables).map(([t,n])=>[t,{kind:"table",sqlName:n.sqlName,columns:Object.fromEntries(Object.entries(n.columns).map(([r,a])=>[r,{...a}])),relations:Object.fromEntries(Object.entries(n.relations).map(([r,a])=>[r,{...a}]))}]))}}function P(e,t,n){let r=e.tables[t];return r?r.columns[n]?.type:void 0}function Kt(e,t,n,r,a,o,i){let s=t.columns[n];if(s){if(s.references&&(s.references.table!==r||s.references.column!==a))throw new Error(`Inferred relation '${e}.${n}' conflicts with explicit references(${s.references.table}.${s.references.column}).`);t.columns[n]={...s,notNull:s.notNull??(s.nullable===true?false:o.notNull),nullable:s.nullable??(o.notNull===false?true:void 0),references:{table:r,column:a,onDelete:s.references?.onDelete??o.onDelete,onUpdate:s.references?.onUpdate??o.onUpdate}};return}t.columns[n]={kind:"column",type:o.fkType??i,sqlName:o.sqlName,notNull:o.notNull??true,nullable:o.notNull===false?true:void 0,references:{table:r,column:a,onDelete:o.onDelete,onUpdate:o.onUpdate}};}function Gt(e,t,n){if(t.notNull===true&&t.nullable===true)throw new Error(`Invalid nullable configuration on '${e}': cannot set both notNull and nullable to true.`);return t.notNull===true?true:t.nullable===true||t.notNull===false?false:n}function Jt(e,t){return `${e}:${t}`}function mr(e,t,n,r){let a=Jt(e,t),o=Jt(n,r);return a<=o?{key:`${a}|${o}`,leftTable:e,leftReferenceField:t,rightTable:n,rightReferenceField:r,sourceIsLeft:true}:{key:`${o}|${a}`,leftTable:n,leftReferenceField:r,rightTable:e,rightReferenceField:t,sourceIsLeft:false}}function gr(e,t){return `${e}${D(t)}Links`}function Zt(e,t,n){let r=J(e),a=J(t);return r===a?`${n}${D(r)}Id`:`${r}Id`}function fr(e,t,n){if(e.junctionTable!==t.junctionTable)throw new Error(`manyToMany pair '${n}' has conflicting junctionTable values ('${e.junctionTable}' vs '${t.junctionTable}').`);if(e.leftField!==t.leftField)throw new Error(`manyToMany pair '${n}' has conflicting left field values ('${e.leftField}' vs '${t.leftField}').`);if(e.rightField!==t.rightField)throw new Error(`manyToMany pair '${n}' has conflicting right field values ('${e.rightField}' vs '${t.rightField}').`);if(e.leftSqlName!==t.leftSqlName)throw new Error(`manyToMany pair '${n}' has conflicting left sql name values ('${e.leftSqlName}' vs '${t.leftSqlName}').`);if(e.rightSqlName!==t.rightSqlName)throw new Error(`manyToMany pair '${n}' has conflicting right sql name values ('${e.rightSqlName}' vs '${t.rightSqlName}').`);if(e.onDelete!==t.onDelete)throw new Error(`manyToMany pair '${n}' has conflicting onDelete values ('${e.onDelete}' vs '${t.onDelete}').`);if(e.onUpdate!==t.onUpdate)throw new Error(`manyToMany pair '${n}' has conflicting onUpdate values ('${e.onUpdate}' vs '${t.onUpdate}').`);return e}function hr(e){let t=new Map;for(let[n,r]of Object.entries(e.tables))for(let a of Object.values(r.relations)){if(a.relation!=="manyToMany")continue;let o=a.referenceField??"id",i=a.targetReferenceField??"id",s=mr(n,o,a.targetTable,i),l=Zt(s.leftTable,s.rightTable,"source"),u=Zt(s.rightTable,s.leftTable,"target"),c={leftTable:s.leftTable,leftReferenceField:s.leftReferenceField,rightTable:s.rightTable,rightReferenceField:s.rightReferenceField,junctionTable:a.junctionTable??gr(s.leftTable,s.rightTable),leftField:s.sourceIsLeft?a.sourceField??l:a.targetField??l,rightField:s.sourceIsLeft?a.targetField??u:a.sourceField??u,leftSqlName:s.sourceIsLeft?a.sourceSqlName:a.targetSqlName,rightSqlName:s.sourceIsLeft?a.targetSqlName:a.sourceSqlName,onDelete:a.onDelete,onUpdate:a.onUpdate};if(c.leftField===c.rightField)throw new Error(`manyToMany pair '${s.key}' resolves to duplicate junction fields '${c.leftField}'. Set sourceField/targetField explicitly.`);let f=t.get(s.key);f?fr(f,c,s.key):t.set(s.key,c),a.referenceField=o,a.targetReferenceField=i,a.junctionTable=c.junctionTable,a.sourceField=s.sourceIsLeft?c.leftField:c.rightField,a.targetField=s.sourceIsLeft?c.rightField:c.leftField,a.sourceSqlName=s.sourceIsLeft?c.leftSqlName:c.rightSqlName,a.targetSqlName=s.sourceIsLeft?c.rightSqlName:c.leftSqlName;}for(let n of t.values()){if(n.junctionTable in e.tables)throw new Error(`manyToMany auto junction table '${n.junctionTable}' conflicts with an existing table. Set a different junctionTable name.`);let r=P(e,n.leftTable,n.leftReferenceField)??"string",a=P(e,n.rightTable,n.rightReferenceField)??"string";e.tables[n.junctionTable]={kind:"table",columns:{[n.leftField]:{kind:"column",type:r,sqlName:n.leftSqlName,notNull:true,nullable:false,references:{table:n.leftTable,column:n.leftReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true},[n.rightField]:{kind:"column",type:a,sqlName:n.rightSqlName,notNull:true,nullable:false,references:{table:n.rightTable,column:n.rightReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true}},relations:{[n.leftTable]:{kind:"relation",relation:"one",targetTable:n.leftTable,field:n.leftField,referenceField:n.leftReferenceField},[n.rightTable]:{kind:"relation",relation:"one",targetTable:n.rightTable,field:n.rightField,referenceField:n.rightReferenceField}}};}}function br(e){for(let[t,n]of Object.entries(e.tables)){for(let[r,a]of Object.entries(n.columns))if(a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`);for(let[r,a]of Object.entries(n.relations))if(a.relation!=="manyToMany"&&a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`)}}function yr(e){br(e);let t=pr(e);for(let[n,r]of Object.entries(t.tables))for(let[a,o]of Object.entries(r.relations)){if(o.relation!=="one")continue;let i=o.referenceField??"id",s=o.field??`${a}Id`;o.field=s;let l=P(t,o.targetTable,i)??o.fkType??"string";Kt(n,r,s,o.targetTable,i,{fkType:o.fkType,sqlName:o.sqlName,notNull:Gt(`${n}.${a}`,o,true),onDelete:o.onDelete,onUpdate:o.onUpdate},l);}for(let[n,r]of Object.entries(t.tables))for(let a of Object.values(r.relations)){if(a.relation!=="many")continue;let o=t.tables[a.targetTable];if(!o)continue;let i=a.referenceField??"id",s=a.field??`${J(n)}Id`;a.field=s;let l=P(t,n,i)??a.fkType??"string";Kt(a.targetTable,o,s,n,i,{fkType:a.fkType,sqlName:a.sqlName,notNull:Gt(`${n}.${a.targetTable}`,a,true),onDelete:a.onDelete,onUpdate:a.onUpdate},l);}return hr(t),t}function Xt(e){return e.primaryKey===true||e.notNull!==true||e.autoIncrement===true||e.sqlDefault!==void 0||e.runtimeDefaultFn!==void 0}function $(e){return e.notNull!==true}function wr(e,t,n){let r=t.sqlName??M(e),a=r!==e;return t.type==="int"?a?`t.int(${h(r)})`:"t.int()":t.type==="string"?t.length!==void 0?a?`t.text(${h(r)}, { length: ${t.length} })`:`t.text({ length: ${t.length} })`:a?`t.text(${h(r)})`:"t.text()":t.type==="boolean"?a?`t.int(${h(r)}, { mode: "boolean" })`:'t.int({ mode: "boolean" })':t.type==="date"?a?`t.int(${h(r)}, { mode: "timestamp_ms" })`:'t.int({ mode: "timestamp_ms" })':n==="camelToSnake"&&a?`t.text(${h(r)})`:"t.text()"}function xr(e,t,n){let r=n.field,a=n.referenceField??"id";if(!r)throw new Error(`Relation on '${e}' targeting '${n.targetTable}' is missing a local field.`);if(!(r in t.columns))throw new Error(`Relation '${e}.${r}' references missing local field '${r}'.`);return {sourceField:r,targetField:a}}function vr(e){return e.size===0?"":`import { ${Array.from(e).sort().join(", ")} } from "./auth.schema";
|
|
8644
8688
|
`}function Tr(e){return Object.values(e.relations).filter(t=>t.relation==="one").map(t=>t.targetTable)}function Rr(e,t,n){if(t.references)return {tableName:t.references.table,fieldName:t.references.column};let r=Object.values(n.relations).find(a=>a.relation==="one"&&a.field===e);if(r)return {tableName:r.targetTable,fieldName:r.referenceField??"id"}}function kr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=[];for(let[o,i]of Object.entries(r.relations))i.relation==="manyToMany"&&i.junctionTable&&a.push(`${h(o)}: {
|
|
8645
8689
|
targetTable: ${h(i.targetTable)},
|
|
8646
8690
|
junctionTable: ${h(i.junctionTable)},
|
|
@@ -8714,7 +8758,7 @@ ${o.join(`
|
|
|
8714
8758
|
${kr(e)}
|
|
8715
8759
|
|
|
8716
8760
|
${Sr(e)}
|
|
8717
|
-
`}function Yt(e,t,n){let r="z.unknown()";return e.type==="int"?r="z.number().int()":e.type==="string"?(r="z.string()",e.length!==void 0&&(r+=`.max(${e.length})`)):e.type==="boolean"?r="z.boolean()":e.type==="date"&&(r="z.date()"),t&&(r+=".optional()"),n&&(r+=".nullable()"),r}function Nr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=
|
|
8761
|
+
`}function Yt(e,t,n){let r="z.unknown()";return e.type==="int"?r="z.number().int()":e.type==="string"?(r="z.string()",e.length!==void 0&&(r+=`.max(${e.length})`)):e.type==="boolean"?r="z.boolean()":e.type==="date"&&(r="z.date()"),t&&(r+=".optional()"),n&&(r+=".nullable()"),r}function Nr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=D(n),o=[],i=[];for(let[s,l]of Object.entries(r.columns))o.push(` ${s}: ${Yt(l,Xt(l),$(l))},`),i.push(` ${s}: ${Yt(l,$(l),$(l))},`);t.push(`export const ${n}InsertSchema = z.object({
|
|
8718
8762
|
${o.join(`
|
|
8719
8763
|
`)}
|
|
8720
8764
|
});
|
|
@@ -8728,7 +8772,7 @@ export type ${a}Select = z.infer<typeof ${n}SelectSchema>;
|
|
|
8728
8772
|
`);}return `import { z } from "zod";
|
|
8729
8773
|
|
|
8730
8774
|
${t.join(`
|
|
8731
|
-
`)}`}function $r(e){return e.type==="int"?"number":e.type==="string"?"string":e.type==="boolean"?"boolean":e.type==="date"?"Date":"unknown"}function qr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=
|
|
8775
|
+
`)}`}function $r(e){return e.type==="int"?"number":e.type==="string"?"string":e.type==="boolean"?"boolean":e.type==="date"?"Date":"unknown"}function qr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=D(n),o=[],i=[];for(let[s,l]of Object.entries(r.columns)){let u=$r(l),c=$(l)?" | null":"";o.push(` ${s}${$(l)?"?":""}: ${u}${c};`),i.push(` ${s}${Xt(l)?"?":""}: ${u}${c};`);}t.push(`export type ${a} = {
|
|
8732
8776
|
${o.join(`
|
|
8733
8777
|
`)}
|
|
8734
8778
|
};
|