sofa-api 0.13.0 → 0.15.0

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/index.js CHANGED
@@ -367,7 +367,7 @@ function createRouter(sofa) {
367
367
  return router;
368
368
  }
369
369
  function createQueryRoute({ sofa, router, fieldName, }) {
370
- var _a, _b, _c, _d, _e, _f;
370
+ var _a, _b, _c, _d, _e, _f, _g;
371
371
  logger.debug(`[Router] Creating ${fieldName} query`);
372
372
  const queryType = sofa.schema.getQueryType();
373
373
  const operationNode = utils.buildOperationNodeForField({
@@ -402,13 +402,14 @@ function createQueryRoute({ sofa, router, fieldName, }) {
402
402
  path: route.path,
403
403
  method: route.method.toUpperCase(),
404
404
  tags: (_e = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.tags) !== null && _e !== void 0 ? _e : [],
405
- description: (_f = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _f !== void 0 ? _f : '',
405
+ description: (_g = (_f = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _f !== void 0 ? _f : field.description) !== null && _g !== void 0 ? _g : '',
406
406
  };
407
407
  }
408
408
  function createMutationRoute({ sofa, router, fieldName, }) {
409
- var _a, _b, _c, _d;
409
+ var _a, _b, _c, _d, _e, _f;
410
410
  logger.debug(`[Router] Creating ${fieldName} mutation`);
411
411
  const mutationType = sofa.schema.getMutationType();
412
+ const field = mutationType.getFields()[fieldName];
412
413
  const operationNode = utils.buildOperationNodeForField({
413
414
  kind: 'mutation',
414
415
  schema: sofa.schema,
@@ -437,7 +438,7 @@ function createMutationRoute({ sofa, router, fieldName, }) {
437
438
  path,
438
439
  method,
439
440
  tags: (routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.tags) || [],
440
- description: (routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) || '',
441
+ description: (_f = (_e = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _e !== void 0 ? _e : field.description) !== null && _f !== void 0 ? _f : '',
441
442
  };
442
443
  }
443
444
  function useHandler(config) {
@@ -480,9 +481,34 @@ function useHandler(config) {
480
481
  });
481
482
  if (result.errors) {
482
483
  const defaultErrorHandler = (errors) => {
483
- return new fetch.Response(JSON.stringify(errors[0]), {
484
- status: 500,
485
- headers: { 'Content-Type': 'application/json' },
484
+ var _a;
485
+ let status;
486
+ const headers = {
487
+ 'Content-Type': 'application/json; charset=utf-8',
488
+ };
489
+ for (const error of errors) {
490
+ if (typeof error === 'object' && error != null && ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.http)) {
491
+ if (error.extensions.http.status &&
492
+ (!status || error.extensions.http.status > status)) {
493
+ status = error.extensions.http.status;
494
+ }
495
+ if (error.extensions.http.headers) {
496
+ Object.assign(headers, error.extensions.http.headers);
497
+ }
498
+ }
499
+ }
500
+ if (!status) {
501
+ status = 500;
502
+ }
503
+ if (errors.length === 1) {
504
+ return new fetch.Response(JSON.stringify(errors[0]), {
505
+ status,
506
+ headers,
507
+ });
508
+ }
509
+ return new fetch.Response(JSON.stringify({ errors }), {
510
+ status,
511
+ headers,
486
512
  });
487
513
  };
488
514
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
@@ -644,6 +670,7 @@ function resolveField(field, opts) {
644
670
  // type -> $ref
645
671
  // scalar -> swagger primitive
646
672
  function resolveFieldType(type, opts) {
673
+ var _a;
647
674
  if (graphql.isNonNullType(type)) {
648
675
  return resolveFieldType(type.ofType, opts);
649
676
  }
@@ -660,9 +687,11 @@ function resolveFieldType(type, opts) {
660
687
  }
661
688
  if (graphql.isScalarType(type)) {
662
689
  return (mapToPrimitive(type.name) ||
663
- opts.customScalars[type.name] || {
664
- type: 'object',
665
- });
690
+ opts.customScalars[type.name] ||
691
+ ((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema)
692
+ || {
693
+ type: 'object',
694
+ });
666
695
  }
667
696
  if (graphql.isEnumType(type)) {
668
697
  return {
package/index.mjs CHANGED
@@ -361,7 +361,7 @@ function createRouter(sofa) {
361
361
  return router;
362
362
  }
363
363
  function createQueryRoute({ sofa, router, fieldName, }) {
364
- var _a, _b, _c, _d, _e, _f;
364
+ var _a, _b, _c, _d, _e, _f, _g;
365
365
  logger.debug(`[Router] Creating ${fieldName} query`);
366
366
  const queryType = sofa.schema.getQueryType();
367
367
  const operationNode = buildOperationNodeForField({
@@ -396,13 +396,14 @@ function createQueryRoute({ sofa, router, fieldName, }) {
396
396
  path: route.path,
397
397
  method: route.method.toUpperCase(),
398
398
  tags: (_e = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.tags) !== null && _e !== void 0 ? _e : [],
399
- description: (_f = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _f !== void 0 ? _f : '',
399
+ description: (_g = (_f = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _f !== void 0 ? _f : field.description) !== null && _g !== void 0 ? _g : '',
400
400
  };
401
401
  }
402
402
  function createMutationRoute({ sofa, router, fieldName, }) {
403
- var _a, _b, _c, _d;
403
+ var _a, _b, _c, _d, _e, _f;
404
404
  logger.debug(`[Router] Creating ${fieldName} mutation`);
405
405
  const mutationType = sofa.schema.getMutationType();
406
+ const field = mutationType.getFields()[fieldName];
406
407
  const operationNode = buildOperationNodeForField({
407
408
  kind: 'mutation',
408
409
  schema: sofa.schema,
@@ -431,7 +432,7 @@ function createMutationRoute({ sofa, router, fieldName, }) {
431
432
  path,
432
433
  method,
433
434
  tags: (routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.tags) || [],
434
- description: (routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) || '',
435
+ description: (_f = (_e = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.description) !== null && _e !== void 0 ? _e : field.description) !== null && _f !== void 0 ? _f : '',
435
436
  };
436
437
  }
437
438
  function useHandler(config) {
@@ -474,9 +475,34 @@ function useHandler(config) {
474
475
  });
475
476
  if (result.errors) {
476
477
  const defaultErrorHandler = (errors) => {
477
- return new Response(JSON.stringify(errors[0]), {
478
- status: 500,
479
- headers: { 'Content-Type': 'application/json' },
478
+ var _a;
479
+ let status;
480
+ const headers = {
481
+ 'Content-Type': 'application/json; charset=utf-8',
482
+ };
483
+ for (const error of errors) {
484
+ if (typeof error === 'object' && error != null && ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.http)) {
485
+ if (error.extensions.http.status &&
486
+ (!status || error.extensions.http.status > status)) {
487
+ status = error.extensions.http.status;
488
+ }
489
+ if (error.extensions.http.headers) {
490
+ Object.assign(headers, error.extensions.http.headers);
491
+ }
492
+ }
493
+ }
494
+ if (!status) {
495
+ status = 500;
496
+ }
497
+ if (errors.length === 1) {
498
+ return new Response(JSON.stringify(errors[0]), {
499
+ status,
500
+ headers,
501
+ });
502
+ }
503
+ return new Response(JSON.stringify({ errors }), {
504
+ status,
505
+ headers,
480
506
  });
481
507
  };
482
508
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
@@ -638,6 +664,7 @@ function resolveField(field, opts) {
638
664
  // type -> $ref
639
665
  // scalar -> swagger primitive
640
666
  function resolveFieldType(type, opts) {
667
+ var _a;
641
668
  if (isNonNullType(type)) {
642
669
  return resolveFieldType(type.ofType, opts);
643
670
  }
@@ -654,9 +681,11 @@ function resolveFieldType(type, opts) {
654
681
  }
655
682
  if (isScalarType(type)) {
656
683
  return (mapToPrimitive(type.name) ||
657
- opts.customScalars[type.name] || {
658
- type: 'object',
659
- });
684
+ opts.customScalars[type.name] ||
685
+ ((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema)
686
+ || {
687
+ type: 'object',
688
+ });
660
689
  }
661
690
  if (isEnumType(type)) {
662
691
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Create REST APIs with GraphQL",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/router.d.ts CHANGED
@@ -1,6 +1,15 @@
1
1
  import { Request as IttyRequest, Router } from 'itty-router';
2
2
  import type { Sofa } from './sofa';
3
3
  export declare type ErrorHandler = (errors: ReadonlyArray<any>) => Response;
4
+ declare module 'graphql' {
5
+ interface GraphQLHTTPErrorExtensions {
6
+ status?: number;
7
+ headers?: Record<string, string>;
8
+ }
9
+ interface GraphQLErrorExtensions {
10
+ http?: GraphQLHTTPErrorExtensions;
11
+ }
12
+ }
4
13
  declare type SofaRequest = IttyRequest & Request;
5
14
  export declare function createRouter(sofa: Sofa): Router<SofaRequest, {}>;
6
15
  export {};