@nocobase/resourcer 2.0.0-alpha.7 → 2.0.0-alpha.70
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.js +1 -0
- package/lib/resourcer.d.ts +3 -0
- package/lib/resourcer.js +20 -0
- package/lib/utils.js +24 -1
- package/package.json +3 -3
package/lib/action.js
CHANGED
|
@@ -180,6 +180,7 @@ const _Action = class _Action {
|
|
|
180
180
|
const handlers = [
|
|
181
181
|
...this.resource.resourcer.getMiddlewares(),
|
|
182
182
|
...this.getMiddlewareHandlers(),
|
|
183
|
+
...this.resource.resourcer.getRegisteredPreActionHandlers(this.resource.getName(), this.name),
|
|
183
184
|
this.getHandler()
|
|
184
185
|
].filter(Boolean);
|
|
185
186
|
return handlers.map((fn) => (0, import_utils.wrapMiddlewareWithLogging)(fn));
|
package/lib/resourcer.d.ts
CHANGED
|
@@ -142,6 +142,7 @@ export declare class ResourceManager {
|
|
|
142
142
|
*/
|
|
143
143
|
protected handlers: Map<ActionName, any>;
|
|
144
144
|
protected actionHandlers: Map<ActionName, any>;
|
|
145
|
+
protected preActionHandlers: Map<ActionName, Toposort<HandlerType>>;
|
|
145
146
|
protected middlewareHandlers: Map<string, any>;
|
|
146
147
|
protected middlewares: Toposort<any>;
|
|
147
148
|
constructor(options?: ResourceManagerOptions);
|
|
@@ -195,6 +196,8 @@ export declare class ResourceManager {
|
|
|
195
196
|
* @internal
|
|
196
197
|
*/
|
|
197
198
|
getRegisteredHandlers(): Map<ActionName, any>;
|
|
199
|
+
registerPreActionHandler(name: ActionName, handler: HandlerType, options?: ToposortOptions): void;
|
|
200
|
+
getRegisteredPreActionHandlers(name: string, action: ActionName): HandlerType[];
|
|
198
201
|
/**
|
|
199
202
|
* @internal
|
|
200
203
|
*/
|
package/lib/resourcer.js
CHANGED
|
@@ -59,6 +59,7 @@ const _ResourceManager = class _ResourceManager {
|
|
|
59
59
|
*/
|
|
60
60
|
handlers = /* @__PURE__ */ new Map();
|
|
61
61
|
actionHandlers = /* @__PURE__ */ new Map();
|
|
62
|
+
preActionHandlers = /* @__PURE__ */ new Map();
|
|
62
63
|
middlewareHandlers = /* @__PURE__ */ new Map();
|
|
63
64
|
middlewares;
|
|
64
65
|
constructor(options = {}) {
|
|
@@ -151,6 +152,22 @@ const _ResourceManager = class _ResourceManager {
|
|
|
151
152
|
getRegisteredHandlers() {
|
|
152
153
|
return this.actionHandlers;
|
|
153
154
|
}
|
|
155
|
+
registerPreActionHandler(name, handler, options = {}) {
|
|
156
|
+
const middlewares = this.preActionHandlers.get(name) || new import_utils.Toposort();
|
|
157
|
+
middlewares.add(handler, options);
|
|
158
|
+
this.preActionHandlers.set(name, middlewares);
|
|
159
|
+
}
|
|
160
|
+
getRegisteredPreActionHandlers(name, action) {
|
|
161
|
+
let specificAction = action;
|
|
162
|
+
if (!action.includes(":")) {
|
|
163
|
+
specificAction = `${name}:${action}`;
|
|
164
|
+
}
|
|
165
|
+
let middlewares = this.preActionHandlers.get(specificAction);
|
|
166
|
+
if (!middlewares) {
|
|
167
|
+
middlewares = this.preActionHandlers.get(action);
|
|
168
|
+
}
|
|
169
|
+
return (middlewares == null ? void 0 : middlewares.nodes) || [];
|
|
170
|
+
}
|
|
154
171
|
/**
|
|
155
172
|
* @internal
|
|
156
173
|
*/
|
|
@@ -176,6 +193,9 @@ const _ResourceManager = class _ResourceManager {
|
|
|
176
193
|
return this.middlewares.nodes;
|
|
177
194
|
}
|
|
178
195
|
use(middlewares, options = {}) {
|
|
196
|
+
if (!options.tag && !options.group && !options.before && !options.after) {
|
|
197
|
+
options.tag = "default";
|
|
198
|
+
}
|
|
179
199
|
this.middlewares.add(middlewares, options);
|
|
180
200
|
}
|
|
181
201
|
middleware({ prefix, accessors, skipIfDataSourceExists = false } = {}) {
|
package/lib/utils.js
CHANGED
|
@@ -205,16 +205,39 @@ function parseRequest(request, options = {}) {
|
|
|
205
205
|
return params;
|
|
206
206
|
}
|
|
207
207
|
__name(parseRequest, "parseRequest");
|
|
208
|
+
function smartParse(str) {
|
|
209
|
+
if (typeof str !== "string") return str;
|
|
210
|
+
const s = str.trim();
|
|
211
|
+
if (!s) return str;
|
|
212
|
+
const first = s[0];
|
|
213
|
+
if (first !== "{" && first !== "[") {
|
|
214
|
+
return str;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const parsed = JSON.parse(s);
|
|
218
|
+
if (Array.isArray(parsed)) return parsed;
|
|
219
|
+
if (parsed !== null && typeof parsed === "object") return parsed;
|
|
220
|
+
return str;
|
|
221
|
+
} catch {
|
|
222
|
+
return str;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
__name(smartParse, "smartParse");
|
|
208
226
|
function parseQuery(input) {
|
|
209
227
|
const query = import_qs.default.parse(input, {
|
|
210
228
|
// 原始 query string 中如果一个键连等号“=”都没有可以被认为是 null 类型
|
|
211
|
-
strictNullHandling: true
|
|
229
|
+
strictNullHandling: true,
|
|
230
|
+
// 允许更大的数组长度,避免像 appends[] 这种参数在超过默认 20 个时被转换成对象
|
|
231
|
+
arrayLimit: 100
|
|
212
232
|
// 逗号分隔转换为数组
|
|
213
233
|
// comma: true,
|
|
214
234
|
});
|
|
215
235
|
if (typeof query.filter === "string") {
|
|
216
236
|
query.filter = JSON.parse(query.filter);
|
|
217
237
|
}
|
|
238
|
+
if (typeof query.filterByTk === "string") {
|
|
239
|
+
query.filterByTk = smartParse(query.filterByTk);
|
|
240
|
+
}
|
|
218
241
|
return query;
|
|
219
242
|
}
|
|
220
243
|
__name(parseQuery, "parseQuery");
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/resourcer",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.70",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "AGPL-3.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/utils": "2.0.0-alpha.
|
|
9
|
+
"@nocobase/utils": "2.0.0-alpha.70",
|
|
10
10
|
"deepmerge": "^4.2.2",
|
|
11
11
|
"koa-compose": "^4.1.0",
|
|
12
12
|
"lodash": "^4.17.21",
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
19
19
|
"directory": "packages/resourcer"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "42556ed4b29d8410e104ffd8af4fa72c9b1bae7f"
|
|
22
22
|
}
|