Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit eb74f0d

Browse files
committed
Refactor methods, name for methods no required
1 parent 82dd23d commit eb74f0d

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

lib/index.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,47 +56,47 @@ class Maker {
5656
* @param {string} method
5757
* @param {function[]} listeners
5858
*/
59-
method(name, method, listeners) {
60-
if (typeof name !== 'string') {
61-
throw new Error('Path should be string')
59+
method(method, listeners) {
60+
let name = ''
61+
if (typeof listeners[0] === 'string') {
62+
name = listeners.splice(0, 1)[0]
6263
}
63-
if (name.indexOf('/') !== 0) {
64-
throw new Error('Path should start from /')
65-
}
66-
if (name.match(/\//g).length > 1) {
64+
name = name.replace(/^\//gm, '')
65+
66+
if ((name.match(/\//g) || []).length > 1) {
6767
throw new Error('Path should not be deep')
6868
}
6969
if (listeners.length === 0) {
7070
throw new Error('Maybe you forget to add listener?')
7171
}
7272

73-
if (name === '/') {
74-
return this.local(method, listeners)
75-
}
73+
if (name !== '') {
74+
let scoped = this.ctx.scoped[name]
75+
if (!scoped) {
76+
scoped = new Maker()
77+
this.ctx.scoped[name] = scoped
78+
}
7679

77-
const absName = name.replace('/', '')
78-
let scoped = this.ctx.scoped[absName]
79-
if (!scoped) {
80-
scoped = new Maker()
81-
this.ctx.scoped[absName] = scoped
80+
scoped.local(method, listeners)
8281
}
83-
scoped.local(method, listeners)
82+
83+
this.local(method, listeners)
8484
}
8585

86-
post(name, ...listeners) {
87-
this.method(name, 'POST', listeners)
86+
post(...listeners) {
87+
this.method('POST', listeners)
8888
}
89-
get(name, ...listeners) {
90-
this.method(name, 'GET', listeners)
89+
get(...listeners) {
90+
this.method('GET', listeners)
9191
}
92-
put(name, ...listeners) {
93-
this.method(name, 'PUT', listeners)
92+
put(...listeners) {
93+
this.method('PUT', listeners)
9494
}
95-
delete(name, ...listeners) {
96-
this.method(name, 'DELETE', listeners)
95+
delete(...listeners) {
96+
this.method('DELETE', listeners)
9797
}
98-
patch(name, ...listeners) {
99-
this.method(name, 'PATCH', listeners)
98+
patch(...listeners) {
99+
this.method('PATCH', listeners)
100100
}
101101
}
102102

0 commit comments

Comments
 (0)