silgi 0.41.0 → 0.41.1
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/index.mjs +1 -1
- package/dist/core/index.d.mts +13 -31
- package/dist/core/index.mjs +46 -65
- package/dist/types/index.d.mts +4 -5
- package/package.json +1 -1
package/dist/cli/index.mjs
CHANGED
package/dist/core/index.d.mts
CHANGED
|
@@ -185,42 +185,24 @@ declare function createService<Path extends string = string, Input extends Stand
|
|
|
185
185
|
};
|
|
186
186
|
|
|
187
187
|
type WildcardVariants<Path extends string, Acc extends string = ''> = Path extends `${infer Head}/${infer Tail}` ? Tail extends '' ? `${Acc}${Head}` : `${Acc}${Head}/${Tail}` | `${Acc}${Head}/*` | `${Acc}${Head}/**` | WildcardVariants<Tail, `${Acc}${Head}/`> : `${Acc}${Path}`;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
188
|
+
type MiddlewarePath<S extends WildcardVariants<keyof ServicesObject>> = S | 'global';
|
|
189
|
+
type CreateMiddlewareParams<S extends WildcardVariants<keyof ServicesObject>, UsedMethod extends readonly HttpMethod[] = readonly HttpMethod[], Key extends string = string> = {
|
|
190
|
+
path: MiddlewarePath<S>;
|
|
191
|
+
methods?: UsedMethod;
|
|
191
192
|
setup: MiddlewareSetup;
|
|
192
|
-
|
|
193
|
-
}): {
|
|
194
|
-
[K in `${UsedMethod[number]}:${S}`]: {
|
|
195
|
-
setup: MiddlewareSetup;
|
|
196
|
-
method: UsedMethod[number];
|
|
197
|
-
global: undefined;
|
|
198
|
-
};
|
|
199
|
-
};
|
|
200
|
-
declare function createMiddleware<Global extends string>(params: {
|
|
201
|
-
global: Global;
|
|
202
|
-
setup: MiddlewareSetup;
|
|
203
|
-
method?: undefined | readonly [];
|
|
204
|
-
path?: undefined;
|
|
205
|
-
}): {
|
|
206
|
-
[K in `GLOBAL:${Global}`]: {
|
|
207
|
-
setup: MiddlewareSetup;
|
|
208
|
-
global: Global;
|
|
209
|
-
method: false;
|
|
210
|
-
};
|
|
193
|
+
key: Key;
|
|
211
194
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
setup: MiddlewareSetup;
|
|
215
|
-
method: UsedMethod;
|
|
216
|
-
path?: undefined;
|
|
217
|
-
}): {
|
|
218
|
-
[K in `${UsedMethod[number]}:${Global}`]: {
|
|
195
|
+
type CreateMiddlewareResult<Path extends string, UsedMethod extends readonly HttpMethod[] = readonly HttpMethod[], Key extends string = string> = {
|
|
196
|
+
[K in `${Key}:${Path}`]: {
|
|
219
197
|
setup: MiddlewareSetup;
|
|
220
|
-
|
|
221
|
-
|
|
198
|
+
methods?: UsedMethod;
|
|
199
|
+
key: Key;
|
|
200
|
+
path: Path;
|
|
222
201
|
};
|
|
223
202
|
};
|
|
203
|
+
declare function createMiddleware<S extends WildcardVariants<keyof ServicesObject>, UsedMethod extends readonly HttpMethod[] = readonly HttpMethod[], Path extends MiddlewarePath<S> = MiddlewarePath<S>, Key extends string = string>(params: CreateMiddlewareParams<S, UsedMethod, Key> & {
|
|
204
|
+
path: Path;
|
|
205
|
+
}): CreateMiddlewareResult<Path, UsedMethod, Key>;
|
|
224
206
|
|
|
225
207
|
declare function createShared(shared: Partial<SilgiRuntimeShareds>): SilgiRuntimeShareds;
|
|
226
208
|
|
package/dist/core/index.mjs
CHANGED
|
@@ -297,17 +297,7 @@ async function createSilgi(config) {
|
|
|
297
297
|
if (!silgi.router) {
|
|
298
298
|
silgi.router = createRouter();
|
|
299
299
|
}
|
|
300
|
-
for (const
|
|
301
|
-
let method = "";
|
|
302
|
-
let route = routeKey;
|
|
303
|
-
if (routeKey.includes(":")) {
|
|
304
|
-
const [methodPart, ...routeParts2] = routeKey.split(":");
|
|
305
|
-
method = methodPart.toUpperCase();
|
|
306
|
-
if (method === "GLOBAL") {
|
|
307
|
-
method = "";
|
|
308
|
-
}
|
|
309
|
-
route = routeParts2.join(":");
|
|
310
|
-
}
|
|
300
|
+
for (const [route, object] of Object.entries(silgi.services)) {
|
|
311
301
|
const routeParts = route.split("/").filter(Boolean);
|
|
312
302
|
if (routeParts.length > 0) {
|
|
313
303
|
const prefix = `/${routeParts[0]}`;
|
|
@@ -315,41 +305,41 @@ async function createSilgi(config) {
|
|
|
315
305
|
silgi.routerPrefixs.push(prefix);
|
|
316
306
|
}
|
|
317
307
|
}
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
308
|
+
const methods = object.methods?.length ? object.methods : ["ALL"];
|
|
309
|
+
for (const method of methods) {
|
|
310
|
+
const globalMethod = method === "ALL" ? "" : method.toUpperCase();
|
|
311
|
+
addRoute(silgi.router, globalMethod, route, {
|
|
312
|
+
method: [method],
|
|
322
313
|
route,
|
|
323
|
-
service:
|
|
314
|
+
service: object
|
|
324
315
|
});
|
|
325
316
|
}
|
|
326
317
|
}
|
|
327
318
|
if (!silgi._middlewareRouter) {
|
|
328
319
|
silgi._middlewareRouter = createRouter();
|
|
329
320
|
}
|
|
330
|
-
for (const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
method = methodPart.toUpperCase();
|
|
337
|
-
if (method === "GLOBAL") {
|
|
338
|
-
method = "";
|
|
339
|
-
}
|
|
340
|
-
route = routeParts.join(":");
|
|
321
|
+
for (const [route, routeObject] of Object.entries(silgi.middlewares)) {
|
|
322
|
+
let _route = "";
|
|
323
|
+
if (route.includes(":")) {
|
|
324
|
+
const [methodPart, ...routeParts] = route.split(":");
|
|
325
|
+
methodPart.toUpperCase();
|
|
326
|
+
_route = routeParts.join(":");
|
|
341
327
|
}
|
|
342
|
-
if (
|
|
328
|
+
if (_route === "global") {
|
|
343
329
|
silgi.globalMiddlewares.push({
|
|
344
330
|
middleware: routeObject.setup,
|
|
345
|
-
method,
|
|
346
|
-
route
|
|
331
|
+
method: routeObject.methods?.length ? routeObject.methods : ["ALL"],
|
|
332
|
+
route: _route
|
|
347
333
|
});
|
|
348
|
-
|
|
349
|
-
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const methods = routeObject.methods?.length ? routeObject.methods : ["ALL"];
|
|
337
|
+
for (const method of methods) {
|
|
338
|
+
const globalMethod = method === "ALL" ? "" : method.toUpperCase();
|
|
339
|
+
addRoute(silgi._middlewareRouter, globalMethod, _route, {
|
|
350
340
|
middleware: routeObject.setup,
|
|
351
|
-
method,
|
|
352
|
-
route
|
|
341
|
+
method: [method],
|
|
342
|
+
route: _route
|
|
353
343
|
});
|
|
354
344
|
}
|
|
355
345
|
}
|
|
@@ -922,7 +912,15 @@ async function middleware(event, url) {
|
|
|
922
912
|
if (_previous !== void 0 && _previous !== kNotFound) {
|
|
923
913
|
return _previous;
|
|
924
914
|
}
|
|
925
|
-
|
|
915
|
+
let allowedMethods = m.method ?? [];
|
|
916
|
+
if (!Array.isArray(allowedMethods)) {
|
|
917
|
+
allowedMethods = [allowedMethods];
|
|
918
|
+
}
|
|
919
|
+
const methodIsAllowed = allowedMethods.length === 0 || allowedMethods.includes("ALL") || allowedMethods.includes(event.req.method);
|
|
920
|
+
if (!methodIsAllowed) {
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
if (!m.middleware) {
|
|
926
924
|
return;
|
|
927
925
|
}
|
|
928
926
|
try {
|
|
@@ -952,7 +950,12 @@ async function middleware(event, url) {
|
|
|
952
950
|
if (_previous !== void 0 && _previous !== kNotFound) {
|
|
953
951
|
return _previous;
|
|
954
952
|
}
|
|
955
|
-
|
|
953
|
+
let allowedMethods = match.data.method ?? [];
|
|
954
|
+
if (!Array.isArray(allowedMethods)) {
|
|
955
|
+
allowedMethods = [allowedMethods];
|
|
956
|
+
}
|
|
957
|
+
const methodIsAllowed = allowedMethods.length === 0 || allowedMethods.includes("ALL") || allowedMethods.includes(event.req.method);
|
|
958
|
+
if (!methodIsAllowed || !match.data.middleware) {
|
|
956
959
|
return;
|
|
957
960
|
}
|
|
958
961
|
event.context.params = match.params;
|
|
@@ -1072,36 +1075,14 @@ function createService(params) {
|
|
|
1072
1075
|
}
|
|
1073
1076
|
|
|
1074
1077
|
function createMiddleware(params) {
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
if (!params.method || params.method.length === 0) {
|
|
1078
|
-
return {
|
|
1079
|
-
[`GLOBAL:${globalKey}`]: {
|
|
1080
|
-
setup: params.setup,
|
|
1081
|
-
global: globalKey,
|
|
1082
|
-
method: false
|
|
1083
|
-
}
|
|
1084
|
-
};
|
|
1085
|
-
}
|
|
1086
|
-
const result2 = {};
|
|
1087
|
-
for (const m of params.method) {
|
|
1088
|
-
result2[`${m}:${globalKey}`] = {
|
|
1089
|
-
setup: params.setup,
|
|
1090
|
-
global: globalKey,
|
|
1091
|
-
method: m
|
|
1092
|
-
};
|
|
1093
|
-
}
|
|
1094
|
-
return result2;
|
|
1095
|
-
}
|
|
1096
|
-
const result = {};
|
|
1097
|
-
for (const m of params.method) {
|
|
1098
|
-
result[`${m}:${params.path}`] = {
|
|
1078
|
+
return {
|
|
1079
|
+
[`${params.key}:${params.path}`]: {
|
|
1099
1080
|
setup: params.setup,
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1081
|
+
methods: params.methods?.length ? params.methods : ["ALL"],
|
|
1082
|
+
key: params.key,
|
|
1083
|
+
path: params.path
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1105
1086
|
}
|
|
1106
1087
|
|
|
1107
1088
|
function createShared(shared) {
|
package/dist/types/index.d.mts
CHANGED
|
@@ -284,7 +284,7 @@ interface ServiceSetup<Input extends StandardSchemaV1 = StandardSchemaV1, Output
|
|
|
284
284
|
interface ResolvedServiceDefinition {
|
|
285
285
|
[routePath: string]: {
|
|
286
286
|
setup: ServiceSetup<any, any, any, any, any, any>;
|
|
287
|
-
methods:
|
|
287
|
+
methods: HTTPMethod[];
|
|
288
288
|
input?: StandardSchemaV1;
|
|
289
289
|
output?: StandardSchemaV1;
|
|
290
290
|
queryParams?: {
|
|
@@ -515,8 +515,7 @@ interface MiddlewareSetup {
|
|
|
515
515
|
interface ResolvedMiddlewareDefinition {
|
|
516
516
|
[methodAndPath: string]: {
|
|
517
517
|
setup: MiddlewareSetup;
|
|
518
|
-
|
|
519
|
-
method?: HTTPMethod | false;
|
|
518
|
+
methods?: HTTPMethod[];
|
|
520
519
|
};
|
|
521
520
|
}
|
|
522
521
|
|
|
@@ -691,13 +690,13 @@ interface ResolvedSchemaDefinition {
|
|
|
691
690
|
[methodAndRoutePath: string]: BaseMethodSchema;
|
|
692
691
|
}
|
|
693
692
|
|
|
694
|
-
type StandardHTTPMethod = 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE';
|
|
693
|
+
type StandardHTTPMethod = 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'ALL';
|
|
695
694
|
type HTTPMethod = SilgiRuntimeMethods extends Record<string, any> ? keyof SilgiRuntimeMethods | StandardHTTPMethod : StandardHTTPMethod;
|
|
696
695
|
interface MetaData extends Record<string, unknown> {
|
|
697
696
|
}
|
|
698
697
|
interface SilgiRoute {
|
|
699
698
|
route?: string;
|
|
700
|
-
method?: HTTPMethod;
|
|
699
|
+
method?: HTTPMethod | HTTPMethod[];
|
|
701
700
|
service?: ResolvedServiceDefinition[string];
|
|
702
701
|
middleware?: MiddlewareSetup;
|
|
703
702
|
}
|