appflare 0.2.29 → 0.2.31
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/Documentation.md +758 -758
- package/cli/commands/index.ts +238 -238
- package/cli/generate.ts +178 -178
- package/cli/index.ts +120 -120
- package/cli/load-config.ts +184 -184
- package/cli/schema-compiler.ts +1183 -1183
- package/cli/templates/auth/README.md +156 -156
- package/cli/templates/auth/config.ts +61 -61
- package/cli/templates/auth/route-config.ts +1 -1
- package/cli/templates/auth/route-handler.ts +1 -1
- package/cli/templates/auth/route-request-utils.ts +5 -5
- package/cli/templates/auth/route.config.ts +18 -18
- package/cli/templates/auth/route.handler.ts +18 -18
- package/cli/templates/auth/route.request-utils.ts +55 -55
- package/cli/templates/auth/route.ts +14 -14
- package/cli/templates/core/README.md +266 -266
- package/cli/templates/core/app-creation.ts +19 -19
- package/cli/templates/core/client/appflare.ts +112 -112
- package/cli/templates/core/client/handlers/index.ts +748 -748
- package/cli/templates/core/client/handlers.ts +1 -1
- package/cli/templates/core/client/index.ts +7 -7
- package/cli/templates/core/client/storage.ts +195 -195
- package/cli/templates/core/client/types.ts +186 -186
- package/cli/templates/core/client-modules/appflare.ts +1 -1
- package/cli/templates/core/client-modules/handlers.ts +1 -1
- package/cli/templates/core/client-modules/index.ts +1 -1
- package/cli/templates/core/client-modules/storage.ts +1 -1
- package/cli/templates/core/client-modules/types.ts +1 -1
- package/cli/templates/core/client.artifacts.ts +39 -39
- package/cli/templates/core/client.ts +4 -4
- package/cli/templates/core/drizzle.ts +15 -15
- package/cli/templates/core/export.ts +14 -14
- package/cli/templates/core/handlers.route.ts +24 -24
- package/cli/templates/core/handlers.ts +1 -1
- package/cli/templates/core/imports.ts +9 -9
- package/cli/templates/core/server.ts +38 -38
- package/cli/templates/core/types.ts +6 -6
- package/cli/templates/core/wrangler.ts +109 -109
- package/cli/templates/dashboard/builders/functions/index.ts +17 -17
- package/cli/templates/dashboard/builders/functions/render-page/header.ts +20 -20
- package/cli/templates/dashboard/builders/functions/render-page/index.ts +33 -33
- package/cli/templates/dashboard/builders/functions/render-page/request-panel.ts +171 -171
- package/cli/templates/dashboard/builders/functions/render-page/result-panel.ts +85 -85
- package/cli/templates/dashboard/builders/functions/render-page/scripts.ts +554 -554
- package/cli/templates/dashboard/builders/navigation.ts +122 -122
- package/cli/templates/dashboard/builders/storage/index.ts +13 -13
- package/cli/templates/dashboard/builders/storage/routes/create-directory-route.ts +29 -29
- package/cli/templates/dashboard/builders/storage/routes/delete-route.ts +18 -18
- package/cli/templates/dashboard/builders/storage/routes/download-route.ts +23 -23
- package/cli/templates/dashboard/builders/storage/routes/index.ts +22 -22
- package/cli/templates/dashboard/builders/storage/routes/list-route.ts +25 -25
- package/cli/templates/dashboard/builders/storage/routes/preview-route.ts +21 -21
- package/cli/templates/dashboard/builders/storage/routes/upload-route.ts +21 -21
- package/cli/templates/dashboard/builders/storage/runtime/helpers.ts +72 -72
- package/cli/templates/dashboard/builders/storage/runtime/storage-page.ts +130 -130
- package/cli/templates/dashboard/builders/table-routes/common/drawer-panel.ts +27 -27
- package/cli/templates/dashboard/builders/table-routes/common/pagination.ts +30 -30
- package/cli/templates/dashboard/builders/table-routes/common/search-bar.ts +23 -23
- package/cli/templates/dashboard/builders/table-routes/fragments.ts +217 -217
- package/cli/templates/dashboard/builders/table-routes/helpers.ts +45 -45
- package/cli/templates/dashboard/builders/table-routes/index.ts +8 -8
- package/cli/templates/dashboard/builders/table-routes/table/actions-cell.ts +71 -71
- package/cli/templates/dashboard/builders/table-routes/table/get-route.ts +291 -291
- package/cli/templates/dashboard/builders/table-routes/table/index.ts +80 -80
- package/cli/templates/dashboard/builders/table-routes/table/post-routes.ts +163 -163
- package/cli/templates/dashboard/builders/table-routes/table-route.ts +7 -7
- package/cli/templates/dashboard/builders/table-routes/users/get-route.ts +69 -69
- package/cli/templates/dashboard/builders/table-routes/users/html/modals.ts +57 -57
- package/cli/templates/dashboard/builders/table-routes/users/html/page.ts +27 -27
- package/cli/templates/dashboard/builders/table-routes/users/html/table.ts +128 -128
- package/cli/templates/dashboard/builders/table-routes/users/index.ts +32 -32
- package/cli/templates/dashboard/builders/table-routes/users/post-routes.ts +150 -150
- package/cli/templates/dashboard/builders/table-routes/users/redirect.ts +14 -14
- package/cli/templates/dashboard/builders/table-routes/users-route.ts +10 -10
- package/cli/templates/dashboard/components/dashboard-home.ts +23 -23
- package/cli/templates/dashboard/components/layout.ts +388 -388
- package/cli/templates/dashboard/components/login-page.ts +65 -65
- package/cli/templates/dashboard/index.ts +61 -61
- package/cli/templates/dashboard/types.ts +9 -9
- package/cli/templates/handlers/README.md +353 -353
- package/cli/templates/handlers/auth.ts +37 -37
- package/cli/templates/handlers/execution.ts +42 -42
- package/cli/templates/handlers/generators/context/context-creation.ts +101 -101
- package/cli/templates/handlers/generators/context/error-helpers.ts +11 -11
- package/cli/templates/handlers/generators/context/scheduler.ts +24 -24
- package/cli/templates/handlers/generators/context/storage-api.ts +82 -82
- package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
- package/cli/templates/handlers/generators/context/types.ts +40 -40
- package/cli/templates/handlers/generators/context.ts +43 -43
- package/cli/templates/handlers/generators/execution.ts +15 -15
- package/cli/templates/handlers/generators/handlers.ts +13 -13
- package/cli/templates/handlers/generators/registration/modules/cron.ts +26 -26
- package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -75
- package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -144
- package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +14 -14
- package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +102 -102
- package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +164 -164
- package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -30
- package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +516 -516
- package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -56
- package/cli/templates/handlers/generators/registration/modules/storage.ts +199 -199
- package/cli/templates/handlers/generators/registration/sections.ts +210 -210
- package/cli/templates/handlers/generators/types/context.ts +92 -92
- package/cli/templates/handlers/generators/types/core.ts +106 -106
- package/cli/templates/handlers/generators/types/operations.ts +135 -135
- package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +281 -259
- package/cli/templates/handlers/generators/types/query-definitions/query-api-types.ts +135 -135
- package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +1103 -1031
- package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -246
- package/cli/templates/handlers/generators/types/query-definitions.ts +13 -13
- package/cli/templates/handlers/generators/types/query-runtime/handled-error.ts +13 -13
- package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts +174 -174
- package/cli/templates/handlers/generators/types/query-runtime/runtime-read.ts +157 -121
- package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
- package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +697 -676
- package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
- package/cli/templates/handlers/index.ts +43 -43
- package/cli/templates/handlers/operations.ts +116 -116
- package/cli/templates/handlers/registration.ts +91 -91
- package/cli/templates/handlers/types.ts +15 -15
- package/cli/templates/handlers/utils.ts +48 -48
- package/cli/types.ts +110 -110
- package/cli/utils/handler-discovery.ts +466 -466
- package/cli/utils/json-utils.ts +24 -24
- package/cli/utils/path-utils.ts +19 -19
- package/cli/utils/schema-discovery.ts +399 -399
- package/dist/cli/index.d.mts +2 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +301 -118
- package/dist/cli/index.mjs +301 -118
- package/index.ts +18 -18
- package/package.json +58 -58
- package/react/index.ts +5 -5
- package/react/use-infinite-query.ts +252 -252
- package/react/use-mutation.ts +89 -89
- package/react/use-query.ts +207 -207
- package/schema.ts +415 -415
- package/test-better-auth-hash.ts +2 -2
- package/tsconfig.json +6 -6
- package/tsup.config.ts +82 -82
|
@@ -1,748 +1,748 @@
|
|
|
1
|
-
import type { DiscoveredHandlerOperation } from "../../../../utils/handler-discovery";
|
|
2
|
-
|
|
3
|
-
type HttpOperation = {
|
|
4
|
-
kind: "query" | "mutation";
|
|
5
|
-
routePath: string;
|
|
6
|
-
queryName: string;
|
|
7
|
-
segments: string[];
|
|
8
|
-
importPath: string;
|
|
9
|
-
exportName: string;
|
|
10
|
-
alias: string;
|
|
11
|
-
schemaConst: string;
|
|
12
|
-
typeBase: string;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
type TreeNode = {
|
|
16
|
-
children: Map<string, TreeNode>;
|
|
17
|
-
operation?: HttpOperation;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
function toSafeIdentifier(value: string): string {
|
|
21
|
-
const sanitized = value.replace(/[^A-Za-z0-9_]/g, "_");
|
|
22
|
-
if (/^[0-9]/.test(sanitized)) {
|
|
23
|
-
return `_${sanitized}`;
|
|
24
|
-
}
|
|
25
|
-
return sanitized || "_route";
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function toPascalCase(value: string): string {
|
|
29
|
-
return value
|
|
30
|
-
.split(/[^A-Za-z0-9]+/)
|
|
31
|
-
.filter(Boolean)
|
|
32
|
-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
33
|
-
.join("");
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function toObjectKey(value: string): string {
|
|
37
|
-
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value)) {
|
|
38
|
-
return value;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return JSON.stringify(value);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function createTree(operations: HttpOperation[]): TreeNode {
|
|
45
|
-
const root: TreeNode = {
|
|
46
|
-
children: new Map<string, TreeNode>(),
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
for (const operation of operations) {
|
|
50
|
-
let cursor = root;
|
|
51
|
-
for (const segment of operation.segments) {
|
|
52
|
-
let child = cursor.children.get(segment);
|
|
53
|
-
if (!child) {
|
|
54
|
-
child = { children: new Map<string, TreeNode>() };
|
|
55
|
-
cursor.children.set(segment, child);
|
|
56
|
-
}
|
|
57
|
-
cursor = child;
|
|
58
|
-
}
|
|
59
|
-
cursor.operation = operation;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return root;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function renderTreeObject(node: TreeNode, indentLevel = 1): string {
|
|
66
|
-
const indent = "\t".repeat(indentLevel);
|
|
67
|
-
const childIndent = "\t".repeat(indentLevel + 1);
|
|
68
|
-
const entries = Array.from(node.children.entries()).sort(([a], [b]) =>
|
|
69
|
-
a.localeCompare(b),
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
if (entries.length === 0) {
|
|
73
|
-
return node.operation ? `${node.operation.alias}Route(runtime)` : "{}";
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const lines: string[] = ["{"];
|
|
77
|
-
for (const [segment, childNode] of entries) {
|
|
78
|
-
lines.push(
|
|
79
|
-
`${childIndent}${toObjectKey(segment)}: ${renderTreeObject(childNode, indentLevel + 1)},`,
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
if (node.operation) {
|
|
83
|
-
lines.push(`${childIndent}run: ${node.operation.alias}Route(runtime).run,`);
|
|
84
|
-
lines.push(
|
|
85
|
-
`${childIndent}schema: ${node.operation.alias}Route(runtime).schema,`,
|
|
86
|
-
);
|
|
87
|
-
if (node.operation.kind === "query") {
|
|
88
|
-
lines.push(
|
|
89
|
-
`${childIndent}subscribe: ${node.operation.alias}Route(runtime).subscribe,`,
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
lines.push(`${indent}}`);
|
|
94
|
-
return lines.join("\n");
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function normalizeOperation(
|
|
98
|
-
operation: DiscoveredHandlerOperation,
|
|
99
|
-
index: number,
|
|
100
|
-
): HttpOperation | null {
|
|
101
|
-
if (operation.kind !== "query" && operation.kind !== "mutation") {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const segments =
|
|
106
|
-
operation.clientSegments && operation.clientSegments.length > 0
|
|
107
|
-
? operation.clientSegments
|
|
108
|
-
: operation.routePath
|
|
109
|
-
.replace(/^\//, "")
|
|
110
|
-
.split("/")
|
|
111
|
-
.filter(Boolean)
|
|
112
|
-
.slice(1);
|
|
113
|
-
|
|
114
|
-
if (segments.length === 0) {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const alias = toSafeIdentifier(
|
|
119
|
-
`op_${index}_${operation.kind}_${segments.join("_")}`,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
kind: operation.kind,
|
|
124
|
-
routePath: operation.routePath,
|
|
125
|
-
queryName: operation.handlerName ?? segments.join("/"),
|
|
126
|
-
segments,
|
|
127
|
-
importPath: operation.clientImportPath,
|
|
128
|
-
exportName: operation.exportName,
|
|
129
|
-
alias,
|
|
130
|
-
schemaConst: `${alias}Schema`,
|
|
131
|
-
typeBase: `${toPascalCase(operation.kind)}${toPascalCase(segments.join("_"))}`,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function renderRouteFactory(operation: HttpOperation): string {
|
|
136
|
-
const inputType = `${operation.typeBase}Input`;
|
|
137
|
-
const outputType = `${operation.typeBase}Output`;
|
|
138
|
-
const schemaType = `${operation.typeBase}Schema`;
|
|
139
|
-
const method = operation.kind === "query" ? "GET" : "POST";
|
|
140
|
-
if (operation.kind === "query") {
|
|
141
|
-
return `const ${operation.alias}Route = (
|
|
142
|
-
runtime: RequestRuntime,
|
|
143
|
-
): AppflareQueryRouteClient<typeof ${operation.schemaConst}, ${outputType}> => {
|
|
144
|
-
const run: AppflareQueryRouteClient<typeof ${operation.schemaConst}, ${outputType}>["run"] = async (
|
|
145
|
-
...params: AppflareRunParams<${inputType}>
|
|
146
|
-
) => {
|
|
147
|
-
const { args, options } = resolveRunParams<${inputType}>(params);
|
|
148
|
-
const mergedOptions = mergeRouteOptions(runtime.options, options);
|
|
149
|
-
const resultOptions: AppflareRouteCallOptions<"return"> = {
|
|
150
|
-
...(mergedOptions ?? {}),
|
|
151
|
-
errorMode: "return",
|
|
152
|
-
};
|
|
153
|
-
const parsed = ${operation.schemaConst}.parse(args);
|
|
154
|
-
return requestRoute<${outputType}>(runtime.endpoint, {
|
|
155
|
-
route: ${JSON.stringify(operation.routePath)},
|
|
156
|
-
method: ${JSON.stringify(method)},
|
|
157
|
-
input: parsed,
|
|
158
|
-
options: resultOptions,
|
|
159
|
-
getAuthToken: runtime.getAuthToken,
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
const subscribe = ({
|
|
163
|
-
onChange,
|
|
164
|
-
onError,
|
|
165
|
-
authToken,
|
|
166
|
-
args,
|
|
167
|
-
requestOptions,
|
|
168
|
-
signal,
|
|
169
|
-
}: AppflareQuerySubscribeOptions<${inputType}, ${outputType}>): AppflareRealtimeSubscription => {
|
|
170
|
-
const mergedOptions = mergeRouteOptions(runtime.options, requestOptions);
|
|
171
|
-
const parsedArgs = ${operation.schemaConst}.parse(normalizeRouteInput(args));
|
|
172
|
-
const requestAuthToken = resolveRealtimeAuthToken(authToken, mergedOptions?.headers);
|
|
173
|
-
|
|
174
|
-
let removed = false;
|
|
175
|
-
let socket: WebSocket | null = null;
|
|
176
|
-
let token: string | null = null;
|
|
177
|
-
let resolvedAuthToken = requestAuthToken;
|
|
178
|
-
|
|
179
|
-
const remove = () => {
|
|
180
|
-
if (removed) {
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
removed = true;
|
|
184
|
-
|
|
185
|
-
if (socket && (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN)) {
|
|
186
|
-
socket.close();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (token) {
|
|
190
|
-
void requestRoute<{ ok: boolean }>(runtime.endpoint, {
|
|
191
|
-
route: "/realtime/unsubscribe",
|
|
192
|
-
method: "POST",
|
|
193
|
-
input: {
|
|
194
|
-
token,
|
|
195
|
-
authToken: resolvedAuthToken,
|
|
196
|
-
},
|
|
197
|
-
options: mergedOptions,
|
|
198
|
-
getAuthToken: runtime.getAuthToken,
|
|
199
|
-
}).catch((error) => {
|
|
200
|
-
onError?.(error);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
if (signal) {
|
|
206
|
-
if (signal.aborted) {
|
|
207
|
-
remove();
|
|
208
|
-
return { remove };
|
|
209
|
-
}
|
|
210
|
-
signal.addEventListener("abort", remove, { once: true });
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
void (async () => {
|
|
214
|
-
try {
|
|
215
|
-
if (!resolvedAuthToken && runtime.getAuthToken) {
|
|
216
|
-
const runtimeAuthToken = await runtime.getAuthToken();
|
|
217
|
-
if (
|
|
218
|
-
typeof runtimeAuthToken === "string" &&
|
|
219
|
-
runtimeAuthToken.trim().length > 0
|
|
220
|
-
) {
|
|
221
|
-
resolvedAuthToken = runtimeAuthToken.trim();
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const subscription = (await requestRoute<RealtimeSubscriptionResponse>(runtime.endpoint, {
|
|
227
|
-
route: "/realtime/subscribe",
|
|
228
|
-
method: "POST",
|
|
229
|
-
input: {
|
|
230
|
-
queryName: ${JSON.stringify(operation.queryName)},
|
|
231
|
-
args: parsedArgs,
|
|
232
|
-
authToken: resolvedAuthToken,
|
|
233
|
-
},
|
|
234
|
-
options: mergedOptions,
|
|
235
|
-
getAuthToken: runtime.getAuthToken,
|
|
236
|
-
})) as RealtimeSubscriptionResponse;
|
|
237
|
-
|
|
238
|
-
if (removed) {
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
token = subscription.token;
|
|
243
|
-
const websocketUrl = createRealtimeWebsocketUrl(
|
|
244
|
-
subscription.websocket.url,
|
|
245
|
-
runtime.wsEndpoint,
|
|
246
|
-
);
|
|
247
|
-
websocketUrl.searchParams.set(subscription.websocket.params.tokenParam, subscription.token);
|
|
248
|
-
websocketUrl.searchParams.set(subscription.websocket.params.authTokenParam, resolvedAuthToken);
|
|
249
|
-
|
|
250
|
-
socket = new WebSocket(websocketUrl.toString(), subscription.websocket.protocol);
|
|
251
|
-
socket.addEventListener("message", (event) => {
|
|
252
|
-
if (removed) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const text = typeof event.data === "string" ? event.data : String(event.data ?? "");
|
|
257
|
-
let message: unknown;
|
|
258
|
-
try {
|
|
259
|
-
message = JSON.parse(text);
|
|
260
|
-
} catch {
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (
|
|
265
|
-
typeof message === "object" &&
|
|
266
|
-
message !== null &&
|
|
267
|
-
"event" in message &&
|
|
268
|
-
(message as { event?: unknown }).event === "query:update"
|
|
269
|
-
) {
|
|
270
|
-
const payload = (message as { payload?: unknown }).payload;
|
|
271
|
-
if (typeof payload === "object" && payload !== null && "data" in payload) {
|
|
272
|
-
onChange(
|
|
273
|
-
(payload as { data: ${outputType} }).data,
|
|
274
|
-
message as AppflareRealtimeQueryUpdate<${outputType}>,
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
socket.addEventListener("error", () => {
|
|
280
|
-
onError?.(new Error("Realtime websocket error"));
|
|
281
|
-
});
|
|
282
|
-
} catch (error) {
|
|
283
|
-
if (onError) {
|
|
284
|
-
onError(error);
|
|
285
|
-
} else {
|
|
286
|
-
console.warn("[appflare:subscribe] Subscription failed:", error);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
})();
|
|
290
|
-
|
|
291
|
-
return {
|
|
292
|
-
remove,
|
|
293
|
-
};
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
return {
|
|
297
|
-
schema: ${schemaType},
|
|
298
|
-
run,
|
|
299
|
-
subscribe,
|
|
300
|
-
};
|
|
301
|
-
};`;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return `const ${operation.alias}Route = (
|
|
305
|
-
runtime: RequestRuntime,
|
|
306
|
-
): AppflareRouteClient<typeof ${operation.schemaConst}, ${outputType}> => {
|
|
307
|
-
const run: AppflareRouteClient<typeof ${operation.schemaConst}, ${outputType}>["run"] = async (
|
|
308
|
-
...params: AppflareRunParams<${inputType}>
|
|
309
|
-
) => {
|
|
310
|
-
const { args, options } = resolveRunParams<${inputType}>(params);
|
|
311
|
-
const mergedOptions = mergeRouteOptions(runtime.options, options);
|
|
312
|
-
const resultOptions: AppflareRouteCallOptions<"return"> = {
|
|
313
|
-
...(mergedOptions ?? {}),
|
|
314
|
-
errorMode: "return",
|
|
315
|
-
};
|
|
316
|
-
const parsed = ${operation.schemaConst}.parse(args);
|
|
317
|
-
return requestRoute<${outputType}>(runtime.endpoint, {
|
|
318
|
-
route: ${JSON.stringify(operation.routePath)},
|
|
319
|
-
method: ${JSON.stringify(method)},
|
|
320
|
-
input: parsed,
|
|
321
|
-
options: resultOptions,
|
|
322
|
-
getAuthToken: runtime.getAuthToken,
|
|
323
|
-
});
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
return {
|
|
327
|
-
schema: ${schemaType},
|
|
328
|
-
run,
|
|
329
|
-
};
|
|
330
|
-
};`;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
export function generateClientHandlersSource(
|
|
334
|
-
operations: DiscoveredHandlerOperation[],
|
|
335
|
-
): string {
|
|
336
|
-
const normalizedOperations = operations
|
|
337
|
-
.map((operation, index) => normalizeOperation(operation, index))
|
|
338
|
-
.filter((operation): operation is HttpOperation => operation !== null);
|
|
339
|
-
|
|
340
|
-
const queryOperations = normalizedOperations.filter(
|
|
341
|
-
(operation) => operation.kind === "query",
|
|
342
|
-
);
|
|
343
|
-
const mutationOperations = normalizedOperations.filter(
|
|
344
|
-
(operation) => operation.kind === "mutation",
|
|
345
|
-
);
|
|
346
|
-
|
|
347
|
-
const imports = normalizedOperations
|
|
348
|
-
.map((operation) => {
|
|
349
|
-
return `import { ${operation.exportName} as ${operation.alias} } from "${operation.importPath}";`;
|
|
350
|
-
})
|
|
351
|
-
.join("\n");
|
|
352
|
-
|
|
353
|
-
const schemaDeclarations = normalizedOperations
|
|
354
|
-
.map((operation) => {
|
|
355
|
-
const inputType = `${operation.typeBase}Input`;
|
|
356
|
-
const outputType = `${operation.typeBase}Output`;
|
|
357
|
-
const schemaType = `${operation.typeBase}Schema`;
|
|
358
|
-
return `const ${operation.schemaConst} = z.object(${operation.alias}.definition.args);
|
|
359
|
-
export type ${inputType} = z.input<typeof ${operation.schemaConst}>;
|
|
360
|
-
export type ${outputType} = Awaited<ReturnType<typeof ${operation.alias}.definition.handler>>;
|
|
361
|
-
export const ${schemaType} = ${operation.schemaConst};`;
|
|
362
|
-
})
|
|
363
|
-
.join("\n\n");
|
|
364
|
-
|
|
365
|
-
const routeFactories = normalizedOperations
|
|
366
|
-
.map((operation) => renderRouteFactory(operation))
|
|
367
|
-
.join("\n\n");
|
|
368
|
-
|
|
369
|
-
const queryTree = renderTreeObject(createTree(queryOperations));
|
|
370
|
-
const mutationTree = renderTreeObject(createTree(mutationOperations));
|
|
371
|
-
|
|
372
|
-
return `import { z } from "zod";
|
|
373
|
-
import type {
|
|
374
|
-
AppflareErrorMode,
|
|
375
|
-
AppflareRequestError,
|
|
376
|
-
AppflareRequestResult,
|
|
377
|
-
AppflareQueryRouteClient,
|
|
378
|
-
AppflareQuerySubscribeOptions,
|
|
379
|
-
AppflareRealtimeQueryUpdate,
|
|
380
|
-
AppflareRealtimeSubscription,
|
|
381
|
-
RealtimeSubscriptionResponse,
|
|
382
|
-
AppflareResultRouteCallOptions,
|
|
383
|
-
AppflareRouteCallOptions,
|
|
384
|
-
AppflareRouteClient,
|
|
385
|
-
AppflareRunParams,
|
|
386
|
-
} from "./types";
|
|
387
|
-
${imports ? `\n${imports}` : ""}
|
|
388
|
-
|
|
389
|
-
${schemaDeclarations}
|
|
390
|
-
|
|
391
|
-
type RequestRuntime = {
|
|
392
|
-
endpoint: string;
|
|
393
|
-
wsEndpoint?: string;
|
|
394
|
-
getAuthToken?: () => string | Promise<string>;
|
|
395
|
-
options?: AppflareResultRouteCallOptions;
|
|
396
|
-
};
|
|
397
|
-
|
|
398
|
-
type AnyRouteCallOptions = AppflareRouteCallOptions<AppflareErrorMode>;
|
|
399
|
-
|
|
400
|
-
type RequestRouteInit = {
|
|
401
|
-
route: string;
|
|
402
|
-
method: "GET" | "POST";
|
|
403
|
-
input: unknown;
|
|
404
|
-
options?: AnyRouteCallOptions;
|
|
405
|
-
getAuthToken?: () => string | Promise<string>;
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
type RequestRouteReturnInit = Omit<RequestRouteInit, "options"> & {
|
|
409
|
-
options: AppflareRouteCallOptions<"return">;
|
|
410
|
-
};
|
|
411
|
-
|
|
412
|
-
function mergeRouteOptions(
|
|
413
|
-
base?: AppflareResultRouteCallOptions,
|
|
414
|
-
override?: AnyRouteCallOptions,
|
|
415
|
-
): AnyRouteCallOptions | undefined {
|
|
416
|
-
if (!base && !override) {
|
|
417
|
-
return undefined;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
return {
|
|
421
|
-
...base,
|
|
422
|
-
...override,
|
|
423
|
-
headers: {
|
|
424
|
-
...(base?.headers ?? {}),
|
|
425
|
-
...(override?.headers ?? {}),
|
|
426
|
-
},
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function normalizeRouteInput<TInput extends Record<string, unknown>>(
|
|
431
|
-
input: TInput | undefined,
|
|
432
|
-
): TInput {
|
|
433
|
-
return (input ?? {}) as TInput;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
function resolveRunParams<TInput extends Record<string, unknown>>(
|
|
437
|
-
params: AppflareRunParams<TInput>,
|
|
438
|
-
): {
|
|
439
|
-
args: TInput;
|
|
440
|
-
options?: AppflareResultRouteCallOptions;
|
|
441
|
-
} {
|
|
442
|
-
const [args, options] = params;
|
|
443
|
-
return {
|
|
444
|
-
args: normalizeRouteInput(args),
|
|
445
|
-
options,
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
function readHeaderCaseInsensitive(
|
|
450
|
-
headers: HeadersInit | undefined,
|
|
451
|
-
headerName: string,
|
|
452
|
-
): string {
|
|
453
|
-
if (!headers) {
|
|
454
|
-
return "";
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
const normalizedHeaderName = headerName.toLowerCase();
|
|
458
|
-
if (Array.isArray(headers)) {
|
|
459
|
-
for (const entry of headers) {
|
|
460
|
-
if (!Array.isArray(entry) || entry.length < 2) {
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const key = String(entry[0]).toLowerCase();
|
|
465
|
-
if (key === normalizedHeaderName) {
|
|
466
|
-
return String(entry[1]);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
return "";
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
if (typeof (headers as { forEach?: unknown }).forEach === "function") {
|
|
474
|
-
let value = "";
|
|
475
|
-
(headers as Headers).forEach((headerValue, key) => {
|
|
476
|
-
if (!value && key.toLowerCase() === normalizedHeaderName) {
|
|
477
|
-
value = String(headerValue);
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
|
-
return value;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
for (const [key, value] of Object.entries(headers as Record<string, unknown>)) {
|
|
484
|
-
if (key.toLowerCase() === normalizedHeaderName) {
|
|
485
|
-
return String(value ?? "");
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
return "";
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
function resolveRealtimeAuthToken(
|
|
493
|
-
provided: string | undefined,
|
|
494
|
-
headers: HeadersInit | undefined,
|
|
495
|
-
): string {
|
|
496
|
-
if (typeof provided === "string" && provided.trim().length > 0) {
|
|
497
|
-
return provided.trim();
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const authorization = readHeaderCaseInsensitive(headers, "authorization");
|
|
501
|
-
const bearerMatch = authorization.match(/^Bearer\\s+(.+)$/i);
|
|
502
|
-
if (bearerMatch && bearerMatch[1]) {
|
|
503
|
-
return bearerMatch[1].trim();
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
return "";
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
function hasAuthorizationHeader(headers: HeadersInit | undefined): boolean {
|
|
510
|
-
if (!headers) {
|
|
511
|
-
return false;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
if (Array.isArray(headers)) {
|
|
515
|
-
return headers.some(
|
|
516
|
-
(entry) =>
|
|
517
|
-
Array.isArray(entry) &&
|
|
518
|
-
String(entry[0]).toLowerCase() === "authorization",
|
|
519
|
-
);
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
if (typeof (headers as { forEach?: unknown }).forEach === "function") {
|
|
523
|
-
return (headers as Headers).has("authorization");
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
for (const key of Object.keys(headers as Record<string, unknown>)) {
|
|
527
|
-
if (key.toLowerCase() === "authorization") {
|
|
528
|
-
return true;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
return false;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
async function resolveRequestAuthToken(
|
|
536
|
-
headers: HeadersInit | undefined,
|
|
537
|
-
getAuthToken: (() => string | Promise<string>) | undefined,
|
|
538
|
-
): Promise<string> {
|
|
539
|
-
if (!getAuthToken || hasAuthorizationHeader(headers)) {
|
|
540
|
-
return "";
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
const token = await getAuthToken();
|
|
544
|
-
if (typeof token === "string" && token.trim().length > 0) {
|
|
545
|
-
return token.trim();
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
return "";
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function createRealtimeWebsocketUrl(
|
|
552
|
-
serverUrl: string,
|
|
553
|
-
wsEndpoint: string | undefined,
|
|
554
|
-
): URL {
|
|
555
|
-
const websocketUrl = new URL(serverUrl);
|
|
556
|
-
if (!wsEndpoint) {
|
|
557
|
-
return websocketUrl;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
const wsBase = new URL(wsEndpoint);
|
|
561
|
-
websocketUrl.protocol = wsBase.protocol;
|
|
562
|
-
websocketUrl.host = wsBase.host;
|
|
563
|
-
if (wsBase.pathname && wsBase.pathname !== "/") {
|
|
564
|
-
websocketUrl.pathname = wsBase.pathname;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
return websocketUrl;
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
function appendQueryParamValue(
|
|
571
|
-
query: URLSearchParams,
|
|
572
|
-
key: string,
|
|
573
|
-
value: unknown,
|
|
574
|
-
): void {
|
|
575
|
-
if (value === undefined || value === null) {
|
|
576
|
-
return;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
if (Array.isArray(value)) {
|
|
580
|
-
for (const entry of value) {
|
|
581
|
-
appendQueryParamValue(query, key, entry);
|
|
582
|
-
}
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
if (value instanceof Date) {
|
|
587
|
-
query.append(key, value.toISOString());
|
|
588
|
-
return;
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
if (typeof value === "object") {
|
|
592
|
-
query.append(key, JSON.stringify(value));
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
query.append(key, String(value));
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
function createQuery(input: unknown): string {
|
|
600
|
-
if (!input || typeof input !== "object") {
|
|
601
|
-
return "";
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
const query = new URLSearchParams();
|
|
605
|
-
for (const [key, value] of Object.entries(input as Record<string, unknown>)) {
|
|
606
|
-
appendQueryParamValue(query, key, value);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
const encoded = query.toString();
|
|
610
|
-
return encoded.length > 0 ? "?" + encoded : "";
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
function createRequestError(
|
|
614
|
-
init: RequestRouteInit,
|
|
615
|
-
response: Response,
|
|
616
|
-
body: unknown,
|
|
617
|
-
responseText: string,
|
|
618
|
-
): AppflareRequestError {
|
|
619
|
-
const fallbackMessage =
|
|
620
|
-
responseText.trim().length > 0
|
|
621
|
-
? responseText
|
|
622
|
-
: "Request failed with status " + response.status;
|
|
623
|
-
const message =
|
|
624
|
-
typeof body === "object" &&
|
|
625
|
-
body !== null &&
|
|
626
|
-
"message" in body &&
|
|
627
|
-
typeof (body as { message?: unknown }).message === "string"
|
|
628
|
-
? ((body as { message: string }).message ?? fallbackMessage)
|
|
629
|
-
: fallbackMessage;
|
|
630
|
-
|
|
631
|
-
return {
|
|
632
|
-
route: init.route,
|
|
633
|
-
method: init.method,
|
|
634
|
-
status: response.status,
|
|
635
|
-
message,
|
|
636
|
-
body,
|
|
637
|
-
responseText,
|
|
638
|
-
};
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
async function requestRoute<TOutput>(
|
|
642
|
-
endpoint: string,
|
|
643
|
-
init: RequestRouteReturnInit,
|
|
644
|
-
): Promise<AppflareRequestResult<TOutput>>;
|
|
645
|
-
async function requestRoute<TOutput>(
|
|
646
|
-
endpoint: string,
|
|
647
|
-
init: RequestRouteInit,
|
|
648
|
-
): Promise<TOutput | AppflareRequestResult<TOutput>>;
|
|
649
|
-
async function requestRoute<TOutput>(
|
|
650
|
-
endpoint: string,
|
|
651
|
-
init: RequestRouteInit,
|
|
652
|
-
): Promise<TOutput | AppflareRequestResult<TOutput>> {
|
|
653
|
-
const requestAuthToken = await resolveRequestAuthToken(
|
|
654
|
-
init.options?.headers,
|
|
655
|
-
init.getAuthToken,
|
|
656
|
-
);
|
|
657
|
-
const requestUrl =
|
|
658
|
-
init.method === "GET"
|
|
659
|
-
? endpoint + init.route + createQuery(init.input)
|
|
660
|
-
: endpoint + init.route;
|
|
661
|
-
const headers: HeadersInit = {
|
|
662
|
-
...(init.options?.headers ?? {}),
|
|
663
|
-
...(requestAuthToken
|
|
664
|
-
? { authorization: "Bearer " + requestAuthToken }
|
|
665
|
-
: {}),
|
|
666
|
-
...(init.method === "POST" ? { "content-type": "application/json" } : {}),
|
|
667
|
-
};
|
|
668
|
-
|
|
669
|
-
const response = await fetch(requestUrl, {
|
|
670
|
-
method: init.method,
|
|
671
|
-
headers,
|
|
672
|
-
body: init.method === "POST" ? JSON.stringify(init.input ?? {}) : undefined,
|
|
673
|
-
signal: init.options?.signal,
|
|
674
|
-
});
|
|
675
|
-
|
|
676
|
-
const responseText = await response.text();
|
|
677
|
-
let body: unknown = undefined;
|
|
678
|
-
if (responseText.length > 0) {
|
|
679
|
-
try {
|
|
680
|
-
body = JSON.parse(responseText);
|
|
681
|
-
} catch {
|
|
682
|
-
body = responseText;
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
if (!response.ok) {
|
|
687
|
-
const requestError = createRequestError(init, response, body, responseText);
|
|
688
|
-
init.options?.onError?.(requestError);
|
|
689
|
-
if (init.options?.errorMode === "return") {
|
|
690
|
-
return {
|
|
691
|
-
data: null,
|
|
692
|
-
error: requestError,
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
const error = new Error(requestError.message) as Error & {
|
|
697
|
-
cause?: AppflareRequestError;
|
|
698
|
-
};
|
|
699
|
-
error.cause = requestError;
|
|
700
|
-
throw error;
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
if (init.options?.errorMode === "return") {
|
|
704
|
-
return {
|
|
705
|
-
data: body as TOutput,
|
|
706
|
-
error: null,
|
|
707
|
-
};
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
return body as TOutput;
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
${routeFactories}
|
|
714
|
-
|
|
715
|
-
export function createQueriesClient(
|
|
716
|
-
endpoint: string,
|
|
717
|
-
options?: AppflareResultRouteCallOptions,
|
|
718
|
-
wsEndpoint?: string,
|
|
719
|
-
getAuthToken?: () => string | Promise<string>,
|
|
720
|
-
) {
|
|
721
|
-
const runtime: RequestRuntime = {
|
|
722
|
-
endpoint: endpoint.replace(/\\/$/, ""),
|
|
723
|
-
wsEndpoint: wsEndpoint?.replace(/\\/$/, ""),
|
|
724
|
-
getAuthToken,
|
|
725
|
-
options,
|
|
726
|
-
};
|
|
727
|
-
|
|
728
|
-
return ${queryTree} as const;
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
export function createMutationsClient(
|
|
732
|
-
endpoint: string,
|
|
733
|
-
options?: AppflareResultRouteCallOptions,
|
|
734
|
-
getAuthToken?: () => string | Promise<string>,
|
|
735
|
-
) {
|
|
736
|
-
const runtime: RequestRuntime = {
|
|
737
|
-
endpoint: endpoint.replace(/\\/$/, ""),
|
|
738
|
-
getAuthToken,
|
|
739
|
-
options,
|
|
740
|
-
};
|
|
741
|
-
|
|
742
|
-
return ${mutationTree} as const;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
export type QueriesClient = ReturnType<typeof createQueriesClient>;
|
|
746
|
-
export type MutationsClient = ReturnType<typeof createMutationsClient>;
|
|
747
|
-
`;
|
|
748
|
-
}
|
|
1
|
+
import type { DiscoveredHandlerOperation } from "../../../../utils/handler-discovery";
|
|
2
|
+
|
|
3
|
+
type HttpOperation = {
|
|
4
|
+
kind: "query" | "mutation";
|
|
5
|
+
routePath: string;
|
|
6
|
+
queryName: string;
|
|
7
|
+
segments: string[];
|
|
8
|
+
importPath: string;
|
|
9
|
+
exportName: string;
|
|
10
|
+
alias: string;
|
|
11
|
+
schemaConst: string;
|
|
12
|
+
typeBase: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type TreeNode = {
|
|
16
|
+
children: Map<string, TreeNode>;
|
|
17
|
+
operation?: HttpOperation;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function toSafeIdentifier(value: string): string {
|
|
21
|
+
const sanitized = value.replace(/[^A-Za-z0-9_]/g, "_");
|
|
22
|
+
if (/^[0-9]/.test(sanitized)) {
|
|
23
|
+
return `_${sanitized}`;
|
|
24
|
+
}
|
|
25
|
+
return sanitized || "_route";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toPascalCase(value: string): string {
|
|
29
|
+
return value
|
|
30
|
+
.split(/[^A-Za-z0-9]+/)
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
33
|
+
.join("");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function toObjectKey(value: string): string {
|
|
37
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value)) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return JSON.stringify(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createTree(operations: HttpOperation[]): TreeNode {
|
|
45
|
+
const root: TreeNode = {
|
|
46
|
+
children: new Map<string, TreeNode>(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
for (const operation of operations) {
|
|
50
|
+
let cursor = root;
|
|
51
|
+
for (const segment of operation.segments) {
|
|
52
|
+
let child = cursor.children.get(segment);
|
|
53
|
+
if (!child) {
|
|
54
|
+
child = { children: new Map<string, TreeNode>() };
|
|
55
|
+
cursor.children.set(segment, child);
|
|
56
|
+
}
|
|
57
|
+
cursor = child;
|
|
58
|
+
}
|
|
59
|
+
cursor.operation = operation;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return root;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function renderTreeObject(node: TreeNode, indentLevel = 1): string {
|
|
66
|
+
const indent = "\t".repeat(indentLevel);
|
|
67
|
+
const childIndent = "\t".repeat(indentLevel + 1);
|
|
68
|
+
const entries = Array.from(node.children.entries()).sort(([a], [b]) =>
|
|
69
|
+
a.localeCompare(b),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (entries.length === 0) {
|
|
73
|
+
return node.operation ? `${node.operation.alias}Route(runtime)` : "{}";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const lines: string[] = ["{"];
|
|
77
|
+
for (const [segment, childNode] of entries) {
|
|
78
|
+
lines.push(
|
|
79
|
+
`${childIndent}${toObjectKey(segment)}: ${renderTreeObject(childNode, indentLevel + 1)},`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (node.operation) {
|
|
83
|
+
lines.push(`${childIndent}run: ${node.operation.alias}Route(runtime).run,`);
|
|
84
|
+
lines.push(
|
|
85
|
+
`${childIndent}schema: ${node.operation.alias}Route(runtime).schema,`,
|
|
86
|
+
);
|
|
87
|
+
if (node.operation.kind === "query") {
|
|
88
|
+
lines.push(
|
|
89
|
+
`${childIndent}subscribe: ${node.operation.alias}Route(runtime).subscribe,`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
lines.push(`${indent}}`);
|
|
94
|
+
return lines.join("\n");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function normalizeOperation(
|
|
98
|
+
operation: DiscoveredHandlerOperation,
|
|
99
|
+
index: number,
|
|
100
|
+
): HttpOperation | null {
|
|
101
|
+
if (operation.kind !== "query" && operation.kind !== "mutation") {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const segments =
|
|
106
|
+
operation.clientSegments && operation.clientSegments.length > 0
|
|
107
|
+
? operation.clientSegments
|
|
108
|
+
: operation.routePath
|
|
109
|
+
.replace(/^\//, "")
|
|
110
|
+
.split("/")
|
|
111
|
+
.filter(Boolean)
|
|
112
|
+
.slice(1);
|
|
113
|
+
|
|
114
|
+
if (segments.length === 0) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const alias = toSafeIdentifier(
|
|
119
|
+
`op_${index}_${operation.kind}_${segments.join("_")}`,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
kind: operation.kind,
|
|
124
|
+
routePath: operation.routePath,
|
|
125
|
+
queryName: operation.handlerName ?? segments.join("/"),
|
|
126
|
+
segments,
|
|
127
|
+
importPath: operation.clientImportPath,
|
|
128
|
+
exportName: operation.exportName,
|
|
129
|
+
alias,
|
|
130
|
+
schemaConst: `${alias}Schema`,
|
|
131
|
+
typeBase: `${toPascalCase(operation.kind)}${toPascalCase(segments.join("_"))}`,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function renderRouteFactory(operation: HttpOperation): string {
|
|
136
|
+
const inputType = `${operation.typeBase}Input`;
|
|
137
|
+
const outputType = `${operation.typeBase}Output`;
|
|
138
|
+
const schemaType = `${operation.typeBase}Schema`;
|
|
139
|
+
const method = operation.kind === "query" ? "GET" : "POST";
|
|
140
|
+
if (operation.kind === "query") {
|
|
141
|
+
return `const ${operation.alias}Route = (
|
|
142
|
+
runtime: RequestRuntime,
|
|
143
|
+
): AppflareQueryRouteClient<typeof ${operation.schemaConst}, ${outputType}> => {
|
|
144
|
+
const run: AppflareQueryRouteClient<typeof ${operation.schemaConst}, ${outputType}>["run"] = async (
|
|
145
|
+
...params: AppflareRunParams<${inputType}>
|
|
146
|
+
) => {
|
|
147
|
+
const { args, options } = resolveRunParams<${inputType}>(params);
|
|
148
|
+
const mergedOptions = mergeRouteOptions(runtime.options, options);
|
|
149
|
+
const resultOptions: AppflareRouteCallOptions<"return"> = {
|
|
150
|
+
...(mergedOptions ?? {}),
|
|
151
|
+
errorMode: "return",
|
|
152
|
+
};
|
|
153
|
+
const parsed = ${operation.schemaConst}.parse(args);
|
|
154
|
+
return requestRoute<${outputType}>(runtime.endpoint, {
|
|
155
|
+
route: ${JSON.stringify(operation.routePath)},
|
|
156
|
+
method: ${JSON.stringify(method)},
|
|
157
|
+
input: parsed,
|
|
158
|
+
options: resultOptions,
|
|
159
|
+
getAuthToken: runtime.getAuthToken,
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const subscribe = ({
|
|
163
|
+
onChange,
|
|
164
|
+
onError,
|
|
165
|
+
authToken,
|
|
166
|
+
args,
|
|
167
|
+
requestOptions,
|
|
168
|
+
signal,
|
|
169
|
+
}: AppflareQuerySubscribeOptions<${inputType}, ${outputType}>): AppflareRealtimeSubscription => {
|
|
170
|
+
const mergedOptions = mergeRouteOptions(runtime.options, requestOptions);
|
|
171
|
+
const parsedArgs = ${operation.schemaConst}.parse(normalizeRouteInput(args));
|
|
172
|
+
const requestAuthToken = resolveRealtimeAuthToken(authToken, mergedOptions?.headers);
|
|
173
|
+
|
|
174
|
+
let removed = false;
|
|
175
|
+
let socket: WebSocket | null = null;
|
|
176
|
+
let token: string | null = null;
|
|
177
|
+
let resolvedAuthToken = requestAuthToken;
|
|
178
|
+
|
|
179
|
+
const remove = () => {
|
|
180
|
+
if (removed) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
removed = true;
|
|
184
|
+
|
|
185
|
+
if (socket && (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN)) {
|
|
186
|
+
socket.close();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (token) {
|
|
190
|
+
void requestRoute<{ ok: boolean }>(runtime.endpoint, {
|
|
191
|
+
route: "/realtime/unsubscribe",
|
|
192
|
+
method: "POST",
|
|
193
|
+
input: {
|
|
194
|
+
token,
|
|
195
|
+
authToken: resolvedAuthToken,
|
|
196
|
+
},
|
|
197
|
+
options: mergedOptions,
|
|
198
|
+
getAuthToken: runtime.getAuthToken,
|
|
199
|
+
}).catch((error) => {
|
|
200
|
+
onError?.(error);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
if (signal) {
|
|
206
|
+
if (signal.aborted) {
|
|
207
|
+
remove();
|
|
208
|
+
return { remove };
|
|
209
|
+
}
|
|
210
|
+
signal.addEventListener("abort", remove, { once: true });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
void (async () => {
|
|
214
|
+
try {
|
|
215
|
+
if (!resolvedAuthToken && runtime.getAuthToken) {
|
|
216
|
+
const runtimeAuthToken = await runtime.getAuthToken();
|
|
217
|
+
if (
|
|
218
|
+
typeof runtimeAuthToken === "string" &&
|
|
219
|
+
runtimeAuthToken.trim().length > 0
|
|
220
|
+
) {
|
|
221
|
+
resolvedAuthToken = runtimeAuthToken.trim();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
const subscription = (await requestRoute<RealtimeSubscriptionResponse>(runtime.endpoint, {
|
|
227
|
+
route: "/realtime/subscribe",
|
|
228
|
+
method: "POST",
|
|
229
|
+
input: {
|
|
230
|
+
queryName: ${JSON.stringify(operation.queryName)},
|
|
231
|
+
args: parsedArgs,
|
|
232
|
+
authToken: resolvedAuthToken,
|
|
233
|
+
},
|
|
234
|
+
options: mergedOptions,
|
|
235
|
+
getAuthToken: runtime.getAuthToken,
|
|
236
|
+
})) as RealtimeSubscriptionResponse;
|
|
237
|
+
|
|
238
|
+
if (removed) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
token = subscription.token;
|
|
243
|
+
const websocketUrl = createRealtimeWebsocketUrl(
|
|
244
|
+
subscription.websocket.url,
|
|
245
|
+
runtime.wsEndpoint,
|
|
246
|
+
);
|
|
247
|
+
websocketUrl.searchParams.set(subscription.websocket.params.tokenParam, subscription.token);
|
|
248
|
+
websocketUrl.searchParams.set(subscription.websocket.params.authTokenParam, resolvedAuthToken);
|
|
249
|
+
|
|
250
|
+
socket = new WebSocket(websocketUrl.toString(), subscription.websocket.protocol);
|
|
251
|
+
socket.addEventListener("message", (event) => {
|
|
252
|
+
if (removed) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const text = typeof event.data === "string" ? event.data : String(event.data ?? "");
|
|
257
|
+
let message: unknown;
|
|
258
|
+
try {
|
|
259
|
+
message = JSON.parse(text);
|
|
260
|
+
} catch {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (
|
|
265
|
+
typeof message === "object" &&
|
|
266
|
+
message !== null &&
|
|
267
|
+
"event" in message &&
|
|
268
|
+
(message as { event?: unknown }).event === "query:update"
|
|
269
|
+
) {
|
|
270
|
+
const payload = (message as { payload?: unknown }).payload;
|
|
271
|
+
if (typeof payload === "object" && payload !== null && "data" in payload) {
|
|
272
|
+
onChange(
|
|
273
|
+
(payload as { data: ${outputType} }).data,
|
|
274
|
+
message as AppflareRealtimeQueryUpdate<${outputType}>,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
socket.addEventListener("error", () => {
|
|
280
|
+
onError?.(new Error("Realtime websocket error"));
|
|
281
|
+
});
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (onError) {
|
|
284
|
+
onError(error);
|
|
285
|
+
} else {
|
|
286
|
+
console.warn("[appflare:subscribe] Subscription failed:", error);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
})();
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
remove,
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
schema: ${schemaType},
|
|
298
|
+
run,
|
|
299
|
+
subscribe,
|
|
300
|
+
};
|
|
301
|
+
};`;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return `const ${operation.alias}Route = (
|
|
305
|
+
runtime: RequestRuntime,
|
|
306
|
+
): AppflareRouteClient<typeof ${operation.schemaConst}, ${outputType}> => {
|
|
307
|
+
const run: AppflareRouteClient<typeof ${operation.schemaConst}, ${outputType}>["run"] = async (
|
|
308
|
+
...params: AppflareRunParams<${inputType}>
|
|
309
|
+
) => {
|
|
310
|
+
const { args, options } = resolveRunParams<${inputType}>(params);
|
|
311
|
+
const mergedOptions = mergeRouteOptions(runtime.options, options);
|
|
312
|
+
const resultOptions: AppflareRouteCallOptions<"return"> = {
|
|
313
|
+
...(mergedOptions ?? {}),
|
|
314
|
+
errorMode: "return",
|
|
315
|
+
};
|
|
316
|
+
const parsed = ${operation.schemaConst}.parse(args);
|
|
317
|
+
return requestRoute<${outputType}>(runtime.endpoint, {
|
|
318
|
+
route: ${JSON.stringify(operation.routePath)},
|
|
319
|
+
method: ${JSON.stringify(method)},
|
|
320
|
+
input: parsed,
|
|
321
|
+
options: resultOptions,
|
|
322
|
+
getAuthToken: runtime.getAuthToken,
|
|
323
|
+
});
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
schema: ${schemaType},
|
|
328
|
+
run,
|
|
329
|
+
};
|
|
330
|
+
};`;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function generateClientHandlersSource(
|
|
334
|
+
operations: DiscoveredHandlerOperation[],
|
|
335
|
+
): string {
|
|
336
|
+
const normalizedOperations = operations
|
|
337
|
+
.map((operation, index) => normalizeOperation(operation, index))
|
|
338
|
+
.filter((operation): operation is HttpOperation => operation !== null);
|
|
339
|
+
|
|
340
|
+
const queryOperations = normalizedOperations.filter(
|
|
341
|
+
(operation) => operation.kind === "query",
|
|
342
|
+
);
|
|
343
|
+
const mutationOperations = normalizedOperations.filter(
|
|
344
|
+
(operation) => operation.kind === "mutation",
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
const imports = normalizedOperations
|
|
348
|
+
.map((operation) => {
|
|
349
|
+
return `import { ${operation.exportName} as ${operation.alias} } from "${operation.importPath}";`;
|
|
350
|
+
})
|
|
351
|
+
.join("\n");
|
|
352
|
+
|
|
353
|
+
const schemaDeclarations = normalizedOperations
|
|
354
|
+
.map((operation) => {
|
|
355
|
+
const inputType = `${operation.typeBase}Input`;
|
|
356
|
+
const outputType = `${operation.typeBase}Output`;
|
|
357
|
+
const schemaType = `${operation.typeBase}Schema`;
|
|
358
|
+
return `const ${operation.schemaConst} = z.object(${operation.alias}.definition.args);
|
|
359
|
+
export type ${inputType} = z.input<typeof ${operation.schemaConst}>;
|
|
360
|
+
export type ${outputType} = Awaited<ReturnType<typeof ${operation.alias}.definition.handler>>;
|
|
361
|
+
export const ${schemaType} = ${operation.schemaConst};`;
|
|
362
|
+
})
|
|
363
|
+
.join("\n\n");
|
|
364
|
+
|
|
365
|
+
const routeFactories = normalizedOperations
|
|
366
|
+
.map((operation) => renderRouteFactory(operation))
|
|
367
|
+
.join("\n\n");
|
|
368
|
+
|
|
369
|
+
const queryTree = renderTreeObject(createTree(queryOperations));
|
|
370
|
+
const mutationTree = renderTreeObject(createTree(mutationOperations));
|
|
371
|
+
|
|
372
|
+
return `import { z } from "zod";
|
|
373
|
+
import type {
|
|
374
|
+
AppflareErrorMode,
|
|
375
|
+
AppflareRequestError,
|
|
376
|
+
AppflareRequestResult,
|
|
377
|
+
AppflareQueryRouteClient,
|
|
378
|
+
AppflareQuerySubscribeOptions,
|
|
379
|
+
AppflareRealtimeQueryUpdate,
|
|
380
|
+
AppflareRealtimeSubscription,
|
|
381
|
+
RealtimeSubscriptionResponse,
|
|
382
|
+
AppflareResultRouteCallOptions,
|
|
383
|
+
AppflareRouteCallOptions,
|
|
384
|
+
AppflareRouteClient,
|
|
385
|
+
AppflareRunParams,
|
|
386
|
+
} from "./types";
|
|
387
|
+
${imports ? `\n${imports}` : ""}
|
|
388
|
+
|
|
389
|
+
${schemaDeclarations}
|
|
390
|
+
|
|
391
|
+
type RequestRuntime = {
|
|
392
|
+
endpoint: string;
|
|
393
|
+
wsEndpoint?: string;
|
|
394
|
+
getAuthToken?: () => string | Promise<string>;
|
|
395
|
+
options?: AppflareResultRouteCallOptions;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
type AnyRouteCallOptions = AppflareRouteCallOptions<AppflareErrorMode>;
|
|
399
|
+
|
|
400
|
+
type RequestRouteInit = {
|
|
401
|
+
route: string;
|
|
402
|
+
method: "GET" | "POST";
|
|
403
|
+
input: unknown;
|
|
404
|
+
options?: AnyRouteCallOptions;
|
|
405
|
+
getAuthToken?: () => string | Promise<string>;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
type RequestRouteReturnInit = Omit<RequestRouteInit, "options"> & {
|
|
409
|
+
options: AppflareRouteCallOptions<"return">;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
function mergeRouteOptions(
|
|
413
|
+
base?: AppflareResultRouteCallOptions,
|
|
414
|
+
override?: AnyRouteCallOptions,
|
|
415
|
+
): AnyRouteCallOptions | undefined {
|
|
416
|
+
if (!base && !override) {
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return {
|
|
421
|
+
...base,
|
|
422
|
+
...override,
|
|
423
|
+
headers: {
|
|
424
|
+
...(base?.headers ?? {}),
|
|
425
|
+
...(override?.headers ?? {}),
|
|
426
|
+
},
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function normalizeRouteInput<TInput extends Record<string, unknown>>(
|
|
431
|
+
input: TInput | undefined,
|
|
432
|
+
): TInput {
|
|
433
|
+
return (input ?? {}) as TInput;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function resolveRunParams<TInput extends Record<string, unknown>>(
|
|
437
|
+
params: AppflareRunParams<TInput>,
|
|
438
|
+
): {
|
|
439
|
+
args: TInput;
|
|
440
|
+
options?: AppflareResultRouteCallOptions;
|
|
441
|
+
} {
|
|
442
|
+
const [args, options] = params;
|
|
443
|
+
return {
|
|
444
|
+
args: normalizeRouteInput(args),
|
|
445
|
+
options,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function readHeaderCaseInsensitive(
|
|
450
|
+
headers: HeadersInit | undefined,
|
|
451
|
+
headerName: string,
|
|
452
|
+
): string {
|
|
453
|
+
if (!headers) {
|
|
454
|
+
return "";
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const normalizedHeaderName = headerName.toLowerCase();
|
|
458
|
+
if (Array.isArray(headers)) {
|
|
459
|
+
for (const entry of headers) {
|
|
460
|
+
if (!Array.isArray(entry) || entry.length < 2) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const key = String(entry[0]).toLowerCase();
|
|
465
|
+
if (key === normalizedHeaderName) {
|
|
466
|
+
return String(entry[1]);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return "";
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (typeof (headers as { forEach?: unknown }).forEach === "function") {
|
|
474
|
+
let value = "";
|
|
475
|
+
(headers as Headers).forEach((headerValue, key) => {
|
|
476
|
+
if (!value && key.toLowerCase() === normalizedHeaderName) {
|
|
477
|
+
value = String(headerValue);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return value;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
for (const [key, value] of Object.entries(headers as Record<string, unknown>)) {
|
|
484
|
+
if (key.toLowerCase() === normalizedHeaderName) {
|
|
485
|
+
return String(value ?? "");
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return "";
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function resolveRealtimeAuthToken(
|
|
493
|
+
provided: string | undefined,
|
|
494
|
+
headers: HeadersInit | undefined,
|
|
495
|
+
): string {
|
|
496
|
+
if (typeof provided === "string" && provided.trim().length > 0) {
|
|
497
|
+
return provided.trim();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const authorization = readHeaderCaseInsensitive(headers, "authorization");
|
|
501
|
+
const bearerMatch = authorization.match(/^Bearer\\s+(.+)$/i);
|
|
502
|
+
if (bearerMatch && bearerMatch[1]) {
|
|
503
|
+
return bearerMatch[1].trim();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return "";
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function hasAuthorizationHeader(headers: HeadersInit | undefined): boolean {
|
|
510
|
+
if (!headers) {
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (Array.isArray(headers)) {
|
|
515
|
+
return headers.some(
|
|
516
|
+
(entry) =>
|
|
517
|
+
Array.isArray(entry) &&
|
|
518
|
+
String(entry[0]).toLowerCase() === "authorization",
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (typeof (headers as { forEach?: unknown }).forEach === "function") {
|
|
523
|
+
return (headers as Headers).has("authorization");
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
for (const key of Object.keys(headers as Record<string, unknown>)) {
|
|
527
|
+
if (key.toLowerCase() === "authorization") {
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
async function resolveRequestAuthToken(
|
|
536
|
+
headers: HeadersInit | undefined,
|
|
537
|
+
getAuthToken: (() => string | Promise<string>) | undefined,
|
|
538
|
+
): Promise<string> {
|
|
539
|
+
if (!getAuthToken || hasAuthorizationHeader(headers)) {
|
|
540
|
+
return "";
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const token = await getAuthToken();
|
|
544
|
+
if (typeof token === "string" && token.trim().length > 0) {
|
|
545
|
+
return token.trim();
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return "";
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function createRealtimeWebsocketUrl(
|
|
552
|
+
serverUrl: string,
|
|
553
|
+
wsEndpoint: string | undefined,
|
|
554
|
+
): URL {
|
|
555
|
+
const websocketUrl = new URL(serverUrl);
|
|
556
|
+
if (!wsEndpoint) {
|
|
557
|
+
return websocketUrl;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const wsBase = new URL(wsEndpoint);
|
|
561
|
+
websocketUrl.protocol = wsBase.protocol;
|
|
562
|
+
websocketUrl.host = wsBase.host;
|
|
563
|
+
if (wsBase.pathname && wsBase.pathname !== "/") {
|
|
564
|
+
websocketUrl.pathname = wsBase.pathname;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return websocketUrl;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function appendQueryParamValue(
|
|
571
|
+
query: URLSearchParams,
|
|
572
|
+
key: string,
|
|
573
|
+
value: unknown,
|
|
574
|
+
): void {
|
|
575
|
+
if (value === undefined || value === null) {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (Array.isArray(value)) {
|
|
580
|
+
for (const entry of value) {
|
|
581
|
+
appendQueryParamValue(query, key, entry);
|
|
582
|
+
}
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (value instanceof Date) {
|
|
587
|
+
query.append(key, value.toISOString());
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if (typeof value === "object") {
|
|
592
|
+
query.append(key, JSON.stringify(value));
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
query.append(key, String(value));
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function createQuery(input: unknown): string {
|
|
600
|
+
if (!input || typeof input !== "object") {
|
|
601
|
+
return "";
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const query = new URLSearchParams();
|
|
605
|
+
for (const [key, value] of Object.entries(input as Record<string, unknown>)) {
|
|
606
|
+
appendQueryParamValue(query, key, value);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const encoded = query.toString();
|
|
610
|
+
return encoded.length > 0 ? "?" + encoded : "";
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function createRequestError(
|
|
614
|
+
init: RequestRouteInit,
|
|
615
|
+
response: Response,
|
|
616
|
+
body: unknown,
|
|
617
|
+
responseText: string,
|
|
618
|
+
): AppflareRequestError {
|
|
619
|
+
const fallbackMessage =
|
|
620
|
+
responseText.trim().length > 0
|
|
621
|
+
? responseText
|
|
622
|
+
: "Request failed with status " + response.status;
|
|
623
|
+
const message =
|
|
624
|
+
typeof body === "object" &&
|
|
625
|
+
body !== null &&
|
|
626
|
+
"message" in body &&
|
|
627
|
+
typeof (body as { message?: unknown }).message === "string"
|
|
628
|
+
? ((body as { message: string }).message ?? fallbackMessage)
|
|
629
|
+
: fallbackMessage;
|
|
630
|
+
|
|
631
|
+
return {
|
|
632
|
+
route: init.route,
|
|
633
|
+
method: init.method,
|
|
634
|
+
status: response.status,
|
|
635
|
+
message,
|
|
636
|
+
body,
|
|
637
|
+
responseText,
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async function requestRoute<TOutput>(
|
|
642
|
+
endpoint: string,
|
|
643
|
+
init: RequestRouteReturnInit,
|
|
644
|
+
): Promise<AppflareRequestResult<TOutput>>;
|
|
645
|
+
async function requestRoute<TOutput>(
|
|
646
|
+
endpoint: string,
|
|
647
|
+
init: RequestRouteInit,
|
|
648
|
+
): Promise<TOutput | AppflareRequestResult<TOutput>>;
|
|
649
|
+
async function requestRoute<TOutput>(
|
|
650
|
+
endpoint: string,
|
|
651
|
+
init: RequestRouteInit,
|
|
652
|
+
): Promise<TOutput | AppflareRequestResult<TOutput>> {
|
|
653
|
+
const requestAuthToken = await resolveRequestAuthToken(
|
|
654
|
+
init.options?.headers,
|
|
655
|
+
init.getAuthToken,
|
|
656
|
+
);
|
|
657
|
+
const requestUrl =
|
|
658
|
+
init.method === "GET"
|
|
659
|
+
? endpoint + init.route + createQuery(init.input)
|
|
660
|
+
: endpoint + init.route;
|
|
661
|
+
const headers: HeadersInit = {
|
|
662
|
+
...(init.options?.headers ?? {}),
|
|
663
|
+
...(requestAuthToken
|
|
664
|
+
? { authorization: "Bearer " + requestAuthToken }
|
|
665
|
+
: {}),
|
|
666
|
+
...(init.method === "POST" ? { "content-type": "application/json" } : {}),
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
const response = await fetch(requestUrl, {
|
|
670
|
+
method: init.method,
|
|
671
|
+
headers,
|
|
672
|
+
body: init.method === "POST" ? JSON.stringify(init.input ?? {}) : undefined,
|
|
673
|
+
signal: init.options?.signal,
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
const responseText = await response.text();
|
|
677
|
+
let body: unknown = undefined;
|
|
678
|
+
if (responseText.length > 0) {
|
|
679
|
+
try {
|
|
680
|
+
body = JSON.parse(responseText);
|
|
681
|
+
} catch {
|
|
682
|
+
body = responseText;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (!response.ok) {
|
|
687
|
+
const requestError = createRequestError(init, response, body, responseText);
|
|
688
|
+
init.options?.onError?.(requestError);
|
|
689
|
+
if (init.options?.errorMode === "return") {
|
|
690
|
+
return {
|
|
691
|
+
data: null,
|
|
692
|
+
error: requestError,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const error = new Error(requestError.message) as Error & {
|
|
697
|
+
cause?: AppflareRequestError;
|
|
698
|
+
};
|
|
699
|
+
error.cause = requestError;
|
|
700
|
+
throw error;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (init.options?.errorMode === "return") {
|
|
704
|
+
return {
|
|
705
|
+
data: body as TOutput,
|
|
706
|
+
error: null,
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
return body as TOutput;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
${routeFactories}
|
|
714
|
+
|
|
715
|
+
export function createQueriesClient(
|
|
716
|
+
endpoint: string,
|
|
717
|
+
options?: AppflareResultRouteCallOptions,
|
|
718
|
+
wsEndpoint?: string,
|
|
719
|
+
getAuthToken?: () => string | Promise<string>,
|
|
720
|
+
) {
|
|
721
|
+
const runtime: RequestRuntime = {
|
|
722
|
+
endpoint: endpoint.replace(/\\/$/, ""),
|
|
723
|
+
wsEndpoint: wsEndpoint?.replace(/\\/$/, ""),
|
|
724
|
+
getAuthToken,
|
|
725
|
+
options,
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
return ${queryTree} as const;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
export function createMutationsClient(
|
|
732
|
+
endpoint: string,
|
|
733
|
+
options?: AppflareResultRouteCallOptions,
|
|
734
|
+
getAuthToken?: () => string | Promise<string>,
|
|
735
|
+
) {
|
|
736
|
+
const runtime: RequestRuntime = {
|
|
737
|
+
endpoint: endpoint.replace(/\\/$/, ""),
|
|
738
|
+
getAuthToken,
|
|
739
|
+
options,
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
return ${mutationTree} as const;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
export type QueriesClient = ReturnType<typeof createQueriesClient>;
|
|
746
|
+
export type MutationsClient = ReturnType<typeof createMutationsClient>;
|
|
747
|
+
`;
|
|
748
|
+
}
|