silgi 0.41.28 → 0.41.30

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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { defineCommand, runMain } from 'citty';
3
3
 
4
- const version = "0.41.28";
4
+ const version = "0.41.30";
5
5
  const packageJson = {
6
6
  version: version};
7
7
 
@@ -1,4 +1,5 @@
1
1
  import { SilgiConfig, Silgi, SilgiEvent, HTTPMethod, SilgiSchema, RouteEntry, CustomRequestInit, SilgiRuntimeContext, BaseMethodSchema, MergeAll, ServiceSetupsForMethods, ServiceDefinitionByMethodAndPath, Routers, ServiceSetup, MiddlewareSetup, SilgiRuntimeShareds, Resolvers, SilgiURL, StorageConfig, SilgiCLI, SilgiStorageBase, SilgiRuntimeConfig } from 'silgi/types';
2
+ import { GraphQLResolveInfo } from 'graphql';
2
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
4
  import { Storage, StorageValue } from 'unstorage';
4
5
  import { UseContext } from 'unctx';
@@ -26,7 +27,7 @@ declare function middleware(event: SilgiEvent, url?: {
26
27
  declare function handler(event: SilgiEvent, url?: {
27
28
  path?: string;
28
29
  method?: HTTPMethod | '';
29
- }, input?: any): Promise<any>;
30
+ }, input?: any, parent?: any, info?: GraphQLResolveInfo): Promise<any>;
30
31
 
