@trenskow/app 0.5.11 → 0.5.12

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.
@@ -64,8 +64,10 @@ export default class Application extends EventEmitter {
64
64
  this.#_rootEndpoint = new Endpoint()
65
65
  .use(() => { throw new ApiError.NotFound(); });
66
66
 
67
- this.#_renderer = async ({ result, response }) => {
68
- if (result instanceof ApiError) {
67
+ this.#_renderer = async ({ result, request, response }) => {
68
+ if (request.method.toLowerCase() === 'head') {
69
+ response.end();
70
+ } else if (result instanceof ApiError) {
69
71
  response.end();
70
72
  } else if (typeof result === 'string') {
71
73
  response.headers.contentType = 'text/plain; charset=utf-8';
package/lib/endpoint.js CHANGED
@@ -224,8 +224,14 @@ export default class Endpoint extends Router {
224
224
 
225
225
  case 'method': {
226
226
 
227
+ let underlyingMethod = context.request.method.toLowerCase();
228
+
229
+ if (underlyingMethod === 'head') {
230
+ underlyingMethod = 'get';
231
+ }
232
+
227
233
  if (layer.match === 'direct' && !path.isLast) return await next();
228
- if (layer.method !== 'all' && layer.method !== context.request.method.toLowerCase()) return await next();
234
+ if (layer.method !== 'all' && layer.method !== underlyingMethod) return await next();
229
235
 
230
236
  for (let handler of layer.handlers) {
231
237
  const result = await handler(context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/app",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "description": "A small HTTP router.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/test/index.js CHANGED
@@ -74,6 +74,19 @@ 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 () => {
78
+
79
+ app.root(
80
+ new Endpoint()
81
+ .get(() => 'Hello, World!')
82
+ );
83
+
84
+ await request
85
+ .head('/')
86
+ .expect(200, undefined);
87
+
88
+ });
89
+
77
90
  it ('should ignore multiple GET method handlers and respond with 200 and `Hello, World!`.', async () => {
78
91
 
79
92
  app.root(