@teambit/express 0.0.864 → 0.0.865

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- require("core-js/modules/es.array.flat-map.js");
4
+ require("core-js/modules/es.array.flat.js");
5
5
  require("core-js/modules/es.array.iterator.js");
6
- require("core-js/modules/es.array.unscopables.flat-map.js");
6
+ require("core-js/modules/es.array.unscopables.flat.js");
7
7
  require("core-js/modules/es.promise.js");
8
8
  Object.defineProperty(exports, "__esModule", {
9
9
  value: true
@@ -147,7 +147,15 @@ class ExpressMain {
147
147
  return next();
148
148
  });
149
149
  if (!(options !== null && options !== void 0 && options.disableBodyParser)) this.bodyParser(app);
150
- this.middlewareSlot.toArray().flatMap(([, middlewares]) => middlewares.flatMap(middlewareManifest => app.use(middlewareManifest.middleware)));
150
+ const middlewaresSlot = this.middlewareSlot.values().flat();
151
+ middlewaresSlot.forEach(({
152
+ route,
153
+ middleware
154
+ }) => {
155
+ if (!route) app.use(middleware);
156
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
157
+ if (route) app.use(route, middleware);
158
+ });
151
159
  sortedRoutes.forEach(routeInfo => {
152
160
  const {
153
161
  method,
@@ -1 +1 @@
1
- {"version":3,"names":["_cli","data","require","_harmony","_logger","_express","_interopRequireDefault","_lodash","_bodyParser","_express2","_middlewares","_types","ExpressMain","constructor","config","moduleSlot","logger","middlewareSlot","listen","port","serverPort","app","createApp","register","routes","registerMiddleware","middlewares","createRootRoutes","namespace","ExpressAspect","id","method","path","disableNamespace","priority","req","res","send","expressApp","options","internalRoutes","createRoutes","allRoutes","concat","sortedRoutes","sortBy","r","reverse","express","use","next","loggerIgnorePath","includes","url","debug","headers","disableBodyParser","bodyParser","toArray","flatMap","middlewareManifest","middleware","forEach","routeInfo","catchErrorsMiddlewares","routesSlots","routeEntries","map","route","flatten","verbValidation","lowerCase","verb","Verb","READ","status","jsonp","message","error","catchErrors","json","limit","raw","type","provider","loggerFactory","routeSlot","createLogger","exports","_defineProperty2","default","MainRuntime","Slot","withType","LoggerAspect","addRuntime"],"sources":["express.main.runtime.ts"],"sourcesContent":["import { MainRuntime } from '@teambit/cli';\nimport { Slot, SlotRegistry } from '@teambit/harmony';\nimport { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';\nimport express, { Express } from 'express';\nimport { concat, flatten, lowerCase, sortBy } from 'lodash';\nimport bodyParser from 'body-parser';\nimport { ExpressAspect } from './express.aspect';\nimport { catchErrors } from './middlewares';\nimport { Middleware, Request, Response, Route, Verb } from './types';\nimport { MiddlewareManifest } from './middleware-manifest';\n\nexport type ExpressConfig = {\n port: number;\n namespace: string;\n loggerIgnorePath: string[];\n};\n\nexport type MiddlewareSlot = SlotRegistry<MiddlewareManifest[]>;\n\nexport type RouteSlot = SlotRegistry<Route[]>;\n\nexport class ExpressMain {\n static runtime = MainRuntime;\n\n constructor(\n /**\n * extension config\n */\n readonly config: ExpressConfig,\n\n /**\n * slot for registering graphql modules\n */\n private moduleSlot: RouteSlot,\n\n /**\n * logger extension.\n */\n readonly logger: Logger,\n\n readonly middlewareSlot: MiddlewareSlot\n ) {}\n\n /**\n * start a express server.\n */\n async listen(port?: number) {\n const serverPort = port || this.config.port;\n const app = this.createApp();\n app.listen(serverPort);\n }\n\n /**\n * register a new express routes.\n * route will be added as `/api/${route}`\n */\n register(routes: Route[]) {\n this.moduleSlot.register(routes);\n return this;\n }\n\n /**\n * register a new middleware into express.\n */\n registerMiddleware(middlewares: MiddlewareManifest[]) {\n this.middlewareSlot.register(middlewares);\n return this;\n }\n\n private createRootRoutes() {\n // TODO: @guy refactor health to service aspect.\n return [\n {\n namespace: ExpressAspect.id,\n method: 'get',\n path: '/_health',\n disableNamespace: false,\n priority: 0,\n middlewares: [async (req: Request, res: Response) => res.send('ok')],\n },\n ];\n }\n\n createApp(expressApp?: Express, options?: { disableBodyParser: true }): Express {\n const internalRoutes = this.createRootRoutes();\n const routes = this.createRoutes();\n const allRoutes = concat(routes, internalRoutes);\n const sortedRoutes = sortBy(allRoutes, (r) => r.priority).reverse();\n const app = expressApp || express();\n app.use((req, res, next) => {\n if (this.config.loggerIgnorePath.includes(req.url)) return next();\n this.logger.debug(`express got a request to a URL: ${req.url}', headers:`, req.headers);\n return next();\n });\n if (!options?.disableBodyParser) this.bodyParser(app);\n\n this.middlewareSlot\n .toArray()\n .flatMap(([, middlewares]) =>\n middlewares.flatMap((middlewareManifest) => app.use(middlewareManifest.middleware))\n );\n sortedRoutes.forEach((routeInfo) => {\n const { method, path, middlewares, disableNamespace } = routeInfo;\n // TODO: @guy make sure to support single middleware here.\n const namespace = disableNamespace ? '' : `/${this.config.namespace}`;\n app[method](`${namespace}${path}`, this.catchErrorsMiddlewares(middlewares));\n });\n\n return app;\n }\n\n private createRoutes() {\n const routesSlots = this.moduleSlot.toArray();\n const routeEntries = routesSlots.map(([, routes]) => {\n return routes.map((route) => {\n const middlewares = flatten([this.verbValidation(route), route.middlewares]);\n return {\n method: lowerCase(route.method),\n path: route.route,\n disableNamespace: route.disableNamespace,\n middlewares,\n priority: route.priority || 0,\n };\n });\n });\n\n return flatten(routeEntries);\n }\n\n private verbValidation(route: Route): Middleware {\n return async (req: express.Request, res: express.Response, next: express.NextFunction) => {\n if (!route.verb) return next();\n const verb = req.headers['x-verb'] || Verb.READ;\n if (verb !== route.verb) {\n res.status(403);\n return res.jsonp({ message: 'You are not authorized', error: 'forbidden' });\n }\n return next();\n };\n }\n\n private catchErrorsMiddlewares(middlewares: Middleware[]) {\n return middlewares.map((middleware) => catchErrors(middleware));\n }\n\n private bodyParser(app: Express) {\n app.use(bodyParser.json({ limit: '5000mb' }));\n app.use(bodyParser.raw({ type: 'application/octet-stream', limit: '5000mb' }));\n }\n\n static slots = [Slot.withType<Route[]>(), Slot.withType<MiddlewareManifest[]>()];\n static dependencies = [LoggerAspect];\n\n static defaultConfig = {\n port: 4001,\n namespace: 'api',\n loggerIgnorePath: ['/api/_health'],\n };\n\n static async provider(\n [loggerFactory]: [LoggerMain],\n config: ExpressConfig,\n [routeSlot, middlewareSlot]: [RouteSlot, MiddlewareSlot]\n ) {\n const logger = loggerFactory.createLogger(ExpressAspect.id);\n return new ExpressMain(config, routeSlot, logger, middlewareSlot);\n }\n}\n\nExpressAspect.addRuntime(ExpressMain);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAAA,KAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,IAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAE,SAAA;EAAA,MAAAF,IAAA,GAAAC,OAAA;EAAAC,QAAA,YAAAA,CAAA;IAAA,OAAAF,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAG,QAAA;EAAA,MAAAH,IAAA,GAAAC,OAAA;EAAAE,OAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAI,SAAA;EAAA,MAAAJ,IAAA,GAAAK,sBAAA,CAAAJ,OAAA;EAAAG,QAAA,YAAAA,CAAA;IAAA,OAAAJ,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAM,QAAA;EAAA,MAAAN,IAAA,GAAAC,OAAA;EAAAK,OAAA,YAAAA,CAAA;IAAA,OAAAN,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAO,YAAA;EAAA,MAAAP,IAAA,GAAAK,sBAAA,CAAAJ,OAAA;EAAAM,WAAA,YAAAA,CAAA;IAAA,OAAAP,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAQ,UAAA;EAAA,MAAAR,IAAA,GAAAC,OAAA;EAAAO,SAAA,YAAAA,CAAA;IAAA,OAAAR,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAS,aAAA;EAAA,MAAAT,IAAA,GAAAC,OAAA;EAAAQ,YAAA,Y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ED,eAAI,CAACC,QAAQ,CAAuB,CAAC,CAAC;AAAA,IAAAJ,gBAAA,GAAAC,OAAA,EAjIrE9E,WAAW,kBAkIA,CAACkF,sBAAY,CAAC;AAAA,IAAAL,gBAAA,GAAAC,OAAA,EAlIzB9E,WAAW,mBAoIC;EACrBO,IAAI,EAAE,IAAI;EACVS,SAAS,EAAE,KAAK;EAChBuB,gBAAgB,EAAE,CAAC,cAAc;AACnC,CAAC;AAYHtB,yBAAa,CAACkE,UAAU,CAACnF,WAAW,CAAC"}
1
+ {"version":3,"names":["_cli","data","require","_harmony","_logger","_express","_interopRequireDefault","_lodash","_bodyParser","_express2","_middlewares","_types","ExpressMain","constructor","config","moduleSlot","logger","middlewareSlot","listen","port","serverPort","app","createApp","register","routes","registerMiddleware","middlewares","createRootRoutes","namespace","ExpressAspect","id","method","path","disableNamespace","priority","req","res","send","expressApp","options","internalRoutes","createRoutes","allRoutes","concat","sortedRoutes","sortBy","r","reverse","express","use","next","loggerIgnorePath","includes","url","debug","headers","disableBodyParser","bodyParser","middlewaresSlot","values","flat","forEach","route","middleware","routeInfo","catchErrorsMiddlewares","routesSlots","toArray","routeEntries","map","flatten","verbValidation","lowerCase","verb","Verb","READ","status","jsonp","message","error","catchErrors","json","limit","raw","type","provider","loggerFactory","routeSlot","createLogger","exports","_defineProperty2","default","MainRuntime","Slot","withType","LoggerAspect","addRuntime"],"sources":["express.main.runtime.ts"],"sourcesContent":["import { MainRuntime } from '@teambit/cli';\nimport { Slot, SlotRegistry } from '@teambit/harmony';\nimport { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';\nimport express, { Express } from 'express';\nimport { concat, flatten, lowerCase, sortBy } from 'lodash';\nimport bodyParser from 'body-parser';\nimport { ExpressAspect } from './express.aspect';\nimport { catchErrors } from './middlewares';\nimport { Middleware, Request, Response, Route, Verb } from './types';\nimport { MiddlewareManifest } from './middleware-manifest';\n\nexport type ExpressConfig = {\n port: number;\n namespace: string;\n loggerIgnorePath: string[];\n};\n\nexport type MiddlewareSlot = SlotRegistry<MiddlewareManifest[]>;\n\nexport type RouteSlot = SlotRegistry<Route[]>;\n\nexport class ExpressMain {\n static runtime = MainRuntime;\n\n constructor(\n /**\n * extension config\n */\n readonly config: ExpressConfig,\n\n /**\n * slot for registering graphql modules\n */\n private moduleSlot: RouteSlot,\n\n /**\n * logger extension.\n */\n readonly logger: Logger,\n\n readonly middlewareSlot: MiddlewareSlot\n ) {}\n\n /**\n * start a express server.\n */\n async listen(port?: number) {\n const serverPort = port || this.config.port;\n const app = this.createApp();\n app.listen(serverPort);\n }\n\n /**\n * register a new express routes.\n * route will be added as `/api/${route}`\n */\n register(routes: Route[]) {\n this.moduleSlot.register(routes);\n return this;\n }\n\n /**\n * register a new middleware into express.\n */\n registerMiddleware(middlewares: MiddlewareManifest[]) {\n this.middlewareSlot.register(middlewares);\n return this;\n }\n\n private createRootRoutes() {\n // TODO: @guy refactor health to service aspect.\n return [\n {\n namespace: ExpressAspect.id,\n method: 'get',\n path: '/_health',\n disableNamespace: false,\n priority: 0,\n middlewares: [async (req: Request, res: Response) => res.send('ok')],\n },\n ];\n }\n\n createApp(expressApp?: Express, options?: { disableBodyParser: true }): Express {\n const internalRoutes = this.createRootRoutes();\n const routes = this.createRoutes();\n const allRoutes = concat(routes, internalRoutes);\n const sortedRoutes = sortBy(allRoutes, (r) => r.priority).reverse();\n const app = expressApp || express();\n app.use((req, res, next) => {\n if (this.config.loggerIgnorePath.includes(req.url)) return next();\n this.logger.debug(`express got a request to a URL: ${req.url}', headers:`, req.headers);\n return next();\n });\n if (!options?.disableBodyParser) this.bodyParser(app);\n\n const middlewaresSlot = this.middlewareSlot.values().flat();\n middlewaresSlot.forEach(({ route, middleware }) => {\n if (!route) app.use(middleware);\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n if (route) app.use(route, middleware);\n });\n\n sortedRoutes.forEach((routeInfo) => {\n const { method, path, middlewares, disableNamespace } = routeInfo;\n // TODO: @guy make sure to support single middleware here.\n const namespace = disableNamespace ? '' : `/${this.config.namespace}`;\n app[method](`${namespace}${path}`, this.catchErrorsMiddlewares(middlewares));\n });\n\n return app;\n }\n\n private createRoutes() {\n const routesSlots = this.moduleSlot.toArray();\n const routeEntries = routesSlots.map(([, routes]) => {\n return routes.map((route) => {\n const middlewares = flatten([this.verbValidation(route), route.middlewares]);\n return {\n method: lowerCase(route.method),\n path: route.route,\n disableNamespace: route.disableNamespace,\n middlewares,\n priority: route.priority || 0,\n };\n });\n });\n\n return flatten(routeEntries);\n }\n\n private verbValidation(route: Route): Middleware {\n return async (req: express.Request, res: express.Response, next: express.NextFunction) => {\n if (!route.verb) return next();\n const verb = req.headers['x-verb'] || Verb.READ;\n if (verb !== route.verb) {\n res.status(403);\n return res.jsonp({ message: 'You are not authorized', error: 'forbidden' });\n }\n return next();\n };\n }\n\n private catchErrorsMiddlewares(middlewares: Middleware[]) {\n return middlewares.map((middleware) => catchErrors(middleware));\n }\n\n private bodyParser(app: Express) {\n app.use(bodyParser.json({ limit: '5000mb' }));\n app.use(bodyParser.raw({ type: 'application/octet-stream', limit: '5000mb' }));\n }\n\n static slots = [Slot.withType<Route[]>(), Slot.withType<MiddlewareManifest[]>()];\n static dependencies = [LoggerAspect];\n\n static defaultConfig = {\n port: 4001,\n namespace: 'api',\n loggerIgnorePath: ['/api/_health'],\n };\n\n static async provider(\n [loggerFactory]: [LoggerMain],\n config: ExpressConfig,\n [routeSlot, middlewareSlot]: [RouteSlot, MiddlewareSlot]\n ) {\n const logger = loggerFactory.createLogger(ExpressAspect.id);\n return new ExpressMain(config, routeSlot, logger, middlewareSlot);\n }\n}\n\nExpressAspect.addRuntime(ExpressMain);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAAA,KAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,IAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAE,SAAA;EAAA,MAAAF,IAAA,GAAA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C;AAYHtB,yBAAa,CAACmE,UAAU,CAACpF,WAAW,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { RouteSlot } from './express.main.runtime';
2
2
  export { Route, Verb } from './types';
3
- export { Request, Response, NextFunction } from './types';
3
+ export { Request, Response, NextFunction, Middleware } from './types';
4
+ export type { MiddlewareManifest } from './middleware-manifest';
4
5
  export type { ExpressMain } from './express.main.runtime';
5
6
  export { ExpressAspect } from './express.aspect';
package/dist/index.js CHANGED
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "ExpressAspect", {
9
9
  return _express().ExpressAspect;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "Middleware", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _types().Middleware;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "NextFunction", {
13
19
  enumerable: true,
14
20
  get: function () {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_expressMain","data","require","_types","_express"],"sources":["index.ts"],"sourcesContent":["export { RouteSlot } from './express.main.runtime';\nexport { Route, Verb } from './types';\nexport { Request, Response, NextFunction } from './types';\nexport type { ExpressMain } from './express.main.runtime';\nexport { ExpressAspect } from './express.aspect';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAAA,aAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,YAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAE,OAAA;EAAA,MAAAF,IAAA,GAAAC,OAAA;EAAAC,MAAA,YAAAA,CAAA;IAAA,OAAAF,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,SAAAG,SAAA;EAAA,MAAAH,IAAA,GAAAC,OAAA;EAAAE,QAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA"}
1
+ {"version":3,"names":["_expressMain","data","require","_types","_express"],"sources":["index.ts"],"sourcesContent":["export { RouteSlot } from './express.main.runtime';\nexport { Route, Verb } from './types';\nexport { Request, Response, NextFunction, Middleware } from './types';\nexport type { MiddlewareManifest } from './middleware-manifest';\nexport type { ExpressMain } from './express.main.runtime';\nexport { ExpressAspect } from './express.aspect';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAAA,aAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,YAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAE,OAAA;EAAA,MAAAF,IAAA,GAAAC,OAAA;EAAAC,MAAA,YAAAA,CAAA;IAAA,OAAAF,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAIA,SAAAG,SAAA;EAAA,MAAAH,IAAA,GAAAC,OAAA;EAAAE,QAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA"}
@@ -1,4 +1,5 @@
1
1
  import { Middleware } from './types';
2
2
  export interface MiddlewareManifest {
3
+ route?: string;
3
4
  middleware: Middleware;
4
5
  }
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["middleware-manifest.ts"],"sourcesContent":["import { Middleware } from './types';\n\nexport interface MiddlewareManifest {\n middleware: Middleware;\n}\n"],"mappings":""}
1
+ {"version":3,"names":[],"sources":["middleware-manifest.ts"],"sourcesContent":["import { Middleware } from './types';\n\nexport interface MiddlewareManifest {\n route?: string;\n middleware: Middleware;\n}\n"],"mappings":""}
@@ -1,5 +1,5 @@
1
- import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.864/dist/express.composition.js';
2
- import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.864/dist/express.docs.mdx';
1
+ import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.865/dist/express.composition.js';
2
+ import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.865/dist/express.docs.mdx';
3
3
 
4
4
  export const compositions = [compositions_0];
5
5
  export const overview = [overview_0];
@@ -5,7 +5,7 @@ import { Response } from './response';
5
5
  /**
6
6
  * define express Middleware
7
7
  */
8
- export declare type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;
8
+ export declare type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<any>;
9
9
  export declare enum Verb {
10
10
  WRITE = "write",
11
11
  READ = "read"
@@ -1 +1 @@
1
- {"version":3,"names":["Verb","exports"],"sources":["route.ts"],"sourcesContent":["import { NextFunction } from './next';\nimport { Request } from './request';\nimport { Response } from './response';\n\n/**\n * define express Middleware\n */\nexport type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;\n\nexport enum Verb {\n WRITE = 'write',\n READ = 'read',\n}\n\n/**\n * express new Route\n */\n\nexport interface Route {\n method: string;\n route: string | RegExp;\n disableNamespace?: boolean;\n verb?: Verb;\n middlewares: Middleware[];\n /** route priority if 2 route with the same name default is 0 */\n priority?: number;\n}\n"],"mappings":";;;;;;AAIA;AACA;AACA;AAFA,IAKYA,IAAI,0BAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAA,OAAJA,IAAI;AAAA;AAKhB;AACA;AACA;AAFAC,OAAA,CAAAD,IAAA,GAAAA,IAAA"}
1
+ {"version":3,"names":["Verb","exports"],"sources":["route.ts"],"sourcesContent":["import { NextFunction } from './next';\nimport { Request } from './request';\nimport { Response } from './response';\n\n/**\n * define express Middleware\n */\nexport type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<any>;\n\nexport enum Verb {\n WRITE = 'write',\n READ = 'read',\n}\n\n/**\n * express new Route\n */\n\nexport interface Route {\n method: string;\n route: string | RegExp;\n disableNamespace?: boolean;\n verb?: Verb;\n middlewares: Middleware[];\n /** route priority if 2 route with the same name default is 0 */\n priority?: number;\n}\n"],"mappings":";;;;;;AAIA;AACA;AACA;AAFA,IAKYA,IAAI,0BAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAA,OAAJA,IAAI;AAAA;AAKhB;AACA;AACA;AAFAC,OAAA,CAAAD,IAAA,GAAAA,IAAA"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/express",
3
- "version": "0.0.864",
3
+ "version": "0.0.865",
4
4
  "homepage": "https://bit.cloud/teambit/harmony/express",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.harmony",
8
8
  "name": "express",
9
- "version": "0.0.864"
9
+ "version": "0.0.865"
10
10
  },
11
11
  "dependencies": {
12
12
  "body-parser": "1.19.0",
package/types/route.ts CHANGED
@@ -5,7 +5,7 @@ import { Response } from './response';
5
5
  /**
6
6
  * define express Middleware
7
7
  */
8
- export type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;
8
+ export type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<any>;
9
9
 
10
10
  export enum Verb {
11
11
  WRITE = 'write',