1 | 'use strict';
|
2 |
|
3 | const debug = require('debug')('grown:router');
|
4 |
|
5 | module.exports = ($, util) => {
|
6 | function _fixMethod(ctx, method) {
|
7 | return function route(path, cb) {
|
8 | const opts = {};
|
9 |
|
10 | if (typeof cb === 'function' || Array.isArray(cb)) {
|
11 | opts.pipeline = util
|
12 | .flattenArgs(Array.prototype.slice.call(arguments, 1))
|
13 | .map(x => util.buildMiddleware(x, path));
|
14 | } else if (typeof cb === 'string') {
|
15 | opts.to = cb;
|
16 | } else {
|
17 | util.extendValues(opts, cb);
|
18 | }
|
19 |
|
20 | if (typeof ctx.router.namespace === 'function') {
|
21 | ctx.router[method.toLowerCase()](path, opts);
|
22 | } else {
|
23 | throw new Error(`Expecting a valid router, given '${ctx.router}'`);
|
24 | }
|
25 |
|
26 | return ctx;
|
27 | };
|
28 | }
|
29 |
|
30 | function _dispatchRoutes(conn, options) {
|
31 | const _method = conn.req.method;
|
32 |
|
33 |
|
34 | debug('#%s Trying to resolve any route matching %s %s', conn.pid, conn.req.method, conn.req.url);
|
35 |
|
36 |
|
37 | if (!this._routes[_method]) {
|
38 | debug('#%s Error. There are no routes matching for this verb', conn.pid);
|
39 |
|
40 | throw util.buildError(405);
|
41 | }
|
42 |
|
43 |
|
44 | const _handler = this._routes[_method](conn.req.url, 1);
|
45 |
|
46 |
|
47 | if (_handler) {
|
48 |
|
49 | if (!_handler.pipeline) {
|
50 | throw new Error(`Missing pipeline, given '${util.inspect(_handler)}'`);
|
51 | }
|
52 |
|
53 |
|
54 | if (typeof _handler.callback !== 'function') {
|
55 | _handler.callback = util.buildPipeline(_handler.path, _handler.pipeline);
|
56 | }
|
57 |
|
58 | conn.req.params = conn.req.params || {};
|
59 |
|
60 | _handler.matcher.keys.forEach((key, i) => {
|
61 | conn.req.params[key] = _handler.matcher.values[i];
|
62 | });
|
63 |
|
64 | return _handler.callback(conn, options);
|
65 | }
|
66 |
|
67 | throw util.buildError(404);
|
68 | }
|
69 |
|
70 | function _groupRoutes(ctx, options) {
|
71 | this._routes = Object.create(null);
|
72 |
|
73 | const _routes = ctx.router.routes;
|
74 |
|
75 | return ctx.emit('before_routes', ctx, _routes)
|
76 | .catch(e => {
|
77 | ctx.emit('failure', e, options);
|
78 | })
|
79 | .then(() => {
|
80 |
|
81 | _routes.forEach(route => {
|
82 |
|
83 | if (!this._routes[route.verb]) {
|
84 | this._routes[route.verb] = [];
|
85 | }
|
86 |
|
87 |
|
88 | this._routes[route.verb].push(route);
|
89 | });
|
90 |
|
91 |
|
92 | Object.keys(this._routes).forEach(verb => {
|
93 | this._routes[verb] = ctx.router.map(this._routes[verb]);
|
94 | });
|
95 | });
|
96 | }
|
97 |
|
98 | return $.module('Router.Mappings', {
|
99 | _dispatchRoutes,
|
100 | _groupRoutes,
|
101 | _fixMethod,
|
102 |
|
103 | install(ctx, options) {
|
104 | const routeMappings = require('route-mappings');
|
105 |
|
106 | const _router = routeMappings();
|
107 |
|
108 |
|
109 | ctx.once('start', () => this._groupRoutes(ctx, options));
|
110 |
|
111 | ctx.mount('Router#pipe', (conn, options) => {
|
112 | try {
|
113 |
|
114 | return this._dispatchRoutes(conn, options);
|
115 | } catch (e) {
|
116 |
|
117 | if (!this.fallthrough) {
|
118 | throw e;
|
119 | }
|
120 | }
|
121 | });
|
122 |
|
123 | return {
|
124 | props: {
|
125 | router: () => _router,
|
126 | },
|
127 | methods: {
|
128 | get: this._fixMethod(ctx, 'GET'),
|
129 | put: this._fixMethod(ctx, 'PUT'),
|
130 | post: this._fixMethod(ctx, 'POST'),
|
131 | patch: this._fixMethod(ctx, 'PATCH'),
|
132 | delete: this._fixMethod(ctx, 'DELETE'),
|
133 | },
|
134 | };
|
135 | },
|
136 | });
|
137 | };
|