@supacloud/compiler 0.6.1 → 0.6.2
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/dist/cli.js +38 -10
- package/dist/index.js +38 -10
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -3056,13 +3056,9 @@ function renderClient(graph, _options) {
|
|
|
3056
3056
|
title: route.title,
|
|
3057
3057
|
data: route.data
|
|
3058
3058
|
});
|
|
3059
|
+
const routeParams = route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? `{ ${(route.pathParams && route.pathParams.length > 0 ? route.pathParams : (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).map((p) => p.slice(1))).map((p) => `${p}: string | number`).join("; ")} }` : "Record<string, string | number>";
|
|
3059
3060
|
routeMethods.push(`
|
|
3060
|
-
${route.handler}: (
|
|
3061
|
-
params${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? "" : "?"}: ${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? `{ ${(route.pathParams && route.pathParams.length > 0 ? route.pathParams : (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).map((p) => p.slice(1))).map((p) => `${p}: string | number`).join("; ")} }` : "Record<string, string | number>"};
|
|
3062
|
-
query?: Record<string, unknown>;
|
|
3063
|
-
body?: unknown;
|
|
3064
|
-
headers?: Record<string, string>;
|
|
3065
|
-
}${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? "" : " = {}"}) => request(${JSON.stringify(route.method)}, ${JSON.stringify(fullPath)}, options),`);
|
|
3061
|
+
${route.handler}: makeRoute<{ params${routeParams.startsWith("{") ? "" : "?"}: ${routeParams}; query?: Record<string, unknown>; body?: unknown; headers?: Record<string, string> }>(${JSON.stringify(route.method)}, ${JSON.stringify(fullPath)}),`);
|
|
3066
3062
|
}
|
|
3067
3063
|
controllerEntries.push(`
|
|
3068
3064
|
${controllerKey}: {${routeMethods.join("")}
|
|
@@ -3079,6 +3075,13 @@ function renderClient(graph, _options) {
|
|
|
3079
3075
|
" headers?: Record<string, string>;",
|
|
3080
3076
|
"}",
|
|
3081
3077
|
"",
|
|
3078
|
+
"export type ResponseDecoder<T> = (value: unknown) => T;",
|
|
3079
|
+
"",
|
|
3080
|
+
"export type RouteMethod<Options extends ClientRequestOptions = ClientRequestOptions> = {",
|
|
3081
|
+
" <T>(options: Options, decode: ResponseDecoder<T>): Promise<T>;",
|
|
3082
|
+
" (options?: Options): Promise<unknown>;",
|
|
3083
|
+
"};",
|
|
3084
|
+
"",
|
|
3082
3085
|
"export type HttpInterceptorFn = (",
|
|
3083
3086
|
" req: { method: string; url: string; headers: Record<string, string>; body?: unknown },",
|
|
3084
3087
|
" next: (req: { method: string; url: string; headers: Record<string, string>; body?: unknown }) => Promise<Response>,",
|
|
@@ -3124,11 +3127,23 @@ function renderClient(graph, _options) {
|
|
|
3124
3127
|
" const fetcher = config.fetch ?? globalThis.fetch.bind(globalThis);",
|
|
3125
3128
|
' const baseUrl = (config.baseUrl ?? "").replace(/\\/+$/, "");',
|
|
3126
3129
|
"",
|
|
3127
|
-
" async function request<T
|
|
3130
|
+
" async function request<T>(",
|
|
3131
|
+
" method: string,",
|
|
3132
|
+
" path: string,",
|
|
3133
|
+
" options: ClientRequestOptions,",
|
|
3134
|
+
" decode: ResponseDecoder<T>,",
|
|
3135
|
+
" ): Promise<T>;",
|
|
3136
|
+
" async function request(",
|
|
3137
|
+
" method: string,",
|
|
3138
|
+
" path: string,",
|
|
3139
|
+
" options?: ClientRequestOptions,",
|
|
3140
|
+
" ): Promise<unknown>;",
|
|
3141
|
+
" async function request<T>(",
|
|
3128
3142
|
" method: string,",
|
|
3129
3143
|
" path: string,",
|
|
3130
3144
|
" options: ClientRequestOptions = {},",
|
|
3131
|
-
"
|
|
3145
|
+
" decode?: ResponseDecoder<T>,",
|
|
3146
|
+
" ): Promise<T | unknown> {",
|
|
3132
3147
|
" let url = `${baseUrl}${path}`;",
|
|
3133
3148
|
" if (options.params) {",
|
|
3134
3149
|
" for (const [key, value] of Object.entries(options.params)) {",
|
|
@@ -3169,10 +3184,23 @@ function renderClient(graph, _options) {
|
|
|
3169
3184
|
" throw new Error(`API request failed: ${method} ${path} -> ${response.status} ${errBody}`);",
|
|
3170
3185
|
" }",
|
|
3171
3186
|
' const contentType = response.headers?.get("content-type") ?? "";',
|
|
3187
|
+
" let value: unknown;",
|
|
3172
3188
|
' if (contentType.includes("application/json")) {',
|
|
3173
|
-
"
|
|
3189
|
+
" value = await response.json();",
|
|
3190
|
+
" } else {",
|
|
3191
|
+
" value = await response.text();",
|
|
3192
|
+
" }",
|
|
3193
|
+
" return decode ? decode(value) : value;",
|
|
3194
|
+
" }",
|
|
3195
|
+
"",
|
|
3196
|
+
" function makeRoute<Options extends ClientRequestOptions>(method: string, path: string): RouteMethod<Options> {",
|
|
3197
|
+
" function route<T>(options: Options, decode: ResponseDecoder<T>): Promise<T>;",
|
|
3198
|
+
" function route(options?: Options): Promise<unknown>;",
|
|
3199
|
+
" function route<T>(options?: Options, decode?: ResponseDecoder<T>): Promise<T | unknown> {",
|
|
3200
|
+
" const requestOptions = options ?? {};",
|
|
3201
|
+
" return decode ? request(method, path, requestOptions, decode) : request(method, path, requestOptions);",
|
|
3174
3202
|
" }",
|
|
3175
|
-
" return
|
|
3203
|
+
" return route;",
|
|
3176
3204
|
" }",
|
|
3177
3205
|
"",
|
|
3178
3206
|
" return {",
|
package/dist/index.js
CHANGED
|
@@ -3050,13 +3050,9 @@ function renderClient(graph, _options) {
|
|
|
3050
3050
|
title: route.title,
|
|
3051
3051
|
data: route.data
|
|
3052
3052
|
});
|
|
3053
|
+
const routeParams = route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? `{ ${(route.pathParams && route.pathParams.length > 0 ? route.pathParams : (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).map((p) => p.slice(1))).map((p) => `${p}: string | number`).join("; ")} }` : "Record<string, string | number>";
|
|
3053
3054
|
routeMethods.push(`
|
|
3054
|
-
${route.handler}: (
|
|
3055
|
-
params${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? "" : "?"}: ${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? `{ ${(route.pathParams && route.pathParams.length > 0 ? route.pathParams : (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).map((p) => p.slice(1))).map((p) => `${p}: string | number`).join("; ")} }` : "Record<string, string | number>"};
|
|
3056
|
-
query?: Record<string, unknown>;
|
|
3057
|
-
body?: unknown;
|
|
3058
|
-
headers?: Record<string, string>;
|
|
3059
|
-
}${route.pathParams && route.pathParams.length > 0 || (fullPath.match(/:([a-zA-Z0-9_]+)/g) ?? []).length > 0 ? "" : " = {}"}) => request(${JSON.stringify(route.method)}, ${JSON.stringify(fullPath)}, options),`);
|
|
3055
|
+
${route.handler}: makeRoute<{ params${routeParams.startsWith("{") ? "" : "?"}: ${routeParams}; query?: Record<string, unknown>; body?: unknown; headers?: Record<string, string> }>(${JSON.stringify(route.method)}, ${JSON.stringify(fullPath)}),`);
|
|
3060
3056
|
}
|
|
3061
3057
|
controllerEntries.push(`
|
|
3062
3058
|
${controllerKey}: {${routeMethods.join("")}
|
|
@@ -3073,6 +3069,13 @@ function renderClient(graph, _options) {
|
|
|
3073
3069
|
" headers?: Record<string, string>;",
|
|
3074
3070
|
"}",
|
|
3075
3071
|
"",
|
|
3072
|
+
"export type ResponseDecoder<T> = (value: unknown) => T;",
|
|
3073
|
+
"",
|
|
3074
|
+
"export type RouteMethod<Options extends ClientRequestOptions = ClientRequestOptions> = {",
|
|
3075
|
+
" <T>(options: Options, decode: ResponseDecoder<T>): Promise<T>;",
|
|
3076
|
+
" (options?: Options): Promise<unknown>;",
|
|
3077
|
+
"};",
|
|
3078
|
+
"",
|
|
3076
3079
|
"export type HttpInterceptorFn = (",
|
|
3077
3080
|
" req: { method: string; url: string; headers: Record<string, string>; body?: unknown },",
|
|
3078
3081
|
" next: (req: { method: string; url: string; headers: Record<string, string>; body?: unknown }) => Promise<Response>,",
|
|
@@ -3118,11 +3121,23 @@ function renderClient(graph, _options) {
|
|
|
3118
3121
|
" const fetcher = config.fetch ?? globalThis.fetch.bind(globalThis);",
|
|
3119
3122
|
' const baseUrl = (config.baseUrl ?? "").replace(/\\/+$/, "");',
|
|
3120
3123
|
"",
|
|
3121
|
-
" async function request<T
|
|
3124
|
+
" async function request<T>(",
|
|
3125
|
+
" method: string,",
|
|
3126
|
+
" path: string,",
|
|
3127
|
+
" options: ClientRequestOptions,",
|
|
3128
|
+
" decode: ResponseDecoder<T>,",
|
|
3129
|
+
" ): Promise<T>;",
|
|
3130
|
+
" async function request(",
|
|
3131
|
+
" method: string,",
|
|
3132
|
+
" path: string,",
|
|
3133
|
+
" options?: ClientRequestOptions,",
|
|
3134
|
+
" ): Promise<unknown>;",
|
|
3135
|
+
" async function request<T>(",
|
|
3122
3136
|
" method: string,",
|
|
3123
3137
|
" path: string,",
|
|
3124
3138
|
" options: ClientRequestOptions = {},",
|
|
3125
|
-
"
|
|
3139
|
+
" decode?: ResponseDecoder<T>,",
|
|
3140
|
+
" ): Promise<T | unknown> {",
|
|
3126
3141
|
" let url = `${baseUrl}${path}`;",
|
|
3127
3142
|
" if (options.params) {",
|
|
3128
3143
|
" for (const [key, value] of Object.entries(options.params)) {",
|
|
@@ -3163,10 +3178,23 @@ function renderClient(graph, _options) {
|
|
|
3163
3178
|
" throw new Error(`API request failed: ${method} ${path} -> ${response.status} ${errBody}`);",
|
|
3164
3179
|
" }",
|
|
3165
3180
|
' const contentType = response.headers?.get("content-type") ?? "";',
|
|
3181
|
+
" let value: unknown;",
|
|
3166
3182
|
' if (contentType.includes("application/json")) {',
|
|
3167
|
-
"
|
|
3183
|
+
" value = await response.json();",
|
|
3184
|
+
" } else {",
|
|
3185
|
+
" value = await response.text();",
|
|
3186
|
+
" }",
|
|
3187
|
+
" return decode ? decode(value) : value;",
|
|
3188
|
+
" }",
|
|
3189
|
+
"",
|
|
3190
|
+
" function makeRoute<Options extends ClientRequestOptions>(method: string, path: string): RouteMethod<Options> {",
|
|
3191
|
+
" function route<T>(options: Options, decode: ResponseDecoder<T>): Promise<T>;",
|
|
3192
|
+
" function route(options?: Options): Promise<unknown>;",
|
|
3193
|
+
" function route<T>(options?: Options, decode?: ResponseDecoder<T>): Promise<T | unknown> {",
|
|
3194
|
+
" const requestOptions = options ?? {};",
|
|
3195
|
+
" return decode ? request(method, path, requestOptions, decode) : request(method, path, requestOptions);",
|
|
3168
3196
|
" }",
|
|
3169
|
-
" return
|
|
3197
|
+
" return route;",
|
|
3170
3198
|
" }",
|
|
3171
3199
|
"",
|
|
3172
3200
|
" return {",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/compiler",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Static compiler for @supacloud/app metadata: builds the application graph from AST, validates it, and generates reflection-free factory code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|