@nocobase/resourcer 0.7.7-alpha.1 → 0.8.0-alpha.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/lib/action.d.ts +2 -2
- package/lib/action.js +10 -9
- package/lib/resource.js +5 -6
- package/lib/resourcer.d.ts +5 -3
- package/lib/resourcer.js +25 -26
- package/package.json +3 -3
package/lib/action.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { MergeStrategies } from './assign';
|
|
2
|
+
import Middleware, { MiddlewareType } from './middleware';
|
|
1
3
|
import Resource from './resource';
|
|
2
4
|
import { HandlerType } from './resourcer';
|
|
3
|
-
import Middleware, { MiddlewareType } from './middleware';
|
|
4
|
-
import { MergeStrategies } from './assign';
|
|
5
5
|
export declare type ActionType = string | HandlerType | ActionOptions;
|
|
6
6
|
export declare type DefaultActionType = 'list' | 'create' | 'get' | 'update' | 'destroy' | 'set' | 'add' | 'remove';
|
|
7
7
|
export declare type ActionName = DefaultActionType | Omit<String, DefaultActionType>;
|
package/lib/action.js
CHANGED
|
@@ -5,10 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.Action = void 0;
|
|
7
7
|
|
|
8
|
-
function
|
|
9
|
-
const data =
|
|
8
|
+
function _utils() {
|
|
9
|
+
const data = require("@nocobase/utils");
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
_utils = function _utils() {
|
|
12
12
|
return data;
|
|
13
13
|
};
|
|
14
14
|
|
|
@@ -25,20 +25,20 @@ function _koaCompose() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
function
|
|
29
|
-
const data = require("
|
|
28
|
+
function _lodash() {
|
|
29
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
_lodash = function _lodash() {
|
|
32
32
|
return data;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
return data;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
var _middleware = _interopRequireDefault(require("./middleware"));
|
|
39
|
-
|
|
40
38
|
var _assign = require("./assign");
|
|
41
39
|
|
|
40
|
+
var _middleware = _interopRequireDefault(require("./middleware"));
|
|
41
|
+
|
|
42
42
|
const _excluded = ["middleware", "middlewares", "handler"];
|
|
43
43
|
|
|
44
44
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -155,7 +155,8 @@ class Action {
|
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
getHandlers() {
|
|
158
|
-
|
|
158
|
+
const handers = [...this.resource.resourcer.getMiddlewares(), ...this.getMiddlewareHandlers(), this.getHandler()].filter(Boolean);
|
|
159
|
+
return handers;
|
|
159
160
|
}
|
|
160
161
|
|
|
161
162
|
execute(context, next) {
|
package/lib/resource.js
CHANGED
|
@@ -45,7 +45,11 @@ class Resource {
|
|
|
45
45
|
const middleware = options.middleware,
|
|
46
46
|
middlewares = options.middlewares,
|
|
47
47
|
_options$actions = options.actions,
|
|
48
|
-
actions = _options$actions === void 0 ? {} : _options$actions
|
|
48
|
+
actions = _options$actions === void 0 ? {} : _options$actions,
|
|
49
|
+
_options$only = options.only,
|
|
50
|
+
only = _options$only === void 0 ? [] : _options$only,
|
|
51
|
+
_options$except = options.except,
|
|
52
|
+
except = _options$except === void 0 ? [] : _options$except;
|
|
49
53
|
this.options = options;
|
|
50
54
|
this.resourcer = resourcer;
|
|
51
55
|
this.middlewares = _middleware.default.toInstanceArray(middleware || middlewares);
|
|
@@ -70,11 +74,6 @@ class Resource {
|
|
|
70
74
|
_iterator.f();
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
const _options$only = options.only,
|
|
74
|
-
only = _options$only === void 0 ? [] : _options$only,
|
|
75
|
-
_options$except = options.except,
|
|
76
|
-
except = _options$except === void 0 ? [] : _options$except;
|
|
77
|
-
|
|
78
77
|
if (except.length > 0) {
|
|
79
78
|
excludes = except;
|
|
80
79
|
} else if (only.length > 0) {
|
package/lib/resourcer.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Toposort, ToposortOptions } from '@nocobase/utils';
|
|
1
2
|
import Action, { ActionName } from './action';
|
|
2
3
|
import Resource, { ResourceOptions } from './resource';
|
|
3
4
|
import { ParsedParams } from './utils';
|
|
@@ -129,7 +130,7 @@ export declare class Resourcer {
|
|
|
129
130
|
protected handlers: Map<ActionName, any>;
|
|
130
131
|
protected actionHandlers: Map<ActionName, any>;
|
|
131
132
|
protected middlewareHandlers: Map<string, any>;
|
|
132
|
-
protected middlewares: any
|
|
133
|
+
protected middlewares: Toposort<any>;
|
|
133
134
|
readonly options: ResourcerOptions;
|
|
134
135
|
constructor(options?: ResourcerOptions);
|
|
135
136
|
/**
|
|
@@ -150,6 +151,7 @@ export declare class Resourcer {
|
|
|
150
151
|
*/
|
|
151
152
|
define(options: ResourceOptions): Resource;
|
|
152
153
|
isDefined(name: string): boolean;
|
|
154
|
+
removeResource(name: any): boolean;
|
|
153
155
|
registerAction(name: ActionName, handler: HandlerType): void;
|
|
154
156
|
registerActions(handlers: Handlers): void;
|
|
155
157
|
/**
|
|
@@ -164,8 +166,8 @@ export declare class Resourcer {
|
|
|
164
166
|
getResource(name: string): Resource;
|
|
165
167
|
getAction(name: string, action: ActionName): Action;
|
|
166
168
|
getMiddlewares(): any[];
|
|
167
|
-
use(middlewares: HandlerType | HandlerType[]): void;
|
|
168
|
-
restApiMiddleware(
|
|
169
|
+
use(middlewares: HandlerType | HandlerType[], options?: ToposortOptions): void;
|
|
170
|
+
restApiMiddleware({ prefix, accessors }?: KoaMiddlewareOptions): (ctx: ResourcerContext, next: () => Promise<any>) => Promise<any>;
|
|
169
171
|
middleware(options?: KoaMiddlewareOptions): (ctx: ResourcerContext, next: () => Promise<any>) => Promise<any>;
|
|
170
172
|
/**
|
|
171
173
|
* 实验性 API
|
package/lib/resourcer.js
CHANGED
|
@@ -5,6 +5,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.Resourcer = void 0;
|
|
7
7
|
|
|
8
|
+
function _utils() {
|
|
9
|
+
const data = require("@nocobase/utils");
|
|
10
|
+
|
|
11
|
+
_utils = function _utils() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
function _glob() {
|
|
9
19
|
const data = _interopRequireDefault(require("glob"));
|
|
10
20
|
|
|
@@ -45,16 +55,6 @@ function _pathToRegexp() {
|
|
|
45
55
|
return data;
|
|
46
56
|
}
|
|
47
57
|
|
|
48
|
-
function _utils() {
|
|
49
|
-
const data = require("@nocobase/utils");
|
|
50
|
-
|
|
51
|
-
_utils = function _utils() {
|
|
52
|
-
return data;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
return data;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
58
|
var _resource = _interopRequireDefault(require("./resource"));
|
|
59
59
|
|
|
60
60
|
var _utils2 = require("./utils");
|
|
@@ -92,9 +92,10 @@ class Resourcer {
|
|
|
92
92
|
this.handlers = new Map();
|
|
93
93
|
this.actionHandlers = new Map();
|
|
94
94
|
this.middlewareHandlers = new Map();
|
|
95
|
-
this.middlewares =
|
|
95
|
+
this.middlewares = void 0;
|
|
96
96
|
this.options = void 0;
|
|
97
97
|
this.options = options;
|
|
98
|
+
this.middlewares = new (_utils().Toposort)();
|
|
98
99
|
}
|
|
99
100
|
/**
|
|
100
101
|
* 载入指定目录下的 resource 配置(配置的文件驱动)
|
|
@@ -144,6 +145,10 @@ class Resourcer {
|
|
|
144
145
|
return this.resources.has(name);
|
|
145
146
|
}
|
|
146
147
|
|
|
148
|
+
removeResource(name) {
|
|
149
|
+
return this.resources.delete(name);
|
|
150
|
+
}
|
|
151
|
+
|
|
147
152
|
registerAction(name, handler) {
|
|
148
153
|
this.registerActionHandler(name, handler);
|
|
149
154
|
}
|
|
@@ -198,24 +203,20 @@ class Resourcer {
|
|
|
198
203
|
}
|
|
199
204
|
|
|
200
205
|
getMiddlewares() {
|
|
201
|
-
return this.middlewares;
|
|
206
|
+
return this.middlewares.nodes;
|
|
202
207
|
}
|
|
203
208
|
|
|
204
|
-
use(middlewares) {
|
|
205
|
-
|
|
206
|
-
this.middlewares.push(middlewares);
|
|
207
|
-
} else if (Array.isArray(middlewares)) {
|
|
208
|
-
this.middlewares.push(...middlewares);
|
|
209
|
-
}
|
|
209
|
+
use(middlewares, options = {}) {
|
|
210
|
+
this.middlewares.add(middlewares, options);
|
|
210
211
|
}
|
|
211
212
|
|
|
212
|
-
restApiMiddleware(
|
|
213
|
+
restApiMiddleware({
|
|
214
|
+
prefix,
|
|
215
|
+
accessors
|
|
216
|
+
} = {}) {
|
|
213
217
|
var _this = this;
|
|
214
218
|
|
|
215
|
-
|
|
216
|
-
accessors = options.accessors;
|
|
217
|
-
|
|
218
|
-
const restApiMiddleware = /*#__PURE__*/function () {
|
|
219
|
+
return /*#__PURE__*/function () {
|
|
219
220
|
var _ref = _asyncToGenerator(function* (ctx, next) {
|
|
220
221
|
ctx.resourcer = _this;
|
|
221
222
|
let params = (0, _utils2.parseRequest)({
|
|
@@ -272,12 +273,10 @@ class Resourcer {
|
|
|
272
273
|
}
|
|
273
274
|
});
|
|
274
275
|
|
|
275
|
-
return function
|
|
276
|
+
return function (_x, _x2) {
|
|
276
277
|
return _ref.apply(this, arguments);
|
|
277
278
|
};
|
|
278
279
|
}();
|
|
279
|
-
|
|
280
|
-
return restApiMiddleware;
|
|
281
280
|
}
|
|
282
281
|
|
|
283
282
|
middleware(options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/resourcer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-alpha.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
}
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@nocobase/utils": "0.
|
|
15
|
+
"@nocobase/utils": "0.8.0-alpha.5",
|
|
16
16
|
"deepmerge": "^4.2.2",
|
|
17
17
|
"koa-compose": "^4.1.0",
|
|
18
18
|
"lodash": "^4.17.21",
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
25
25
|
"directory": "packages/resourcer"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "335f62b0098089cc43a0488ea5c76136a655f97e"
|
|
28
28
|
}
|