silgi 0.41.0 → 0.41.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.
@@ -1212,8 +1212,16 @@ async function prepareScanFile(silgi) {
1212
1212
  }),
1213
1213
  ""
1214
1214
  ];
1215
- const generateExport = (name, items, defaultEmpty = "{}") => {
1215
+ const generateExport = (name, items, defaultEmpty = "{}", asArray = false) => {
1216
1216
  const hasItems = items.length > 0;
1217
+ if (asArray) {
1218
+ return [
1219
+ `export const ${name} = [`,
1220
+ ...items.map((item) => ` ${item},`),
1221
+ "]",
1222
+ ""
1223
+ ];
1224
+ }
1217
1225
  if (hasItems) {
1218
1226
  return [
1219
1227
  `export const ${name} = {`,
@@ -1233,7 +1241,8 @@ async function prepareScanFile(silgi) {
1233
1241
  ...generateExport("schemas", scanned.schemas),
1234
1242
  ...generateExport("services", scanned.services),
1235
1243
  ...generateExport("shareds", scanned.shareds, "undefined"),
1236
- ...generateExport("resolvers", scanned.resolvers, "[]"),
1244
+ ...generateExport("resolvers", scanned.resolvers, "[]", true),
1245
+ // array olarak export
1237
1246
  ...generateExport("middlewares", scanned.middlewares)
1238
1247
  ];
1239
1248
  await silgi.callHook("after:scan.ts", importData);
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { defineCommand, runMain } from 'citty';
3
3
 
4
- const version = "0.41.0";
4
+ const version = "0.41.2";
5
5
  const packageJson = {
6
6
  version: version};
7
7
 
@@ -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
- declare function createMiddleware<S extends WildcardVariants<keyof ServicesObject>, Method extends HttpMethod, UsedMethod extends readonly Method[]>(params: {
189
- path: S;
190
- method: UsedMethod;
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
- global?: undefined;
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
- declare function createMiddleware<Global extends string, Method extends HttpMethod, UsedMethod extends readonly Method[]>(params: {
213
- global: Global;
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
- global: Global;
221
- method: UsedMethod[number];
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
 
@@ -297,17 +297,7 @@ async function createSilgi(config) {
297
297
  if (!silgi.router) {
298
298
  silgi.router = createRouter();
299
299
  }
300
- for (const routeKey in silgi.services) {
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 methodObject = silgi.services[routeKey];
319
- if (methodObject) {
320
- addRoute(silgi.router, method, route, {
321
- method,
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: methodObject
314
+ service: object
324
315
  });
325
316
  }
326
317
  }
327
318
  if (!silgi._middlewareRouter) {
328
319
  silgi._middlewareRouter = createRouter();
329
320
  }
330
- for (const routeKey in silgi.middlewares) {
331
- const routeObject = silgi.middlewares[routeKey];
332
- let method = "";
333
- let route = routeKey;
334
- if (routeKey.includes(":")) {
335
- const [methodPart, ...routeParts] = routeKey.split(":");
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 (routeObject.global) {
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
- } else {
349
- addRoute(silgi._middlewareRouter, method, route, {
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
- if (m.method && m.method !== "GLOBAL" && m.method !== event.req.method || !m.middleware) {
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
- if (match.data.method && match.data.method !== event.req.method || !match.data.middleware) {
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
- if (params.global) {
1076
- const globalKey = params.global;
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
- method: m,
1101
- global: void 0
1102
- };
1103
- }
1104
- return result;
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) {
@@ -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: string[];
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
- global?: string | undefined;
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
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "silgi",
3
3
  "type": "module",
4
- "version": "0.41.0",
4
+ "version": "0.41.2",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {