express-router-api
Advanced tools
Comparing version 1.3.9 to 1.4.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ExpressApiRouter = exports.ApiResponse = exports.ApiNext = exports.ApiError = exports.ExpressApiRouterError = void 0; | ||
const express_1 = require("express"); | ||
@@ -35,9 +36,9 @@ const rxjs_1 = require("rxjs"); | ||
function resolve(value) { | ||
if (rxjs_1.isObservable(value)) { | ||
if ((0, rxjs_1.isObservable)(value)) { | ||
return value; | ||
} | ||
if (isPromise(value)) { | ||
return rxjs_1.from(value); | ||
return (0, rxjs_1.from)(value); | ||
} | ||
return rxjs_1.of(value); | ||
return (0, rxjs_1.of)(value); | ||
} | ||
@@ -47,9 +48,9 @@ const promiseProps = (obj) => { | ||
if (!keys.length) { | ||
return rxjs_1.of(obj); | ||
return (0, rxjs_1.of)(obj); | ||
} | ||
return rxjs_1.forkJoin(Object.keys(obj).map((key) => { | ||
return resolve(obj[key]).pipe(operators_1.map((val) => { | ||
return (0, rxjs_1.forkJoin)(Object.keys(obj).map((key) => { | ||
return resolve(obj[key]).pipe((0, operators_1.map)((val) => { | ||
return { [key]: val }; | ||
})); | ||
})).pipe(operators_1.map((results) => Object.assign({}, ...results))); | ||
})).pipe((0, operators_1.map)((results) => Object.assign({}, ...results))); | ||
}; | ||
@@ -83,10 +84,10 @@ function sendApiResponse(res, apiResponse) { | ||
const processApiError = (err, req, res) => { | ||
return rxjs_1.defer(() => resolve(this.errorFormatter(err, req, res))) | ||
.pipe(operators_1.map((formatted) => formatted ? new ApiResponse(formatted) | ||
return (0, rxjs_1.defer)(() => resolve(this.errorFormatter(err, req, res))) | ||
.pipe((0, operators_1.map)((formatted) => formatted ? new ApiResponse(formatted) | ||
: new ApiResponse(err.message, err.statusCode || 500))); | ||
}; | ||
return (req, res, next) => { | ||
const formatterOperator = operators_1.switchMap((data) => { | ||
const formatterOperator = (0, operators_1.switchMap)((data) => { | ||
if (data === exports.ApiNext) { | ||
return rxjs_1.of(data); | ||
return (0, rxjs_1.of)(data); | ||
} | ||
@@ -97,6 +98,6 @@ return resolve(this.successFormatter(data, req, res)); | ||
if (this.errorFormatter) { | ||
return rxjs_1.defer(() => resolve(this.errorFormatter(err, req, res))) | ||
.pipe(operators_1.map((formatted) => new ApiResponse(formatted, err.statusCode || 500))); | ||
return (0, rxjs_1.defer)(() => resolve(this.errorFormatter(err, req, res))) | ||
.pipe((0, operators_1.map)((formatted) => new ApiResponse(formatted, err.statusCode || 500))); | ||
} | ||
return rxjs_1.of(undefined); | ||
return (0, rxjs_1.of)(undefined); | ||
}; | ||
@@ -109,6 +110,6 @@ const isPlainObject = (obj) => { | ||
}; | ||
const subscription = rxjs_1.defer(() => resolve(origHandler(req, res, next))) | ||
.pipe(operators_1.switchMap((obj) => { | ||
return isPlainObject(obj) ? promiseProps(obj) : resolve(obj); | ||
}), formatterOperator, operators_1.switchMap((data) => { | ||
const subscription = (0, rxjs_1.defer)(() => resolve(origHandler(req, res, next))) | ||
.pipe((0, operators_1.switchMap)((obj) => { | ||
return (obj && isPlainObject(obj)) ? promiseProps(obj) : resolve(obj); | ||
}), formatterOperator, (0, operators_1.switchMap)((data) => { | ||
if (data instanceof ApiError) { | ||
@@ -118,6 +119,6 @@ return processApiError(data, req, res); | ||
else if (!(data instanceof ApiResponse)) { | ||
return rxjs_1.of(new ApiResponse(data, 200)); | ||
return (0, rxjs_1.of)(new ApiResponse(data, 200)); | ||
} | ||
return rxjs_1.of(data); | ||
}), operators_1.catchError((err) => { | ||
return (0, rxjs_1.of)(data); | ||
}), (0, operators_1.catchError)((err) => { | ||
if (err instanceof ExpressApiRouterError) { | ||
@@ -129,3 +130,3 @@ res.emit('expressApiRouterError', err); | ||
} | ||
return rxjs_1.of(undefined); | ||
return (0, rxjs_1.of)(undefined); | ||
} | ||
@@ -135,9 +136,9 @@ else if (err instanceof ApiError) { | ||
} | ||
return rxjs_1.throwError(err); | ||
}), operators_1.catchError((err) => { | ||
return resolve(formatError(err)).pipe(operators_1.map((jsonError) => { | ||
return (0, rxjs_1.throwError)(err); | ||
}), (0, operators_1.catchError)((err) => { | ||
return resolve(formatError(err)).pipe((0, operators_1.map)((jsonError) => { | ||
return new ApiResponse(jsonError || internalServerError, (jsonError && jsonError.statusCode) || 500); | ||
})); | ||
}), operators_1.catchError((err) => { | ||
return rxjs_1.of(new ApiResponse(internalServerError, err.statusCode || 500)); | ||
}), (0, operators_1.catchError)((err) => { | ||
return (0, rxjs_1.of)(new ApiResponse(internalServerError, err.statusCode || 500)); | ||
})) | ||
@@ -151,3 +152,7 @@ .subscribe((apiResponse) => { | ||
}); | ||
req.on('close', () => subscription.unsubscribe()); | ||
const unSub = () => subscription.unsubscribe(); | ||
req.on('end', () => unSub); | ||
req.on('finish', () => unSub); | ||
req.socket.on('close', () => unSub); | ||
req.socket.on('error', () => unSub); | ||
}; | ||
@@ -160,4 +165,4 @@ } | ||
'unsubscribe', 'patch', 'search', 'connect']; | ||
const defaultErrorFormatter = (err) => rxjs_1.of(undefined); | ||
const defaultSuccessFormatter = (data) => rxjs_1.of(data); | ||
const defaultErrorFormatter = (err) => (0, rxjs_1.of)(undefined); | ||
const defaultSuccessFormatter = (data) => (0, rxjs_1.of)(data); | ||
function patchMethods(apiRouter, options) { | ||
@@ -199,3 +204,3 @@ methods.forEach((method) => { | ||
function ExpressApiRouter(options) { | ||
const router = express_1.Router(options); | ||
const router = (0, express_1.Router)(options); | ||
const errorFormatter = (options && options.errorFormatter) || defaultErrorFormatter; | ||
@@ -202,0 +207,0 @@ const successFormatter = (options && options.successFormatter) || defaultSuccessFormatter; |
{ | ||
"name": "express-router-api", | ||
"version": "1.3.9", | ||
"version": "1.4.0", | ||
"description": "Express router that lets you construct your API entirely on promises", | ||
@@ -17,17 +17,18 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/express": "^4.16.0", | ||
"@types/express": "^4.17.13", | ||
"@types/methods": "^1.1.0", | ||
"@types/node": "^16.11.12", | ||
"bluebird": "^3.5.2", | ||
"chai": "^3.5.0", | ||
"express": "^4.16.4", | ||
"express": "^4.17.1", | ||
"mocha": "^5.2.0", | ||
"request": "^2.88.0", | ||
"request-promise": "^4.2.2", | ||
"ts-node": "^7.0.1", | ||
"ts-node": "^10.4.0", | ||
"tslint": "^5.11.0", | ||
"typescript": "~3.4.5" | ||
"typescript": "^4.4.0" | ||
}, | ||
"peerDependencies": { | ||
"express": "^4.13.4" | ||
"express": "^4.17.1" | ||
} | ||
} |
@@ -140,4 +140,4 @@ import { | ||
.pipe( | ||
switchMap((obj: ApiResult) => { | ||
return isPlainObject(obj) ? promiseProps(obj as { [key: string]: any }) : resolve(obj); | ||
switchMap((obj: ApiResult | void) => { | ||
return (obj && isPlainObject(obj)) ? promiseProps(obj as { [key: string]: any }) : resolve(obj); | ||
}), | ||
@@ -169,3 +169,4 @@ formatterOperator, | ||
return resolve(formatError(err)).pipe(map((jsonError) => { | ||
return new ApiResponse(jsonError || internalServerError, (jsonError && (jsonError as any).statusCode) || 500); | ||
return new ApiResponse(jsonError || internalServerError, | ||
(jsonError && (jsonError as any).statusCode) || 500); | ||
})); | ||
@@ -184,3 +185,7 @@ }), | ||
}); | ||
req.on('close', () => subscription.unsubscribe()); | ||
const unSub = () => subscription.unsubscribe(); | ||
req.on('end', () => unSub); | ||
req.on('finish', () => unSub); | ||
req.socket.on('close', () => unSub); | ||
req.socket.on('error', () => unSub); | ||
}; | ||
@@ -224,3 +229,3 @@ } | ||
}); | ||
oldImplementation.call(apiRouter, path, ...callbacks); | ||
(oldImplementation as any).call(apiRouter, path, ...callbacks); | ||
return apiRouter; | ||
@@ -227,0 +232,0 @@ }; |
39113
980
12