UNPKG

3.83 kBTypeScriptView Raw
1import type { GrownInterface, GrownUtils, Plug } from '@grown/bud';
2
3declare module '@grown/router' {
4 interface RouteMappings {
5 mappings: UrlMap;
6 routes: UrlInfo;
7
8 namespace(path: string, group: RoutesCallback): this;
9 resources(path: string, opts?: RouteOptions): this;
10 resource(path: string, opts?: RouteOptions): this;
11
12 get(path: string, handler: string): this;
13 get(path: string, opts: RouteOptions, handler: string): this;
14
15 post(path: string, handler: string): this;
16 post(path: string, opts: RouteOptions, handler: string): this;
17
18 put(path: string, handler: string): this;
19 put(path: string, opts: RouteOptions, handler: string): this;
20
21 patch(path: string, handler: string): this;
22 patch(path: string, opts: RouteOptions, handler: string): this;
23
24 delete(path: string, handler: string): this;
25 delete(path: string, opts: RouteOptions, handler: string): this;
26 }
27
28 interface RoutesCallback {
29 (router: RouterFactory): RouteMappings;
30 }
31
32 interface RouterFactory {
33 (opts?: RouteOptions): RouteMappings;
34 }
35
36 type ControllerInfo = {
37 instance: any;
38 definition: any;
39 };
40
41 type PipelineInfo = {
42 call: [Object, string];
43 name: string;
44 type: string;
45 };
46
47 type UrlParam = string | number | { [key: string]: string | number; };
48
49 /**
50 Registered routes can be accesed by alias, path or index.
51
52 Names are set from their `as` option or from its path, e.g. `/foo/bar => foo.bar`
53
54 Example
55 ```js
56 app.router.get('/:foo/bar/baz', { as: 'baz' }, 'Foo');
57 app.router.get('/:buzz', 'Bar');
58
59 app.router.mappings.baz.url({ foo: 42 });
60 app.router.mappings.buzz.url();
61 ```
62 */
63 interface UrlInfo {
64 /**
65 Renders URL from its template, e.g. `/:foo` will render `/42` if `{ foo: 42 }` is given.
66 @param args List of values to render in the URL template, can be scalars or objects
67 */
68 (...args: UrlParam[]): string;
69 [key: string]: UrlInfo;
70 url: UrlInfo;
71 }
72
73 interface UrlMap {
74 /**
75 FIXME MAP
76 @param path Named route to render from, e.g. `foo.bar`
77 @param args List of values to render in the URL template, can be scalars or objects
78 */
79 (path: string, ...args: UrlParam[]): UrlInfo | string;
80 [key: string]: UrlInfo;
81 }
82
83 type RouteOptions = {
84 to?: string;
85 as?: string;
86 };
87
88 /**
89 ROUTER CLASS
90 */
91 type RouterClass = {
92 Controllers: Plug;
93 Mappings: Plug;
94 };
95
96 /**
97 Router PLUG
98 */
99 type RouterPlug = {
100 Router: RouterClass;
101 };
102}
103
104declare module '@grown/server' {
105 interface Connection {
106 /**
107 Router CHECK
108 */
109 routes: UrlMap;
110 }
111}
112
113declare module '@grown/bud' {
114 interface GrownInterface {
115 /**
116 Router PROP
117 */
118 Router: RouterClass;
119 }
120
121 interface GrownInstance {
122 /**
123 Router PROP II
124 */
125 router: RouteMappings;
126
127 namespace(path: string, group: RoutesCallback): RouteMappings;
128 resources(path: string, opts?: RouteOptions): RouteMappings;
129 resource(path: string, opts?: RouteOptions): RouteMappings;
130
131 get(path: string, handler: string): RouteMappings;
132 get(path: string, opts: RouteOptions, handler: string): RouteMappings;
133
134 post(path: string, handler: string): RouteMappings;
135 post(path: string, opts: RouteOptions, handler: string): RouteMappings;
136
137 put(path: string, handler: string): RouteMappings;
138 put(path: string, opts: RouteOptions, handler: string): RouteMappings;
139
140 patch(path: string, handler: string): RouteMappings;
141 patch(path: string, opts: RouteOptions, handler: string): RouteMappings;
142
143 delete(path: string, handler: string): RouteMappings;
144 delete(path: string, opts: RouteOptions, handler: string): RouteMappings;
145 }
146}
147
148/**
149Router WRAPPER
150*/
151declare function RouterPlug(ctx: GrownInterface, util: GrownUtils): RouterClass;
152
153export default RouterPlug;