@trenskow/app 0.6.14 → 0.7.2
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/.vscode/launch.json +1 -1
- package/README.md +20 -15
- package/lib/endpoint.js +44 -55
- package/lib/index.js +10 -0
- package/lib/router.js +14 -18
- package/package.json +4 -4
package/.vscode/launch.json
CHANGED
package/README.md
CHANGED
|
@@ -123,21 +123,26 @@ import { Application, Endpoint } from '@trenskow/app';
|
|
|
123
123
|
|
|
124
124
|
const app = new Application({ port: 8080 });
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
.renderer(async ({ result, response }) => {
|
|
126
|
+
try {
|
|
127
|
+
|
|
128
|
+
const root = new Endpoint()
|
|
129
|
+
.mount('iam', await import('./iam.js'));
|
|
130
|
+
|
|
131
|
+
const renderer = async ({ result, response }) => {
|
|
133
132
|
response.headers.contentType = 'text/plain';
|
|
134
133
|
response.end(result);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
await app
|
|
137
|
+
.root(root)
|
|
138
|
+
.renderer(renderer)
|
|
139
|
+
.start();
|
|
140
|
+
|
|
141
|
+
console.info(`Application is running on port ${app.port}`)
|
|
142
|
+
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error(error);
|
|
145
|
+
}
|
|
141
146
|
````
|
|
142
147
|
|
|
143
148
|
````javascript
|
|
@@ -510,8 +515,8 @@ Below is an example on how to use the method.
|
|
|
510
515
|
default export ({ endpoint }) => {
|
|
511
516
|
endpoint
|
|
512
517
|
.get(
|
|
513
|
-
|
|
514
|
-
|
|
518
|
+
async (context) => 'Hello, world!',
|
|
519
|
+
() => console.info("Said hello."));
|
|
515
520
|
};
|
|
516
521
|
````
|
|
517
522
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
|
205
|
+
async _handleMount(layer, path, context, next) {
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
return await path.pushed(async (component) => {
|
|
208
208
|
|
|
209
|
-
|
|
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
|
-
|
|
215
|
+
return path.popped(next);
|
|
226
216
|
|
|
227
|
-
|
|
217
|
+
});
|
|
228
218
|
|
|
229
|
-
|
|
230
|
-
underlyingMethod = 'get';
|
|
231
|
-
}
|
|
219
|
+
}
|
|
232
220
|
|
|
233
|
-
|
|
234
|
-
if (layer.method !== 'all' && layer.method !== underlyingMethod) return await next();
|
|
221
|
+
async _handleMethod(layer, path, context, next) {
|
|
235
222
|
|
|
236
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
239
|
+
return context.result;
|
|
250
240
|
|
|
251
|
-
|
|
241
|
+
}
|
|
252
242
|
|
|
253
|
-
|
|
243
|
+
async _handleParameter(layer, path, context, next) {
|
|
254
244
|
|
|
255
|
-
|
|
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
|
-
|
|
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
|
-
|
|
271
|
-
return await layer.router._route(path, context, next);
|
|
272
|
-
}
|
|
260
|
+
return await layer.endpoint._route(path, context, next);
|
|
273
261
|
|
|
274
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
72
|
-
|
|
73
|
-
switch (layer.type) {
|
|
74
|
-
|
|
75
|
-
case 'use': {
|
|
71
|
+
async _handleUse(layer, _, context, next) {
|
|
76
72
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.7.2",
|
|
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.
|
|
28
|
+
"eslint": "^8.20.0",
|
|
29
29
|
"mocha": "^10.0.0",
|
|
30
|
-
"supertest": "^6.2.
|
|
30
|
+
"supertest": "^6.2.4"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@trenskow/api-error": "^2.2
|
|
33
|
+
"@trenskow/api-error": "^2.3.2",
|
|
34
34
|
"@trenskow/caseit": "^1.3.0"
|
|
35
35
|
}
|
|
36
36
|
}
|