@teambit/express 0.0.939 → 0.0.940
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["teambit.harmony/express-preview"]=t():e["teambit.harmony/express-preview"]=t()}(self,(()=>(()=>{"use strict";var e={83874:(e,t,o)=>{var r={id:"teambit.harmony/express@0.0.940",homepage:"https://bit.cloud/teambit/harmony/express",exported:!0};function n(){const e=i(o(87363));return n=function(){return e},e}function i(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.Logo=void 0,n.__bit_component=r,i.__bit_component=r;const s=()=>n().default.createElement("div",{style:{height:"100%",display:"flex",justifyContent:"center"}},n().default.createElement("img",{style:{width:70},src:"https://static.bit.dev/extensions-icons/express.svg"}));s.__bit_component=r,t.Logo=s},87363:e=>{e.exports=React}},t={};function o(r){var n=t[r];if(void 0!==n)return n.exports;var i=t[r]={exports:{}};return e[r](i,i.exports,o),i.exports}o.d=(e,t)=>{for(var r in t)o.o(t,r)&&!o.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};return(()=>{o.r(r),o.d(r,{compositions:()=>u,compositions_metadata:()=>l,overview:()=>f});var e={};o.r(e),o.d(e,{default:()=>d});var t=o(83874);o(87363);const n=MdxJsReact,i=TeambitMdxUiMdxScopeContext;var s=["components"];function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r])}return e},p.apply(this,arguments)}var a={},c="wrapper";function d(e){var t=e.components,o=function(e,t){if(null==e)return{};var o,r,n=function(e,t){if(null==e)return{};var o,r,n={},i=Object.keys(e);for(r=0;r<i.length;r++)o=i[r],t.indexOf(o)>=0||(n[o]=e[o]);return n}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)o=i[r],t.indexOf(o)>=0||Object.prototype.propertyIsEnumerable.call(e,o)&&(n[o]=e[o])}return n}(e,s);return(0,n.mdx)(c,p({},a,o,{components:t,mdxType:"MDXLayout"}),(0,n.mdx)(i.MDXScopeProvider,{components:{},mdxType:"MDXScopeProvider"}))}d.isMDXComponent=!0;const u=[t],f=[e],l={compositions:[{displayName:"Logo",identifier:"Logo"}]}})(),r})()));
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.
|
2
|
-
import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.
|
1
|
+
import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.940/dist/express.composition.js';
|
2
|
+
import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.harmony_express@0.0.940/dist/express.docs.mdx';
|
3
3
|
|
4
4
|
export const compositions = [compositions_0];
|
5
5
|
export const overview = [overview_0];
|
package/package.json
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
{
|
2
2
|
"name": "@teambit/express",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.940",
|
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.
|
9
|
+
"version": "0.0.940"
|
10
10
|
},
|
11
11
|
"dependencies": {
|
12
12
|
"body-parser": "1.19.0",
|
13
13
|
"express": "4.17.1",
|
14
14
|
"lodash": "4.17.21",
|
15
15
|
"@teambit/harmony": "0.4.6",
|
16
|
-
"@teambit/cli": "0.0.
|
17
|
-
"@teambit/logger": "0.0.
|
16
|
+
"@teambit/cli": "0.0.841",
|
17
|
+
"@teambit/logger": "0.0.934"
|
18
18
|
},
|
19
19
|
"devDependencies": {
|
20
20
|
"@types/express": "4.17.13",
|
@@ -22,7 +22,7 @@
|
|
22
22
|
"@types/mocha": "9.1.0",
|
23
23
|
"@types/jest": "^29.2.2",
|
24
24
|
"@types/testing-library__jest-dom": "^5.9.5",
|
25
|
-
"@teambit/harmony.envs.core-aspect-env": "0.0.
|
25
|
+
"@teambit/harmony.envs.core-aspect-env": "0.0.14"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
28
|
"react": "^17.0.0 || ^18.0.0",
|
package/express.aspect.ts
DELETED
package/express.main.runtime.ts
DELETED
@@ -1,174 +0,0 @@
|
|
1
|
-
import { MainRuntime } from '@teambit/cli';
|
2
|
-
import { Server } from 'http';
|
3
|
-
import { Slot, SlotRegistry } from '@teambit/harmony';
|
4
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
5
|
-
import express, { Express } from 'express';
|
6
|
-
import { concat, flatten, lowerCase, sortBy } from 'lodash';
|
7
|
-
import bodyParser from 'body-parser';
|
8
|
-
import { ExpressAspect } from './express.aspect';
|
9
|
-
import { catchErrors } from './middlewares';
|
10
|
-
import { Middleware, Request, Response, Route, Verb } from './types';
|
11
|
-
import { MiddlewareManifest } from './middleware-manifest';
|
12
|
-
|
13
|
-
export type ExpressConfig = {
|
14
|
-
port: number;
|
15
|
-
namespace: string;
|
16
|
-
loggerIgnorePath: string[];
|
17
|
-
};
|
18
|
-
|
19
|
-
export type MiddlewareSlot = SlotRegistry<MiddlewareManifest[]>;
|
20
|
-
|
21
|
-
export type RouteSlot = SlotRegistry<Route[]>;
|
22
|
-
|
23
|
-
export class ExpressMain {
|
24
|
-
static runtime = MainRuntime;
|
25
|
-
|
26
|
-
constructor(
|
27
|
-
/**
|
28
|
-
* extension config
|
29
|
-
*/
|
30
|
-
readonly config: ExpressConfig,
|
31
|
-
|
32
|
-
/**
|
33
|
-
* slot for registering graphql modules
|
34
|
-
*/
|
35
|
-
private moduleSlot: RouteSlot,
|
36
|
-
|
37
|
-
/**
|
38
|
-
* logger extension.
|
39
|
-
*/
|
40
|
-
readonly logger: Logger,
|
41
|
-
|
42
|
-
readonly middlewareSlot: MiddlewareSlot
|
43
|
-
) {}
|
44
|
-
|
45
|
-
/**
|
46
|
-
* start an express server.
|
47
|
-
*/
|
48
|
-
async listen(port?: number): Promise<Server> {
|
49
|
-
const serverPort = port || this.config.port;
|
50
|
-
const app = this.createApp();
|
51
|
-
return app.listen(serverPort);
|
52
|
-
}
|
53
|
-
|
54
|
-
/**
|
55
|
-
* register a new express routes.
|
56
|
-
* route will be added as `/api/${route}`
|
57
|
-
*/
|
58
|
-
register(routes: Route[]) {
|
59
|
-
this.moduleSlot.register(routes);
|
60
|
-
return this;
|
61
|
-
}
|
62
|
-
|
63
|
-
/**
|
64
|
-
* register a new middleware into express.
|
65
|
-
*/
|
66
|
-
registerMiddleware(middlewares: MiddlewareManifest[]) {
|
67
|
-
this.middlewareSlot.register(middlewares);
|
68
|
-
return this;
|
69
|
-
}
|
70
|
-
|
71
|
-
private createRootRoutes() {
|
72
|
-
// TODO: @guy refactor health to service aspect.
|
73
|
-
return [
|
74
|
-
{
|
75
|
-
namespace: ExpressAspect.id,
|
76
|
-
method: 'get',
|
77
|
-
path: '/_health',
|
78
|
-
disableNamespace: false,
|
79
|
-
priority: 0,
|
80
|
-
middlewares: [async (req: Request, res: Response) => res.send('ok')],
|
81
|
-
},
|
82
|
-
];
|
83
|
-
}
|
84
|
-
|
85
|
-
createApp(expressApp?: Express, options?: { disableBodyParser: true }): Express {
|
86
|
-
const internalRoutes = this.createRootRoutes();
|
87
|
-
const routes = this.createRoutes();
|
88
|
-
const allRoutes = concat(routes, internalRoutes);
|
89
|
-
const sortedRoutes = sortBy(allRoutes, (r) => r.priority).reverse();
|
90
|
-
const app = expressApp || express();
|
91
|
-
app.use((req, res, next) => {
|
92
|
-
if (this.config.loggerIgnorePath.includes(req.url)) return next();
|
93
|
-
this.logger.debug(`express got a request to a URL: ${req.url}', headers:`, req.headers);
|
94
|
-
return next();
|
95
|
-
});
|
96
|
-
if (!options?.disableBodyParser) this.bodyParser(app);
|
97
|
-
|
98
|
-
const middlewaresSlot = this.middlewareSlot.values().flat();
|
99
|
-
middlewaresSlot.forEach(({ route, middleware }) => {
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
101
|
-
if (!route) app.use(middleware);
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
103
|
-
if (route) app.use(route, middleware);
|
104
|
-
});
|
105
|
-
|
106
|
-
sortedRoutes.forEach((routeInfo) => {
|
107
|
-
const { method, path, middlewares, disableNamespace } = routeInfo;
|
108
|
-
// TODO: @guy make sure to support single middleware here.
|
109
|
-
const namespace = disableNamespace ? '' : `/${this.config.namespace}`;
|
110
|
-
app[method](`${namespace}${path}`, this.catchErrorsMiddlewares(middlewares));
|
111
|
-
});
|
112
|
-
|
113
|
-
return app;
|
114
|
-
}
|
115
|
-
|
116
|
-
private createRoutes() {
|
117
|
-
const routesSlots = this.moduleSlot.toArray();
|
118
|
-
const routeEntries = routesSlots.map(([, routes]) => {
|
119
|
-
return routes.map((route) => {
|
120
|
-
const middlewares = flatten([this.verbValidation(route), route.middlewares]);
|
121
|
-
return {
|
122
|
-
method: lowerCase(route.method),
|
123
|
-
path: route.route,
|
124
|
-
disableNamespace: route.disableNamespace,
|
125
|
-
middlewares,
|
126
|
-
priority: route.priority || 0,
|
127
|
-
};
|
128
|
-
});
|
129
|
-
});
|
130
|
-
|
131
|
-
return flatten(routeEntries);
|
132
|
-
}
|
133
|
-
|
134
|
-
private verbValidation(route: Route): Middleware {
|
135
|
-
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
136
|
-
if (!route.verb) return next();
|
137
|
-
const verb = req.headers['x-verb'] || Verb.READ;
|
138
|
-
if (verb !== route.verb) {
|
139
|
-
res.status(403);
|
140
|
-
return res.jsonp({ message: 'You are not authorized', error: 'forbidden' });
|
141
|
-
}
|
142
|
-
return next();
|
143
|
-
};
|
144
|
-
}
|
145
|
-
|
146
|
-
private catchErrorsMiddlewares(middlewares: Middleware[]) {
|
147
|
-
return middlewares.map((middleware) => catchErrors(middleware));
|
148
|
-
}
|
149
|
-
|
150
|
-
private bodyParser(app: Express) {
|
151
|
-
app.use(bodyParser.json({ limit: '5000mb' }));
|
152
|
-
app.use(bodyParser.raw({ type: 'application/octet-stream', limit: '5000mb' }));
|
153
|
-
}
|
154
|
-
|
155
|
-
static slots = [Slot.withType<Route[]>(), Slot.withType<MiddlewareManifest[]>()];
|
156
|
-
static dependencies = [LoggerAspect];
|
157
|
-
|
158
|
-
static defaultConfig = {
|
159
|
-
port: 4001,
|
160
|
-
namespace: 'api',
|
161
|
-
loggerIgnorePath: ['/api/_health'],
|
162
|
-
};
|
163
|
-
|
164
|
-
static async provider(
|
165
|
-
[loggerFactory]: [LoggerMain],
|
166
|
-
config: ExpressConfig,
|
167
|
-
[routeSlot, middlewareSlot]: [RouteSlot, MiddlewareSlot]
|
168
|
-
) {
|
169
|
-
const logger = loggerFactory.createLogger(ExpressAspect.id);
|
170
|
-
return new ExpressMain(config, routeSlot, logger, middlewareSlot);
|
171
|
-
}
|
172
|
-
}
|
173
|
-
|
174
|
-
ExpressAspect.addRuntime(ExpressMain);
|
package/index.ts
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
export type { RouteSlot } from './express.main.runtime';
|
2
|
-
export type { Route } from './types';
|
3
|
-
export { Verb } from './types';
|
4
|
-
export type { Request, Response, NextFunction, Middleware } from './types';
|
5
|
-
export type { MiddlewareManifest } from './middleware-manifest';
|
6
|
-
export type { ExpressMain } from './express.main.runtime';
|
7
|
-
export { ExpressAspect } from './express.aspect';
|