@teambit/express 0.0.474 → 0.0.478
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/express.main.runtime.d.ts +32 -1
- package/dist/types/route.d.ts +6 -0
- package/package.json +21 -6
- package/tsconfig.json +1 -2
- package/express.aspect.ts +0 -7
- package/express.main.runtime.ts +0 -164
- package/index.ts +0 -5
- package/middleware-manifest.ts +0 -5
- package/middlewares/error.ts +0 -29
- package/middlewares/index.ts +0 -1
- package/package-tar/teambit-express-0.0.474.tgz +0 -0
- package/types/asset.d.ts +0 -29
- package/types/index.ts +0 -4
- package/types/next.ts +0 -3
- package/types/request.ts +0 -1
- package/types/response.ts +0 -3
- package/types/route.ts +0 -24
- package/types/style.d.ts +0 -42
|
@@ -11,14 +11,45 @@ export declare type ExpressConfig = {
|
|
|
11
11
|
export declare type MiddlewareSlot = SlotRegistry<MiddlewareManifest[]>;
|
|
12
12
|
export declare type RouteSlot = SlotRegistry<Route[]>;
|
|
13
13
|
export declare class ExpressMain {
|
|
14
|
+
/**
|
|
15
|
+
* extension config
|
|
16
|
+
*/
|
|
14
17
|
readonly config: ExpressConfig;
|
|
18
|
+
/**
|
|
19
|
+
* slot for registering graphql modules
|
|
20
|
+
*/
|
|
15
21
|
private moduleSlot;
|
|
22
|
+
/**
|
|
23
|
+
* logger extension.
|
|
24
|
+
*/
|
|
16
25
|
readonly logger: Logger;
|
|
17
26
|
readonly middlewareSlot: MiddlewareSlot;
|
|
18
27
|
static runtime: import("@teambit/harmony").RuntimeDefinition;
|
|
19
|
-
constructor(
|
|
28
|
+
constructor(
|
|
29
|
+
/**
|
|
30
|
+
* extension config
|
|
31
|
+
*/
|
|
32
|
+
config: ExpressConfig,
|
|
33
|
+
/**
|
|
34
|
+
* slot for registering graphql modules
|
|
35
|
+
*/
|
|
36
|
+
moduleSlot: RouteSlot,
|
|
37
|
+
/**
|
|
38
|
+
* logger extension.
|
|
39
|
+
*/
|
|
40
|
+
logger: Logger, middlewareSlot: MiddlewareSlot);
|
|
41
|
+
/**
|
|
42
|
+
* start a express server.
|
|
43
|
+
*/
|
|
20
44
|
listen(port?: number): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* register a new express routes.
|
|
47
|
+
* route will be added as `/api/${route}`
|
|
48
|
+
*/
|
|
21
49
|
register(routes: Route[]): this;
|
|
50
|
+
/**
|
|
51
|
+
* register a new middleware into express.
|
|
52
|
+
*/
|
|
22
53
|
registerMiddleware(middlewares: MiddlewareManifest[]): this;
|
|
23
54
|
private createRootRoutes;
|
|
24
55
|
createApp(expressApp?: Express, options?: {
|
package/dist/types/route.d.ts
CHANGED
|
@@ -2,11 +2,17 @@
|
|
|
2
2
|
import { NextFunction } from './next';
|
|
3
3
|
import { Request } from './request';
|
|
4
4
|
import { Response } from './response';
|
|
5
|
+
/**
|
|
6
|
+
* define express Middleware
|
|
7
|
+
*/
|
|
5
8
|
export declare type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
6
9
|
export declare enum Verb {
|
|
7
10
|
WRITE = "write",
|
|
8
11
|
READ = "read"
|
|
9
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* express new Route
|
|
15
|
+
*/
|
|
10
16
|
export interface Route {
|
|
11
17
|
method: string;
|
|
12
18
|
route: string | RegExp;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.478",
|
|
4
4
|
"homepage": "https://bit.dev/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.478"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@teambit/harmony": "0.2.11",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"lodash": "4.17.21",
|
|
16
16
|
"@babel/runtime": "7.12.18",
|
|
17
17
|
"core-js": "^3.0.0",
|
|
18
|
-
"@teambit/cli": "0.0.
|
|
19
|
-
"@teambit/logger": "0.0.
|
|
18
|
+
"@teambit/cli": "0.0.389",
|
|
19
|
+
"@teambit/logger": "0.0.474"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/express": "4.17.9",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@types/node": "12.20.4"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@teambit/legacy": "1.0.
|
|
32
|
+
"@teambit/legacy": "1.0.175",
|
|
33
33
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
34
34
|
"react": "^16.8.0 || ^17.0.0"
|
|
35
35
|
},
|
|
@@ -57,12 +57,27 @@
|
|
|
57
57
|
"react": "-"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@teambit/legacy": "1.0.
|
|
60
|
+
"@teambit/legacy": "1.0.175",
|
|
61
61
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
62
62
|
"react": "^16.8.0 || ^17.0.0"
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
|
+
"files": [
|
|
67
|
+
"dist",
|
|
68
|
+
"!dist/tsconfig.tsbuildinfo",
|
|
69
|
+
"**/*.md",
|
|
70
|
+
"**/*.mdx",
|
|
71
|
+
"**/*.js",
|
|
72
|
+
"**/*.json",
|
|
73
|
+
"**/*.sass",
|
|
74
|
+
"**/*.scss",
|
|
75
|
+
"**/*.less",
|
|
76
|
+
"**/*.css",
|
|
77
|
+
"**/*.css",
|
|
78
|
+
"**/*.jpeg",
|
|
79
|
+
"**/*.gif"
|
|
80
|
+
],
|
|
66
81
|
"private": false,
|
|
67
82
|
"engines": {
|
|
68
83
|
"node": ">=12.22.0"
|
package/tsconfig.json
CHANGED
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"skipLibCheck": true,
|
|
16
16
|
"moduleResolution": "node",
|
|
17
17
|
"esModuleInterop": true,
|
|
18
|
-
"outDir": "dist",
|
|
19
18
|
"composite": true,
|
|
20
19
|
"emitDeclarationOnly": true,
|
|
20
|
+
"outDir": "dist",
|
|
21
21
|
"experimentalDecorators": true,
|
|
22
22
|
"emitDecoratorMetadata": true,
|
|
23
23
|
"allowSyntheticDefaultImports": true,
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"strict": true,
|
|
26
26
|
"noImplicitAny": false,
|
|
27
27
|
"rootDir": ".",
|
|
28
|
-
"removeComments": true,
|
|
29
28
|
"preserveConstEnums": true,
|
|
30
29
|
"resolveJsonModule": true
|
|
31
30
|
},
|
package/express.aspect.ts
DELETED
package/express.main.runtime.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { MainRuntime } from '@teambit/cli';
|
|
2
|
-
import { Slot, SlotRegistry } from '@teambit/harmony';
|
|
3
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
|
4
|
-
import express, { Express } from 'express';
|
|
5
|
-
import { concat, flatten, lowerCase } from 'lodash';
|
|
6
|
-
import bodyParser from 'body-parser';
|
|
7
|
-
import { ExpressAspect } from './express.aspect';
|
|
8
|
-
import { catchErrors } from './middlewares';
|
|
9
|
-
import { Middleware, Request, Response, Route, Verb } from './types';
|
|
10
|
-
import { MiddlewareManifest } from './middleware-manifest';
|
|
11
|
-
|
|
12
|
-
export type ExpressConfig = {
|
|
13
|
-
port: number;
|
|
14
|
-
namespace: string;
|
|
15
|
-
loggerIgnorePath: string[];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type MiddlewareSlot = SlotRegistry<MiddlewareManifest[]>;
|
|
19
|
-
|
|
20
|
-
export type RouteSlot = SlotRegistry<Route[]>;
|
|
21
|
-
|
|
22
|
-
export class ExpressMain {
|
|
23
|
-
static runtime = MainRuntime;
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
/**
|
|
27
|
-
* extension config
|
|
28
|
-
*/
|
|
29
|
-
readonly config: ExpressConfig,
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* slot for registering graphql modules
|
|
33
|
-
*/
|
|
34
|
-
private moduleSlot: RouteSlot,
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* logger extension.
|
|
38
|
-
*/
|
|
39
|
-
readonly logger: Logger,
|
|
40
|
-
|
|
41
|
-
readonly middlewareSlot: MiddlewareSlot
|
|
42
|
-
) {}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* start a express server.
|
|
46
|
-
*/
|
|
47
|
-
async listen(port?: number) {
|
|
48
|
-
const serverPort = port || this.config.port;
|
|
49
|
-
const app = this.createApp();
|
|
50
|
-
app.listen(serverPort);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* register a new express routes.
|
|
55
|
-
* route will be added as `/api/${route}`
|
|
56
|
-
*/
|
|
57
|
-
register(routes: Route[]) {
|
|
58
|
-
this.moduleSlot.register(routes);
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* register a new middleware into express.
|
|
64
|
-
*/
|
|
65
|
-
registerMiddleware(middlewares: MiddlewareManifest[]) {
|
|
66
|
-
this.middlewareSlot.register(middlewares);
|
|
67
|
-
return this;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private createRootRoutes() {
|
|
71
|
-
// TODO: @guy refactor health to service aspect.
|
|
72
|
-
return [
|
|
73
|
-
{
|
|
74
|
-
namespace: ExpressAspect.id,
|
|
75
|
-
method: 'get',
|
|
76
|
-
path: '/_health',
|
|
77
|
-
middlewares: [async (req: Request, res: Response) => res.send('ok')],
|
|
78
|
-
},
|
|
79
|
-
];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
createApp(expressApp?: Express, options?: { disableBodyParser: true }): Express {
|
|
83
|
-
const internalRoutes = this.createRootRoutes();
|
|
84
|
-
const routes = this.createRoutes();
|
|
85
|
-
const allRoutes = concat(routes, internalRoutes);
|
|
86
|
-
const app = expressApp || express();
|
|
87
|
-
app.use((req, res, next) => {
|
|
88
|
-
if (this.config.loggerIgnorePath.includes(req.url)) return next();
|
|
89
|
-
this.logger.debug(`express got a request to a URL: ${req.url}', headers:`, req.headers);
|
|
90
|
-
return next();
|
|
91
|
-
});
|
|
92
|
-
if (!options?.disableBodyParser) this.bodyParser(app);
|
|
93
|
-
|
|
94
|
-
this.middlewareSlot
|
|
95
|
-
.toArray()
|
|
96
|
-
.flatMap(([, middlewares]) =>
|
|
97
|
-
middlewares.flatMap((middlewareManifest) => app.use(middlewareManifest.middleware))
|
|
98
|
-
);
|
|
99
|
-
allRoutes.forEach((routeInfo) => {
|
|
100
|
-
const { method, path, middlewares } = routeInfo;
|
|
101
|
-
// TODO: @guy make sure to support single middleware here.
|
|
102
|
-
app[method](`/${this.config.namespace}${path}`, this.catchErrorsMiddlewares(middlewares));
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
return app;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private createRoutes() {
|
|
109
|
-
const routesSlots = this.moduleSlot.toArray();
|
|
110
|
-
const routeEntries = routesSlots.map(([, routes]) => {
|
|
111
|
-
return routes.map((route) => {
|
|
112
|
-
const middlewares = flatten([this.verbValidation(route), route.middlewares]);
|
|
113
|
-
return {
|
|
114
|
-
method: lowerCase(route.method),
|
|
115
|
-
path: route.route,
|
|
116
|
-
middlewares,
|
|
117
|
-
};
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
return flatten(routeEntries);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private verbValidation(route: Route): Middleware {
|
|
125
|
-
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
126
|
-
if (!route.verb) return next();
|
|
127
|
-
const verb = req.headers['x-verb'] || Verb.READ;
|
|
128
|
-
if (verb !== route.verb) {
|
|
129
|
-
res.status(403);
|
|
130
|
-
return res.jsonp({ message: 'You are not authorized', error: 'forbidden' });
|
|
131
|
-
}
|
|
132
|
-
return next();
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private catchErrorsMiddlewares(middlewares: Middleware[]) {
|
|
137
|
-
return middlewares.map((middleware) => catchErrors(middleware));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private bodyParser(app: Express) {
|
|
141
|
-
app.use(bodyParser.json({ limit: '5000mb' }));
|
|
142
|
-
app.use(bodyParser.raw({ type: 'application/octet-stream', limit: '5000mb' }));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
static slots = [Slot.withType<Route[]>(), Slot.withType<MiddlewareManifest[]>()];
|
|
146
|
-
static dependencies = [LoggerAspect];
|
|
147
|
-
|
|
148
|
-
static defaultConfig = {
|
|
149
|
-
port: 4001,
|
|
150
|
-
namespace: 'api',
|
|
151
|
-
loggerIgnorePath: ['/api/_health'],
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
static async provider(
|
|
155
|
-
[loggerFactory]: [LoggerMain],
|
|
156
|
-
config: ExpressConfig,
|
|
157
|
-
[routeSlot, middlewareSlot]: [RouteSlot, MiddlewareSlot]
|
|
158
|
-
) {
|
|
159
|
-
const logger = loggerFactory.createLogger(ExpressAspect.id);
|
|
160
|
-
return new ExpressMain(config, routeSlot, logger, middlewareSlot);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
ExpressAspect.addRuntime(ExpressMain);
|
package/index.ts
DELETED
package/middleware-manifest.ts
DELETED
package/middlewares/error.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import * as express from 'express';
|
|
2
|
-
import logger from '@teambit/legacy/dist/logger/logger';
|
|
3
|
-
|
|
4
|
-
interface ResponseError {
|
|
5
|
-
status?: number;
|
|
6
|
-
message?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const catchErrors = (action: any) => (req: express.Request, res: express.Response, next: express.NextFunction) =>
|
|
10
|
-
// TODO: @guy please take care of it
|
|
11
|
-
// eslint-disable-next-line promise/no-callback-in-promise
|
|
12
|
-
action(req, res, next).catch((error: ResponseError) => errorHandle(error, req, res, next));
|
|
13
|
-
|
|
14
|
-
export function errorHandle(
|
|
15
|
-
err: ResponseError,
|
|
16
|
-
req: express.Request,
|
|
17
|
-
res: express.Response,
|
|
18
|
-
// TODO: Do not remove unused next, it's needed for express to catch errors!
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
20
|
-
next: express.NextFunction
|
|
21
|
-
) {
|
|
22
|
-
logger.error(`express.errorHandle, url ${req.url}, error:`, err);
|
|
23
|
-
err.status = err.status || 500;
|
|
24
|
-
res.status(err.status);
|
|
25
|
-
return res.jsonp({
|
|
26
|
-
message: err.message,
|
|
27
|
-
error: err,
|
|
28
|
-
});
|
|
29
|
-
}
|
package/middlewares/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { catchErrors, errorHandle } from './error';
|
|
Binary file
|
package/types/asset.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
declare module '*.png' {
|
|
2
|
-
const value: any;
|
|
3
|
-
export = value;
|
|
4
|
-
}
|
|
5
|
-
declare module '*.svg' {
|
|
6
|
-
import type { FunctionComponent, SVGProps } from 'react';
|
|
7
|
-
|
|
8
|
-
export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
|
|
9
|
-
const src: string;
|
|
10
|
-
export default src;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// @TODO Gilad
|
|
14
|
-
declare module '*.jpg' {
|
|
15
|
-
const value: any;
|
|
16
|
-
export = value;
|
|
17
|
-
}
|
|
18
|
-
declare module '*.jpeg' {
|
|
19
|
-
const value: any;
|
|
20
|
-
export = value;
|
|
21
|
-
}
|
|
22
|
-
declare module '*.gif' {
|
|
23
|
-
const value: any;
|
|
24
|
-
export = value;
|
|
25
|
-
}
|
|
26
|
-
declare module '*.bmp' {
|
|
27
|
-
const value: any;
|
|
28
|
-
export = value;
|
|
29
|
-
}
|
package/types/index.ts
DELETED
package/types/next.ts
DELETED
package/types/request.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { Request } from 'express';
|
package/types/response.ts
DELETED
package/types/route.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { NextFunction } from './next';
|
|
2
|
-
import { Request } from './request';
|
|
3
|
-
import { Response } from './response';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* define express Middleware
|
|
7
|
-
*/
|
|
8
|
-
export type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
9
|
-
|
|
10
|
-
export enum Verb {
|
|
11
|
-
WRITE = 'write',
|
|
12
|
-
READ = 'read',
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* express new Route
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
export interface Route {
|
|
20
|
-
method: string;
|
|
21
|
-
route: string | RegExp;
|
|
22
|
-
verb?: Verb;
|
|
23
|
-
middlewares: Middleware[];
|
|
24
|
-
}
|
package/types/style.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
declare module '*.module.css' {
|
|
2
|
-
const classes: { readonly [key: string]: string };
|
|
3
|
-
export default classes;
|
|
4
|
-
}
|
|
5
|
-
declare module '*.module.scss' {
|
|
6
|
-
const classes: { readonly [key: string]: string };
|
|
7
|
-
export default classes;
|
|
8
|
-
}
|
|
9
|
-
declare module '*.module.sass' {
|
|
10
|
-
const classes: { readonly [key: string]: string };
|
|
11
|
-
export default classes;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
declare module '*.module.less' {
|
|
15
|
-
const classes: { readonly [key: string]: string };
|
|
16
|
-
export default classes;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare module '*.less' {
|
|
20
|
-
const classes: { readonly [key: string]: string };
|
|
21
|
-
export default classes;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare module '*.css' {
|
|
25
|
-
const classes: { readonly [key: string]: string };
|
|
26
|
-
export default classes;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module '*.sass' {
|
|
30
|
-
const classes: { readonly [key: string]: string };
|
|
31
|
-
export default classes;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
declare module '*.scss' {
|
|
35
|
-
const classes: { readonly [key: string]: string };
|
|
36
|
-
export default classes;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
declare module '*.mdx' {
|
|
40
|
-
const component: any;
|
|
41
|
-
export default component;
|
|
42
|
-
}
|