@tachybase/resourcer 0.23.8
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/.turbo/turbo-build.log +9 -0
- package/LICENSE +201 -0
- package/lib/action.d.ts +257 -0
- package/lib/action.js +210 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +29 -0
- package/lib/middleware.d.ts +32 -0
- package/lib/middleware.js +113 -0
- package/lib/resource.d.ts +53 -0
- package/lib/resource.js +98 -0
- package/lib/resourcer.d.ts +216 -0
- package/lib/resourcer.js +266 -0
- package/lib/utils.d.ts +31 -0
- package/lib/utils.js +274 -0
- package/package.json +23 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./utils"), module.exports);
|
|
18
|
+
__reExport(src_exports, require("./middleware"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./action"), module.exports);
|
|
20
|
+
__reExport(src_exports, require("./resource"), module.exports);
|
|
21
|
+
__reExport(src_exports, require("./resourcer"), module.exports);
|
|
22
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
23
|
+
0 && (module.exports = {
|
|
24
|
+
...require("./utils"),
|
|
25
|
+
...require("./middleware"),
|
|
26
|
+
...require("./action"),
|
|
27
|
+
...require("./resource"),
|
|
28
|
+
...require("./resourcer")
|
|
29
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ActionName } from './action';
|
|
2
|
+
import { HandlerType } from './resourcer';
|
|
3
|
+
export type MiddlewareType = string | string[] | HandlerType | HandlerType[] | MiddlewareOptions | MiddlewareOptions[];
|
|
4
|
+
export interface MiddlewareOptions {
|
|
5
|
+
/**
|
|
6
|
+
* actions 白名单,默认有 list、get、create、update、delete
|
|
7
|
+
*/
|
|
8
|
+
only?: Array<ActionName>;
|
|
9
|
+
/**
|
|
10
|
+
* actions 黑名单,默认有 list、get、create、update、delete
|
|
11
|
+
*/
|
|
12
|
+
except?: Array<ActionName>;
|
|
13
|
+
handler?: HandlerType | Function;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
16
|
+
export declare class Middleware {
|
|
17
|
+
protected options: MiddlewareOptions;
|
|
18
|
+
private middlewares;
|
|
19
|
+
constructor(options: MiddlewareOptions | Function);
|
|
20
|
+
getHandler(): (ctx: any, next: any) => any;
|
|
21
|
+
use(middleware: HandlerType): void;
|
|
22
|
+
disuse(middleware: HandlerType): void;
|
|
23
|
+
canAccess(name: ActionName): boolean;
|
|
24
|
+
static toInstanceArray(middlewares: any): Middleware[];
|
|
25
|
+
}
|
|
26
|
+
export default Middleware;
|
|
27
|
+
export declare function branch(map: {
|
|
28
|
+
[key: string]: HandlerType;
|
|
29
|
+
}, reducer: (ctx: any) => string, options?: {
|
|
30
|
+
keyNotFound?(ctx: any, next: any): void;
|
|
31
|
+
handlerNotSet?(ctx: any, next: any): void;
|
|
32
|
+
}): HandlerType;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var middleware_exports = {};
|
|
30
|
+
__export(middleware_exports, {
|
|
31
|
+
Middleware: () => Middleware,
|
|
32
|
+
branch: () => branch,
|
|
33
|
+
default: () => middleware_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
36
|
+
var import_utils = require("@tachybase/utils");
|
|
37
|
+
var import_koa_compose = __toESM(require("koa-compose"));
|
|
38
|
+
const _Middleware = class _Middleware {
|
|
39
|
+
options;
|
|
40
|
+
middlewares = [];
|
|
41
|
+
constructor(options) {
|
|
42
|
+
options = (0, import_utils.requireModule)(options);
|
|
43
|
+
if (typeof options === "function") {
|
|
44
|
+
this.options = { handler: options };
|
|
45
|
+
} else {
|
|
46
|
+
this.options = options;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
getHandler() {
|
|
50
|
+
const handler = (0, import_utils.requireModule)(this.options.handler);
|
|
51
|
+
if (typeof handler !== "function") {
|
|
52
|
+
throw new Error("Handler must be a function!");
|
|
53
|
+
}
|
|
54
|
+
return (ctx, next) => (0, import_koa_compose.default)([handler, ...this.middlewares])(ctx, next);
|
|
55
|
+
}
|
|
56
|
+
use(middleware) {
|
|
57
|
+
this.middlewares.push(middleware);
|
|
58
|
+
}
|
|
59
|
+
disuse(middleware) {
|
|
60
|
+
this.middlewares.splice(this.middlewares.indexOf(middleware), 1);
|
|
61
|
+
}
|
|
62
|
+
canAccess(name) {
|
|
63
|
+
const { only = [], except = [] } = this.options;
|
|
64
|
+
if (only.length > 0) {
|
|
65
|
+
return only.includes(name);
|
|
66
|
+
}
|
|
67
|
+
if (except.length > 0) {
|
|
68
|
+
return !except.includes(name);
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
static toInstanceArray(middlewares) {
|
|
73
|
+
if (!middlewares) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
if (!Array.isArray(middlewares)) {
|
|
77
|
+
middlewares = [middlewares];
|
|
78
|
+
}
|
|
79
|
+
return middlewares.map((middleware) => {
|
|
80
|
+
if (middleware instanceof _Middleware) {
|
|
81
|
+
return middleware;
|
|
82
|
+
}
|
|
83
|
+
if (typeof middleware === "object") {
|
|
84
|
+
return new _Middleware(middleware);
|
|
85
|
+
}
|
|
86
|
+
if (typeof middleware === "function") {
|
|
87
|
+
return new _Middleware({ handler: middleware });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
__name(_Middleware, "Middleware");
|
|
93
|
+
let Middleware = _Middleware;
|
|
94
|
+
var middleware_default = Middleware;
|
|
95
|
+
function branch(map = {}, reducer, options = {}) {
|
|
96
|
+
return (ctx, next) => {
|
|
97
|
+
const key = reducer(ctx);
|
|
98
|
+
if (!key) {
|
|
99
|
+
return options.keyNotFound ? options.keyNotFound(ctx, next) : ctx.throw(404);
|
|
100
|
+
}
|
|
101
|
+
const handler = map[key];
|
|
102
|
+
if (!handler) {
|
|
103
|
+
return options.handlerNotSet ? options.handlerNotSet(ctx, next) : ctx.throw(404);
|
|
104
|
+
}
|
|
105
|
+
return handler(ctx, next);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
__name(branch, "branch");
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
Middleware,
|
|
112
|
+
branch
|
|
113
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import Action, { ActionName, ActionType } from './action';
|
|
2
|
+
import Middleware, { MiddlewareType } from './middleware';
|
|
3
|
+
import { HandlerType, Resourcer } from './resourcer';
|
|
4
|
+
export type ResourceType = 'single' | 'hasOne' | 'hasMany' | 'belongsTo' | 'belongsToMany';
|
|
5
|
+
export interface ResourceOptions {
|
|
6
|
+
/**
|
|
7
|
+
* 资源名称
|
|
8
|
+
*/
|
|
9
|
+
name: string;
|
|
10
|
+
/**
|
|
11
|
+
* 资源类型,默认为 single
|
|
12
|
+
*/
|
|
13
|
+
type?: ResourceType;
|
|
14
|
+
/**
|
|
15
|
+
* 资源的行为
|
|
16
|
+
*/
|
|
17
|
+
actions?: {
|
|
18
|
+
[key: string]: ActionType;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* actions 白名单,默认有 list、get、create、update、delete
|
|
22
|
+
*/
|
|
23
|
+
only?: Array<ActionName>;
|
|
24
|
+
/**
|
|
25
|
+
* actions 黑名单,默认有 list、get、create、update、delete
|
|
26
|
+
*/
|
|
27
|
+
except?: Array<ActionName>;
|
|
28
|
+
/**
|
|
29
|
+
* 中间件
|
|
30
|
+
*/
|
|
31
|
+
middleware?: MiddlewareType;
|
|
32
|
+
/**
|
|
33
|
+
* 中间件
|
|
34
|
+
*/
|
|
35
|
+
middlewares?: MiddlewareType;
|
|
36
|
+
/**
|
|
37
|
+
* 额外的一些参数
|
|
38
|
+
*/
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
}
|
|
41
|
+
export declare class Resource {
|
|
42
|
+
readonly resourcer: Resourcer;
|
|
43
|
+
readonly middlewares: Middleware[];
|
|
44
|
+
readonly actions: Map<ActionName, Action>;
|
|
45
|
+
readonly options: ResourceOptions;
|
|
46
|
+
readonly except: Array<ActionName>;
|
|
47
|
+
constructor(options: ResourceOptions, resourcer: Resourcer);
|
|
48
|
+
getName(): string;
|
|
49
|
+
getExcept(): ActionName[];
|
|
50
|
+
addAction(name: ActionName, handler: HandlerType): void;
|
|
51
|
+
getAction(action: ActionName): Action;
|
|
52
|
+
}
|
|
53
|
+
export default Resource;
|
package/lib/resource.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var resource_exports = {};
|
|
30
|
+
__export(resource_exports, {
|
|
31
|
+
Resource: () => Resource,
|
|
32
|
+
default: () => resource_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(resource_exports);
|
|
35
|
+
var import_lodash = __toESM(require("lodash"));
|
|
36
|
+
var import_action = __toESM(require("./action"));
|
|
37
|
+
var import_middleware = __toESM(require("./middleware"));
|
|
38
|
+
const _Resource = class _Resource {
|
|
39
|
+
resourcer;
|
|
40
|
+
middlewares;
|
|
41
|
+
actions = /* @__PURE__ */ new Map();
|
|
42
|
+
options;
|
|
43
|
+
except;
|
|
44
|
+
constructor(options, resourcer) {
|
|
45
|
+
const { middleware, middlewares, actions = {}, only = [], except = [] } = options;
|
|
46
|
+
this.options = options;
|
|
47
|
+
this.resourcer = resourcer;
|
|
48
|
+
this.middlewares = import_middleware.default.toInstanceArray(middleware || middlewares);
|
|
49
|
+
let excludes = [];
|
|
50
|
+
for (const [name, handler] of resourcer.getRegisteredHandlers()) {
|
|
51
|
+
if (!actions[name]) {
|
|
52
|
+
actions[name] = handler;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (except.length > 0) {
|
|
56
|
+
excludes = except;
|
|
57
|
+
} else if (only.length > 0) {
|
|
58
|
+
excludes = Object.keys(actions).filter((name) => !only.includes(name));
|
|
59
|
+
}
|
|
60
|
+
this.except = excludes;
|
|
61
|
+
this.actions = import_action.default.toInstanceMap(import_lodash.default.omit(actions, excludes), this);
|
|
62
|
+
}
|
|
63
|
+
getName() {
|
|
64
|
+
return this.options.name;
|
|
65
|
+
}
|
|
66
|
+
getExcept() {
|
|
67
|
+
return this.except;
|
|
68
|
+
}
|
|
69
|
+
addAction(name, handler) {
|
|
70
|
+
if (this.except.includes(name)) {
|
|
71
|
+
throw new Error(`${name} action is not allowed`);
|
|
72
|
+
}
|
|
73
|
+
if (this.actions.has(name)) {
|
|
74
|
+
throw new Error(`${name} action already exists`);
|
|
75
|
+
}
|
|
76
|
+
const action = new import_action.default(handler);
|
|
77
|
+
action.setName(name);
|
|
78
|
+
action.setResource(this);
|
|
79
|
+
action.middlewares.unshift(...this.middlewares);
|
|
80
|
+
this.actions.set(name, action);
|
|
81
|
+
}
|
|
82
|
+
getAction(action) {
|
|
83
|
+
if (this.except.includes(action)) {
|
|
84
|
+
throw new Error(`${action} action is not allowed`);
|
|
85
|
+
}
|
|
86
|
+
if (!this.actions.has(action)) {
|
|
87
|
+
throw new Error(`${action} action does not exist`);
|
|
88
|
+
}
|
|
89
|
+
return this.actions.get(action);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
__name(_Resource, "Resource");
|
|
93
|
+
let Resource = _Resource;
|
|
94
|
+
var resource_default = Resource;
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
Resource
|
|
98
|
+
});
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { Toposort, ToposortOptions } from '@tachybase/utils';
|
|
2
|
+
import Action, { ActionName } from './action';
|
|
3
|
+
import Resource, { ResourceOptions } from './resource';
|
|
4
|
+
import { ParsedParams } from './utils';
|
|
5
|
+
export interface ResourcerContext {
|
|
6
|
+
resourcer?: Resourcer;
|
|
7
|
+
action?: Action;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
export interface KoaMiddlewareOptions {
|
|
11
|
+
skipIfDataSourceExists?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* 前缀
|
|
14
|
+
*/
|
|
15
|
+
prefix?: string;
|
|
16
|
+
/**
|
|
17
|
+
* 自定义 resource name 的获取规则
|
|
18
|
+
*
|
|
19
|
+
* 默认规则 relatedTable ? relatedTable.table : table
|
|
20
|
+
*/
|
|
21
|
+
nameRule?: (params: ParsedParams) => string;
|
|
22
|
+
/**
|
|
23
|
+
* 自定义 action name
|
|
24
|
+
*
|
|
25
|
+
* 默认为
|
|
26
|
+
*
|
|
27
|
+
* - list 查看列表
|
|
28
|
+
* - create 新增数据
|
|
29
|
+
* - get 查看数据详情
|
|
30
|
+
* - update 更新数据
|
|
31
|
+
* - delete 删除数据
|
|
32
|
+
*/
|
|
33
|
+
accessors?: {
|
|
34
|
+
/**
|
|
35
|
+
* 查看列表
|
|
36
|
+
*/
|
|
37
|
+
list?: string;
|
|
38
|
+
/**
|
|
39
|
+
* 新增数据
|
|
40
|
+
*/
|
|
41
|
+
create?: string;
|
|
42
|
+
/**
|
|
43
|
+
* 查看数据详情
|
|
44
|
+
*/
|
|
45
|
+
get?: string;
|
|
46
|
+
/**
|
|
47
|
+
* 更新数据
|
|
48
|
+
*/
|
|
49
|
+
update?: string;
|
|
50
|
+
/**
|
|
51
|
+
* 删除数据
|
|
52
|
+
*/
|
|
53
|
+
delete?: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export interface ResourcerOptions {
|
|
57
|
+
/**
|
|
58
|
+
* 前缀
|
|
59
|
+
*/
|
|
60
|
+
prefix?: string;
|
|
61
|
+
/**
|
|
62
|
+
* 自定义 action name
|
|
63
|
+
*
|
|
64
|
+
* 默认为
|
|
65
|
+
*
|
|
66
|
+
* - list 查看列表
|
|
67
|
+
* - create 新增数据
|
|
68
|
+
* - get 查看数据详情
|
|
69
|
+
* - update 更新数据
|
|
70
|
+
* - delete 删除数据
|
|
71
|
+
*/
|
|
72
|
+
accessors?: {
|
|
73
|
+
/**
|
|
74
|
+
* 查看列表
|
|
75
|
+
*/
|
|
76
|
+
list?: string;
|
|
77
|
+
/**
|
|
78
|
+
* 新增数据
|
|
79
|
+
*/
|
|
80
|
+
create?: string;
|
|
81
|
+
/**
|
|
82
|
+
* 查看数据详情
|
|
83
|
+
*/
|
|
84
|
+
get?: string;
|
|
85
|
+
/**
|
|
86
|
+
* 更新数据
|
|
87
|
+
*/
|
|
88
|
+
update?: string;
|
|
89
|
+
/**
|
|
90
|
+
* 删除数据
|
|
91
|
+
*/
|
|
92
|
+
delete?: string;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export interface ExecuteOptions {
|
|
96
|
+
/**
|
|
97
|
+
* 资源名称
|
|
98
|
+
*/
|
|
99
|
+
resource: string;
|
|
100
|
+
/**
|
|
101
|
+
* 自定义 action name
|
|
102
|
+
*
|
|
103
|
+
* 默认
|
|
104
|
+
* - list 查看列表
|
|
105
|
+
* - create 新增数据
|
|
106
|
+
* - get 查看数据详情
|
|
107
|
+
* - update 更新数据
|
|
108
|
+
* - delete 删除数据
|
|
109
|
+
*/
|
|
110
|
+
action: ActionName;
|
|
111
|
+
}
|
|
112
|
+
export type HandlerType = (ctx: ResourcerContext, next: () => Promise<any>) => any;
|
|
113
|
+
export interface Handlers {
|
|
114
|
+
[key: string]: HandlerType;
|
|
115
|
+
}
|
|
116
|
+
export interface ImportOptions {
|
|
117
|
+
/**
|
|
118
|
+
* 指定配置所在路径
|
|
119
|
+
*/
|
|
120
|
+
directory: string;
|
|
121
|
+
/**
|
|
122
|
+
* 文件后缀,默认值 ['js', 'ts', 'json']
|
|
123
|
+
*/
|
|
124
|
+
extensions?: string[];
|
|
125
|
+
}
|
|
126
|
+
export declare class Resourcer {
|
|
127
|
+
/**
|
|
128
|
+
* @internal
|
|
129
|
+
*/
|
|
130
|
+
readonly options: ResourcerOptions;
|
|
131
|
+
protected resources: Map<string, Resource>;
|
|
132
|
+
/**
|
|
133
|
+
* 全局定义的 action handlers
|
|
134
|
+
*/
|
|
135
|
+
protected handlers: Map<ActionName, any>;
|
|
136
|
+
protected actionHandlers: Map<ActionName, any>;
|
|
137
|
+
protected middlewareHandlers: Map<string, any>;
|
|
138
|
+
protected middlewares: Toposort<any>;
|
|
139
|
+
middlewareSourceMap: WeakMap<HandlerType | HandlerType[], string>;
|
|
140
|
+
constructor(options?: ResourcerOptions);
|
|
141
|
+
/**
|
|
142
|
+
* 载入指定目录下的 resource 配置(配置的文件驱动)
|
|
143
|
+
*
|
|
144
|
+
* TODO: 配置的文件驱动现在会全部初始化,大数据时可能存在性能瓶颈,后续可以加入动态加载
|
|
145
|
+
*
|
|
146
|
+
* @param {object} [options]
|
|
147
|
+
* @param {string} [options.directory] 指定配置所在路径
|
|
148
|
+
* @param {array} [options.extensions = ['js', 'ts', 'json']] 文件后缀
|
|
149
|
+
*
|
|
150
|
+
*/
|
|
151
|
+
import(options: ImportOptions): Promise<Map<string, Resource>>;
|
|
152
|
+
/**
|
|
153
|
+
* resource 配置
|
|
154
|
+
*
|
|
155
|
+
* @param name
|
|
156
|
+
* @param options
|
|
157
|
+
*/
|
|
158
|
+
define(options: ResourceOptions): Resource;
|
|
159
|
+
isDefined(name: string): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* @internal
|
|
162
|
+
*/
|
|
163
|
+
removeResource(name: any): boolean;
|
|
164
|
+
/**
|
|
165
|
+
* This method is deprecated and should not be used.
|
|
166
|
+
* Use {@link this.registerActionHandler()} instead.
|
|
167
|
+
* @deprecated
|
|
168
|
+
*/
|
|
169
|
+
registerAction(name: ActionName, handler: HandlerType): void;
|
|
170
|
+
/**
|
|
171
|
+
* This method is deprecated and should not be used.
|
|
172
|
+
* Use {@link this.registerActionHandlers()} instead.
|
|
173
|
+
* @deprecated
|
|
174
|
+
*/
|
|
175
|
+
registerActions(handlers: Handlers): void;
|
|
176
|
+
/**
|
|
177
|
+
* 注册全局的 action handlers
|
|
178
|
+
*
|
|
179
|
+
* @param handlers
|
|
180
|
+
*/
|
|
181
|
+
registerActionHandlers(handlers: Handlers): void;
|
|
182
|
+
registerActionHandler(name: ActionName, handler: HandlerType): void;
|
|
183
|
+
/**
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
getRegisteredHandler(name: ActionName): any;
|
|
187
|
+
/**
|
|
188
|
+
* @internal
|
|
189
|
+
*/
|
|
190
|
+
getRegisteredHandlers(): Map<ActionName, any>;
|
|
191
|
+
/**
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
194
|
+
getResource(name: string): Resource;
|
|
195
|
+
/**
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
198
|
+
getAction(name: string, action: ActionName): Action;
|
|
199
|
+
/**
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
getMiddlewares(): any[];
|
|
203
|
+
use(middlewares: HandlerType | HandlerType[], options?: ToposortOptions): void;
|
|
204
|
+
/**
|
|
205
|
+
* This method is deprecated and should not be used.
|
|
206
|
+
* Use {@link this.middleware()} instead.
|
|
207
|
+
* @deprecated
|
|
208
|
+
*/
|
|
209
|
+
restApiMiddleware({ prefix, accessors, skipIfDataSourceExists }?: KoaMiddlewareOptions): (ctx: ResourcerContext, next: () => Promise<any>) => Promise<any>;
|
|
210
|
+
middleware(options?: KoaMiddlewareOptions): (ctx: ResourcerContext, next: () => Promise<any>) => Promise<any>;
|
|
211
|
+
/**
|
|
212
|
+
* @internal
|
|
213
|
+
*/
|
|
214
|
+
execute(options: ExecuteOptions, context?: ResourcerContext, next?: any): Promise<any>;
|
|
215
|
+
}
|
|
216
|
+
export default Resourcer;
|