@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/resourcer.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
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 resourcer_exports = {};
|
|
30
|
+
__export(resourcer_exports, {
|
|
31
|
+
Resourcer: () => Resourcer,
|
|
32
|
+
default: () => resourcer_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(resourcer_exports);
|
|
35
|
+
var import_utils = require("@tachybase/utils");
|
|
36
|
+
var import_glob = require("glob");
|
|
37
|
+
var import_koa_compose = __toESM(require("koa-compose"));
|
|
38
|
+
var import_lodash = __toESM(require("lodash"));
|
|
39
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
40
|
+
var import_resource = __toESM(require("./resource"));
|
|
41
|
+
var import_utils2 = require("./utils");
|
|
42
|
+
const _Resourcer = class _Resourcer {
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
options;
|
|
47
|
+
resources = /* @__PURE__ */ new Map();
|
|
48
|
+
/**
|
|
49
|
+
* 全局定义的 action handlers
|
|
50
|
+
*/
|
|
51
|
+
handlers = /* @__PURE__ */ new Map();
|
|
52
|
+
actionHandlers = /* @__PURE__ */ new Map();
|
|
53
|
+
middlewareHandlers = /* @__PURE__ */ new Map();
|
|
54
|
+
middlewares;
|
|
55
|
+
middlewareSourceMap = /* @__PURE__ */ new WeakMap();
|
|
56
|
+
constructor(options = {}) {
|
|
57
|
+
this.options = options;
|
|
58
|
+
this.middlewares = new import_utils.Toposort();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 载入指定目录下的 resource 配置(配置的文件驱动)
|
|
62
|
+
*
|
|
63
|
+
* TODO: 配置的文件驱动现在会全部初始化,大数据时可能存在性能瓶颈,后续可以加入动态加载
|
|
64
|
+
*
|
|
65
|
+
* @param {object} [options]
|
|
66
|
+
* @param {string} [options.directory] 指定配置所在路径
|
|
67
|
+
* @param {array} [options.extensions = ['js', 'ts', 'json']] 文件后缀
|
|
68
|
+
*
|
|
69
|
+
*/
|
|
70
|
+
async import(options) {
|
|
71
|
+
const { extensions = ["js", "ts", "json"], directory } = options;
|
|
72
|
+
const patten = `${directory}/*.{${extensions.join(",")}}`;
|
|
73
|
+
const files = (0, import_glob.globSync)(patten, {
|
|
74
|
+
ignore: ["**/*.d.ts"]
|
|
75
|
+
});
|
|
76
|
+
const resources = /* @__PURE__ */ new Map();
|
|
77
|
+
for (const file of files) {
|
|
78
|
+
const options2 = await (0, import_utils.importModule)(file);
|
|
79
|
+
const table = this.define(typeof options2 === "function" ? options2(this) : options2);
|
|
80
|
+
resources.set(table.getName(), table);
|
|
81
|
+
}
|
|
82
|
+
return resources;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* resource 配置
|
|
86
|
+
*
|
|
87
|
+
* @param name
|
|
88
|
+
* @param options
|
|
89
|
+
*/
|
|
90
|
+
define(options) {
|
|
91
|
+
const { name } = options;
|
|
92
|
+
const resource = new import_resource.default(options, this);
|
|
93
|
+
this.resources.set(name, resource);
|
|
94
|
+
return resource;
|
|
95
|
+
}
|
|
96
|
+
isDefined(name) {
|
|
97
|
+
return this.resources.has(name);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
removeResource(name) {
|
|
103
|
+
return this.resources.delete(name);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* This method is deprecated and should not be used.
|
|
107
|
+
* Use {@link this.registerActionHandler()} instead.
|
|
108
|
+
* @deprecated
|
|
109
|
+
*/
|
|
110
|
+
registerAction(name, handler) {
|
|
111
|
+
this.registerActionHandler(name, handler);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* This method is deprecated and should not be used.
|
|
115
|
+
* Use {@link this.registerActionHandlers()} instead.
|
|
116
|
+
* @deprecated
|
|
117
|
+
*/
|
|
118
|
+
registerActions(handlers) {
|
|
119
|
+
this.registerActionHandlers(handlers);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 注册全局的 action handlers
|
|
123
|
+
*
|
|
124
|
+
* @param handlers
|
|
125
|
+
*/
|
|
126
|
+
registerActionHandlers(handlers) {
|
|
127
|
+
for (const [name, handler] of Object.entries(handlers)) {
|
|
128
|
+
this.registerActionHandler(name, handler);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
registerActionHandler(name, handler) {
|
|
132
|
+
this.actionHandlers.set(name, handler);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* @internal
|
|
136
|
+
*/
|
|
137
|
+
getRegisteredHandler(name) {
|
|
138
|
+
return this.actionHandlers.get(name);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @internal
|
|
142
|
+
*/
|
|
143
|
+
getRegisteredHandlers() {
|
|
144
|
+
return this.actionHandlers;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @internal
|
|
148
|
+
*/
|
|
149
|
+
getResource(name) {
|
|
150
|
+
if (!this.resources.has(name)) {
|
|
151
|
+
throw new Error(`${name} resource does not exist`);
|
|
152
|
+
}
|
|
153
|
+
return this.resources.get(name);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
getAction(name, action) {
|
|
159
|
+
if (this.actionHandlers.has(`${name}:${action}`)) {
|
|
160
|
+
return this.getResource(name).getAction(`${name}:${action}`);
|
|
161
|
+
}
|
|
162
|
+
return this.getResource(name).getAction(action);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @internal
|
|
166
|
+
*/
|
|
167
|
+
getMiddlewares() {
|
|
168
|
+
return this.middlewares.nodes;
|
|
169
|
+
}
|
|
170
|
+
use(middlewares, options = {}) {
|
|
171
|
+
this.middlewareSourceMap.set(middlewares, (0, import_utils.getCurrentStacks)());
|
|
172
|
+
this.middlewares.add(middlewares, options);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* This method is deprecated and should not be used.
|
|
176
|
+
* Use {@link this.middleware()} instead.
|
|
177
|
+
* @deprecated
|
|
178
|
+
*/
|
|
179
|
+
restApiMiddleware({ prefix, accessors, skipIfDataSourceExists = false } = {}) {
|
|
180
|
+
return async (ctx, next) => {
|
|
181
|
+
if (skipIfDataSourceExists) {
|
|
182
|
+
const dataSource = ctx.get("x-data-source");
|
|
183
|
+
if (dataSource) {
|
|
184
|
+
return next();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
ctx.resourcer = this;
|
|
188
|
+
let params = (0, import_utils2.parseRequest)(
|
|
189
|
+
{
|
|
190
|
+
path: ctx.request.path,
|
|
191
|
+
method: ctx.request.method
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
prefix: this.options.prefix || prefix,
|
|
195
|
+
accessors: this.options.accessors || accessors
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
if (!params) {
|
|
199
|
+
return next();
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const resource = this.getResource((0, import_utils2.getNameByParams)(params));
|
|
203
|
+
if (resource.options.type && resource.options.type !== "single") {
|
|
204
|
+
params = (0, import_utils2.parseRequest)(
|
|
205
|
+
{
|
|
206
|
+
path: ctx.request.path,
|
|
207
|
+
method: ctx.request.method,
|
|
208
|
+
type: resource.options.type
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
prefix: this.options.prefix || prefix,
|
|
212
|
+
accessors: this.options.accessors || accessors
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
if (!params) {
|
|
216
|
+
return next();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
ctx.action = this.getAction((0, import_utils2.getNameByParams)(params), params.actionName).clone();
|
|
220
|
+
ctx.action.setContext(ctx);
|
|
221
|
+
ctx.action.actionName = params.actionName;
|
|
222
|
+
ctx.action.sourceId = params.associatedIndex;
|
|
223
|
+
ctx.action.resourceOf = params.associatedIndex;
|
|
224
|
+
ctx.action.resourceName = params.associatedName ? `${params.associatedName}.${params.resourceName}` : params.resourceName;
|
|
225
|
+
ctx.action.params.filterByTk = params.resourceIndex;
|
|
226
|
+
const query = (0, import_utils2.parseQuery)(ctx.request.querystring);
|
|
227
|
+
if ((0, import_path_to_regexp.pathToRegexp)("/resourcer/{:associatedName.}:resourceName{\\::actionName}").regexp.test(ctx.request.path)) {
|
|
228
|
+
ctx.action.mergeParams({
|
|
229
|
+
...query,
|
|
230
|
+
...params,
|
|
231
|
+
...ctx.request.body
|
|
232
|
+
});
|
|
233
|
+
} else {
|
|
234
|
+
ctx.action.mergeParams({
|
|
235
|
+
...query,
|
|
236
|
+
...params,
|
|
237
|
+
...import_lodash.default.isEmpty(ctx.request.body) ? {} : { values: ctx.request.body }
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return (0, import_koa_compose.default)(ctx.action.getHandlers())(ctx, next);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.log(error);
|
|
243
|
+
return next();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
middleware(options = {}) {
|
|
248
|
+
return this.restApiMiddleware(options);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
253
|
+
async execute(options, context = {}, next) {
|
|
254
|
+
const { resource, action } = options;
|
|
255
|
+
context.resourcer = this;
|
|
256
|
+
context.action = this.getAction(resource, action);
|
|
257
|
+
return await context.action.execute(context, next);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
__name(_Resourcer, "Resourcer");
|
|
261
|
+
let Resourcer = _Resourcer;
|
|
262
|
+
var resourcer_default = Resourcer;
|
|
263
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
264
|
+
0 && (module.exports = {
|
|
265
|
+
Resourcer
|
|
266
|
+
});
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ResourceType } from './resource';
|
|
2
|
+
export interface ParseRequest {
|
|
3
|
+
path: string;
|
|
4
|
+
method: string;
|
|
5
|
+
namespace?: string;
|
|
6
|
+
type?: ResourceType;
|
|
7
|
+
}
|
|
8
|
+
export interface ParseOptions {
|
|
9
|
+
prefix?: string;
|
|
10
|
+
accessors?: {
|
|
11
|
+
list?: string;
|
|
12
|
+
create?: string;
|
|
13
|
+
get?: string;
|
|
14
|
+
update?: string;
|
|
15
|
+
delete?: string;
|
|
16
|
+
set?: string;
|
|
17
|
+
add?: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface ParsedParams {
|
|
21
|
+
actionName?: string;
|
|
22
|
+
resourceName?: string;
|
|
23
|
+
resourceIndex?: string;
|
|
24
|
+
associatedName?: string;
|
|
25
|
+
associatedIndex?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function getNameByParams(params: ParsedParams): string;
|
|
28
|
+
export declare function parseRequest(request: ParseRequest, options?: ParseOptions): ParsedParams | false;
|
|
29
|
+
export declare function parseQuery(input: string): any;
|
|
30
|
+
export declare function parseFields(fields: any): any;
|
|
31
|
+
export declare function mergeFields(defaults: any, inputs: any): any;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
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 utils_exports = {};
|
|
30
|
+
__export(utils_exports, {
|
|
31
|
+
getNameByParams: () => getNameByParams,
|
|
32
|
+
mergeFields: () => mergeFields,
|
|
33
|
+
parseFields: () => parseFields,
|
|
34
|
+
parseQuery: () => parseQuery,
|
|
35
|
+
parseRequest: () => parseRequest
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(utils_exports);
|
|
38
|
+
var import_lodash = __toESM(require("lodash"));
|
|
39
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
40
|
+
var import_qs = __toESM(require("qs"));
|
|
41
|
+
function getNameByParams(params) {
|
|
42
|
+
const { resourceName, associatedName } = params;
|
|
43
|
+
return associatedName ? `${associatedName}.${resourceName}` : resourceName;
|
|
44
|
+
}
|
|
45
|
+
__name(getNameByParams, "getNameByParams");
|
|
46
|
+
function parseRequest(request, options = {}) {
|
|
47
|
+
const accessors = {
|
|
48
|
+
// 常规 actions
|
|
49
|
+
list: "list",
|
|
50
|
+
create: "create",
|
|
51
|
+
get: "get",
|
|
52
|
+
update: "update",
|
|
53
|
+
delete: "destroy",
|
|
54
|
+
// associate 操作
|
|
55
|
+
add: "add",
|
|
56
|
+
set: "set",
|
|
57
|
+
remove: "remove",
|
|
58
|
+
...options.accessors || {}
|
|
59
|
+
};
|
|
60
|
+
const { regexp, keys } = (0, import_path_to_regexp.pathToRegexp)("/resourcer/{:associatedName.}:resourceName{\\::actionName}");
|
|
61
|
+
const reqPath = decodeURI(request.path);
|
|
62
|
+
const matches = regexp.exec(reqPath);
|
|
63
|
+
if (matches) {
|
|
64
|
+
const params2 = {};
|
|
65
|
+
keys.forEach((obj, index) => {
|
|
66
|
+
if (matches[index + 1] === void 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
params2[obj.name] = matches[index + 1];
|
|
70
|
+
});
|
|
71
|
+
return params2;
|
|
72
|
+
}
|
|
73
|
+
const defaults = {
|
|
74
|
+
single: {
|
|
75
|
+
"/:resourceName": {
|
|
76
|
+
get: accessors.list,
|
|
77
|
+
post: accessors.create,
|
|
78
|
+
delete: accessors.delete
|
|
79
|
+
},
|
|
80
|
+
"/:resourceName/:resourceIndex": {
|
|
81
|
+
get: accessors.get,
|
|
82
|
+
put: accessors.update,
|
|
83
|
+
patch: accessors.update,
|
|
84
|
+
delete: accessors.delete
|
|
85
|
+
},
|
|
86
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
87
|
+
get: accessors.list,
|
|
88
|
+
post: accessors.create,
|
|
89
|
+
delete: accessors.delete
|
|
90
|
+
},
|
|
91
|
+
"/:associatedName/:associatedIndex/:resourceName/:resourceIndex": {
|
|
92
|
+
get: accessors.get,
|
|
93
|
+
post: accessors.create,
|
|
94
|
+
put: accessors.update,
|
|
95
|
+
patch: accessors.update,
|
|
96
|
+
delete: accessors.delete
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
hasOne: {
|
|
100
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
101
|
+
get: accessors.get,
|
|
102
|
+
post: accessors.update,
|
|
103
|
+
put: accessors.update,
|
|
104
|
+
patch: accessors.update,
|
|
105
|
+
delete: accessors.delete
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
hasMany: {
|
|
109
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
110
|
+
get: accessors.list,
|
|
111
|
+
post: accessors.create,
|
|
112
|
+
delete: accessors.delete
|
|
113
|
+
},
|
|
114
|
+
"/:associatedName/:associatedIndex/:resourceName/:resourceIndex": {
|
|
115
|
+
get: accessors.get,
|
|
116
|
+
post: accessors.create,
|
|
117
|
+
put: accessors.update,
|
|
118
|
+
patch: accessors.update,
|
|
119
|
+
delete: accessors.delete
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
belongsTo: {
|
|
123
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
124
|
+
get: accessors.get,
|
|
125
|
+
delete: accessors.remove
|
|
126
|
+
},
|
|
127
|
+
"/:associatedName/:associatedIndex/:resourceName/:resourceIndex": {
|
|
128
|
+
post: accessors.set
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
belongsToMany: {
|
|
132
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
133
|
+
get: accessors.list,
|
|
134
|
+
post: accessors.set
|
|
135
|
+
},
|
|
136
|
+
"/:associatedName/:associatedIndex/:resourceName/:resourceIndex": {
|
|
137
|
+
get: accessors.get,
|
|
138
|
+
post: accessors.add,
|
|
139
|
+
put: accessors.update,
|
|
140
|
+
// Many to Many 的 update 是针对 through
|
|
141
|
+
patch: accessors.update,
|
|
142
|
+
// Many to Many 的 update 是针对 through
|
|
143
|
+
delete: accessors.remove
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
set: {
|
|
147
|
+
"/:associatedName/:associatedIndex/:resourceName": {
|
|
148
|
+
get: accessors.list,
|
|
149
|
+
post: accessors.add,
|
|
150
|
+
delete: accessors.remove
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const params = {};
|
|
155
|
+
let prefix = (options.prefix || "").trim().replace(/\/$/, "");
|
|
156
|
+
if (prefix && !prefix.startsWith("/")) {
|
|
157
|
+
prefix = `/${prefix}`;
|
|
158
|
+
}
|
|
159
|
+
const { type = "single" } = request;
|
|
160
|
+
for (const path in defaults[type]) {
|
|
161
|
+
const { regexp: regexp2, keys: keys2 } = (0, import_path_to_regexp.pathToRegexp)(`${prefix}${path}`);
|
|
162
|
+
const matches2 = regexp2.exec(reqPath);
|
|
163
|
+
if (!matches2) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
keys2.forEach((obj, index) => {
|
|
167
|
+
if (matches2[index + 1] === void 0) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
params[obj.name] = matches2[index + 1];
|
|
171
|
+
});
|
|
172
|
+
params.actionName = import_lodash.default.get(defaults, [type, path, request.method.toLowerCase()]);
|
|
173
|
+
}
|
|
174
|
+
if (Object.keys(params).length === 0) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (params.resourceName) {
|
|
178
|
+
const [resourceName, actionName] = params.resourceName.split(/:|@/);
|
|
179
|
+
if (actionName) {
|
|
180
|
+
params.resourceName = resourceName;
|
|
181
|
+
params.actionName = actionName;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return params;
|
|
185
|
+
}
|
|
186
|
+
__name(parseRequest, "parseRequest");
|
|
187
|
+
function parseQuery(input) {
|
|
188
|
+
const query = import_qs.default.parse(input, {
|
|
189
|
+
// 原始 query string 中如果一个键连等号“=”都没有可以被认为是 null 类型
|
|
190
|
+
strictNullHandling: true
|
|
191
|
+
// 逗号分隔转换为数组
|
|
192
|
+
// comma: true,
|
|
193
|
+
});
|
|
194
|
+
if (typeof query.filter === "string") {
|
|
195
|
+
query.filter = JSON.parse(query.filter);
|
|
196
|
+
}
|
|
197
|
+
return query;
|
|
198
|
+
}
|
|
199
|
+
__name(parseQuery, "parseQuery");
|
|
200
|
+
function parseFields(fields) {
|
|
201
|
+
if (!fields) {
|
|
202
|
+
return {};
|
|
203
|
+
}
|
|
204
|
+
if (typeof fields === "string") {
|
|
205
|
+
fields = fields.split(",").map((field) => field.trim());
|
|
206
|
+
}
|
|
207
|
+
if (Array.isArray(fields)) {
|
|
208
|
+
const onlyFields = [];
|
|
209
|
+
const output = {};
|
|
210
|
+
fields.forEach((item) => {
|
|
211
|
+
if (typeof item === "string") {
|
|
212
|
+
onlyFields.push(item);
|
|
213
|
+
} else if (typeof item === "object") {
|
|
214
|
+
if (item.only) {
|
|
215
|
+
onlyFields.push(...item.only.toString().split(","));
|
|
216
|
+
}
|
|
217
|
+
Object.assign(output, parseFields(item));
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
if (onlyFields.length) {
|
|
221
|
+
output.only = onlyFields;
|
|
222
|
+
}
|
|
223
|
+
return output;
|
|
224
|
+
}
|
|
225
|
+
if (fields.only && typeof fields.only === "string") {
|
|
226
|
+
fields.only = fields.only.split(",").map((field) => field.trim());
|
|
227
|
+
}
|
|
228
|
+
if (fields.except && typeof fields.except === "string") {
|
|
229
|
+
fields.except = fields.except.split(",").map((field) => field.trim());
|
|
230
|
+
}
|
|
231
|
+
if (fields.appends && typeof fields.appends === "string") {
|
|
232
|
+
fields.appends = fields.appends.split(",").map((field) => field.trim());
|
|
233
|
+
}
|
|
234
|
+
return fields;
|
|
235
|
+
}
|
|
236
|
+
__name(parseFields, "parseFields");
|
|
237
|
+
function mergeFields(defaults, inputs) {
|
|
238
|
+
let fields = {};
|
|
239
|
+
defaults = parseFields(defaults);
|
|
240
|
+
inputs = parseFields(inputs);
|
|
241
|
+
if (inputs.only) {
|
|
242
|
+
if (defaults.only) {
|
|
243
|
+
fields.only = defaults.only.filter((field) => inputs.only.includes(field));
|
|
244
|
+
} else if (defaults.except) {
|
|
245
|
+
fields.only = inputs.only.filter((field) => !defaults.except.includes(field));
|
|
246
|
+
} else {
|
|
247
|
+
fields.only = inputs.only;
|
|
248
|
+
}
|
|
249
|
+
} else if (inputs.except) {
|
|
250
|
+
if (defaults.only) {
|
|
251
|
+
fields.only = defaults.only.filter((field) => !inputs.except.includes(field));
|
|
252
|
+
} else {
|
|
253
|
+
fields.except = import_lodash.default.uniq([...inputs.except, ...defaults.except || []]);
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
fields = defaults;
|
|
257
|
+
}
|
|
258
|
+
if (!import_lodash.default.isEmpty(inputs.appends)) {
|
|
259
|
+
fields.appends = import_lodash.default.uniq([...inputs.appends, ...defaults.appends || []]);
|
|
260
|
+
}
|
|
261
|
+
if (!fields.appends) {
|
|
262
|
+
fields.appends = [];
|
|
263
|
+
}
|
|
264
|
+
return fields;
|
|
265
|
+
}
|
|
266
|
+
__name(mergeFields, "mergeFields");
|
|
267
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
268
|
+
0 && (module.exports = {
|
|
269
|
+
getNameByParams,
|
|
270
|
+
mergeFields,
|
|
271
|
+
parseFields,
|
|
272
|
+
parseQuery,
|
|
273
|
+
parseRequest
|
|
274
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tachybase/resourcer",
|
|
3
|
+
"version": "0.23.8",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"deepmerge": "^4.3.1",
|
|
10
|
+
"glob": "^11.0.0",
|
|
11
|
+
"koa-compose": "^4.1.0",
|
|
12
|
+
"lodash": "^4.17.21",
|
|
13
|
+
"path-to-regexp": "8.2.0",
|
|
14
|
+
"qs": "^6.13.1",
|
|
15
|
+
"@tachybase/utils": "0.23.8"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "20.17.10"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tachybase-build --no-dts @tachybase/resourcer"
|
|
22
|
+
}
|
|
23
|
+
}
|