slower 2.0.2 → 2.1.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/slower.js +73 -4
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "path-to-regexp": "^6.2.2"
4
4
  },
5
5
  "name": "slower",
6
- "version": "2.0.2",
6
+ "version": "2.1.1",
7
7
  "main": "index.js",
8
8
  "devDependencies": {},
9
9
  "scripts": {
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,75 @@ 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
+ this.#setRoute(layer.method, mount(layer.path), layer.handler);
183
+ }
184
+ return this;
185
+ }
186
+ }
187
+
188
+ class SlowerSubRouter {
189
+ constructor () {
190
+ this.METHODS = HTTP_VERBS;
191
+ this.layers = new Array();
192
+
193
+ // Create basic route shortcuts
194
+ // get(), post(), put(), delete()
195
+ for (let verb of HTTP_VERBS) {
196
+ this[verb] = function (path, callback) {
197
+ return this.#setRoute(verb, path, callback);
198
+ };
199
+ }
200
+ }
201
+
202
+ #setRoute (method, path, handler) {
203
+ if (typeof method !== 'string')
204
+ throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
205
+ if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
206
+ throw new Error('<SlowerSubRouter>.route :: "path" parameter must be of type Function, String or RegExp');
207
+ if (typeof handler !== 'function')
208
+ throw new Error('<SlowerSubRouter>.route :: "handler" parameter must be of type Function');
209
+ this.layers.push({ method, path, handler });
210
+ return this;
211
+ }
212
+
213
+ all (path, handler) {
214
+ if (typeof path === 'string')
215
+ for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
216
+ else if (typeof path !== 'function')
217
+ throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
218
+ else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
219
+ return this;
220
+ }
221
+ // Just a more comprehensive call to app.all for defining middlewares
222
+ use (...b) { this.all(...b); return this; };
223
+
224
+ static (directoryPath, mountPath = '') {
225
+ this.layers.push({ static: true, directoryPath, mountPath });
226
+ return this;
227
+ }
228
+ }
229
+
230
+ // Create basic main routing creation
231
+ function Slower (options) {
232
+ return new SlowerRouter(options);
233
+ }
234
+ // Add the subrouter class
235
+ Slower.Router = function () {
236
+ return new SlowerSubRouter();
168
237
  }
169
238
 
170
- module.exports = Slower = options => new SlowerRouter(options);
239
+ module.exports = Slower;