slower 2.0.2 → 2.1.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/package.json +1 -1
- package/src/slower.js +74 -4
package/package.json
CHANGED
package/src/slower.js
CHANGED
|
@@ -80,13 +80,13 @@ class SlowerRouter {
|
|
|
80
80
|
#setRoute (method, path, handler) {
|
|
81
81
|
if (typeof method !== 'string')
|
|
82
82
|
throw new Error('<SlowerRouter>.route :: "method" parameter must be of type String');
|
|
83
|
-
if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
|
|
84
|
-
throw new Error('<SlowerRouter>.route :: "path" parameter must be of type String or RegExp');
|
|
83
|
+
if (typeof path !== 'string' && typeof path !== 'function' && path?.constructor?.name !== 'RegExp')
|
|
84
|
+
throw new Error('<SlowerRouter>.route :: "path" parameter must be of type Function, String or RegExp');
|
|
85
85
|
if (typeof handler !== 'function')
|
|
86
86
|
throw new Error('<SlowerRouter>.route :: "handler" parameter must be of type Function');
|
|
87
87
|
if (!this.layers.get(method))
|
|
88
88
|
this.layers.set(method, new Map());
|
|
89
|
-
if (typeof path === 'string')
|
|
89
|
+
if (typeof path === 'string' || path?.constructor?.name !== 'RegExp')
|
|
90
90
|
path = match(path, { decode: decodeURIComponent }); // 'path' is a function now
|
|
91
91
|
this.layers.get(method).set(path, handler);
|
|
92
92
|
return this;
|
|
@@ -165,6 +165,76 @@ class SlowerRouter {
|
|
|
165
165
|
}
|
|
166
166
|
return this;
|
|
167
167
|
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Import and use another SlowerRouter, mounted at a specific path
|
|
171
|
+
* @param {string} mountPath Mount the router at a specific path
|
|
172
|
+
* @param {SlowerRouter} SlowerRouterInstance
|
|
173
|
+
*/
|
|
174
|
+
useRouter (SlowerSubRouterInstance, mountPath = '/') {
|
|
175
|
+
const mount = p => mountPath ? (mountPath + '/' + p).replace(/\/{2,}/g, '/') : p;
|
|
176
|
+
const layers = SlowerSubRouterInstance.layers;
|
|
177
|
+
for (let layer of layers) {
|
|
178
|
+
if (!!layer.static) {
|
|
179
|
+
this.static(layer.directoryPath, mount(layer.mountPath));
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
console.log(layer)
|
|
183
|
+
this.#setRoute(layer.method, mount(layer.path), layer.handler);
|
|
184
|
+
}
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
class SlowerSubRouter {
|
|
190
|
+
constructor () {
|
|
191
|
+
this.METHODS = HTTP_VERBS;
|
|
192
|
+
this.layers = new Array();
|
|
193
|
+
|
|
194
|
+
// Create basic route shortcuts
|
|
195
|
+
// get(), post(), put(), delete()
|
|
196
|
+
for (let verb of HTTP_VERBS) {
|
|
197
|
+
this[verb] = function (path, callback) {
|
|
198
|
+
return this.#setRoute(verb, path, callback);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#setRoute (method, path, handler) {
|
|
204
|
+
if (typeof method !== 'string')
|
|
205
|
+
throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
|
|
206
|
+
if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
|
|
207
|
+
throw new Error('<SlowerSubRouter>.route :: "path" parameter must be of type Function, String or RegExp');
|
|
208
|
+
if (typeof handler !== 'function')
|
|
209
|
+
throw new Error('<SlowerSubRouter>.route :: "handler" parameter must be of type Function');
|
|
210
|
+
this.layers.push({ method, path, handler });
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
all (path, handler) {
|
|
215
|
+
if (typeof path === 'string')
|
|
216
|
+
for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
|
|
217
|
+
else if (typeof path !== 'function')
|
|
218
|
+
throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
|
|
219
|
+
else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
// Just a more comprehensive call to app.all for defining middlewares
|
|
223
|
+
use (...b) { this.all(...b); return this; };
|
|
224
|
+
|
|
225
|
+
static (directoryPath, mountPath = '') {
|
|
226
|
+
this.layers.push({ static: true, directoryPath, mountPath });
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Create basic main routing creation
|
|
232
|
+
function Slower (options) {
|
|
233
|
+
return new SlowerRouter(options);
|
|
234
|
+
}
|
|
235
|
+
// Add the subrouter class
|
|
236
|
+
Slower.Router = function () {
|
|
237
|
+
return new SlowerSubRouter();
|
|
168
238
|
}
|
|
169
239
|
|
|
170
|
-
module.exports = Slower
|
|
240
|
+
module.exports = Slower;
|