@api-ts/express-wrapper
Advanced tools
Comparing version 1.0.0-beta.6 to 1.0.0-beta.7
@@ -8,7 +8,6 @@ /** | ||
import { RouteHandler } from './request'; | ||
export declare function createServer<Spec extends ApiSpec>(spec: Spec, configureExpressApplication: (app: express.Application) => { | ||
[ApiName in keyof Spec]: { | ||
[Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>; | ||
}; | ||
}): express.Application; | ||
import { ResponseEncoder } from './response'; | ||
export type { ResponseEncoder } from './response'; | ||
export declare const createServerWithResponseEncoder: (encoder: ResponseEncoder) => <Spec extends ApiSpec>(spec: ApiSpec, configureExpressApplication: (app: express.Application) => { [ApiName in keyof Spec]: { [Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>; }; }) => express.Application; | ||
export declare const createServer: <Spec extends ApiSpec>(spec: ApiSpec, configureExpressApplication: (app: express.Application) => { [ApiName in keyof Spec]: { [Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>; }; }) => express.Application; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,8 +10,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createServer = void 0; | ||
exports.createServer = exports.createServerWithResponseEncoder = void 0; | ||
const express_1 = __importDefault(require("express")); | ||
const path_1 = require("./path"); | ||
const request_1 = require("./request"); | ||
const response_1 = require("./response"); | ||
const isHttpVerb = (verb) => verb === 'get' || verb === 'put' || verb === 'post' || verb === 'delete'; | ||
function createServer(spec, configureExpressApplication) { | ||
const createServerWithResponseEncoder = (encoder) => (spec, configureExpressApplication) => { | ||
const app = (0, express_1.default)(); | ||
@@ -28,4 +29,6 @@ const routes = configureExpressApplication(app); | ||
const routeHandler = routes[apiName][method]; | ||
const expressRouteHandler = (0, request_1.decodeRequestAndEncodeResponse)(apiName, httpRoute, // TODO: wat | ||
(0, request_1.getServiceFunction)(routeHandler)); | ||
const expressRouteHandler = (0, request_1.decodeRequestAndEncodeResponse)(apiName, httpRoute, | ||
// FIXME: TS is complaining that `routeHandler` is not necessarily guaranteed to be a | ||
// `ServiceFunction`, because subtypes of Spec[string][string] can have arbitrary extra keys. | ||
(0, request_1.getServiceFunction)(routeHandler), encoder); | ||
const handlers = [...(0, request_1.getMiddleware)(routeHandler), expressRouteHandler]; | ||
@@ -38,4 +41,5 @@ const expressPath = (0, path_1.apiTsPathToExpress)(httpRoute.path); | ||
return app; | ||
} | ||
exports.createServer = createServer; | ||
}; | ||
exports.createServerWithResponseEncoder = createServerWithResponseEncoder; | ||
exports.createServer = (0, exports.createServerWithResponseEncoder)(response_1.defaultResponseEncoder); | ||
//# sourceMappingURL=index.js.map |
@@ -6,10 +6,4 @@ /** | ||
import express from 'express'; | ||
import * as t from 'io-ts'; | ||
import { HttpRoute, HttpToKeyStatus, RequestType, ResponseType } from '@api-ts/io-ts-http'; | ||
declare type NumericOrKeyedResponseType<R extends HttpRoute> = ResponseType<R> | { | ||
[S in keyof R['response']]: S extends keyof HttpToKeyStatus ? { | ||
type: HttpToKeyStatus[S]; | ||
payload: t.TypeOf<R['response'][S]>; | ||
} : never; | ||
}[keyof R['response']]; | ||
import { HttpRoute, RequestType } from '@api-ts/io-ts-http'; | ||
import type { NumericOrKeyedResponseType, ResponseEncoder } from './response'; | ||
export declare type ServiceFunction<R extends HttpRoute> = (input: RequestType<R>) => NumericOrKeyedResponseType<R> | Promise<NumericOrKeyedResponseType<R>>; | ||
@@ -22,4 +16,3 @@ export declare type RouteHandler<R extends HttpRoute> = ServiceFunction<R> | { | ||
export declare const getMiddleware: <R extends HttpRoute>(routeHandler: RouteHandler<R>) => express.RequestHandler[]; | ||
export declare const decodeRequestAndEncodeResponse: <Route extends HttpRoute>(apiName: string, httpRoute: Route, handler: ServiceFunction<Route>) => express.RequestHandler; | ||
export {}; | ||
export declare const decodeRequestAndEncodeResponse: (apiName: string, httpRoute: HttpRoute, handler: ServiceFunction<HttpRoute>, responseEncoder: ResponseEncoder) => express.RequestHandler; | ||
//# sourceMappingURL=request.d.ts.map |
@@ -28,3 +28,2 @@ "use strict"; | ||
const PathReporter = __importStar(require("io-ts/lib/PathReporter")); | ||
const io_ts_http_1 = require("@api-ts/io-ts-http"); | ||
const getServiceFunction = (routeHandler) => 'handler' in routeHandler ? routeHandler.handler : routeHandler; | ||
@@ -39,3 +38,3 @@ exports.getServiceFunction = getServiceFunction; | ||
const createNamedFunction = (name, fn) => Object.defineProperty(fn, 'name', { value: name }); | ||
const decodeRequestAndEncodeResponse = (apiName, httpRoute, handler) => { | ||
const decodeRequestAndEncodeResponse = (apiName, httpRoute, handler, responseEncoder) => { | ||
return createNamedFunction('decodeRequestAndEncodeResponse' + httpRoute.method + apiName, async (req, res) => { | ||
@@ -61,16 +60,3 @@ const maybeRequest = httpRoute.request.decode(req); | ||
} | ||
const { type, payload } = rawResponse; | ||
const status = typeof type === 'number' ? type : io_ts_http_1.KeyToHttpStatus[type]; | ||
if (status === undefined) { | ||
console.warn('Unknown status code returned'); | ||
res.status(500).end(); | ||
return; | ||
} | ||
const responseCodec = httpRoute.response[status]; | ||
if (responseCodec === undefined || !responseCodec.is(payload)) { | ||
console.warn("Unable to encode route's return value, did you return the expected type?"); | ||
res.status(500).end(); | ||
return; | ||
} | ||
res.status(status).json(responseCodec.encode(payload)).end(); | ||
responseEncoder(httpRoute, rawResponse, res); | ||
}); | ||
@@ -77,0 +63,0 @@ }; |
{ | ||
"name": "@api-ts/express-wrapper", | ||
"version": "1.0.0-beta.6", | ||
"version": "1.0.0-beta.7", | ||
"description": "Implement an HTTP specification with Express", | ||
@@ -5,0 +5,0 @@ "author": "Eric Crosson <ericcrosson@bitgo.com>", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16978
19
171
0