@domain.js/main 0.3.1 → 0.3.4
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/sequelize/index.d.ts +7 -5
- package/dist/http/router.d.ts +27 -6
- 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
|
+
}
|
|
@@ -18,6 +18,12 @@ export declare function Main(cnf: Cnf, deps: Deps): {
|
|
|
18
18
|
[propName: string]: Sequelize;
|
|
19
19
|
};
|
|
20
20
|
export declare const Deps: string[];
|
|
21
|
+
/** Model 上的 sort 设定类型 */
|
|
22
|
+
export interface ModelSort<Fields extends string> {
|
|
23
|
+
default: Fields;
|
|
24
|
+
allow: Fields[];
|
|
25
|
+
defaultDirection?: "DESC" | "ASC";
|
|
26
|
+
}
|
|
21
27
|
/**
|
|
22
28
|
* Model 基类
|
|
23
29
|
*/
|
|
@@ -49,11 +55,7 @@ export declare class ModelBase<Attrs extends {} = any, Attrs4Create extends {} =
|
|
|
49
55
|
maxResultsLimit: number;
|
|
50
56
|
};
|
|
51
57
|
/** 列表查询时候排序控制参数 */
|
|
52
|
-
static sort?:
|
|
53
|
-
default: string;
|
|
54
|
-
allow: string[];
|
|
55
|
-
defaultDirection?: "DESC" | "ASC";
|
|
56
|
-
};
|
|
58
|
+
static sort?: ModelSort<string>;
|
|
57
59
|
/** 关联资源设定, 除非要关联过滤,否则不要设置资源之间的关联关系 */
|
|
58
60
|
static includes?: {
|
|
59
61
|
[k: string]: {
|
package/dist/http/router.d.ts
CHANGED
|
@@ -20,18 +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
|
-
declare type
|
|
30
|
-
declare type
|
|
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;
|
|
31
48
|
/**
|
|
32
49
|
* 利用领域方法路径类型集合,收窄 methodPath, 同时可以自动提示
|
|
33
50
|
*/
|
|
34
|
-
export declare type NarrowDomainPaths<Paths extends string> =
|
|
35
|
-
[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"]>;
|
|
53
|
+
} & {
|
|
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"]>;
|
|
36
57
|
};
|
|
37
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`) => {
|