@domain.js/main 0.3.0 → 0.3.3

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.
@@ -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]: {
@@ -25,13 +25,34 @@ export declare function Router(deps: Deps): {
25
25
  resource: (res: string, routePath?: string) => void;
26
26
  };
27
27
  declare type TRouter = ReturnType<typeof Router>;
28
- declare type normal = Parameters<TRouter["get"]>;
29
- declare type ReplaceArrayItem<T extends any[], index extends number, R, S extends any[] = [], L extends number = S["length"]> = T extends [infer A, ...infer rest] ? L extends index ? [...S, R, ...rest] : ReplaceArrayItem<rest, index, R, [...S, A]> : never;
30
- declare type Keys = "get" | "post" | "put" | "patch" | "del";
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 ? [Lowercase<Collects>, Keys] : never : never;
47
+ export declare type PickCollections<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? PickCollection<Keys, paths> : never;
31
48
  /**
32
49
  * 利用领域方法路径类型集合,收窄 methodPath, 同时可以自动提示
33
50
  */
34
- export declare type NarrowDomainPaths<Paths extends string> = Omit<TRouter, Keys> & {
35
- [k in Keys]: (...args: ReplaceArrayItem<normal, 1, Paths>) => void;
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: PickCollections<Paths> extends [infer R, infer C] ? (res: R, _routePath?: string, controller?: C) => ReturnType<TRouter["collection"]> : never;
56
+ resource: (res: PickResources<Paths>, routePath?: string) => ReturnType<TRouter["resource"]>;
36
57
  };
37
58
  export {};
@@ -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 _ = __importStar(require("lodash"));
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 || !_.isFunction(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 (!_.isObject(results)) {
121
+ else if (!lodash_1.default.isObject(results)) {
119
122
  if (code === 204) {
120
123
  res.send(code);
121
124
  }
@@ -1 +1,22 @@
1
+ /** 将 readonly string[] 转换成联合类型 */
1
2
  export declare type ReadonlyArray2union<T extends ReadonlyArray<any>> = T extends ReadonlyArray<infer A> ? A : never;
3
+ /** 接口参数 schema 定义的类型 */
4
+ export interface ParamsSchema<Params extends {}, Keys extends string | number | symbol = keyof Params> {
5
+ /** 接口整体描述信息 */
6
+ description: string;
7
+ /** 固定为 object params 必然是一个对象, 可以是一个空对象 */
8
+ type: "object";
9
+ /** 必填选项 */
10
+ required?: (keyof Params)[];
11
+ /** 多选1的设定 */
12
+ oneOf?: any[];
13
+ /** 是否允许添加额外的属性 */
14
+ additionalProperties?: boolean;
15
+ /** params 参数每一个属性的描述 */
16
+ properties: Record<Keys, Record<string, any> & {
17
+ /** 当前参数的描述信息 */
18
+ description: string;
19
+ /** 当前参数的类型 */
20
+ type: "string" | "number" | "integer" | "object" | "array";
21
+ }>;
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.0",
3
+ "version": "0.3.3",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {