@trenskow/app 0.5.10 → 0.5.13
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 +3 -1
- package/lib/application.js +4 -2
- package/lib/endpoint.js +24 -12
- package/package.json +6 -6
- package/test/index.js +16 -1
package/README.md
CHANGED
|
@@ -497,6 +497,8 @@ Supported HTTP methods are the same as those returned by [`http.METHODS`](https:
|
|
|
497
497
|
|
|
498
498
|
You can only call these methods once per method per endpoint – calling it multiple times will result in only the last one getting used.
|
|
499
499
|
|
|
500
|
+
These also ends routing. After a method route has been called, the routing will go strait to the renderer.
|
|
501
|
+
|
|
500
502
|
> Returns the endpoint.
|
|
501
503
|
|
|
502
504
|
###### Parameters
|
|
@@ -505,7 +507,7 @@ You can only call these methods once per method per endpoint – calling it mult
|
|
|
505
507
|
| ---------- | ------------------------------ | :------------------------------------------------------: | :----------------: | :-----------: |
|
|
506
508
|
| `handlers` | A (or an array of) handlers. * | Function, AsyncFunction or Array ([see also](#handlers)) | :white_check_mark: | |
|
|
507
509
|
|
|
508
|
-
> \* When more than one handler
|
|
510
|
+
> \* When more than one handler is provided only the return value of the last handler that returned a non-undefined value will be send the the renderer.
|
|
509
511
|
|
|
510
512
|
###### Example
|
|
511
513
|
|
package/lib/application.js
CHANGED
|
@@ -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 (
|
|
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
|
@@ -178,16 +178,22 @@ export default class Endpoint extends Router {
|
|
|
178
178
|
|
|
179
179
|
async _route(path, context, next, idx = 0) {
|
|
180
180
|
|
|
181
|
-
if (idx === this._layers.length
|
|
181
|
+
if (idx === this._layers.length) {
|
|
182
182
|
|
|
183
|
-
|
|
184
|
-
.filter((layer) => layer.type === 'method')
|
|
185
|
-
.map((layer) => layer.method.toUpperCase())
|
|
186
|
-
.filter((value, index, array) => array.indexOf(value) === index);
|
|
183
|
+
if (path.isLast) {
|
|
187
184
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
185
|
+
const methods = this._layers
|
|
186
|
+
.filter((layer) => layer.type === 'method')
|
|
187
|
+
.map((layer) => layer.method.toUpperCase())
|
|
188
|
+
.filter((value, index, array) => array.indexOf(value) === index);
|
|
189
|
+
|
|
190
|
+
if (methods.length) {
|
|
191
|
+
context.response.headers.allow = methods.join(', ');
|
|
192
|
+
throw new ApiError.MethodNotAllowed();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
} else {
|
|
196
|
+
throw new ApiError.NotFound();
|
|
191
197
|
}
|
|
192
198
|
|
|
193
199
|
}
|
|
@@ -218,13 +224,19 @@ export default class Endpoint extends Router {
|
|
|
218
224
|
|
|
219
225
|
case 'method': {
|
|
220
226
|
|
|
227
|
+
let underlyingMethod = context.request.method.toLowerCase();
|
|
228
|
+
|
|
229
|
+
if (underlyingMethod === 'head') {
|
|
230
|
+
underlyingMethod = 'get';
|
|
231
|
+
}
|
|
232
|
+
|
|
221
233
|
if (layer.match === 'direct' && !path.isLast) return await next();
|
|
222
|
-
if (layer.method !== 'all' && layer.method !==
|
|
234
|
+
if (layer.method !== 'all' && layer.method !== underlyingMethod) return await next();
|
|
223
235
|
|
|
224
236
|
for (let handler of layer.handlers) {
|
|
225
237
|
const result = await handler(context);
|
|
226
238
|
if (context.state !== 'routing') return;
|
|
227
|
-
context.result = result;
|
|
239
|
+
if (typeof result !== 'undefined') context.result = result;
|
|
228
240
|
}
|
|
229
241
|
|
|
230
242
|
return context.result;
|
|
@@ -242,8 +254,8 @@ export default class Endpoint extends Router {
|
|
|
242
254
|
if (typeof layer.transform === 'function') {
|
|
243
255
|
context.parameters[layer.name] = await layer.transform(
|
|
244
256
|
Object.fromEntries([
|
|
245
|
-
[
|
|
246
|
-
[
|
|
257
|
+
['context', context],
|
|
258
|
+
[layer.name, component]
|
|
247
259
|
])
|
|
248
260
|
);
|
|
249
261
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/app",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.13",
|
|
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.
|
|
29
|
-
"mocha": "^9.
|
|
30
|
-
"supertest": "^6.
|
|
28
|
+
"eslint": "^8.13.0",
|
|
29
|
+
"mocha": "^9.2.2",
|
|
30
|
+
"supertest": "^6.2.2"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@trenskow/api-error": "^2.2.
|
|
34
|
-
"@trenskow/caseit": "^1.1.
|
|
33
|
+
"@trenskow/api-error": "^2.2.5",
|
|
34
|
+
"@trenskow/caseit": "^1.1.3"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/test/index.js
CHANGED
|
@@ -74,13 +74,28 @@ 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(
|
|
80
93
|
new Endpoint()
|
|
81
94
|
.get(() => 'Ignore this!')
|
|
82
95
|
.post(() => 'Not used')
|
|
83
|
-
.get(
|
|
96
|
+
.get(
|
|
97
|
+
() => 'Hello, World!',
|
|
98
|
+
() => { /* Ignore this */ })
|
|
84
99
|
);
|
|
85
100
|
|
|
86
101
|
await request
|