@domain.js/main 0.3.2 → 0.3.5
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/.vscode/settings.json +10 -2
- package/dist/deps/rest/index.d.ts +11 -5
- package/dist/deps/rest/index.js +5 -16
- package/dist/http/router.d.ts +26 -13
- package/dist/http/router.js +19 -12
- package/package.json +1 -1
package/.vscode/settings.json
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
-
"typescript.tsdk": "node_modules/typescript/lib"
|
|
3
|
-
|
|
2
|
+
"typescript.tsdk": "node_modules/typescript/lib",
|
|
3
|
+
"[markdown]": {
|
|
4
|
+
"editor.quickSuggestions": true
|
|
5
|
+
},
|
|
6
|
+
"editor.quickSuggestions": {
|
|
7
|
+
"other": true,
|
|
8
|
+
"comments": true,
|
|
9
|
+
"strings": true
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -27,11 +27,17 @@ export declare function Main(cnf: Cnf, deps: Deps, utils: ReturnType<typeof Util
|
|
|
27
27
|
modify: <T extends ModelBase<any, any>>(Model: ModelStatic<T>, model: T, params: Record<string, any>, isAdmin?: boolean, _cols?: string[] | undefined) => Promise<T>;
|
|
28
28
|
add: <T_1 extends ModelBase<any, any>>(Model: ModelStatic<T_1>, params: Record<string, any>, isAdmin: boolean | undefined, _cols: string[] | undefined, { creatorId, clientIp }: CreatorAndClientIp) => Promise<T_1>;
|
|
29
29
|
remove: (model: Sequelize.Model, deletorId: UserId) => Promise<void | Sequelize.Model<any, any>>;
|
|
30
|
-
list:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
list: {
|
|
31
|
+
<T_2 extends ModelBase<any, any>, J = ReturnType<T_2["toJSON"]>>(Model: ModelStatic<T_2>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: false | undefined): Promise<{
|
|
32
|
+
rows: T_2[];
|
|
33
|
+
count: number;
|
|
34
|
+
}>;
|
|
35
|
+
<T_3 extends ModelBase<any, any>, J_1 = ReturnType<T_3["toJSON"]>>(Model: ModelStatic<T_3>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: true | undefined): Promise<{
|
|
36
|
+
rows: J_1[];
|
|
37
|
+
count: number;
|
|
38
|
+
}>;
|
|
39
|
+
};
|
|
40
|
+
stats: <T_4 extends ModelBase<any, any>>(Model: ModelStatic<T_4>, params: Record<string, any>, where?: any, conf?: {
|
|
35
41
|
dimensions?: Record<string, string> | undefined;
|
|
36
42
|
metrics: Record<string, string>;
|
|
37
43
|
pagination?: {
|
package/dist/deps/rest/index.js
CHANGED
|
@@ -99,15 +99,7 @@ function Main(cnf, deps, utils) {
|
|
|
99
99
|
};
|
|
100
100
|
// count条件所需属性
|
|
101
101
|
const COUNT_OPT = Object.freeze(["where", "include"]);
|
|
102
|
-
|
|
103
|
-
* Restful list (R of CRUD) for list resource
|
|
104
|
-
* @param Model Model definition of resources
|
|
105
|
-
* @param params parameters for updating
|
|
106
|
-
* @param allowAttrs Allow columns to be returned
|
|
107
|
-
* @param toJSON Whether to directly return JSON formatted objects
|
|
108
|
-
* @returns findAll resource result, object propoties has count, rows
|
|
109
|
-
*/
|
|
110
|
-
const list = async (Model, params, allowAttrs, toJSON) => {
|
|
102
|
+
async function list(Model, params, allowAttrs, toJSON) {
|
|
111
103
|
const opt = findAllOpts(Model, params);
|
|
112
104
|
const { _ignoreTotal } = params;
|
|
113
105
|
// 提高查询速度
|
|
@@ -117,13 +109,10 @@ function Main(cnf, deps, utils) {
|
|
|
117
109
|
if (Array.isArray(allowAttrs) && allowAttrs.length)
|
|
118
110
|
opt.attributes = allowAttrs;
|
|
119
111
|
const rows = await Model.findAll(opt);
|
|
120
|
-
if (toJSON)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
return { count, rows };
|
|
126
|
-
};
|
|
112
|
+
if (!toJSON)
|
|
113
|
+
return { rows, count };
|
|
114
|
+
return { count, rows: rows.map((x) => x.toJSON()) };
|
|
115
|
+
}
|
|
127
116
|
return { modify, add, remove, list, stats: (0, stats_1.Stats)(cnf, deps, utils) };
|
|
128
117
|
}
|
|
129
118
|
exports.Main = Main;
|
package/dist/http/router.d.ts
CHANGED
|
@@ -20,26 +20,39 @@ export declare function Router(deps: Deps): {
|
|
|
20
20
|
put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
21
21
|
del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
22
22
|
} & {
|
|
23
|
-
collection: (res: string, _routePath?: string | undefined
|
|
23
|
+
collection: (res: string, _routePath?: string | undefined) => void;
|
|
24
24
|
model: (res: string, routePath?: string) => void;
|
|
25
25
|
resource: (res: string, routePath?: string) => void;
|
|
26
26
|
};
|
|
27
27
|
declare type TRouter = ReturnType<typeof Router>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
declare type
|
|
31
|
-
/**
|
|
32
|
-
declare type
|
|
33
|
-
/**
|
|
34
|
-
|
|
28
|
+
/** 普通路径动作类型集合 */
|
|
29
|
+
declare type normalVerb = "get" | "post" | "put" | "del";
|
|
30
|
+
declare type NoramVerbArguments = Parameters<TRouter["get"]>;
|
|
31
|
+
/** 从用点分隔的字符串中提取第一部分 */
|
|
32
|
+
declare type PickFirst<paths extends string> = paths extends string ? paths extends `${infer F}.${string}` ? F : never : never;
|
|
33
|
+
/**
|
|
34
|
+
* 从 services 路径中题可能的 model 名称的联合类型
|
|
35
|
+
*/
|
|
36
|
+
declare type PickModels<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? `${Keys}.detail` | `${Keys}.modify` | `${Keys}.remove` extends paths ? Keys : never : never;
|
|
37
|
+
/**
|
|
38
|
+
* 从 services 路径中题可能的 resource 名称的联合类型
|
|
39
|
+
*/
|
|
40
|
+
declare type PickResources<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? `${Keys}.add` | `${Keys}.list` | `${Keys}.detail` | `${Keys}.modify` | `${Keys}.remove` extends paths ? Keys : never : never;
|
|
41
|
+
/**
|
|
42
|
+
* 根据指定的 controller 尝试提取存在的 collectname
|
|
43
|
+
* type t2 = PickCollection<"user", "user.addFile" | "user.Files"> // File
|
|
44
|
+
*/
|
|
45
|
+
declare type PickCollect<Keys extends string, paths extends string> = paths extends `${Keys}.add${infer A}` ? A : never;
|
|
46
|
+
declare type PickCollection<Keys extends string, paths extends string, Collects extends string = PickCollect<Keys, paths>> = Collects extends any ? `${Keys}.add${Collects}` | `${Keys}.${Lowercase<Collects>}s` extends paths ? `${Keys}::${Lowercase<Collects>}` : never : never;
|
|
47
|
+
export declare type PickCollections<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? PickCollection<Keys, paths> | (`${Keys}.add` | `${Keys}.list` extends paths ? Keys : never) : never;
|
|
35
48
|
/**
|
|
36
49
|
* 利用领域方法路径类型集合,收窄 methodPath, 同时可以自动提示
|
|
37
50
|
*/
|
|
38
|
-
export declare type NarrowDomainPaths<Paths extends string
|
|
39
|
-
[k in
|
|
51
|
+
export declare type NarrowDomainPaths<Paths extends string> = {
|
|
52
|
+
[k in normalVerb]: (routePath: NoramVerbArguments[0], ctlAct: Paths, code?: NoramVerbArguments[2], isList?: NoramVerbArguments[3], handler?: NoramVerbArguments[4], resHandler?: NoramVerbArguments[5]) => ReturnType<TRouter["get"]>;
|
|
40
53
|
} & {
|
|
41
|
-
model:
|
|
42
|
-
collection:
|
|
43
|
-
resource:
|
|
54
|
+
model: (res: PickModels<Paths>, routePath?: string) => ReturnType<TRouter["model"]>;
|
|
55
|
+
collection: (res: PickCollections<Paths>, routePath?: string) => ReturnType<TRouter["collection"]>;
|
|
56
|
+
resource: (res: PickResources<Paths>, routePath?: string) => ReturnType<TRouter["resource"]>;
|
|
44
57
|
};
|
|
45
58
|
export {};
|
package/dist/http/router.js
CHANGED
|
@@ -18,9 +18,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
21
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
25
|
exports.Router = void 0;
|
|
23
|
-
const
|
|
26
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
24
27
|
const errors = __importStar(require("restify-errors"));
|
|
25
28
|
function Router(deps) {
|
|
26
29
|
const { domain, apisRoute, utils, server, httpCodes = {}, makeProfileHook } = deps;
|
|
@@ -84,7 +87,7 @@ function Router(deps) {
|
|
|
84
87
|
throw Error(`Missing domain method: ${methodPath}`);
|
|
85
88
|
const { method } = domain[methodPath];
|
|
86
89
|
/** 如果都没有则抛出异常 */
|
|
87
|
-
if (!method || !
|
|
90
|
+
if (!method || !lodash_1.default.isFunction(method)) {
|
|
88
91
|
throw Error(`Missing domain method: ${methodPath}`);
|
|
89
92
|
}
|
|
90
93
|
server[verb](route, async (req, res, next) => {
|
|
@@ -115,7 +118,7 @@ function Router(deps) {
|
|
|
115
118
|
if (!ok)
|
|
116
119
|
res.send(code, results.rows);
|
|
117
120
|
}
|
|
118
|
-
else if (!
|
|
121
|
+
else if (!lodash_1.default.isObject(results)) {
|
|
119
122
|
if (code === 204) {
|
|
120
123
|
res.send(code);
|
|
121
124
|
}
|
|
@@ -150,29 +153,33 @@ function Router(deps) {
|
|
|
150
153
|
del: RouterVerbFn("del"),
|
|
151
154
|
};
|
|
152
155
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
156
|
+
* 集合方法,集合方法包含了向集合添加元素,以及查看集合(列表)
|
|
157
|
+
* @param res 资源名称,如果有父级资源用双分号隔开 eg user, user::file
|
|
158
|
+
* @param _routePath 路径地址,可选,默认按照既定规则拼接
|
|
155
159
|
*/
|
|
156
|
-
const collection = (res, _routePath
|
|
160
|
+
const collection = (res, _routePath) => {
|
|
161
|
+
const arr = res.split("::");
|
|
162
|
+
const name = arr[1] ? arr[1] : arr[0];
|
|
163
|
+
const controller = arr[1] ? arr[0] : null;
|
|
157
164
|
let routePath;
|
|
158
165
|
if (typeof _routePath !== "string") {
|
|
159
166
|
if (controller) {
|
|
160
|
-
routePath = `/${controller}s/:${controller}Id/${
|
|
167
|
+
routePath = `/${controller}s/:${controller}Id/${name}s`;
|
|
161
168
|
}
|
|
162
169
|
else {
|
|
163
|
-
routePath = `/${
|
|
170
|
+
routePath = `/${name}s`;
|
|
164
171
|
}
|
|
165
172
|
}
|
|
166
173
|
else {
|
|
167
174
|
routePath = _routePath;
|
|
168
175
|
}
|
|
169
176
|
if (controller) {
|
|
170
|
-
register("get", routePath, `${controller}.${
|
|
171
|
-
register("post", routePath, `${controller}.add${ucwords(
|
|
177
|
+
register("get", routePath, `${controller}.${name}s`, 200, true);
|
|
178
|
+
register("post", routePath, `${controller}.add${ucwords(name)}`, 201);
|
|
172
179
|
}
|
|
173
180
|
else {
|
|
174
|
-
register("get", routePath, `${
|
|
175
|
-
register("post", routePath, `${
|
|
181
|
+
register("get", routePath, `${name}.list`, 200, true);
|
|
182
|
+
register("post", routePath, `${name}.add`, 201);
|
|
176
183
|
}
|
|
177
184
|
};
|
|
178
185
|
const model = (res, routePath = `/${res}s/:id`) => {
|