@doracli/define-server 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,171 @@
1
+ 'use strict';
2
+
3
+ function _applyDecoratedDescriptor(i, e, r, n, l) {
4
+ var a = {};
5
+ return Object.keys(n).forEach(function (i) {
6
+ a[i] = n[i];
7
+ }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
8
+ return n(i, e, r) || r;
9
+ }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
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 _toPrimitive(t, r) {
34
+ if ("object" != typeof t || !t) return t;
35
+ var e = t[Symbol.toPrimitive];
36
+ if (void 0 !== e) {
37
+ var i = e.call(t, r);
38
+ if ("object" != typeof i) return i;
39
+ throw new TypeError("@@toPrimitive must return a primitive value.");
40
+ }
41
+ return ("string" === r ? String : Number)(t);
42
+ }
43
+ function _toPropertyKey(t) {
44
+ var i = _toPrimitive(t, "string");
45
+ return "symbol" == typeof i ? i : i + "";
46
+ }
47
+
48
+ var ComponentBaseImpl = /*#__PURE__*/_createClass(function ComponentBaseImpl() {
49
+ _classCallCheck(this, ComponentBaseImpl);
50
+ });
51
+ /** 标记为服务组件 */
52
+ _defineProperty(ComponentBaseImpl, "handlerServer", void 0);
53
+ /** handler类型 */
54
+ _defineProperty(ComponentBaseImpl, "defineType", void 0);
55
+ /** 默认的方法处理类型 */
56
+ _defineProperty(ComponentBaseImpl, "defaultHandlerType", void 0);
57
+ /** 类的子路由与方法名进行绑定 */
58
+ _defineProperty(ComponentBaseImpl, "routeFunNameMap", void 0);
59
+ /** 方法名 -> 请求类型 */
60
+ _defineProperty(ComponentBaseImpl, "funNameMethodMap", void 0);
61
+ /** 方法名 -> 方法解析类型 */
62
+ _defineProperty(ComponentBaseImpl, "funNameHandlerTypeMap", void 0);
63
+
64
+ var DefaultHandlerName = 'handlerFun';
65
+
66
+ var alias = function alias(name) {
67
+ return function (target, propertyKey, descriptor) {
68
+ var constructor = target.constructor;
69
+ if (!constructor.routeFunNameMap) {
70
+ constructor.routeFunNameMap = {};
71
+ }
72
+ constructor.routeFunNameMap[name] = propertyKey;
73
+ return descriptor;
74
+ };
75
+ };
76
+
77
+ /**
78
+ * 类装饰器,标记为服务组件
79
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
80
+ * @returns
81
+ */
82
+ var component = function component(type) {
83
+ return function (Target) {
84
+ Target.handlerServer = true;
85
+ Target.type = type || 'CLASS';
86
+ };
87
+ };
88
+
89
+ /**
90
+ * 类装饰器,标记为服务组件
91
+ * @returns
92
+ */
93
+ var controller = function controller() {
94
+ var defineType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'CLASS';
95
+ return function (Target) {
96
+ Target.handlerServer = true;
97
+ Target.defineType = defineType;
98
+ Target.defaultHandlerType = 'REST';
99
+ };
100
+ };
101
+
102
+ var method = function method() {
103
+ for (var _len = arguments.length, methods = new Array(_len), _key = 0; _key < _len; _key++) {
104
+ methods[_key] = arguments[_key];
105
+ }
106
+ return function (target, propertyKey, descriptor) {
107
+ var constructor = target.constructor;
108
+ if (!constructor.funNameMethodMap) {
109
+ constructor.funNameMethodMap = {};
110
+ }
111
+ constructor.funNameMethodMap[propertyKey] = methods;
112
+ return descriptor;
113
+ };
114
+ };
115
+
116
+ /**
117
+ * 类装饰器,标记为服务组件
118
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
119
+ * @returns
120
+ */
121
+ var restController = function restController() {
122
+ return function (Target) {
123
+ Target.handlerServer = true;
124
+ Target.defineType = 'CLASS';
125
+ Target.defaultHandlerType = 'REST';
126
+ };
127
+ };
128
+
129
+ /**
130
+ * 定义路由
131
+ * @param name 路由名称,默认使用方法名
132
+ */
133
+ var route = function route(name) {
134
+ return function (target, propertyKey, descriptor) {
135
+ var constructor = target.constructor;
136
+ if (!constructor.routeFunNameMap) {
137
+ constructor.routeFunNameMap = {};
138
+ }
139
+ constructor.routeFunNameMap[name || propertyKey] = propertyKey;
140
+ return descriptor;
141
+ };
142
+ };
143
+
144
+ var defineControllerFun = function defineControllerFun(cb) {
145
+ var _dec, _dec2, _class, _class2;
146
+ var HandlerClass = (_dec = controller('FUN'), _dec2 = route(), _dec(_class = (_class2 = /*#__PURE__*/function () {
147
+ function HandlerClass() {
148
+ _classCallCheck(this, HandlerClass);
149
+ }
150
+ return _createClass(HandlerClass, [{
151
+ key: "handlerFun",
152
+ value: function handlerFun() {
153
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
154
+ args[_key] = arguments[_key];
155
+ }
156
+ return cb.apply(this, args);
157
+ }
158
+ }]);
159
+ }(), _applyDecoratedDescriptor(_class2.prototype, "handlerFun", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handlerFun"), _class2.prototype), _class2)) || _class);
160
+ return HandlerClass;
161
+ };
162
+
163
+ exports.ComponentBaseImpl = ComponentBaseImpl;
164
+ exports.DefaultHandlerName = DefaultHandlerName;
165
+ exports.alias = alias;
166
+ exports.component = component;
167
+ exports.controller = controller;
168
+ exports.defineControllerFun = defineControllerFun;
169
+ exports.method = method;
170
+ exports.restController = restController;
171
+ exports.route = route;
@@ -0,0 +1,161 @@
1
+ function _applyDecoratedDescriptor(i, e, r, n, l) {
2
+ var a = {};
3
+ return Object.keys(n).forEach(function (i) {
4
+ a[i] = n[i];
5
+ }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
6
+ return n(i, e, r) || r;
7
+ }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
8
+ }
9
+ function _classCallCheck(a, n) {
10
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
11
+ }
12
+ function _defineProperties(e, r) {
13
+ for (var t = 0; t < r.length; t++) {
14
+ var o = r[t];
15
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
16
+ }
17
+ }
18
+ function _createClass(e, r, t) {
19
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
20
+ writable: false
21
+ }), e;
22
+ }
23
+ function _defineProperty(e, r, t) {
24
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
25
+ value: t,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ }) : e[r] = t, e;
30
+ }
31
+ function _toPrimitive(t, r) {
32
+ if ("object" != typeof t || !t) return t;
33
+ var e = t[Symbol.toPrimitive];
34
+ if (void 0 !== e) {
35
+ var i = e.call(t, r);
36
+ if ("object" != typeof i) return i;
37
+ throw new TypeError("@@toPrimitive must return a primitive value.");
38
+ }
39
+ return ("string" === r ? String : Number)(t);
40
+ }
41
+ function _toPropertyKey(t) {
42
+ var i = _toPrimitive(t, "string");
43
+ return "symbol" == typeof i ? i : i + "";
44
+ }
45
+
46
+ var ComponentBaseImpl = /*#__PURE__*/_createClass(function ComponentBaseImpl() {
47
+ _classCallCheck(this, ComponentBaseImpl);
48
+ });
49
+ /** 标记为服务组件 */
50
+ _defineProperty(ComponentBaseImpl, "handlerServer", void 0);
51
+ /** handler类型 */
52
+ _defineProperty(ComponentBaseImpl, "defineType", void 0);
53
+ /** 默认的方法处理类型 */
54
+ _defineProperty(ComponentBaseImpl, "defaultHandlerType", void 0);
55
+ /** 类的子路由与方法名进行绑定 */
56
+ _defineProperty(ComponentBaseImpl, "routeFunNameMap", void 0);
57
+ /** 方法名 -> 请求类型 */
58
+ _defineProperty(ComponentBaseImpl, "funNameMethodMap", void 0);
59
+ /** 方法名 -> 方法解析类型 */
60
+ _defineProperty(ComponentBaseImpl, "funNameHandlerTypeMap", void 0);
61
+
62
+ var DefaultHandlerName = 'handlerFun';
63
+
64
+ var alias = function alias(name) {
65
+ return function (target, propertyKey, descriptor) {
66
+ var constructor = target.constructor;
67
+ if (!constructor.routeFunNameMap) {
68
+ constructor.routeFunNameMap = {};
69
+ }
70
+ constructor.routeFunNameMap[name] = propertyKey;
71
+ return descriptor;
72
+ };
73
+ };
74
+
75
+ /**
76
+ * 类装饰器,标记为服务组件
77
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
78
+ * @returns
79
+ */
80
+ var component = function component(type) {
81
+ return function (Target) {
82
+ Target.handlerServer = true;
83
+ Target.type = type || 'CLASS';
84
+ };
85
+ };
86
+
87
+ /**
88
+ * 类装饰器,标记为服务组件
89
+ * @returns
90
+ */
91
+ var controller = function controller() {
92
+ var defineType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'CLASS';
93
+ return function (Target) {
94
+ Target.handlerServer = true;
95
+ Target.defineType = defineType;
96
+ Target.defaultHandlerType = 'REST';
97
+ };
98
+ };
99
+
100
+ var method = function method() {
101
+ for (var _len = arguments.length, methods = new Array(_len), _key = 0; _key < _len; _key++) {
102
+ methods[_key] = arguments[_key];
103
+ }
104
+ return function (target, propertyKey, descriptor) {
105
+ var constructor = target.constructor;
106
+ if (!constructor.funNameMethodMap) {
107
+ constructor.funNameMethodMap = {};
108
+ }
109
+ constructor.funNameMethodMap[propertyKey] = methods;
110
+ return descriptor;
111
+ };
112
+ };
113
+
114
+ /**
115
+ * 类装饰器,标记为服务组件
116
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
117
+ * @returns
118
+ */
119
+ var restController = function restController() {
120
+ return function (Target) {
121
+ Target.handlerServer = true;
122
+ Target.defineType = 'CLASS';
123
+ Target.defaultHandlerType = 'REST';
124
+ };
125
+ };
126
+
127
+ /**
128
+ * 定义路由
129
+ * @param name 路由名称,默认使用方法名
130
+ */
131
+ var route = function route(name) {
132
+ return function (target, propertyKey, descriptor) {
133
+ var constructor = target.constructor;
134
+ if (!constructor.routeFunNameMap) {
135
+ constructor.routeFunNameMap = {};
136
+ }
137
+ constructor.routeFunNameMap[name || propertyKey] = propertyKey;
138
+ return descriptor;
139
+ };
140
+ };
141
+
142
+ var defineControllerFun = function defineControllerFun(cb) {
143
+ var _dec, _dec2, _class, _class2;
144
+ var HandlerClass = (_dec = controller('FUN'), _dec2 = route(), _dec(_class = (_class2 = /*#__PURE__*/function () {
145
+ function HandlerClass() {
146
+ _classCallCheck(this, HandlerClass);
147
+ }
148
+ return _createClass(HandlerClass, [{
149
+ key: "handlerFun",
150
+ value: function handlerFun() {
151
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
152
+ args[_key] = arguments[_key];
153
+ }
154
+ return cb.apply(this, args);
155
+ }
156
+ }]);
157
+ }(), _applyDecoratedDescriptor(_class2.prototype, "handlerFun", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handlerFun"), _class2.prototype), _class2)) || _class);
158
+ return HandlerClass;
159
+ };
160
+
161
+ export { ComponentBaseImpl, DefaultHandlerName, alias, component, controller, defineControllerFun, method, restController, route };
@@ -0,0 +1,92 @@
1
+ import { TPlainObject, TPlainFunction, TAny } from '@cclr/lang';
2
+ import { Request, Response } from 'express';
3
+ export { Request, Response } from 'express';
4
+
5
+ type TMethod = 'get' | 'post' | 'put' | 'delete' | 'all' | 'options';
6
+ type TDefineType = 'CLASS' | 'FUN';
7
+ type TControllerType = 'REST' | 'BASE';
8
+ type TRoutesMap = {
9
+ /**
10
+ * 路由 -> 请求类型 -> 方法名
11
+ */
12
+ [route: string]: Record<TMethod, string>;
13
+ };
14
+
15
+ type TRouterHandlerFun = (req: Request, res: Response) => void | Promise<void>;
16
+ type THandlerConfig = {
17
+ /** 路由前缀 */
18
+ route: string;
19
+ /** handler文件相对路径 */
20
+ relativeFilePath: string;
21
+ /** 文件后缀 */
22
+ fileExt: string;
23
+ /** 文件名称 */
24
+ fileName: string;
25
+ /** 文件原始内容,类,函数,json */
26
+ orange: TPlainObject;
27
+ /** 统一后的类型 */
28
+ Controller: TController;
29
+ };
30
+
31
+ type TController = typeof ComponentBaseImpl;
32
+ declare class ComponentBaseImpl {
33
+ /** 标记为服务组件 */
34
+ static handlerServer: boolean;
35
+ /** handler类型 */
36
+ static defineType: TDefineType;
37
+ /** 默认的方法处理类型 */
38
+ static defaultHandlerType: TControllerType;
39
+ /** 类的子路由与方法名进行绑定 */
40
+ static routeFunNameMap: {
41
+ [route: string]: string;
42
+ };
43
+ /** 方法名 -> 请求类型 */
44
+ static funNameMethodMap: {
45
+ [funName: string]: TMethod[];
46
+ };
47
+ /** 方法名 -> 方法解析类型 */
48
+ static funNameHandlerTypeMap: {
49
+ [funName: string]: TControllerType;
50
+ };
51
+ }
52
+
53
+ declare const DefaultHandlerName = "handlerFun";
54
+
55
+ declare const alias: (name: string) => <T extends TypedPropertyDescriptor<TPlainFunction>>(target: TAny, propertyKey: string, descriptor: T) => T;
56
+
57
+ /**
58
+ * 类装饰器,标记为服务组件
59
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
60
+ * @returns
61
+ */
62
+ declare const component: (type?: TControllerType) => <T extends new (...args: TAny[]) => TAny>(Target: T) => void;
63
+
64
+ /**
65
+ * 类装饰器,标记为服务组件
66
+ * @returns
67
+ */
68
+ declare const controller: (defineType?: TDefineType) => <T extends new (...args: TAny[]) => TAny>(Target: T) => void;
69
+
70
+ declare const method: (...methods: TMethod[]) => <T extends TypedPropertyDescriptor<TPlainFunction>>(target: TAny, propertyKey: string, descriptor: T) => T;
71
+
72
+ /**
73
+ * 类装饰器,标记为服务组件
74
+ * @param type 决定解析方法,不需要传,默认就是`类`解析
75
+ * @returns
76
+ */
77
+ declare const restController: () => <T extends new (...args: TAny[]) => TAny>(Target: T) => void;
78
+
79
+ /**
80
+ * 定义路由
81
+ * @param name 路由名称,默认使用方法名
82
+ */
83
+ declare const route: (name?: string) => <T extends TypedPropertyDescriptor<TAny>>(target: TAny, propertyKey: string, descriptor: T) => T;
84
+
85
+ declare const defineControllerFun: (cb: TRouterHandlerFun) => {
86
+ new (): {
87
+ handlerFun(...args: any[]): any;
88
+ };
89
+ };
90
+
91
+ export { ComponentBaseImpl, DefaultHandlerName, alias, component, controller, defineControllerFun, method, restController, route };
92
+ export type { TController, TControllerType, TDefineType, THandlerConfig, TMethod, TRouterHandlerFun, TRoutesMap };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@doracli/define-server",
3
+ "version": "0.0.1",
4
+ "description": "",
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": "drn build",
26
+ "g:build": "drn build"
27
+ },
28
+ "dependencies": {
29
+ "@cclr/lang": "^0.1.28",
30
+ "@types/express": "^5.0.3"
31
+ },
32
+ "gitHead": "40ab496f25042487a671979074515f3345c96453"
33
+ }