@trenskow/app 0.7.12 → 0.8.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 +31 -3
- package/lib/router.js +30 -2
- package/package.json +1 -1
- package/test/index.js +19 -2
package/README.md
CHANGED
|
@@ -717,9 +717,9 @@ Typically used by middleware.
|
|
|
717
717
|
|
|
718
718
|
###### Parameters
|
|
719
719
|
|
|
720
|
-
| Name | Description
|
|
721
|
-
| --------- |
|
|
722
|
-
| `handler` | A (or
|
|
720
|
+
| Name | Description | Type | Required | Default value |
|
|
721
|
+
| --------- | ------------------------- | :------------------------------------------------------: | :----------------: | :-----------: |
|
|
722
|
+
| `handler` | A (or multiple) handlers. | Function, AsyncFunction or Array ([see also](#handlers)) | :white_check_mark: | |
|
|
723
723
|
|
|
724
724
|
###### Example
|
|
725
725
|
|
|
@@ -761,6 +761,34 @@ export default new Router()
|
|
|
761
761
|
});
|
|
762
762
|
````
|
|
763
763
|
|
|
764
|
+
##### `transform`
|
|
765
|
+
|
|
766
|
+
This method hooks into the response chain and allows to change the result of the request. It will transform any value from below the routing tree from which it is added.
|
|
767
|
+
|
|
768
|
+
###### Parameterts
|
|
769
|
+
|
|
770
|
+
| Name | Description | Type | Required | Default value |
|
|
771
|
+
| ----------- | --------------------------- | :-----------------------: | :----------------: | :-----------: |
|
|
772
|
+
| `transform` | A (or multiple) transforms. | [`function`](#endpoint-2) | :white_check_mark: | |
|
|
773
|
+
|
|
774
|
+
###### Example
|
|
775
|
+
|
|
776
|
+
Below is an example of how to use a transform.
|
|
777
|
+
|
|
778
|
+
````javascript
|
|
779
|
+
import { Endpoint } from '@trenskow/app';
|
|
780
|
+
|
|
781
|
+
export default new Endpoint()
|
|
782
|
+
.transform(async ({ result, context }) => {
|
|
783
|
+
return (await result()) + ', World!';
|
|
784
|
+
})
|
|
785
|
+
.get(() => {
|
|
786
|
+
return 'Hello';
|
|
787
|
+
});
|
|
788
|
+
````
|
|
789
|
+
|
|
790
|
+
> When endpoint is called using HTTP GET, it will return `'Hello, World!'`.
|
|
791
|
+
|
|
764
792
|
### `Request`
|
|
765
793
|
|
|
766
794
|
This class represents the request. Each individual request has its own instance assigned to `context.request`, where it accessible from endpoint, routers and handlers.
|
package/lib/router.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
//
|
|
2
2
|
// router.js
|
|
3
3
|
// @trenskow/app
|
|
4
|
-
//
|
|
4
|
+
//
|
|
5
5
|
// Created by Kristian Trenskow on 2021/11/07
|
|
6
6
|
// For license see LICENSE.
|
|
7
|
-
//
|
|
7
|
+
//
|
|
8
8
|
|
|
9
9
|
import { isObject, resolveInlineImport } from './util.js';
|
|
10
10
|
|
|
@@ -56,6 +56,30 @@ export default class Router {
|
|
|
56
56
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
transform(...transforms) {
|
|
60
|
+
|
|
61
|
+
transforms = [].concat(...transforms);
|
|
62
|
+
|
|
63
|
+
if (!transforms.length) return this;
|
|
64
|
+
|
|
65
|
+
transforms = transforms.map(resolveInlineImport);
|
|
66
|
+
|
|
67
|
+
if (transforms.filter((hook) => typeof hook !== 'function').length) {
|
|
68
|
+
throw new Error('Hook must be a function.');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this._layers = this._layers
|
|
72
|
+
.concat(transforms.map((hook) => {
|
|
73
|
+
return {
|
|
74
|
+
handler: this._handleTransform,
|
|
75
|
+
hook
|
|
76
|
+
};
|
|
77
|
+
}));
|
|
78
|
+
|
|
79
|
+
return this;
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
59
83
|
async _route(path, context, next, idx = 0) {
|
|
60
84
|
|
|
61
85
|
if (context.state !== 'routing') return;
|
|
@@ -83,6 +107,10 @@ export default class Router {
|
|
|
83
107
|
return await layer.router._route(path, context, next);
|
|
84
108
|
}
|
|
85
109
|
|
|
110
|
+
async _handleTransform(layer, _, context, next) {
|
|
111
|
+
return context.result = await layer.hook({ result: next, context });
|
|
112
|
+
}
|
|
113
|
+
|
|
86
114
|
async _handle(layer, path, context, next) {
|
|
87
115
|
return await layer.handler.call(this, layer, path, context, next);
|
|
88
116
|
}
|
package/package.json
CHANGED
package/test/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
//
|
|
2
2
|
// index.js
|
|
3
3
|
// @trenskow/app
|
|
4
|
-
//
|
|
4
|
+
//
|
|
5
5
|
// Created by Kristian Trenskow on 2021/11/08
|
|
6
6
|
// For license see LICENSE.
|
|
7
|
-
//
|
|
7
|
+
//
|
|
8
8
|
|
|
9
9
|
import supertest from 'supertest';
|
|
10
10
|
import { Endpoint, Router } from '../lib/index.js';
|
|
@@ -383,6 +383,23 @@ describe('Application', () => {
|
|
|
383
383
|
|
|
384
384
|
});
|
|
385
385
|
|
|
386
|
+
it ('should come back with value transformed in a transform.', async () => {
|
|
387
|
+
|
|
388
|
+
app.root(
|
|
389
|
+
new Endpoint()
|
|
390
|
+
.transform(async ({ result }) => {
|
|
391
|
+
return (await result()) + ', World!';
|
|
392
|
+
})
|
|
393
|
+
.get(() => 'Hello')
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
await request
|
|
397
|
+
.get('/')
|
|
398
|
+
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
399
|
+
.expect(200, 'Hello, World!');
|
|
400
|
+
|
|
401
|
+
});
|
|
402
|
+
|
|
386
403
|
after(async () => {
|
|
387
404
|
await app.close({ awaitAllConnections: true });
|
|
388
405
|
});
|