31
32
  /**
32
33
  * LICENSE: MIT
@@ -312,9 +312,20 @@ async function createSilgi(config) {
312
312
  }
313
313
  }
314
314
  let routeWithParams = path;
315
+ let pathParamNames = {};
316
+ let queryParamNames = {};
315
317
  if (object.pathParams) {
316
318
  const jsonSchema = await toJsonSchema(object.pathParams);
317
319
  if (jsonSchema && jsonSchema.properties) {
320
+ pathParamNames = Object.keys(jsonSchema.properties).reduce((acc, key) => {
321
+ const prop = jsonSchema.properties?.[key];
322
+ if (typeof prop === "object" && prop !== null && "type" in prop) {
323
+ acc[key] = prop.type ?? "string";
324
+ } else {
325
+ acc[key] = "string";
326
+ }
327
+ return acc;
328
+ }, {});
318
329
  const paramNames = Object.keys(jsonSchema.properties);
319
330
  if (paramNames.length > 0) {
320
331
  const routeParts2 = routeWithParams.split("/");
@@ -329,22 +340,42 @@ async function createSilgi(config) {
329
340
  }
330
341
  }
331
342
  }
343
+ if (object.queryParams) {
344
+ const jsonSchema = await toJsonSchema(object.queryParams);
345
+ if (jsonSchema && jsonSchema.properties) {
346
+ queryParamNames = Object.keys(jsonSchema.properties).reduce((acc, key) => {
347
+ const prop = jsonSchema.properties?.[key];
348
+ if (typeof prop === "object" && prop !== null && "type" in prop) {
349
+ acc[key] = prop.type ?? "string";
350
+ } else {
351
+ acc[key] = "string";
352
+ }
353
+ return acc;
354
+ }, {});
355
+ }
356
+ }
332
357
  if (object.apiType === "rest-graphql") {
333
358
  addRoute(silgi.router, "", routeWithParams, {
334
359
  method: "",
335
360
  route: routeWithParams,
336
- service: object
361
+ service: object,
362
+ queryParams: queryParamNames,
363
+ pathParams: pathParamNames
337
364
  });
338
365
  addRoute(silgi.router, method, routeWithParams, {
339
366
  method,
340
367
  route: routeWithParams,
341
- service: object
368
+ service: object,
369
+ queryParams: queryParamNames,
370
+ pathParams: pathParamNames
342
371
  });
343
372
  } else {
344
373
  addRoute(silgi.router, method, routeWithParams, {
345
374
  method,
346
375
  route: routeWithParams,
347
- service: object
376
+ service: object,
377
+ queryParams: queryParamNames,
378
+ pathParams: pathParamNames
348
379
  });
349
380
  }
350
381
  }
@@ -612,7 +643,7 @@ function getUrlPrefix(path, method) {
612
643
  };
613
644
  }
614
645
 
615
- async function orchestrate(route, event, _input) {
646
+ async function orchestrate(route, event, _input, parent, info) {
616
647
  const silgiCtx = useSilgi();
617
648
  const isGraphQL = event.context.protocol === "GRAPHQL";
618
649
  const silgiURL = !isGraphQL ? getUrlPrefix(route.route || event.req.url, route.method) : {
@@ -1024,7 +1055,7 @@ async function middleware(event, url) {
1024
1055
  }
1025
1056
  return _chain;
1026
1057
  }
1027
- async function handler(event, url, input) {
1058
+ async function handler(event, url, input, parent, info) {
1028
1059
  url ??= {};
1029
1060
  url.method = event.context.protocol === "GRAPHQL" ? "" : url.method || event.req.method;
1030
1061
  const silgiCtx = useSilgi();
@@ -1036,6 +1067,36 @@ async function handler(event, url, input) {
1036
1067
  if (silgiCtx.router) {
1037
1068
  const match = findRoute(silgiCtx.router, url.method, pathname);
1038
1069
  if (match) {
1070
+ if (url.method === "" && input) {
1071
+ if (input?.path) {
1072
+ const expectedPathKeys = Object.keys(match.data.pathParams ?? {});
1073
+ const inputPathKeys = Object.keys(input.path);
1074
+ for (const key of inputPathKeys) {
1075
+ if (!expectedPathKeys.includes(key)) {
1076
+ throw createError({
1077
+ message: `Unexpected path param key: ${key}`,
1078
+ statusCode: 400,
1079
+ statusMessage: "Invalid path parameter"
1080
+ });
1081
+ }
1082
+ }
1083
+ match.data.pathParams = input.path;
1084
+ }
1085
+ if (input?.query) {
1086
+ const expectedQueryKeys = Object.keys(match.data.queryParams ?? {});
1087
+ const inputQueryKeys = Object.keys(input.query);
1088
+ for (const key of inputQueryKeys) {
1089
+ if (!expectedQueryKeys.includes(key)) {
1090
+ throw createError({
1091
+ message: `Unexpected query param key: ${key}`,
1092
+ statusCode: 400,
1093
+ statusMessage: "Invalid query parameter"
1094
+ });
1095
+ }
1096
+ }
1097
+ match.data.queryParams = input.query;
1098
+ }
1099
+ }
1039
1100
  if (_chain) {
1040
1101
  return _chain.then(async (_previous) => {
1041
1102
  if (_previous !== void 0 && _previous !== kNotFound) {
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { SilgiError, autoImportTypes, createError, createMiddleware, createResolver, createSchema, createService, createShared, createSilgi, createStorage, deepMergeObjects, defineServiceSetup, getEvent, getEventContext, getUrlPrefix, handleResponse, handler, isError, kHandled, kNotFound, middleware, replaceRuntimeValues, silgiCLICtx, silgiCtx, silgiFetch, storageMount, tryUseSilgi, tryUseSilgiCLI, updateRuntimeStorage, useRuntime, useSilgi, useSilgiCLI, useSilgiStorage } from './core/index.mjs';
2
2
  import 'silgi/types';
3
+ import 'graphql';
3
4
  import '@standard-schema/spec';
4
5
  import 'unstorage';
5
6
  import 'unctx';
@@ -709,6 +709,8 @@ interface SilgiRoute {
709
709
  method?: HTTPMethod;
710
710
  service?: ResolvedServiceDefinition[keyof ResolvedServiceDefinition];
711
711
  middleware?: MiddlewareSetup;
712
+ queryParams?: Record<string, string>;
713
+ pathParams?: Record<string, string>;
712
714
  }
713
715
  interface Silgi {
714
716
  router: RouterContext<SilgiRoute>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "silgi",
3
3
  "type": "module",
4
- "version": "0.41.28",
4
+ "version": "0.41.30",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {