sofa-api 0.14.0 → 0.15.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/index.js CHANGED
@@ -49,8 +49,7 @@ function resolveVariable({ value, type, schema, }) {
49
49
  if (graphql.isScalarType(namedType)) {
50
50
  // GraphQLBoolean.serialize expects a boolean or a number only
51
51
  if (graphql.isEqualType(graphql.GraphQLBoolean, namedType)) {
52
- // we don't support TRUE
53
- value = value === 'true';
52
+ value = (value === 'true' || value === true);
54
53
  }
55
54
  return namedType.serialize(value);
56
55
  }
@@ -481,9 +480,34 @@ function useHandler(config) {
481
480
  });
482
481
  if (result.errors) {
483
482
  const defaultErrorHandler = (errors) => {
484
- return new fetch.Response(JSON.stringify(errors[0]), {
485
- status: 500,
486
- headers: { 'Content-Type': 'application/json' },
483
+ var _a;
484
+ let status;
485
+ const headers = {
486
+ 'Content-Type': 'application/json; charset=utf-8',
487
+ };
488
+ for (const error of errors) {
489
+ if (typeof error === 'object' && error != null && ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.http)) {
490
+ if (error.extensions.http.status &&
491
+ (!status || error.extensions.http.status > status)) {
492
+ status = error.extensions.http.status;
493
+ }
494
+ if (error.extensions.http.headers) {
495
+ Object.assign(headers, error.extensions.http.headers);
496
+ }
497
+ }
498
+ }
499
+ if (!status) {
500
+ status = 500;
501
+ }
502
+ if (errors.length === 1) {
503
+ return new fetch.Response(JSON.stringify(errors[0]), {
504
+ status,
505
+ headers,
506
+ });
507
+ }
508
+ return new fetch.Response(JSON.stringify({ errors }), {
509
+ status,
510
+ headers,
487
511
  });
488
512
  };
489
513
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
package/index.mjs CHANGED
@@ -43,8 +43,7 @@ function resolveVariable({ value, type, schema, }) {
43
43
  if (isScalarType(namedType)) {
44
44
  // GraphQLBoolean.serialize expects a boolean or a number only
45
45
  if (isEqualType(GraphQLBoolean, namedType)) {
46
- // we don't support TRUE
47
- value = value === 'true';
46
+ value = (value === 'true' || value === true);
48
47
  }
49
48
  return namedType.serialize(value);
50
49
  }
@@ -475,9 +474,34 @@ function useHandler(config) {
475
474
  });
476
475
  if (result.errors) {
477
476
  const defaultErrorHandler = (errors) => {
478
- return new Response(JSON.stringify(errors[0]), {
479
- status: 500,
480
- headers: { 'Content-Type': 'application/json' },
477
+ var _a;
478
+ let status;
479
+ const headers = {
480
+ 'Content-Type': 'application/json; charset=utf-8',
481
+ };
482
+ for (const error of errors) {
483
+ if (typeof error === 'object' && error != null && ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.http)) {
484
+ if (error.extensions.http.status &&
485
+ (!status || error.extensions.http.status > status)) {
486
+ status = error.extensions.http.status;
487
+ }
488
+ if (error.extensions.http.headers) {
489
+ Object.assign(headers, error.extensions.http.headers);
490
+ }
491
+ }
492
+ }
493
+ if (!status) {
494
+ status = 500;
495
+ }
496
+ if (errors.length === 1) {
497
+ return new Response(JSON.stringify(errors[0]), {
498
+ status,
499
+ headers,
500
+ });
501
+ }
502
+ return new Response(JSON.stringify({ errors }), {
503
+ status,
504
+ headers,
481
505
  });
482
506
  };
483
507
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.14.0",
3
+ "version": "0.15.1",
4
4
  "description": "Create REST APIs with GraphQL",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "graphql": "^0.13.2 || ^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-tools/utils": "8.12.0",
11
- "@whatwg-node/fetch": "^0.4.3",
10
+ "@graphql-tools/utils": "9.1.0",
11
+ "@whatwg-node/fetch": "^0.5.0",
12
12
  "@whatwg-node/server": "^0.4.1",
13
13
  "ansi-colors": "4.1.3",
14
14
  "itty-router": "^2.6.1",
15
15
  "openapi-types": "12.0.2",
16
16
  "param-case": "3.0.4",
17
17
  "title-case": "3.0.3",
18
- "tslib": "2.4.0"
18
+ "tslib": "2.4.1"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
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 {};