@trenskow/app 0.6.14 → 0.7.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/lib/endpoint.js CHANGED
@@ -65,7 +65,7 @@ export default class Endpoint extends Router {
65
65
  }
66
66
 
67
67
  this._layers.push({
68
- type: 'method',
68
+ handler: this._handleMethod,
69
69
  method,
70
70
  handlers,
71
71
  match
@@ -91,7 +91,7 @@ export default class Endpoint extends Router {
91
91
  }
92
92
 
93
93
  this._layers.push({
94
- type: 'mount',
94
+ handler: this._handleMount,
95
95
  path,
96
96
  endpoint
97
97
  });
@@ -130,7 +130,7 @@ export default class Endpoint extends Router {
130
130
  }
131
131
 
132
132
  this._layers.push({
133
- type: 'parameter',
133
+ handler: this._handleParameter,
134
134
  name,
135
135
  transform,
136
136
  endpoint
@@ -168,7 +168,7 @@ export default class Endpoint extends Router {
168
168
  }
169
169
 
170
170
  this._layers.push({
171
- type: 'middleware',
171
+ handler: this._handleMiddleware,
172
172
  router
173
173
  });
174
174
 
@@ -183,7 +183,7 @@ export default class Endpoint extends Router {
183
183
  if (path.isLast) {
184
184
 
185
185
  const methods = this._layers
186
- .filter((layer) => layer.type === 'method')
186
+ .filter((layer) => layer.handler === this._handleMethod)
187
187
  .map((layer) => layer.method.toUpperCase())
188
188
  .filter((value, index, array) => array.indexOf(value) === index);
189
189
 
@@ -202,80 +202,69 @@ export default class Endpoint extends Router {
202
202
 
203
203
  }
204
204
 
205
- async _handle(layer, path, context, next) {
205
+ async _handleMount(layer, path, context, next) {
206
206
 
207
- switch (layer.type) {
207
+ return await path.pushed(async (component) => {
208
208
 
209
- case 'mount': {
210
-
211
- return await path.pushed(async (component) => {
212
-
213
- if (!component) return path.popped(next);
214
-
215
- if (matchPath(component, layer.path, context)) {
216
- return await layer.endpoint._route(path, context, next);
217
- }
218
-
219
- return path.popped(next);
220
-
221
- });
209
+ if (!component) return path.popped(next);
222
210
 
211
+ if (matchPath(component, layer.path, context)) {
212
+ return await layer.endpoint._route(path, context, next);
223
213
  }
224
214
 
225
- case 'method': {
215
+ return path.popped(next);
226
216
 
227
- let underlyingMethod = context.request.method.toLowerCase();
217
+ });
228
218
 
229
- if (underlyingMethod === 'head' && !this._layers.some((layer) => layer.method === 'head')) {
230
- underlyingMethod = 'get';
231
- }
219
+ }
232
220
 
233
- if (layer.match === 'direct' && !path.isLast) return await next();
234
- if (layer.method !== 'all' && layer.method !== underlyingMethod) return await next();
221
+ async _handleMethod(layer, path, context, next) {
235
222
 
236
- for (let handler of layer.handlers) {
237
- const result = await handler(context);
238
- if (layer.underlyingMethod === 'head') return;
239
- if (context.state !== 'routing') return;
240
- if (typeof result !== 'undefined') context.result = result;
241
- }
223
+ let underlyingMethod = context.request.method.toLowerCase();
242
224
 
243
- return context.result;
225
+ if (underlyingMethod === 'head' && !this._layers.some((layer) => layer.method === 'head')) {
226
+ underlyingMethod = 'get';
227
+ }
244
228
 
245
- }
229
+ if (layer.match === 'direct' && !path.isLast) return await next();
230
+ if (layer.method !== 'all' && layer.method !== underlyingMethod) return await next();
246
231
 
247
- case 'parameter': {
232
+ for (let handler of layer.handlers) {
233
+ const result = await handler(context);
234
+ if (layer.underlyingMethod === 'head') return;
235
+ if (context.state !== 'routing') return;
236
+ if (typeof result !== 'undefined') context.result = result;
237
+ }
248
238
 
249
- return await path.pushed(async (component) => {
239
+ return context.result;
250
240
 
251
- if (!component) return await path.popped(next);
241
+ }
252
242
 
253
- context.parameters[layer.name] = component;
243
+ async _handleParameter(layer, path, context, next) {
254
244
 
255
- if (typeof layer.transform === 'function') {
256
- context.parameters[layer.name] = await layer.transform(
257
- Object.fromEntries([
258
- ['context', context],
259
- [layer.name, component]
260
- ])
261
- );
262
- }
245
+ return await path.pushed(async (component) => {
263
246
 
264
- return await layer.endpoint._route(path, context, next);
247
+ if (!component) return await path.popped(next);
265
248
 
266
- });
249
+ context.parameters[layer.name] = component;
267
250
 
251
+ if (typeof layer.transform === 'function') {
252
+ context.parameters[layer.name] = await layer.transform(
253
+ Object.fromEntries([
254
+ ['context', context],
255
+ [layer.name, component]
256
+ ])
257
+ );
268
258
  }
269
259
 
270
- case 'middleware': {
271
- return await layer.router._route(path, context, next);
272
- }
260
+ return await layer.endpoint._route(path, context, next);
273
261
 
274
- default:
275
- return await super._handle(layer, path, context, next);
262
+ });
276
263
 
277
- }
264
+ }
278
265
 
266
+ async _handleMiddleware(layer, path, context, next) {
267
+ return await layer.router._route(path, context, next);
279
268
  }
280
269
 
281
270
  }
package/lib/index.js CHANGED
@@ -13,6 +13,16 @@ import Request from './request.js';
13
13
  import Response from './response.js';
14
14
  import ApiError from '@trenskow/api-error';
15
15
 
16
+ import { isObject, matchPath, resolveInlineImport } from './util.js';
17
+
16
18
  export default Application;
17
19
 
20
+ Application.plugin = (plugin) => {
21
+ plugin({ Router, Endpoint, Application, Request, Response, Error: ApiError, util: {
22
+ isObject,
23
+ matchPath,
24
+ resolveInlineImport
25
+ } });
26
+ };
27
+
18
28
  export { Router, Endpoint, Application, Request, Response, ApiError as Error };
package/lib/router.js CHANGED
@@ -27,7 +27,7 @@ export default class Router {
27
27
  }
28
28
 
29
29
  this._layers.push({
30
- type: 'use',
30
+ handler: this._handleUse,
31
31
  handlers
32
32
  });
33
33
 
@@ -48,7 +48,7 @@ export default class Router {
48
48
  }
49
49
 
50
50
  this._layers.push({
51
- type: 'mixin',
51
+ handler: this._handleMixin,
52
52
  router: router
53
53
  });
54
54
 
@@ -68,27 +68,23 @@ export default class Router {
68
68
 
69
69
  }
70
70
 
71
- async _handle(layer, path, context, next) {
72
-
73
- switch (layer.type) {
74
-
75
- case 'use': {
71
+ async _handleUse(layer, _, context, next) {
76
72
 
77
- for (let handler of layer.handlers) {
78
- await handler(context);
79
- if (context.state !== 'routing') return;
80
- }
81
-
82
- return await next();
73
+ for (let handler of layer.handlers) {
74
+ await handler(context);
75
+ if (context.state !== 'routing') return;
76
+ }
83
77
 
84
- }
78
+ return await next();
85
79
 
86
- case 'mixin': {
87
- return await layer.router._route(path, context, next);
88
- }
80
+ }
89
81
 
90
- }
82
+ async _handleMixin(layer, path, context, next) {
83
+ return await layer.router._route(path, context, next);
84
+ }
91
85
 
86
+ async _handle(layer, path, context, next) {
87
+ return await layer.handler.call(this, layer, path, context, next);
92
88
  }
93
89
 
94
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/app",
3
- "version": "0.6.14",
3
+ "version": "0.7.0",
4
4
  "description": "A small HTTP router.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -25,12 +25,12 @@
25
25
  },
26
26
  "homepage": "https://github.com/trenskow/app#readme",
27
27
  "devDependencies": {
28
- "eslint": "^8.13.0",
28
+ "eslint": "^8.20.0",
29
29
  "mocha": "^10.0.0",
30
- "supertest": "^6.2.2"
30
+ "supertest": "^6.2.4"
31
31
  },
32
32
  "dependencies": {
33
- "@trenskow/api-error": "^2.2.6",
33
+ "@trenskow/api-error": "^2.3.2",
34
34
  "@trenskow/caseit": "^1.3.0"
35
35
  }
36
36
  }