Comparing version 0.14.0 to 0.15.0
31
index.js
@@ -484,5 +484,30 @@ 'use strict'; | ||
const defaultErrorHandler = (errors) => { | ||
return new fetch.Response(JSON.stringify(errors[0]), { | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' }, | ||
var _a; | ||
let status; | ||
const headers = { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
}; | ||
for (const error of errors) { | ||
if (typeof error === 'object' && error != null && ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.http)) { | ||
if (error.extensions.http.status && | ||
(!status || error.extensions.http.status > status)) { | ||
status = error.extensions.http.status; | ||
} | ||
if (error.extensions.http.headers) { | ||
Object.assign(headers, error.extensions.http.headers); | ||
} | ||
} | ||
} | ||
if (!status) { | ||
status = 500; | ||
} | ||
if (errors.length === 1) { | ||
return new fetch.Response(JSON.stringify(errors[0]), { | ||
status, | ||
headers, | ||
}); | ||
} | ||
return new fetch.Response(JSON.stringify({ errors }), { | ||
status, | ||
headers, | ||
}); | ||
@@ -489,0 +514,0 @@ }; |
{ | ||
"name": "sofa-api", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "Create REST APIs with GraphQL", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
import { Request as IttyRequest, Router } from 'itty-router'; | ||
import type { Sofa } from './sofa'; | ||
export declare type ErrorHandler = (errors: ReadonlyArray<any>) => Response; | ||
declare module 'graphql' { | ||
interface GraphQLHTTPErrorExtensions { | ||
status?: number; | ||
headers?: Record<string, string>; | ||
} | ||
interface GraphQLErrorExtensions { | ||
http?: GraphQLHTTPErrorExtensions; | ||
} | ||
} | ||
declare type SofaRequest = IttyRequest & Request; | ||
export declare function createRouter(sofa: Sofa): Router<SofaRequest, {}>; | ||
export {}; |
Sorry, the diff of this file is not supported yet
85252
1923
12