@whatwg-node/server
Advanced tools
Comparing version 0.5.0-alpha-20221220144402-dce2fb1 to 0.5.0-alpha-20221220145232-524e5a0
@@ -6,1 +6,2 @@ export * from './createServerAdapter'; | ||
export * from './middlewares/withErrorHandling'; | ||
export { Response } from '@whatwg-node/fetch'; |
28
index.js
@@ -416,3 +416,3 @@ 'use strict'; | ||
} | ||
function withCORS(obj, options) { | ||
function withCORS(obj, options, ResponseCtor = fetch.Response) { | ||
let corsOptionsFactory = () => ({}); | ||
@@ -436,3 +436,3 @@ if (options != null) { | ||
if (request.method.toUpperCase() === 'OPTIONS') { | ||
response = new fetch.Response(null, { | ||
response = new ResponseCtor(null, { | ||
status: 204, | ||
@@ -462,9 +462,11 @@ }); | ||
const defaultErrorHandler = function defaultErrorHandler(e) { | ||
return new fetch.Response(e.stack || e.message || e.toString(), { | ||
status: e.statusCode || e.status || 500, | ||
statusText: e.statusText || 'Internal Server Error', | ||
}); | ||
}; | ||
function withErrorHandling(obj, onError = defaultErrorHandler) { | ||
function createDefaultErrorHandler(ResponseCtor = fetch.Response) { | ||
return function defaultErrorHandler(e) { | ||
return new ResponseCtor(e.stack || e.message || e.toString(), { | ||
status: e.statusCode || e.status || 500, | ||
statusText: e.statusText || 'Internal Server Error', | ||
}); | ||
}; | ||
} | ||
function withErrorHandling(obj, onError = createDefaultErrorHandler()) { | ||
async function handleWithErrorHandling(request, ctx) { | ||
@@ -489,4 +491,10 @@ try { | ||
Object.defineProperty(exports, 'Response', { | ||
enumerable: true, | ||
get: function () { | ||
return fetch.Response; | ||
} | ||
}); | ||
exports.createDefaultErrorHandler = createDefaultErrorHandler; | ||
exports.createServerAdapter = createServerAdapter; | ||
exports.defaultErrorHandler = defaultErrorHandler; | ||
exports.getCORSHeadersByRequestAndOptions = getCORSHeadersByRequestAndOptions; | ||
@@ -493,0 +501,0 @@ exports.isAsyncIterable = isAsyncIterable; |
@@ -13,2 +13,2 @@ import { DefaultServerAdapterContext, ServerAdapterBaseObject } from '../types'; | ||
export declare function getCORSHeadersByRequestAndOptions(request: Request, corsOptions: CORSOptions): Record<string, string>; | ||
export declare function withCORS<TServerContext = DefaultServerAdapterContext, TBaseObject extends ServerAdapterBaseObject<TServerContext> = ServerAdapterBaseObject<TServerContext>>(obj: TBaseObject, options: WithCORSOptions<TServerContext>): TBaseObject; | ||
export declare function withCORS<TServerContext = DefaultServerAdapterContext, TBaseObject extends ServerAdapterBaseObject<TServerContext> = ServerAdapterBaseObject<TServerContext>>(obj: TBaseObject, options: WithCORSOptions<TServerContext>, ResponseCtor?: typeof Response): TBaseObject; |
import { DefaultServerAdapterContext, ServerAdapterBaseObject } from '../types'; | ||
export declare const defaultErrorHandler: ErrorHandler<any>; | ||
export declare function createDefaultErrorHandler<TServerContext = DefaultServerAdapterContext>(ResponseCtor?: typeof Response): ErrorHandler<TServerContext>; | ||
export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => Response | Promise<Response>; | ||
export declare function withErrorHandling<TServerContext = DefaultServerAdapterContext, TBaseObject extends ServerAdapterBaseObject<TServerContext> = ServerAdapterBaseObject<TServerContext>>(obj: TBaseObject, onError?: ErrorHandler<TServerContext>): TBaseObject; |
{ | ||
"name": "@whatwg-node/server", | ||
"version": "0.5.0-alpha-20221220144402-dce2fb1", | ||
"version": "0.5.0-alpha-20221220145232-524e5a0", | ||
"description": "Fetch API compliant HTTP Server adapter", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
56853
1093
4