slower 2.1.4 → 2.1.6

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 +177 -39
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.1.4",
6
+ "version": "2.1.6",
7
7
  "main": "index.js",
8
8
  "devDependencies": {},
9
9
  "scripts": {
package/src/slower.js CHANGED
@@ -34,20 +34,24 @@ class SlowerRouter {
34
34
  this.layers = new Map();
35
35
 
36
36
  // Create basic route shortcuts
37
- // get(), post(), put(), delete()
38
- // They can be used with either 1 or 2 params
37
+ // get(), post(), put(), delete(), ...
38
+ // They can be used with either 1 or more params
39
39
  /**
40
- * app.get({path}, {handler}) -> apply handler for a specific route
40
+ * app.get({path}, {handler}) -> apply handlers for a specific route
41
+ * app.get({path}, {handler}, {handler}, ...) -> apply handlers for a specific route
41
42
  * or
42
- * app.get({handler}) -> apply handler for any route for a specific method
43
+ * app.get({handler}) -> apply handlers for any route for a specific method
44
+ * app.get({handler}, {handler}, ...) -> apply handlers for any route for a specific method
43
45
  *
44
46
  */
45
47
  for (let verb of HTTP_VERBS) {
46
48
  this.layers.set(verb, new Map());
47
- this[verb] = function (path, callback) {
49
+ this[verb] = function (path, ...callbacks) {
48
50
  if (typeof path === 'function')
49
- return this.#setRoute(verb, MATCH_ANY, path);
50
- return this.#setRoute(verb, path, callback);
51
+ this.#setRoute(verb, MATCH_ANY, path);
52
+ for (let callback of callbacks)
53
+ this.#setRoute(verb, path, callback);
54
+ return this;
51
55
  };
52
56
  }
53
57
 
@@ -111,7 +115,7 @@ class SlowerRouter {
111
115
  * Create a middleware for all HTTP methods, for a specific path
112
116
  * @overload
113
117
  * @param {String} path
114
- * @param {Function} handler
118
+ * @param {...Function} handlers
115
119
  * @returns {SlowerRouter}
116
120
  * @example Applies a middleware for all methods, for a specific path
117
121
  * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
@@ -121,7 +125,7 @@ class SlowerRouter {
121
125
  * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
122
126
  * @overload
123
127
  * @param {string} verb (one of the HTTP verbs)
124
- * @param {Function} handler
128
+ * @param {...Function} handlers
125
129
  * @returns {SlowerRouter}
126
130
  * @example Applies a middleware for all GET requests
127
131
  * app.all('get', (req, res, next) => {console.log(...); next()})
@@ -129,28 +133,39 @@ class SlowerRouter {
129
133
  /**
130
134
  * Create a global middleware (all paths and all HTTP methods)
131
135
  * @overload
132
- * @param {Function} handler
136
+ * @param {...Function} handlers
133
137
  * @returns {SlowerRouter}
134
138
  * @example Applies a middleware for all HTTP requests (of any method)
135
139
  * app.all((req, res, next) => {console.log(...); next()})
136
140
  */
137
- all (path, handler) {
138
- // function signature: app.all({METHOD}, {HANDLER})
141
+ all (path, ...handlers) {
142
+
143
+ // function signature: app.all({METHOD}, ...{HANDLERS})
139
144
  // example: app.all('post', () => {});
140
145
  if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
141
- this.#setRoute(path, MATCH_ANY, handler);
146
+ for (let handler of handlers)
147
+ this.#setRoute(path, MATCH_ANY, handler);
148
+
142
149
 
143
- // function signature: app.all({PATH}, {HANDLER})
150
+ // function signature: app.all({PATH}, ...{HANDLERS})
144
151
  // example: app.all('/api', () => {});
145
152
  else if (typeof path === 'string')
146
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
147
-
148
- // function signature: app.all({HANDLER})
153
+ for (let handler of handlers)
154
+ for (let verb of HTTP_VERBS)
155
+ this.#setRoute(verb, path, handler);
156
+
157
+ // function signature: app.all(...{HANDLERS})
149
158
  // example: app.all(() => {});
150
159
  else if (typeof path === 'function')
151
- for (let verb of HTTP_VERBS) this.#setRoute(verb, MATCH_ANY, path);
160
+ for (let verb of HTTP_VERBS) {
161
+ this.#setRoute(verb, MATCH_ANY, path);
162
+ for (let handler of handlers)
163
+ this.#setRoute(verb, MATCH_ANY, handler);
164
+ }
152
165
 
166
+
153
167
  else throw new Error('<SlowerRouter>.use :: "handler" parameter must be of type Function');
168
+
154
169
  return this;
155
170
  }
156
171
  // Just a more comprehensive call to app.all for defining middlewares
@@ -251,11 +266,34 @@ class SlowerSubRouter {
251
266
  this.METHODS = HTTP_VERBS;
252
267
  this.layers = new Array();
253
268
 
269
+ // OLD version: (Do not delete until new version is properly tested)
270
+ // Create basic route shortcuts
271
+ // get(), post(), put(), delete()
272
+ // for (let verb of HTTP_VERBS) {
273
+ // this[verb] = function (path, callback) {
274
+ // return this.#setRoute(verb, path, callback);
275
+ // };
276
+ // }
277
+
278
+
254
279
  // Create basic route shortcuts
255
- // get(), post(), put(), delete()
280
+ // get(), post(), put(), delete(), ...
281
+ // They can be used with either 1 or more params
282
+ /**
283
+ * app.get({path}, {handler}) -> apply handlers for a specific route
284
+ * app.get({path}, {handler}, {handler}, ...) -> apply handlers for a specific route
285
+ * or
286
+ * app.get({handler}) -> apply handlers for any route for a specific method
287
+ * app.get({handler}, {handler}, ...) -> apply handlers for any route for a specific method
288
+ *
289
+ */
256
290
  for (let verb of HTTP_VERBS) {
257
- this[verb] = function (path, callback) {
258
- return this.#setRoute(verb, path, callback);
291
+ this[verb] = function (path, ...callbacks) {
292
+ if (typeof path === 'function')
293
+ this.#setRoute(verb, MATCH_ANY, path);
294
+ for (let callback of callbacks)
295
+ this.#setRoute(verb, path, callback);
296
+ return this;
259
297
  };
260
298
  }
261
299
  }
@@ -271,12 +309,62 @@ class SlowerSubRouter {
271
309
  return this;
272
310
  }
273
311
 
274
- all (path, handler) {
275
- if (typeof path === 'string')
276
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
277
- else if (typeof path !== 'function')
278
- throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
279
- else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
312
+ // Add middleware
313
+ /**
314
+ * Create a middleware for all HTTP methods, for a specific path
315
+ * @overload
316
+ * @param {String} path
317
+ * @param {...Function} handlers
318
+ * @returns {SlowerRouter}
319
+ * @example Applies a middleware for all methods, for a specific path
320
+ * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
321
+ */
322
+ /**
323
+ * Create a global middleware for a specific verb (all paths for that method)
324
+ * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
325
+ * @overload
326
+ * @param {string} verb (one of the HTTP verbs)
327
+ * @param {...Function} handlers
328
+ * @returns {SlowerRouter}
329
+ * @example Applies a middleware for all GET requests
330
+ * app.all('get', (req, res, next) => {console.log(...); next()})
331
+ */
332
+ /**
333
+ * Create a global middleware (all paths and all HTTP methods)
334
+ * @overload
335
+ * @param {...Function} handlers
336
+ * @returns {SlowerRouter}
337
+ * @example Applies a middleware for all HTTP requests (of any method)
338
+ * app.all((req, res, next) => {console.log(...); next()})
339
+ */
340
+ all (path, ...handlers) {
341
+
342
+ // function signature: app.all({METHOD}, ...{HANDLERS})
343
+ // example: app.all('post', () => {});
344
+ if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
345
+ for (let handler of handlers)
346
+ this.#setRoute(path, MATCH_ANY, handler);
347
+
348
+
349
+ // function signature: app.all({PATH}, ...{HANDLERS})
350
+ // example: app.all('/api', () => {});
351
+ else if (typeof path === 'string')
352
+ for (let handler of handlers)
353
+ for (let verb of HTTP_VERBS)
354
+ this.#setRoute(verb, path, handler);
355
+
356
+ // function signature: app.all(...{HANDLERS})
357
+ // example: app.all(() => {});
358
+ else if (typeof path === 'function')
359
+ for (let verb of HTTP_VERBS) {
360
+ this.#setRoute(verb, MATCH_ANY, path);
361
+ for (let handler of handlers)
362
+ this.#setRoute(verb, MATCH_ANY, handler);
363
+ }
364
+
365
+
366
+ else throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
367
+
280
368
  return this;
281
369
  }
282
370
  // Just a more comprehensive call to app.all for defining middlewares
@@ -321,8 +409,9 @@ class SlowerMicroRouter {
321
409
  // Create basic route shortcuts
322
410
  // get(), post(), put(), delete()
323
411
  for (let verb of HTTP_VERBS) {
324
- this[verb] = function (callback) {
325
- return this.#setRoute(verb, callback);
412
+ this[verb] = function (...callbacks) {
413
+ for (let callback of callbacks)
414
+ this.#setRoute(verb, callback);
326
415
  };
327
416
  }
328
417
  }
@@ -330,22 +419,71 @@ class SlowerMicroRouter {
330
419
  #setRoute (method, handler) {
331
420
  const path = this.path;
332
421
  if (typeof method !== 'string')
333
- throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
422
+ throw new Error('<SlowerMicroRouter>.route :: "method" parameter must be of type String');
334
423
  if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
335
- throw new Error('<SlowerSubRouter>.route :: "path" parameter must be of type Function, String or RegExp');
424
+ throw new Error('<SlowerMicroRouter>.route :: "path" parameter must be of type Function, String or RegExp');
336
425
  if (typeof handler !== 'function')
337
- throw new Error('<SlowerSubRouter>.route :: "handler" parameter must be of type Function');
426
+ throw new Error('<SlowerMicroRouter>.route :: "handler" parameter must be of type Function');
338
427
  this.layers.push({ method, path, handler });
339
428
  return this;
340
429
  }
341
430
 
342
- all (handler) {
343
- const path = this.path;
344
- if (typeof path === 'string')
345
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
346
- else if (typeof path !== 'function')
347
- throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
348
- else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
431
+ // Add middleware
432
+ /**
433
+ * Create a middleware for all HTTP methods, for a specific path
434
+ * @overload
435
+ * @param {String} path
436
+ * @param {...Function} handlers
437
+ * @returns {SlowerRouter}
438
+ * @example Applies a middleware for all methods, for a specific path
439
+ * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
440
+ */
441
+ /**
442
+ * Create a global middleware for a specific verb (all paths for that method)
443
+ * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
444
+ * @overload
445
+ * @param {string} verb (one of the HTTP verbs)
446
+ * @param {...Function} handlers
447
+ * @returns {SlowerRouter}
448
+ * @example Applies a middleware for all GET requests
449
+ * app.all('get', (req, res, next) => {console.log(...); next()})
450
+ */
451
+ /**
452
+ * Create a global middleware (all paths and all HTTP methods)
453
+ * @overload
454
+ * @param {...Function} handlers
455
+ * @returns {SlowerRouter}
456
+ * @example Applies a middleware for all HTTP requests (of any method)
457
+ * app.all((req, res, next) => {console.log(...); next()})
458
+ */
459
+ all (path, ...handlers) {
460
+
461
+ // function signature: app.all({METHOD}, ...{HANDLERS})
462
+ // example: app.all('post', () => {});
463
+ if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
464
+ for (let handler of handlers)
465
+ this.#setRoute(path, MATCH_ANY, handler);
466
+
467
+
468
+ // function signature: app.all({PATH}, ...{HANDLERS})
469
+ // example: app.all('/api', () => {});
470
+ else if (typeof path === 'string')
471
+ for (let handler of handlers)
472
+ for (let verb of HTTP_VERBS)
473
+ this.#setRoute(verb, path, handler);
474
+
475
+ // function signature: app.all(...{HANDLERS})
476
+ // example: app.all(() => {});
477
+ else if (typeof path === 'function')
478
+ for (let verb of HTTP_VERBS) {
479
+ this.#setRoute(verb, MATCH_ANY, path);
480
+ for (let handler of handlers)
481
+ this.#setRoute(verb, MATCH_ANY, handler);
482
+ }
483
+
484
+
485
+ else throw new Error('<SlowerMicroRouter>.use :: "handler" parameter must be of type Function');
486
+
349
487
  return this;
350
488
  }
351
489
  // Just a more comprehensive call to app.all for defining middlewares