sofa-api 0.14.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
@@ -481,9 +481,34 @@ function useHandler(config) {
481
481
  });
482
482
  if (result.errors) {
483
483
  const defaultErrorHandler = (errors) => {
484
- return new fetch.Response(JSON.stringify(errors[0]), {
485
- status: 500,
486
- 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,
487
512
  });
488
513
  };
489
514
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
package/index.mjs CHANGED
@@ -475,9 +475,34 @@ function useHandler(config) {
475
475
  });
476
476
  if (result.errors) {
477
477
  const defaultErrorHandler = (errors) => {
478
- return new Response(JSON.stringify(errors[0]), {
479
- status: 500,
480
- 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,
481
506
  });
482
507
  };
483
508
  const errorHandler = sofa.errorHandler || defaultErrorHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.14.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 {};