@doracli/express 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-2029 zhangzhiguo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # `express-mock`
2
+
3
+ > @doracli/express-mock
4
+
5
+ ## Usage
@@ -0,0 +1,155 @@
1
+ 'use strict';
2
+
3
+ var express = require('express');
4
+ var filePro = require('@dorabag/file-pro');
5
+ var lang = require('@cclr/lang');
6
+ var helper = require('@doracli/helper');
7
+
8
+ var createServer = function createServer(config) {
9
+ var app = express();
10
+ return app;
11
+ };
12
+
13
+ function _classCallCheck(a, n) {
14
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
15
+ }
16
+ function _defineProperties(e, r) {
17
+ for (var t = 0; t < r.length; t++) {
18
+ var o = r[t];
19
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
20
+ }
21
+ }
22
+ function _createClass(e, r, t) {
23
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
24
+ writable: false
25
+ }), e;
26
+ }
27
+ function _defineProperty(e, r, t) {
28
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
29
+ value: t,
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true
33
+ }) : e[r] = t, e;
34
+ }
35
+ function ownKeys(e, r) {
36
+ var t = Object.keys(e);
37
+ if (Object.getOwnPropertySymbols) {
38
+ var o = Object.getOwnPropertySymbols(e);
39
+ r && (o = o.filter(function (r) {
40
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
41
+ })), t.push.apply(t, o);
42
+ }
43
+ return t;
44
+ }
45
+ function _objectSpread2(e) {
46
+ for (var r = 1; r < arguments.length; r++) {
47
+ var t = null != arguments[r] ? arguments[r] : {};
48
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
49
+ _defineProperty(e, r, t[r]);
50
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
51
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
52
+ });
53
+ }
54
+ return e;
55
+ }
56
+ function _toPrimitive(t, r) {
57
+ if ("object" != typeof t || !t) return t;
58
+ var e = t[Symbol.toPrimitive];
59
+ if (void 0 !== e) {
60
+ var i = e.call(t, r);
61
+ if ("object" != typeof i) return i;
62
+ throw new TypeError("@@toPrimitive must return a primitive value.");
63
+ }
64
+ return (String )(t);
65
+ }
66
+ function _toPropertyKey(t) {
67
+ var i = _toPrimitive(t, "string");
68
+ return "symbol" == typeof i ? i : i + "";
69
+ }
70
+
71
+ var Ctx = /*#__PURE__*/function () {
72
+ function Ctx(configCtx) {
73
+ _classCallCheck(this, Ctx);
74
+ _defineProperty(this, "configCtx", void 0);
75
+ _defineProperty(this, "envParams", {
76
+ scan: './',
77
+ workRootDir: filePro.getCwd()
78
+ });
79
+ /** 路由 -》文件夹/文件的映射 */
80
+ _defineProperty(this, "routePathMap", {});
81
+ this.configCtx = configCtx;
82
+ }
83
+ return _createClass(Ctx, [{
84
+ key: "updateEnvParams",
85
+ value: function updateEnvParams(params) {
86
+ this.envParams = _objectSpread2(_objectSpread2({}, this.envParams), params);
87
+ }
88
+ }]);
89
+ }();
90
+
91
+ var RouteManager = /*#__PURE__*/function () {
92
+ function RouteManager(app) {
93
+ _classCallCheck(this, RouteManager);
94
+ _defineProperty(this, "app", void 0);
95
+ _defineProperty(this, "routesMap", {});
96
+ this.app = app;
97
+ }
98
+ return _createClass(RouteManager, [{
99
+ key: "add",
100
+ value: function add(route, method, handler) {
101
+ this.updateMap(route, method, handler);
102
+ this.app.route(route)[method](this.getProxyHandler(route, method));
103
+ }
104
+ }, {
105
+ key: "update",
106
+ value: function update(route, method, handler) {
107
+ this.updateMap(route, method, handler, 'updated');
108
+ }
109
+ }, {
110
+ key: "remove",
111
+ value: function remove(route, method) {
112
+ this.updateMap(route, method, null, 'removed');
113
+ // this.app._router.stack = this.app._router.stack.filter((layer) => {
114
+ // return !(layer.route && layer.route.route === route);
115
+ // });
116
+ }
117
+ }, {
118
+ key: "destroy",
119
+ value: function destroy() {
120
+ this.routesMap = {};
121
+ }
122
+ }, {
123
+ key: "updateMap",
124
+ value: function updateMap(route, method, handler) {
125
+ var state = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'added';
126
+ lang.set(this.routesMap, "".concat(route, ".").concat(method), {
127
+ state: state,
128
+ handler: handler
129
+ });
130
+ }
131
+ }, {
132
+ key: "getProxyHandler",
133
+ value: function getProxyHandler(route, method) {
134
+ var _this = this;
135
+ return function (req, res) {
136
+ var record = lang.get(_this.routesMap, "".concat(route, ".").concat(method), {});
137
+ if (record.handler) {
138
+ return record.handler(req, res);
139
+ } else {
140
+ return res.status(404).send("".concat(method, " ").concat(route, " route not found"));
141
+ }
142
+ };
143
+ }
144
+ }]);
145
+ }();
146
+
147
+ var loggerMiddleware = function loggerMiddleware(req, res, next) {
148
+ helper.cLog.log('ser', 'request', "Request: [".concat(req.method, "] ").concat(req.url));
149
+ next();
150
+ };
151
+
152
+ exports.Ctx = Ctx;
153
+ exports.RouteManager = RouteManager;
154
+ exports.createServer = createServer;
155
+ exports.loggerMiddleware = loggerMiddleware;
@@ -0,0 +1,150 @@
1
+ import express from 'express';
2
+ import { getCwd } from '@dorabag/file-pro';
3
+ import { set, get } from '@cclr/lang';
4
+ import { cLog } from '@doracli/helper';
5
+
6
+ var createServer = function createServer(config) {
7
+ var app = express();
8
+ return app;
9
+ };
10
+
11
+ function _classCallCheck(a, n) {
12
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
13
+ }
14
+ function _defineProperties(e, r) {
15
+ for (var t = 0; t < r.length; t++) {
16
+ var o = r[t];
17
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
18
+ }
19
+ }
20
+ function _createClass(e, r, t) {
21
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
22
+ writable: false
23
+ }), e;
24
+ }
25
+ function _defineProperty(e, r, t) {
26
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
27
+ value: t,
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true
31
+ }) : e[r] = t, e;
32
+ }
33
+ function ownKeys(e, r) {
34
+ var t = Object.keys(e);
35
+ if (Object.getOwnPropertySymbols) {
36
+ var o = Object.getOwnPropertySymbols(e);
37
+ r && (o = o.filter(function (r) {
38
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
39
+ })), t.push.apply(t, o);
40
+ }
41
+ return t;
42
+ }
43
+ function _objectSpread2(e) {
44
+ for (var r = 1; r < arguments.length; r++) {
45
+ var t = null != arguments[r] ? arguments[r] : {};
46
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
47
+ _defineProperty(e, r, t[r]);
48
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
49
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
50
+ });
51
+ }
52
+ return e;
53
+ }
54
+ function _toPrimitive(t, r) {
55
+ if ("object" != typeof t || !t) return t;
56
+ var e = t[Symbol.toPrimitive];
57
+ if (void 0 !== e) {
58
+ var i = e.call(t, r);
59
+ if ("object" != typeof i) return i;
60
+ throw new TypeError("@@toPrimitive must return a primitive value.");
61
+ }
62
+ return (String )(t);
63
+ }
64
+ function _toPropertyKey(t) {
65
+ var i = _toPrimitive(t, "string");
66
+ return "symbol" == typeof i ? i : i + "";
67
+ }
68
+
69
+ var Ctx = /*#__PURE__*/function () {
70
+ function Ctx(configCtx) {
71
+ _classCallCheck(this, Ctx);
72
+ _defineProperty(this, "configCtx", void 0);
73
+ _defineProperty(this, "envParams", {
74
+ scan: './',
75
+ workRootDir: getCwd()
76
+ });
77
+ /** 路由 -》文件夹/文件的映射 */
78
+ _defineProperty(this, "routePathMap", {});
79
+ this.configCtx = configCtx;
80
+ }
81
+ return _createClass(Ctx, [{
82
+ key: "updateEnvParams",
83
+ value: function updateEnvParams(params) {
84
+ this.envParams = _objectSpread2(_objectSpread2({}, this.envParams), params);
85
+ }
86
+ }]);
87
+ }();
88
+
89
+ var RouteManager = /*#__PURE__*/function () {
90
+ function RouteManager(app) {
91
+ _classCallCheck(this, RouteManager);
92
+ _defineProperty(this, "app", void 0);
93
+ _defineProperty(this, "routesMap", {});
94
+ this.app = app;
95
+ }
96
+ return _createClass(RouteManager, [{
97
+ key: "add",
98
+ value: function add(route, method, handler) {
99
+ this.updateMap(route, method, handler);
100
+ this.app.route(route)[method](this.getProxyHandler(route, method));
101
+ }
102
+ }, {
103
+ key: "update",
104
+ value: function update(route, method, handler) {
105
+ this.updateMap(route, method, handler, 'updated');
106
+ }
107
+ }, {
108
+ key: "remove",
109
+ value: function remove(route, method) {
110
+ this.updateMap(route, method, null, 'removed');
111
+ // this.app._router.stack = this.app._router.stack.filter((layer) => {
112
+ // return !(layer.route && layer.route.route === route);
113
+ // });
114
+ }
115
+ }, {
116
+ key: "destroy",
117
+ value: function destroy() {
118
+ this.routesMap = {};
119
+ }
120
+ }, {
121
+ key: "updateMap",
122
+ value: function updateMap(route, method, handler) {
123
+ var state = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'added';
124
+ set(this.routesMap, "".concat(route, ".").concat(method), {
125
+ state: state,
126
+ handler: handler
127
+ });
128
+ }
129
+ }, {
130
+ key: "getProxyHandler",
131
+ value: function getProxyHandler(route, method) {
132
+ var _this = this;
133
+ return function (req, res) {
134
+ var record = get(_this.routesMap, "".concat(route, ".").concat(method), {});
135
+ if (record.handler) {
136
+ return record.handler(req, res);
137
+ } else {
138
+ return res.status(404).send("".concat(method, " ").concat(route, " route not found"));
139
+ }
140
+ };
141
+ }
142
+ }]);
143
+ }();
144
+
145
+ var loggerMiddleware = function loggerMiddleware(req, res, next) {
146
+ cLog.log('ser', 'request', "Request: [".concat(req.method, "] ").concat(req.url));
147
+ next();
148
+ };
149
+
150
+ export { Ctx, RouteManager, createServer, loggerMiddleware };
@@ -0,0 +1,47 @@
1
+ import { Application } from 'express';
2
+ import { THandlerConfig, TMethod, TRouterHandlerFun, Request, Response } from '@doracli/define-server';
3
+ import { ConfigCtx } from '@doracli/helper';
4
+ import { TNoop } from '@cclr/lang';
5
+
6
+ declare const createServer: (config: {
7
+ port?: number;
8
+ }) => Application;
9
+
10
+ type TEnvParams = {
11
+ /** 服务文件 */
12
+ scan: string;
13
+ /** 工作根目录 */
14
+ workRootDir: string;
15
+ };
16
+
17
+ declare class Ctx {
18
+ configCtx: ConfigCtx;
19
+ envParams: TEnvParams;
20
+ /** 路由 -》文件夹/文件的映射 */
21
+ routePathMap: Record<string, THandlerConfig>;
22
+ constructor(configCtx: ConfigCtx);
23
+ updateEnvParams(params: Partial<TEnvParams>): void;
24
+ }
25
+
26
+ declare class RouteManager {
27
+ private app;
28
+ private routesMap;
29
+ constructor(app: Application);
30
+ add(route: string, method: TMethod, handler: TRouterHandlerFun): void;
31
+ update(route: string, method: TMethod, handler: TRouterHandlerFun): void;
32
+ remove(route: string, method: TMethod): void;
33
+ destroy(): void;
34
+ private updateMap;
35
+ private getProxyHandler;
36
+ }
37
+
38
+ declare const loggerMiddleware: (req: Request, res: Response, next: TNoop) => void;
39
+
40
+ type THandlerOpt = {
41
+ state: 'added' | 'updated' | 'removed';
42
+ handler: TRouterHandlerFun | null;
43
+ };
44
+ type TMethodHandlerMap = Partial<Record<TMethod, TRouterHandlerFun>>;
45
+
46
+ export { Ctx, RouteManager, createServer, loggerMiddleware };
47
+ export type { TEnvParams, THandlerOpt, TMethodHandlerMap };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@doracli/express",
3
+ "version": "0.0.1",
4
+ "description": "use express",
5
+ "author": "cclr <18843152354@163.com>",
6
+ "homepage": "",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "exports": {
10
+ "import": "./lib/esm/index.js",
11
+ "require": "./lib/cjs/index.js"
12
+ },
13
+ "types": "lib/type/index.d.ts",
14
+ "files": [
15
+ "lib",
16
+ "README.md"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org/"
21
+ },
22
+ "scripts": {
23
+ "test": "vitest",
24
+ "g:test": "vitest run",
25
+ "build": "drr build",
26
+ "g:build": "drr build"
27
+ },
28
+ "dependencies": {
29
+ "@cclr/lang": "^0.1.28",
30
+ "@cclr/utils": "^0.1.28",
31
+ "@dorabag/file-pro": "^1.0.10",
32
+ "@doracli/define-server": "0.0.1",
33
+ "@doracli/helper": "0.0.1",
34
+ "@doracli/type": "0.0.1",
35
+ "@types/express": "^5.0.3",
36
+ "express": "^5.1.0"
37
+ },
38
+ "gitHead": "40ab496f25042487a671979074515f3345c96453"
39
+ }