@api-ts/express-wrapper
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
@@ -8,3 +8,7 @@ "use strict"; | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -39,3 +43,3 @@ if (k2 === undefined) k2 = k; | ||
const decodeRequestAndEncodeResponse = (apiName, httpRoute, handler, responseEncoder) => { | ||
return createNamedFunction('decodeRequestAndEncodeResponse' + httpRoute.method + apiName, async (req, res) => { | ||
return createNamedFunction('decodeRequestAndEncodeResponse' + httpRoute.method + apiName, async (req, res, next) => { | ||
const maybeRequest = httpRoute.request.decode(req); | ||
@@ -58,5 +62,7 @@ if (maybeRequest._tag === 'Left') { | ||
res.status(500).end(); | ||
next(); | ||
return; | ||
} | ||
responseEncoder(httpRoute, rawResponse, res); | ||
const expressHandler = responseEncoder(httpRoute, rawResponse); | ||
expressHandler(req, res, next); | ||
}); | ||
@@ -63,0 +69,0 @@ }; |
@@ -10,4 +10,4 @@ import express from 'express'; | ||
}[keyof R['response'] & keyof HttpToKeyStatus]; | ||
export declare type ResponseEncoder = (route: HttpRoute, serviceFnResponse: NumericOrKeyedResponseType<HttpRoute>, expressRes: express.Response) => void; | ||
export declare type ResponseEncoder = (route: HttpRoute, serviceFnResponse: NumericOrKeyedResponseType<HttpRoute>) => express.RequestHandler; | ||
export declare const defaultResponseEncoder: ResponseEncoder; | ||
//# sourceMappingURL=response.d.ts.map |
@@ -5,3 +5,3 @@ "use strict"; | ||
const io_ts_http_1 = require("@api-ts/io-ts-http"); | ||
const defaultResponseEncoder = (route, serviceFnResponse, expressRes) => { | ||
const defaultResponseEncoder = (route, serviceFnResponse) => (_req, res) => { | ||
const { type, payload } = serviceFnResponse; | ||
@@ -11,14 +11,15 @@ const status = typeof type === 'number' ? type : io_ts_http_1.KeyToHttpStatus[type]; | ||
console.warn('Unknown status code returned'); | ||
expressRes.status(500).end(); | ||
res.status(500).end(); | ||
return; | ||
} | ||
const responseCodec = route.response[status]; | ||
if (responseCodec === undefined || !responseCodec.is(payload)) { | ||
try { | ||
res.status(status).json(responseCodec.encode(payload)).end(); | ||
} | ||
catch { | ||
console.warn("Unable to encode route's return value, did you return the expected type?"); | ||
expressRes.status(500).end(); | ||
return; | ||
res.status(500).end(); | ||
} | ||
expressRes.status(status).json(responseCodec.encode(payload)).end(); | ||
}; | ||
exports.defaultResponseEncoder = defaultResponseEncoder; | ||
//# sourceMappingURL=response.js.map |
{ | ||
"name": "@api-ts/express-wrapper", | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"description": "Implement an HTTP specification with Express", | ||
@@ -27,7 +27,6 @@ "author": "Eric Crosson <ericcrosson@bitgo.com>", | ||
"@types/express": "4.17.13", | ||
"@types/node": "16.11.7", | ||
"ava": "4.0.1", | ||
"ava": "4.2.0", | ||
"c8": "7.11.2", | ||
"ts-node": "10.4.0", | ||
"typescript": "4.5.5" | ||
"ts-node": "10.7.0", | ||
"typescript": "4.6.4" | ||
}, | ||
@@ -34,0 +33,0 @@ "ava": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17781
7
190