@trenskow/app 0.5.13 → 0.6.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.
package/README.md CHANGED
@@ -499,6 +499,8 @@ You can only call these methods once per method per endpoint – calling it mult
499
499
 
500
500
  These also ends routing. After a method route has been called, the routing will go strait to the renderer.
501
501
 
502
+ > Notice: If no `head` method is implemented on endpoint, `get` will instead be called (if availble). When client requests a `head` the result will be ignored.
503
+
502
504
  > Returns the endpoint.
503
505
 
504
506
  ###### Parameters
package/lib/endpoint.js CHANGED
@@ -226,7 +226,7 @@ export default class Endpoint extends Router {
226
226
 
227
227
  let underlyingMethod = context.request.method.toLowerCase();
228
228
 
229
- if (underlyingMethod === 'head') {
229
+ if (underlyingMethod === 'head' && !this._layers.some((layer) => layer.method === 'head')) {
230
230
  underlyingMethod = 'get';
231
231
  }
232
232
 
@@ -235,6 +235,7 @@ export default class Endpoint extends Router {
235
235
 
236
236
  for (let handler of layer.handlers) {
237
237
  const result = await handler(context);
238
+ if (layer.underlyingMethod === 'head') return;
238
239
  if (context.state !== 'routing') return;
239
240
  if (typeof result !== 'undefined') context.result = result;
240
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/app",
3
- "version": "0.5.13",
3
+ "version": "0.6.1",
4
4
  "description": "A small HTTP router.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -31,6 +31,6 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@trenskow/api-error": "^2.2.5",
34
- "@trenskow/caseit": "^1.1.3"
34
+ "@trenskow/caseit": "^1.3.0"
35
35
  }
36
36
  }
package/test/index.js CHANGED
@@ -74,7 +74,24 @@ describe('Application', () => {
74
74
 
75
75
  });
76
76
 
77
- it ('should respond with 200 and no content when a GET method is configured and HEAD is requested.', async () => {
77
+ it ('should respond with 200 when HEAD method is configured and HEAD method is requested', async () => {
78
+
79
+ app.root(
80
+ new Endpoint()
81
+ .get(() => 'Hello, World!')
82
+ .head(({ response }) => {
83
+ response.statusCode = 204;
84
+ return 'Hello, World!';
85
+ })
86
+ );
87
+
88
+ await request
89
+ .head('/')
90
+ .expect(204, undefined);
91
+
92
+ });
93
+
94
+ it ('should respond with 200 and no content when a GET method is configured (but no HEAD method) and HEAD is requested.', async () => {
78
95
 
79
96
  app.root(
80
97
  new Endpoint()