@trenskow/app 0.5.7 → 0.5.11
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/application.js +9 -2
- package/lib/endpoint.js +28 -14
- package/package.json +1 -1
package/lib/application.js
CHANGED
|
@@ -256,7 +256,7 @@ export default class Application extends EventEmitter {
|
|
|
256
256
|
({ error, brutally } = error);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
if (!['completed', 'aborted'].includes(state)) {
|
|
259
|
+
if (!['completed', 'aborted', 'ignored'].includes(state)) {
|
|
260
260
|
state = 'aborted';
|
|
261
261
|
if (error) context.result = error;
|
|
262
262
|
}
|
|
@@ -269,6 +269,11 @@ export default class Application extends EventEmitter {
|
|
|
269
269
|
});
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
},
|
|
273
|
+
ignore: () => {
|
|
274
|
+
if (!['completed', 'aborted', 'ignored'].includes(state)) {
|
|
275
|
+
state = 'ignored';
|
|
276
|
+
}
|
|
272
277
|
}
|
|
273
278
|
|
|
274
279
|
};
|
|
@@ -321,8 +326,10 @@ export default class Application extends EventEmitter {
|
|
|
321
326
|
});
|
|
322
327
|
});
|
|
323
328
|
|
|
329
|
+
if (state === 'ignored') return;
|
|
330
|
+
|
|
324
331
|
if (context.response.statusCode === 200) {
|
|
325
|
-
context.response.statusCode = context.result ? 200 : 204;
|
|
332
|
+
context.response.statusCode = typeof context.result !== 'undefined' ? 200 : 204;
|
|
326
333
|
}
|
|
327
334
|
|
|
328
335
|
} catch (error) {
|
package/lib/endpoint.js
CHANGED
|
@@ -121,6 +121,10 @@ export default class Endpoint extends Router {
|
|
|
121
121
|
|
|
122
122
|
endpoint = resolveInlineImport(endpoint);
|
|
123
123
|
|
|
124
|
+
if (typeof transform !== 'undefined' && typeof transform !== 'function') {
|
|
125
|
+
throw new Error('Transforms must be a function.');
|
|
126
|
+
}
|
|
127
|
+
|
|
124
128
|
if (!(endpoint instanceof Endpoint)) {
|
|
125
129
|
throw new Error('Endpoints must be of type Endpoint');
|
|
126
130
|
}
|
|
@@ -174,16 +178,22 @@ export default class Endpoint extends Router {
|
|
|
174
178
|
|
|
175
179
|
async _route(path, context, next, idx = 0) {
|
|
176
180
|
|
|
177
|
-
if (idx === this._layers.length
|
|
181
|
+
if (idx === this._layers.length) {
|
|
178
182
|
|
|
179
|
-
|
|
180
|
-
.filter((layer) => layer.type === 'method')
|
|
181
|
-
.map((layer) => layer.method.toUpperCase())
|
|
182
|
-
.filter((value, index, array) => array.indexOf(value) === index);
|
|
183
|
+
if (path.isLast) {
|
|
183
184
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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();
|
|
187
197
|
}
|
|
188
198
|
|
|
189
199
|
}
|
|
@@ -233,12 +243,16 @@ export default class Endpoint extends Router {
|
|
|
233
243
|
|
|
234
244
|
if (!component) return await path.popped(next);
|
|
235
245
|
|
|
236
|
-
context.parameters[layer.name] =
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
246
|
+
context.parameters[layer.name] = component;
|
|
247
|
+
|
|
248
|
+
if (typeof layer.transform === 'function') {
|
|
249
|
+
context.parameters[layer.name] = await layer.transform(
|
|
250
|
+
Object.fromEntries([
|
|
251
|
+
['context', context],
|
|
252
|
+
[layer.name, component]
|
|
253
|
+
])
|
|
254
|
+
);
|
|
255
|
+
}
|
|
242
256
|
|
|
243
257
|
return await layer.endpoint._route(path, context, next);
|
|
244
258
|
|