UNPKG

@reflet/express

Version:

Well-defined and well-typed express decorators

115 lines (114 loc) 3.81 kB
"use strict"; /* istanbul ignore file */ // https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards Object.defineProperty(exports, "__esModule", { value: true }); exports.isObservable = exports.isPathParams = exports.isPath = exports.isErrorHandlerParams = exports.isErrorHandler = exports.isExpressRouter = exports.isExpressApp = exports.isWritableStream = exports.isReadableStream = exports.isClass = exports.isAsyncFunction = exports.isPromise = void 0; const express = require("express"); const stream_1 = require("stream"); const http_1 = require("http"); /** * Checks if given object is a Promise or Promise-like. * @internal */ function isPromise(obj) { return obj instanceof Promise || (!!obj && typeof obj === 'object' && typeof obj.then === 'function'); } exports.isPromise = isPromise; /** * @internal */ function isAsyncFunction(value) { return value[Symbol.toStringTag] === 'AsyncFunction'; } exports.isAsyncFunction = isAsyncFunction; /** * Simply checks if given object is a function to distinguish between a class and its instance. * @internal */ function isClass(obj) { return typeof obj === 'function'; } exports.isClass = isClass; /** * Checks if given object is a Readable Stream or Readable Stream-like. * @see https://nodejs.org/api/stream.html#class-streamreadable * @internal */ function isReadableStream(obj) { return (obj instanceof stream_1.Readable || (!!obj && typeof obj === 'object' && typeof obj.pipe === 'function' && typeof obj.read === 'function' && typeof obj._readableState === 'object')); } exports.isReadableStream = isReadableStream; /** * Checks if given object is a Writable Stream or Writable Stream-like. * @see https://nodejs.org/api/stream.html#class-streamwritable * @internal */ function isWritableStream(obj) { return (obj instanceof stream_1.Writable || (!!obj && typeof obj === 'object' && typeof obj.pipe === 'function' && typeof obj.write === 'function' && typeof obj._writableState === 'object')); } exports.isWritableStream = isWritableStream; /** * @internal */ function isExpressApp(obj) { return typeof obj === 'function' && obj.request instanceof http_1.IncomingMessage && obj.response instanceof http_1.ServerResponse; } exports.isExpressApp = isExpressApp; /** * @internal */ function isExpressRouter(obj) { return Object.getPrototypeOf(obj) === express.Router; } exports.isExpressRouter = isExpressRouter; /** * Checks if given object is an express error handler. * @internal */ function isErrorHandler(obj) { return typeof obj === 'function' && obj.length === 4; } exports.isErrorHandler = isErrorHandler; /** * Checks if given object is an express `app.use` parameter that contains at least one error handler. * @internal */ function isErrorHandlerParams(obj) { return isErrorHandler(obj) || (Array.isArray(obj) && obj.some(isErrorHandler)); } exports.isErrorHandlerParams = isErrorHandlerParams; /** * Checks if given value is a path handled by express. * @internal */ function isPath(val) { return typeof val === 'string' || val instanceof RegExp; } exports.isPath = isPath; /** * Checks if given object is an express `app.use` parameter that contains paths only. * @internal */ function isPathParams(obj) { return isPath(obj) || (Array.isArray(obj) && obj.every(isPath)); } exports.isPathParams = isPathParams; /** * Checks if given object is Observable-like. * @see https://github.com/ReactiveX/rxjs/blob/master/src/internal/util/isObservable.ts * @internal */ function isObservable(obj) { return !!obj && typeof obj.lift === 'function' && typeof obj.subscribe === 'function'; } exports.isObservable = isObservable;