@trenskow/app-express 0.0.1 → 0.0.4

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.
Files changed (4) hide show
  1. package/README.md +1 -2
  2. package/index.js +20 -0
  3. package/package.json +4 -1
  4. package/test.js +17 -0
package/README.md CHANGED
@@ -25,10 +25,9 @@ const root = new Endpoint()
25
25
 
26
26
  await app
27
27
  .root(root)
28
- .start();
28
+ .open();
29
29
  ````
30
30
 
31
31
  # LICENSE
32
32
 
33
33
  See license in LICENSE.
34
-
package/index.js CHANGED
@@ -6,13 +6,29 @@
6
6
  // For license see LICENSE.
7
7
  //
8
8
 
9
+ import caseit from '@trenskow/caseit';
10
+
9
11
  export default ({ Router, util: { resolveInlineImport } }) => {
10
12
 
11
13
  Router.prototype.express = function(router) {
14
+
12
15
  router = resolveInlineImport(router);
16
+
17
+ if (Array.isArray(router)) return router.reduce((last, router) => {
18
+ return last.express(router);
19
+ }, this);
20
+
13
21
  this._layers.push({
14
22
  handler: (_, __, { ignore, request, response }, next) => {
15
23
 
24
+ const headers = request.headers;
25
+
26
+ request.headers = Object.fromEntries([].concat(...Object.keys(headers).map((key) => {
27
+ return [[key, headers[key]]].concat(['http', 'kebab'].map((casing) => {
28
+ return [caseit(key, casing), headers[key]];
29
+ }));
30
+ })));
31
+
16
32
  return new Promise((resolve, reject) => {
17
33
 
18
34
  const writeHeadEventHandler = () => {
@@ -45,6 +61,8 @@ export default ({ Router, util: { resolveInlineImport } }) => {
45
61
 
46
62
  if (error) return reject(error);
47
63
 
64
+ request.headers = headers;
65
+
48
66
  resolve(next());
49
67
 
50
68
  });
@@ -57,7 +75,9 @@ export default ({ Router, util: { resolveInlineImport } }) => {
57
75
 
58
76
  }
59
77
  });
78
+
60
79
  return this;
80
+
61
81
  };
62
82
 
63
83
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/app-express",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "A plugin for `@trenskow/app` that allows for using routes for express.`",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,5 +27,8 @@
27
27
  "chai": "^4.3.6",
28
28
  "mocha": "^10.0.0",
29
29
  "supertest": "^6.2.4"
30
+ },
31
+ "dependencies": {
32
+ "@trenskow/caseit": "^1.3.3"
30
33
  }
31
34
  }
package/test.js CHANGED
@@ -48,6 +48,23 @@ describe('app-express', () => {
48
48
 
49
49
  });
50
50
 
51
+ it ('should handle an array of routes', async () => {
52
+
53
+ app.root(
54
+ new Endpoint()
55
+ .express([
56
+ (_, __, next) => next(),
57
+ (_, __, next) => next(),
58
+ (_, res) => res.send('ok')
59
+ ])
60
+ );
61
+
62
+ await request
63
+ .get('/')
64
+ .expect(200, 'ok');
65
+
66
+ });
67
+
51
68
  it ('should handle error from express route', async () => {
52
69
  app.root(
53
70
  new Endpoint()