This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (91 loc) · 2.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
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
47
48
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
'use strict';
const Router = require('find-my-way');
const compose = require('koa-compose');
const httpMethods = require('http').METHODS;
function getRouteHandler(r) {
if (r.handler) {
return r.handler;
}
if (!r.handlers) {
throw new Error(`Route ${r.method} ${r.path} does not have a handler`);
}
return r.handlers.length > 1 ? compose(r.handlers) : r.handlers[0];
}
function KoaRouter() {
if (!(this instanceof KoaRouter)) {
return new KoaRouter();
}
return Router.apply(this, arguments);
}
KoaRouter.prototype = Object.create(Router.prototype);
KoaRouter.prototype.constructor = KoaRouter;
// Static methods
// Untouched find-my-way function for compatibility
KoaRouter._sanitizeUrl = function _sanitizeUrl (url) {
for (var i = 0, len = url.length; i < len; i++) {
var charCode = url.charCodeAt(i);
if (charCode === 63 || charCode === 59 || charCode === 35) {
return url.slice(0, i);
}
}
return url;
};
// Instance methods
// Get rid of "store" (useless since koa has ctx) and allow multiple middlewares
// Make this method chainable
KoaRouter.prototype.on = function on (method, path, opts, ...handlers) {
if (typeof opts === 'function') {
handlers.unshift(opts);
opts = {};
}
let store;
if (handlers.length && typeof handlers[handlers.length -1] !== 'function') {
store = handlers.pop();
}
if (!handlers.length) {
throw new Error(`A handler must be defined for ${method} - ${path}`);
}
const handler = handlers.length > 1 ? compose(handlers) : handlers[0];
Router.prototype.on.call(this, method, path, opts, handler, store);
return this;
};
KoaRouter.prototype.lookup = function lookup (ctx, next) {
const handle = this.find(ctx.req.method, this.constructor._sanitizeUrl(ctx.req.url), this.versioning.deriveVersion(ctx.req, ctx));
if (handle !== null) {
ctx.params = handle.params;
return handle.handler(ctx, next);
}
if (this.defaultRoute !== null) {
return this.defaultRoute(ctx, next);
}
};
KoaRouter.prototype.load = function load(obj) {
const routes = typeof obj === 'object' && 'routes' in obj ? obj.routes : obj;
if (!Array.isArray(routes)) {
throw new Error('Invalid routes object');
}
routes.forEach(r => {
const handler = getRouteHandler(r);
if (r.method === 'ALL') {
return this.on(httpMethods, r.path, handler);
}
this.on(r.method, r.path, handler);
});
return this;
};
// Export router middleware
// Cannot name it "routes", that keyword is already used in find-my-way
KoaRouter.prototype.middleware = function middleware () {
return this.lookup.bind(this);
};
// Change method signature for multiple handlers, ...handlers
KoaRouter.prototype.all = function all (path, ...handlers) {
this.on(httpMethods, path, ...handlers);
return this;
};
httpMethods.forEach(m => {
KoaRouter.prototype[m.toLowerCase()] = function (path, ...handlers) {
return this.on(m, path, ...handlers);
};
});
module.exports = KoaRouter;