@eggjs/router 4.0.0-beta.34 → 4.0.0-beta.36
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/dist/EggRouter.d.ts +104 -101
- package/dist/EggRouter.js +199 -231
- package/dist/Layer.d.ts +115 -111
- package/dist/Layer.js +201 -238
- package/dist/Router.d.ts +490 -489
- package/dist/Router.js +677 -778
- package/dist/index.d.ts +9 -7
- package/dist/index.js +9 -6
- package/dist/types.d.ts +18 -15
- package/package.json +33 -39
- package/dist/types.js +0 -2
package/dist/EggRouter.js
CHANGED
|
@@ -1,241 +1,209 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { encodeURIComponent as safeEncodeURIComponent } from 'utility';
|
|
3
|
-
import { singularize, pluralize } from 'inflection';
|
|
4
|
-
import methods from 'methods';
|
|
5
|
-
import { isGeneratorFunction } from 'is-type-of';
|
|
6
|
-
import { Layer } from "./Layer.js";
|
|
1
|
+
import "./Layer.js";
|
|
7
2
|
import { Router } from "./Router.js";
|
|
8
|
-
import
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import methods from "methods";
|
|
5
|
+
import { isGeneratorFunction } from "is-type-of";
|
|
6
|
+
import { encodeURIComponent } from "utility";
|
|
7
|
+
import { pluralize, singularize } from "inflection";
|
|
8
|
+
|
|
9
|
+
//#region src/EggRouter.ts
|
|
9
10
|
const REST_MAP = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
11
|
+
index: {
|
|
12
|
+
suffix: "",
|
|
13
|
+
method: "GET"
|
|
14
|
+
},
|
|
15
|
+
new: {
|
|
16
|
+
namePrefix: "new_",
|
|
17
|
+
member: true,
|
|
18
|
+
suffix: "new",
|
|
19
|
+
method: "GET"
|
|
20
|
+
},
|
|
21
|
+
create: {
|
|
22
|
+
suffix: "",
|
|
23
|
+
method: "POST"
|
|
24
|
+
},
|
|
25
|
+
show: {
|
|
26
|
+
member: true,
|
|
27
|
+
suffix: ":id",
|
|
28
|
+
method: "GET"
|
|
29
|
+
},
|
|
30
|
+
edit: {
|
|
31
|
+
member: true,
|
|
32
|
+
namePrefix: "edit_",
|
|
33
|
+
suffix: ":id/edit",
|
|
34
|
+
method: "GET"
|
|
35
|
+
},
|
|
36
|
+
update: {
|
|
37
|
+
member: true,
|
|
38
|
+
namePrefix: "",
|
|
39
|
+
suffix: ":id",
|
|
40
|
+
method: ["PATCH", "PUT"]
|
|
41
|
+
},
|
|
42
|
+
destroy: {
|
|
43
|
+
member: true,
|
|
44
|
+
namePrefix: "destroy_",
|
|
45
|
+
suffix: ":id",
|
|
46
|
+
method: "DELETE"
|
|
47
|
+
}
|
|
47
48
|
};
|
|
48
49
|
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const values = args[key];
|
|
169
|
-
const encodedKey = safeEncodeURIComponent(key);
|
|
170
|
-
if (Array.isArray(values)) {
|
|
171
|
-
for (const val of values) {
|
|
172
|
-
queries.push(`${encodedKey}=${safeEncodeURIComponent(String(val))}`);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
queries.push(`${encodedKey}=${safeEncodeURIComponent(String(values))}`);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if (queries.length > 0) {
|
|
181
|
-
const queryStr = queries.join('&');
|
|
182
|
-
if (!url.includes('?')) {
|
|
183
|
-
url = `${url}?${queryStr}`;
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
url = `${url}&${queryStr}`;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return url;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* @alias to url()
|
|
193
|
-
*/
|
|
194
|
-
pathFor(name, params) {
|
|
195
|
-
return this.url(name, params);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
50
|
+
* FIXME: move these patch into @eggjs/router
|
|
51
|
+
*/
|
|
52
|
+
var EggRouter = class extends Router {
|
|
53
|
+
app;
|
|
54
|
+
/**
|
|
55
|
+
* @class
|
|
56
|
+
* @param {Object} opts - Router options.
|
|
57
|
+
* @param {Application} app - Application object.
|
|
58
|
+
*/
|
|
59
|
+
constructor(opts, app) {
|
|
60
|
+
super(opts);
|
|
61
|
+
this.app = app;
|
|
62
|
+
}
|
|
63
|
+
verb(method, nameOrPath, pathOrMiddleware, ...middleware) {
|
|
64
|
+
const { path, middlewares, options } = this._formatRouteParams(nameOrPath, pathOrMiddleware, middleware);
|
|
65
|
+
if (typeof method === "string") method = [method];
|
|
66
|
+
this.register(path, method, middlewares, options);
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
head(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
70
|
+
return this.verb("head", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
71
|
+
}
|
|
72
|
+
options(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
73
|
+
return this.verb("options", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
74
|
+
}
|
|
75
|
+
get(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
76
|
+
return this.verb("get", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
77
|
+
}
|
|
78
|
+
put(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
79
|
+
return this.verb("put", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
80
|
+
}
|
|
81
|
+
patch(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
82
|
+
return this.verb("patch", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
83
|
+
}
|
|
84
|
+
post(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
85
|
+
return this.verb("post", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
86
|
+
}
|
|
87
|
+
delete(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
88
|
+
return this.verb("delete", nameOrPath, pathOrMiddleware, ...middlewares);
|
|
89
|
+
}
|
|
90
|
+
all(nameOrPath, pathOrMiddleware, ...middlewares) {
|
|
91
|
+
return this.verb(methods, nameOrPath, pathOrMiddleware, ...middlewares);
|
|
92
|
+
}
|
|
93
|
+
register(path, methods$1, middleware, opts) {
|
|
94
|
+
middleware = Array.isArray(middleware) ? middleware : [middleware];
|
|
95
|
+
for (const mw of middleware) if (isGeneratorFunction(mw)) throw new TypeError(methods$1.toString() + " `" + path + "`: Please use async function instead of generator function");
|
|
96
|
+
const middlewares = convertMiddlewares(middleware, this.app);
|
|
97
|
+
return super.register(path, methods$1, middlewares, opts);
|
|
98
|
+
}
|
|
99
|
+
resources(nameOrPath, pathOrMiddleware, ...middleware) {
|
|
100
|
+
const { path, middlewares, options } = this._formatRouteParams(nameOrPath, pathOrMiddleware, middleware);
|
|
101
|
+
const controller = resolveController(middlewares.pop(), this.app);
|
|
102
|
+
for (const key in REST_MAP) {
|
|
103
|
+
const action = controller[key];
|
|
104
|
+
if (!action) continue;
|
|
105
|
+
const opts = REST_MAP[key];
|
|
106
|
+
let routeName;
|
|
107
|
+
if (opts.member) routeName = singularize(options.name ?? "");
|
|
108
|
+
else routeName = pluralize(options.name ?? "");
|
|
109
|
+
if (opts.namePrefix) routeName = opts.namePrefix + routeName;
|
|
110
|
+
const prefix = path.replace(/\/$/, "");
|
|
111
|
+
const urlPath = opts.suffix ? `${prefix}/${opts.suffix}` : prefix;
|
|
112
|
+
const method = Array.isArray(opts.method) ? opts.method : [opts.method];
|
|
113
|
+
this.register(urlPath, method, middlewares.concat(action), { name: routeName });
|
|
114
|
+
}
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @param {String} name - Router name
|
|
119
|
+
* @param {Object} params - more parameters
|
|
120
|
+
* @example
|
|
121
|
+
* ```js
|
|
122
|
+
* router.url('edit_post', { id: 1, name: 'foo', page: 2 })
|
|
123
|
+
* => /posts/1/edit?name=foo&page=2
|
|
124
|
+
* router.url('posts', { name: 'foo&1', page: 2 })
|
|
125
|
+
* => /posts?name=foo%261&page=2
|
|
126
|
+
* ```
|
|
127
|
+
* @return {String} url by path name and query params.
|
|
128
|
+
* @since 1.0.0
|
|
129
|
+
*/
|
|
130
|
+
url(name, params) {
|
|
131
|
+
const route = this.route(name);
|
|
132
|
+
if (!route) return "";
|
|
133
|
+
const args = params;
|
|
134
|
+
let url = route.path;
|
|
135
|
+
assert(!(url instanceof RegExp), `Can't get the url for regExp ${url} for by name '${name}'`);
|
|
136
|
+
const queries = [];
|
|
137
|
+
if (typeof args === "object" && args !== null) {
|
|
138
|
+
const replacedParams = [];
|
|
139
|
+
url = url.replace(/:([a-zA-Z_]\w*)/g, ($0, key) => {
|
|
140
|
+
if (key in args) {
|
|
141
|
+
const values = args[key];
|
|
142
|
+
replacedParams.push(key);
|
|
143
|
+
return encodeURIComponent(Array.isArray(values) ? String(values[0]) : String(values));
|
|
144
|
+
}
|
|
145
|
+
return $0;
|
|
146
|
+
});
|
|
147
|
+
for (const key in args) {
|
|
148
|
+
if (replacedParams.includes(key)) continue;
|
|
149
|
+
const values = args[key];
|
|
150
|
+
const encodedKey = encodeURIComponent(key);
|
|
151
|
+
if (Array.isArray(values)) for (const val of values) queries.push(`${encodedKey}=${encodeURIComponent(String(val))}`);
|
|
152
|
+
else queries.push(`${encodedKey}=${encodeURIComponent(String(values))}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (queries.length > 0) {
|
|
156
|
+
const queryStr = queries.join("&");
|
|
157
|
+
if (!url.includes("?")) url = `${url}?${queryStr}`;
|
|
158
|
+
else url = `${url}&${queryStr}`;
|
|
159
|
+
}
|
|
160
|
+
return url;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @alias to url()
|
|
164
|
+
*/
|
|
165
|
+
pathFor(name, params) {
|
|
166
|
+
return this.url(name, params);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
198
169
|
/**
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
170
|
+
* resolve controller from string to function
|
|
171
|
+
* @param {String|Function} controller input controller
|
|
172
|
+
* @param {Application} app egg application instance
|
|
173
|
+
*/
|
|
203
174
|
function resolveController(controller, app) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// ensure controller is exists
|
|
216
|
-
if (!controller)
|
|
217
|
-
throw new Error('controller not exists');
|
|
218
|
-
return controller;
|
|
175
|
+
if (typeof controller === "string") {
|
|
176
|
+
const actions = controller.split(".");
|
|
177
|
+
let obj = app.controller;
|
|
178
|
+
actions.forEach((key) => {
|
|
179
|
+
obj = obj[key];
|
|
180
|
+
if (!obj) throw new Error(`app.controller.${controller} not exists`);
|
|
181
|
+
});
|
|
182
|
+
controller = obj;
|
|
183
|
+
}
|
|
184
|
+
if (!controller) throw new Error("controller not exists");
|
|
185
|
+
return controller;
|
|
219
186
|
}
|
|
220
187
|
/**
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
188
|
+
* 1. ensure controller(last argument) support string
|
|
189
|
+
* - [url, controller]: app.get('/home', 'home');
|
|
190
|
+
* - [name, url, controller(string)]: app.get('posts', '/posts', 'posts.list');
|
|
191
|
+
* - [name, url, controller]: app.get('posts', '/posts', app.controller.posts.list);
|
|
192
|
+
* - [name, url(regexp), controller]: app.get('regRouter', /\/home\/index/, 'home.index');
|
|
193
|
+
* - [name, url, middleware, [...], controller]: `app.get(/user/:id', hasLogin, canGetUser, 'user.show');`
|
|
194
|
+
*
|
|
195
|
+
* 2. bind ctx to controller `this`
|
|
196
|
+
*
|
|
197
|
+
* @param {Array} middlewares middlewares and controller(last middleware)
|
|
198
|
+
* @param {Application} app egg application instance
|
|
199
|
+
*/
|
|
233
200
|
function convertMiddlewares(middlewares, app) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
return [...middlewares, wrappedController];
|
|
201
|
+
const controller = resolveController(middlewares.pop(), app);
|
|
202
|
+
function wrappedController(ctx, next) {
|
|
203
|
+
return controller.apply(ctx, [ctx, next]);
|
|
204
|
+
}
|
|
205
|
+
return [...middlewares, wrappedController];
|
|
240
206
|
}
|
|
241
|
-
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
export { EggRouter };
|