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