@eggjs/router 2.0.0 → 3.0.0
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/README.md +92 -55
- package/dist/commonjs/EggRouter.d.ts +102 -0
- package/dist/commonjs/EggRouter.js +235 -0
- package/dist/commonjs/Layer.d.ts +114 -0
- package/dist/commonjs/Layer.js +237 -0
- package/dist/commonjs/Router.d.ts +494 -0
- package/dist/commonjs/Router.js +760 -0
- package/dist/commonjs/index.d.ts +5 -0
- package/dist/commonjs/index.js +23 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/types.d.ts +18 -0
- package/dist/commonjs/types.js +3 -0
- package/dist/esm/EggRouter.d.ts +102 -0
- package/dist/esm/EggRouter.js +228 -0
- package/dist/esm/Layer.d.ts +114 -0
- package/dist/esm/Layer.js +230 -0
- package/dist/esm/Router.d.ts +494 -0
- package/dist/esm/Router.js +753 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/types.d.ts +18 -0
- package/dist/esm/types.js +2 -0
- package/package.json +64 -32
- package/src/EggRouter.ts +338 -0
- package/src/Layer.ts +274 -0
- package/src/Router.ts +1060 -0
- package/src/index.ts +7 -0
- package/src/types.ts +15 -0
- package/History.md +0 -181
- package/index.js +0 -10
- package/lib/egg_router.js +0 -327
- package/lib/layer.js +0 -217
- package/lib/router.js +0 -728
- package/lib/utils.js +0 -18
package/README.md
CHANGED
|
@@ -3,53 +3,60 @@
|
|
|
3
3
|
Router core component for [Egg.js](https://github.com/eggjs).
|
|
4
4
|
|
|
5
5
|
> **This repository is a fork of [koa-router](https://github.com/alexmingoia/koa-router).** with some additional features.
|
|
6
|
-
|
|
7
|
-
> And thanks for the greate work of @alexmingoia and the original team.
|
|
6
|
+
> And thanks for the great work of @alexmingoia and the original team.
|
|
8
7
|
|
|
9
8
|
## API Reference
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
- [@eggjs/router](#eggjsrouter)
|
|
11
|
+
- [API Reference](#api-reference)
|
|
12
|
+
- [Router ⏏](#router-)
|
|
13
|
+
- [new Router(\[opts\])](#new-routeropts)
|
|
14
|
+
- [router.get|put|post|patch|delete|del ⇒ Router](#routergetputpostpatchdeletedel--router)
|
|
15
|
+
- [Named routes](#named-routes)
|
|
16
|
+
- [Multiple middleware](#multiple-middleware)
|
|
17
|
+
- [Nested routers](#nested-routers)
|
|
18
|
+
- [Router prefixes](#router-prefixes)
|
|
19
|
+
- [URL parameters](#url-parameters)
|
|
20
|
+
- [router.routes ⇒ function](#routerroutes--function)
|
|
21
|
+
- [router.use(\[path\], middleware) ⇒ Router](#routerusepath-middleware--router)
|
|
22
|
+
- [router.prefix(prefix) ⇒ Router](#routerprefixprefix--router)
|
|
23
|
+
- [router.allowedMethods(\[options\]) ⇒ function](#routerallowedmethodsoptions--function)
|
|
24
|
+
- [router.redirect(source, destination, \[code\]) ⇒ Router](#routerredirectsource-destination-code--router)
|
|
25
|
+
- [router.route(name) ⇒ Layer | false](#routerroutename--layer--false)
|
|
26
|
+
- [router.url(name, params, \[options\]) ⇒ String | Error](#routerurlname-params-options--string--error)
|
|
27
|
+
- [router.param(param, middleware) ⇒ Router](#routerparamparam-middleware--router)
|
|
28
|
+
- [Router.url(path, params \[, options\]) ⇒ String](#routerurlpath-params--options--string)
|
|
29
|
+
- [Tests](#tests)
|
|
30
|
+
- [Breaking changes on v3](#breaking-changes-on-v3)
|
|
31
|
+
- [License](#license)
|
|
26
32
|
|
|
27
33
|
<a name="exp_module_egg-router--Router"></a>
|
|
28
34
|
|
|
29
35
|
### Router ⏏
|
|
36
|
+
|
|
30
37
|
**Kind**: Exported class
|
|
31
38
|
<a name="new_module_egg-router--Router_new"></a>
|
|
32
39
|
|
|
33
40
|
#### new Router([opts])
|
|
34
|
-
Create a new router.
|
|
35
41
|
|
|
42
|
+
Create a new router.
|
|
36
43
|
|
|
37
44
|
| Param | Type | Description |
|
|
38
|
-
| ---
|
|
45
|
+
| --- | --- | --- |
|
|
39
46
|
| [opts] | <code>Object</code> | |
|
|
40
47
|
| [opts.prefix] | <code>String</code> | prefix router paths |
|
|
41
48
|
|
|
42
49
|
**Example**
|
|
43
50
|
Basic usage:
|
|
44
51
|
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
```ts
|
|
53
|
+
import Koa from '@eggjs/koa';
|
|
54
|
+
import Router from '@eggjs/router';
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
const app = new Koa();
|
|
57
|
+
const router = new Router();
|
|
51
58
|
|
|
52
|
-
router.get('/', (ctx, next) => {
|
|
59
|
+
router.get('/', async (ctx, next) => {
|
|
53
60
|
// ctx.router available
|
|
54
61
|
});
|
|
55
62
|
|
|
@@ -57,9 +64,11 @@ app
|
|
|
57
64
|
.use(router.routes())
|
|
58
65
|
.use(router.allowedMethods());
|
|
59
66
|
```
|
|
67
|
+
|
|
60
68
|
<a name="module_egg-router--Router+get|put|post|patch|delete|del"></a>
|
|
61
69
|
|
|
62
70
|
#### router.get|put|post|patch|delete|del ⇒ <code>Router</code>
|
|
71
|
+
|
|
63
72
|
Create `router.verb()` methods, where *verb* is one of the HTTP verbs such
|
|
64
73
|
as `router.get()` or `router.post()`.
|
|
65
74
|
|
|
@@ -68,7 +77,7 @@ where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()
|
|
|
68
77
|
|
|
69
78
|
Additionaly, `router.all()` can be used to match against all methods.
|
|
70
79
|
|
|
71
|
-
```
|
|
80
|
+
```ts
|
|
72
81
|
router
|
|
73
82
|
.get('/', (ctx, next) => {
|
|
74
83
|
ctx.body = 'Hello World!';
|
|
@@ -87,8 +96,8 @@ router
|
|
|
87
96
|
});
|
|
88
97
|
```
|
|
89
98
|
|
|
90
|
-
When a route is matched, its path is available at `ctx.
|
|
91
|
-
the name is available at `ctx.
|
|
99
|
+
When a route is matched, its path is available at `ctx.routePath` and if named,
|
|
100
|
+
the name is available at `ctx.routeName`
|
|
92
101
|
|
|
93
102
|
Route paths will be translated to regular expressions using
|
|
94
103
|
[path-to-regexp](https://github.com/pillarjs/path-to-regexp).
|
|
@@ -100,7 +109,7 @@ Query strings will not be considered when matching requests.
|
|
|
100
109
|
Routes can optionally have names. This allows generation of URLs and easy
|
|
101
110
|
renaming of URLs during development.
|
|
102
111
|
|
|
103
|
-
```
|
|
112
|
+
```ts
|
|
104
113
|
router.get('user', '/users/:id', (ctx, next) => {
|
|
105
114
|
// ...
|
|
106
115
|
});
|
|
@@ -113,7 +122,7 @@ router.url('user', 3);
|
|
|
113
122
|
|
|
114
123
|
Multiple middleware may be given:
|
|
115
124
|
|
|
116
|
-
```
|
|
125
|
+
```ts
|
|
117
126
|
router.get(
|
|
118
127
|
'/users/:id',
|
|
119
128
|
(ctx, next) => {
|
|
@@ -133,9 +142,9 @@ router.get(
|
|
|
133
142
|
|
|
134
143
|
Nesting routers is supported:
|
|
135
144
|
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
|
|
145
|
+
```ts
|
|
146
|
+
const forums = new Router();
|
|
147
|
+
const posts = new Router();
|
|
139
148
|
|
|
140
149
|
posts.get('/', (ctx, next) => {...});
|
|
141
150
|
posts.get('/:pid', (ctx, next) => {...});
|
|
@@ -149,8 +158,8 @@ app.use(forums.routes());
|
|
|
149
158
|
|
|
150
159
|
Route paths can be prefixed at the router level:
|
|
151
160
|
|
|
152
|
-
```
|
|
153
|
-
|
|
161
|
+
```ts
|
|
162
|
+
const router = new Router({
|
|
154
163
|
prefix: '/users'
|
|
155
164
|
});
|
|
156
165
|
|
|
@@ -162,7 +171,7 @@ router.get('/:id', ...); // responds to "/users/:id"
|
|
|
162
171
|
|
|
163
172
|
Named route parameters are captured and added to `ctx.params`.
|
|
164
173
|
|
|
165
|
-
```
|
|
174
|
+
```ts
|
|
166
175
|
router.get('/:category/:title', (ctx, next) => {
|
|
167
176
|
console.log(ctx.params);
|
|
168
177
|
// => { category: 'programming', title: 'how-to-node' }
|
|
@@ -175,7 +184,7 @@ used to convert paths to regular expressions.
|
|
|
175
184
|
**Kind**: instance property of <code>[Router](#exp_module_egg-router--Router)</code>
|
|
176
185
|
|
|
177
186
|
| Param | Type | Description |
|
|
178
|
-
| ---
|
|
187
|
+
| --- | --- | --- |
|
|
179
188
|
| path | <code>String</code> | |
|
|
180
189
|
| [middleware] | <code>function</code> | route middleware(s) |
|
|
181
190
|
| callback | <code>function</code> | route callback |
|
|
@@ -189,6 +198,7 @@ Returns router middleware which dispatches a route matching the request.
|
|
|
189
198
|
<a name="module_egg-router--Router+use"></a>
|
|
190
199
|
|
|
191
200
|
#### router.use([path], middleware) ⇒ <code>Router</code>
|
|
201
|
+
|
|
192
202
|
Use given middleware.
|
|
193
203
|
|
|
194
204
|
Middleware run in the order they are defined by `.use()`. They are invoked
|
|
@@ -204,7 +214,8 @@ sequentially, requests start at the first middleware and work their way
|
|
|
204
214
|
| [...] | <code>function</code> |
|
|
205
215
|
|
|
206
216
|
**Example**
|
|
207
|
-
|
|
217
|
+
|
|
218
|
+
```ts
|
|
208
219
|
// session middleware will run before authorize
|
|
209
220
|
router
|
|
210
221
|
.use(session())
|
|
@@ -218,9 +229,11 @@ router.use(['/users', '/admin'], userAuth());
|
|
|
218
229
|
|
|
219
230
|
app.use(router.routes());
|
|
220
231
|
```
|
|
232
|
+
|
|
221
233
|
<a name="module_egg-router--Router+prefix"></a>
|
|
222
234
|
|
|
223
235
|
#### router.prefix(prefix) ⇒ <code>Router</code>
|
|
236
|
+
|
|
224
237
|
Set the path prefix for a Router instance that was already initialized.
|
|
225
238
|
|
|
226
239
|
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>
|
|
@@ -230,12 +243,15 @@ Set the path prefix for a Router instance that was already initialized.
|
|
|
230
243
|
| prefix | <code>String</code> |
|
|
231
244
|
|
|
232
245
|
**Example**
|
|
233
|
-
|
|
246
|
+
|
|
247
|
+
```ts
|
|
234
248
|
router.prefix('/things/:thing_id')
|
|
235
249
|
```
|
|
250
|
+
|
|
236
251
|
<a name="module_egg-router--Router+allowedMethods"></a>
|
|
237
252
|
|
|
238
253
|
#### router.allowedMethods([options]) ⇒ <code>function</code>
|
|
254
|
+
|
|
239
255
|
Returns separate middleware for responding to `OPTIONS` requests with
|
|
240
256
|
an `Allow` header containing the allowed methods, as well as responding
|
|
241
257
|
with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
|
@@ -250,12 +266,13 @@ with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
|
|
250
266
|
| [options.methodNotAllowed] | <code>function</code> | throw the returned value in place of the default MethodNotAllowed error |
|
|
251
267
|
|
|
252
268
|
**Example**
|
|
253
|
-
```javascript
|
|
254
|
-
var Koa = require('koa');
|
|
255
|
-
var Router = require('egg-router');
|
|
256
269
|
|
|
257
|
-
|
|
258
|
-
|
|
270
|
+
```ts
|
|
271
|
+
import Koa from '@eggjs/koa';
|
|
272
|
+
import Router from '@eggjs/router';
|
|
273
|
+
|
|
274
|
+
const app = new Koa();
|
|
275
|
+
const router = new Router();
|
|
259
276
|
|
|
260
277
|
app.use(router.routes());
|
|
261
278
|
app.use(router.allowedMethods());
|
|
@@ -263,13 +280,13 @@ app.use(router.allowedMethods());
|
|
|
263
280
|
|
|
264
281
|
**Example with [Boom](https://github.com/hapijs/boom)**
|
|
265
282
|
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
283
|
+
```ts
|
|
284
|
+
import Koa from '@eggjs/koa';
|
|
285
|
+
import Router from '@eggjs/router';
|
|
286
|
+
import Boom from 'boom';
|
|
270
287
|
|
|
271
|
-
|
|
272
|
-
|
|
288
|
+
const app = new Koa();
|
|
289
|
+
const router = new Router();
|
|
273
290
|
|
|
274
291
|
app.use(router.routes());
|
|
275
292
|
app.use(router.allowedMethods({
|
|
@@ -278,9 +295,11 @@ app.use(router.allowedMethods({
|
|
|
278
295
|
methodNotAllowed: () => new Boom.methodNotAllowed()
|
|
279
296
|
}));
|
|
280
297
|
```
|
|
298
|
+
|
|
281
299
|
<a name="module_egg-router--Router+redirect"></a>
|
|
282
300
|
|
|
283
301
|
#### router.redirect(source, destination, [code]) ⇒ <code>Router</code>
|
|
302
|
+
|
|
284
303
|
Redirect `source` to `destination` URL with optional 30x status `code`.
|
|
285
304
|
|
|
286
305
|
Both `source` and `destination` can be route names.
|
|
@@ -291,7 +310,7 @@ router.redirect('/login', 'sign-in');
|
|
|
291
310
|
|
|
292
311
|
This is equivalent to:
|
|
293
312
|
|
|
294
|
-
```
|
|
313
|
+
```ts
|
|
295
314
|
router.all('/login', ctx => {
|
|
296
315
|
ctx.redirect('/sign-in');
|
|
297
316
|
ctx.status = 301;
|
|
@@ -309,6 +328,7 @@ router.all('/login', ctx => {
|
|
|
309
328
|
<a name="module_egg-router--Router+route"></a>
|
|
310
329
|
|
|
311
330
|
#### router.route(name) ⇒ <code>Layer</code> | <code>false</code>
|
|
331
|
+
|
|
312
332
|
Lookup route with given `name`.
|
|
313
333
|
|
|
314
334
|
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>
|
|
@@ -320,6 +340,7 @@ Lookup route with given `name`.
|
|
|
320
340
|
<a name="module_egg-router--Router+url"></a>
|
|
321
341
|
|
|
322
342
|
#### router.url(name, params, [options]) ⇒ <code>String</code> | <code>Error</code>
|
|
343
|
+
|
|
323
344
|
Generate URL for route. Takes a route name and map of named `params`.
|
|
324
345
|
|
|
325
346
|
**Kind**: instance method of <code>[Router](#exp_module_egg-router--Router)</code>
|
|
@@ -332,7 +353,8 @@ Generate URL for route. Takes a route name and map of named `params`.
|
|
|
332
353
|
| [options.query] | <code>Object</code> | <code>String</code> | query options |
|
|
333
354
|
|
|
334
355
|
**Example**
|
|
335
|
-
|
|
356
|
+
|
|
357
|
+
```ts
|
|
336
358
|
router.get('user', '/users/:id', (ctx, next) => {
|
|
337
359
|
// ...
|
|
338
360
|
});
|
|
@@ -354,9 +376,11 @@ router.url('user', { id: 3 }, { query: { limit: 1 } });
|
|
|
354
376
|
router.url('user', { id: 3 }, { query: "limit=1" });
|
|
355
377
|
// => "/users/3?limit=1"
|
|
356
378
|
```
|
|
379
|
+
|
|
357
380
|
<a name="module_egg-router--Router+param"></a>
|
|
358
381
|
|
|
359
382
|
#### router.param(param, middleware) ⇒ <code>Router</code>
|
|
383
|
+
|
|
360
384
|
Run middleware for named route parameters. Useful for auto-loading or
|
|
361
385
|
validation.
|
|
362
386
|
|
|
@@ -368,7 +392,8 @@ validation.
|
|
|
368
392
|
| middleware | <code>function</code> |
|
|
369
393
|
|
|
370
394
|
**Example**
|
|
371
|
-
|
|
395
|
+
|
|
396
|
+
```ts
|
|
372
397
|
router
|
|
373
398
|
.param('user', (id, ctx, next) => {
|
|
374
399
|
ctx.user = users[id];
|
|
@@ -386,9 +411,11 @@ router
|
|
|
386
411
|
// /users/3 => {"id": 3, "name": "Alex"}
|
|
387
412
|
// /users/3/friends => [{"id": 4, "name": "TJ"}]
|
|
388
413
|
```
|
|
414
|
+
|
|
389
415
|
<a name="module_egg-router--Router.url"></a>
|
|
390
416
|
|
|
391
417
|
#### Router.url(path, params [, options]) ⇒ <code>String</code>
|
|
418
|
+
|
|
392
419
|
Generate URL from url pattern and given `params`.
|
|
393
420
|
|
|
394
421
|
**Kind**: static method of <code>[Router](#exp_module_egg-router--Router)</code>
|
|
@@ -401,8 +428,9 @@ Generate URL from url pattern and given `params`.
|
|
|
401
428
|
| [options.query] | <code>Object</code> | <code>String</code> | query options |
|
|
402
429
|
|
|
403
430
|
**Example**
|
|
404
|
-
|
|
405
|
-
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
const url = Router.url('/users/:id', {id: 1});
|
|
406
434
|
// => "/users/1"
|
|
407
435
|
|
|
408
436
|
const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
|
|
@@ -412,3 +440,12 @@ const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
|
|
|
412
440
|
## Tests
|
|
413
441
|
|
|
414
442
|
Run tests using `npm test`.
|
|
443
|
+
|
|
444
|
+
## Breaking changes on v3
|
|
445
|
+
|
|
446
|
+
- Drop generator function support
|
|
447
|
+
- Drop Node.js < 18.7.0 support
|
|
448
|
+
|
|
449
|
+
## License
|
|
450
|
+
|
|
451
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { RegisterOptions, Router, RouterMethod, RouterOptions } from './Router.js';
|
|
2
|
+
import { MiddlewareFunc, ResourcesController } from './types.js';
|
|
3
|
+
interface Application {
|
|
4
|
+
controller: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* FIXME: move these patch into @eggjs/router
|
|
8
|
+
*/
|
|
9
|
+
export declare class EggRouter extends Router {
|
|
10
|
+
readonly app: Application;
|
|
11
|
+
/**
|
|
12
|
+
* @class
|
|
13
|
+
* @param {Object} opts - Router options.
|
|
14
|
+
* @param {Application} app - Application object.
|
|
15
|
+
*/
|
|
16
|
+
constructor(opts: RouterOptions, app: Application);
|
|
17
|
+
verb(method: RouterMethod | RouterMethod[], nameOrPath: string | RegExp | (string | RegExp)[], pathOrMiddleware: string | RegExp | (string | RegExp)[] | MiddlewareFunc, ...middleware: (MiddlewareFunc | string)[]): this;
|
|
18
|
+
head(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
19
|
+
head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
20
|
+
options(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
21
|
+
options(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
22
|
+
get(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
23
|
+
get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
24
|
+
put(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
25
|
+
put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
26
|
+
patch(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
27
|
+
patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
28
|
+
post(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
29
|
+
post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
30
|
+
delete(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
31
|
+
delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
32
|
+
all(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
33
|
+
all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): Router;
|
|
34
|
+
register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | string | (MiddlewareFunc | string | ResourcesController)[], opts?: RegisterOptions): import("./Layer.js").Layer | import("./Layer.js").Layer[];
|
|
35
|
+
/**
|
|
36
|
+
* restful router api
|
|
37
|
+
* @param {String} name - Router name
|
|
38
|
+
* @param {String} prefix - url prefix
|
|
39
|
+
* @param {Function} middleware - middleware or controller
|
|
40
|
+
* @example
|
|
41
|
+
* ```js
|
|
42
|
+
* app.resources('/posts', 'posts')
|
|
43
|
+
* app.resources('posts', '/posts', 'posts')
|
|
44
|
+
* app.resources('posts', '/posts', app.role.can('user'), app.controller.posts)
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* Examples:
|
|
48
|
+
*
|
|
49
|
+
* ```js
|
|
50
|
+
* app.resources('/posts', 'posts')
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* yield router mapping
|
|
54
|
+
*
|
|
55
|
+
* Method | Path | Route Name | Controller.Action
|
|
56
|
+
* -------|-----------------|----------------|-----------------------------
|
|
57
|
+
* GET | /posts | posts | app.controller.posts.index
|
|
58
|
+
* GET | /posts/new | new_post | app.controller.posts.new
|
|
59
|
+
* GET | /posts/:id | post | app.controller.posts.show
|
|
60
|
+
* GET | /posts/:id/edit | edit_post | app.controller.posts.edit
|
|
61
|
+
* POST | /posts | posts | app.controller.posts.create
|
|
62
|
+
* PATCH | /posts/:id | post | app.controller.posts.update
|
|
63
|
+
* DELETE | /posts/:id | post | app.controller.posts.destroy
|
|
64
|
+
*
|
|
65
|
+
* app.router.url can generate url based on arguments
|
|
66
|
+
* ```js
|
|
67
|
+
* app.router.url('posts')
|
|
68
|
+
* => /posts
|
|
69
|
+
* app.router.url('post', { id: 1 })
|
|
70
|
+
* => /posts/1
|
|
71
|
+
* app.router.url('new_post')
|
|
72
|
+
* => /posts/new
|
|
73
|
+
* app.router.url('edit_post', { id: 1 })
|
|
74
|
+
* => /posts/1/edit
|
|
75
|
+
* ```
|
|
76
|
+
* @return {Router} return route object.
|
|
77
|
+
* @since 1.0.0
|
|
78
|
+
*/
|
|
79
|
+
resources(prefix: string, controller: string | ResourcesController): Router;
|
|
80
|
+
resources(prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): Router;
|
|
81
|
+
resources(name: string, prefix: string, controller: string | ResourcesController): Router;
|
|
82
|
+
resources(name: string, prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): Router;
|
|
83
|
+
/**
|
|
84
|
+
* @param {String} name - Router name
|
|
85
|
+
* @param {Object} params - more parameters
|
|
86
|
+
* @example
|
|
87
|
+
* ```js
|
|
88
|
+
* router.url('edit_post', { id: 1, name: 'foo', page: 2 })
|
|
89
|
+
* => /posts/1/edit?name=foo&page=2
|
|
90
|
+
* router.url('posts', { name: 'foo&1', page: 2 })
|
|
91
|
+
* => /posts?name=foo%261&page=2
|
|
92
|
+
* ```
|
|
93
|
+
* @return {String} url by path name and query params.
|
|
94
|
+
* @since 1.0.0
|
|
95
|
+
*/
|
|
96
|
+
url(name: string, params?: Record<string, string | number | (string | number)[]>): string;
|
|
97
|
+
/**
|
|
98
|
+
* @alias to url()
|
|
99
|
+
*/
|
|
100
|
+
pathFor(name: string, params?: Record<string, string | number | (string | number)[]>): string;
|
|
101
|
+
}
|
|
102
|
+
export {};
|