@trenskow/app-express 0.0.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/.eslintrc.cjs ADDED
@@ -0,0 +1,63 @@
1
+ module.exports = {
2
+ 'env': {
3
+ 'es6': true,
4
+ 'node': true,
5
+ 'mocha': true
6
+ },
7
+ 'parserOptions': {
8
+ 'ecmaVersion': 2022,
9
+ 'sourceType': 'module'
10
+ },
11
+ 'extends': 'eslint:recommended',
12
+ 'rules': {
13
+ 'indent': [
14
+ 'error',
15
+ 'tab',
16
+ { 'SwitchCase': 1 }
17
+ ],
18
+ 'linebreak-style': [
19
+ 'error',
20
+ 'unix'
21
+ ],
22
+ 'quotes': [
23
+ 'error',
24
+ 'single'
25
+ ],
26
+ 'semi': [
27
+ 'error',
28
+ 'always'
29
+ ],
30
+ 'no-console': [
31
+ 'error', {
32
+ 'allow': [
33
+ 'warn',
34
+ 'error',
35
+ 'info'
36
+ ]
37
+ }
38
+ ],
39
+ 'no-unused-vars': [
40
+ 'error', {
41
+ 'argsIgnorePattern': '^_'
42
+ }
43
+ ],
44
+ 'no-empty': [
45
+ 'error', {
46
+ 'allowEmptyCatch': true
47
+ }
48
+ ],
49
+ 'no-trailing-spaces': [
50
+ 'error', {
51
+ 'ignoreComments': true
52
+ }
53
+ ],
54
+ 'require-atomic-updates': 'off',
55
+ 'no-implicit-globals': [
56
+ 'error'
57
+ ],
58
+ 'eol-last': [
59
+ 'error',
60
+ 'always'
61
+ ]
62
+ }
63
+ };
@@ -0,0 +1,22 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Test",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
15
+ "args": [
16
+ "${workspaceFolder}/test.js",
17
+ "--timeout",
18
+ "0"
19
+ ]
20
+ }
21
+ ]
22
+ }
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright 2021 Kristian Trenskow
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ @trenskow/app-express
2
+ ----
3
+
4
+ A plugin for [@trenskow/app](https://github.com/trenskow/app) that enables it to handle [express.js](https://expressjs.com) routes.
5
+
6
+ > ⚠️ Be aware that this plugin is experimental. Not all features of express is currently supported.
7
+
8
+ # Installation and usage
9
+
10
+ Installation is pretty simple. You install it by registering the plugin.
11
+
12
+ ````javascript
13
+ import { Application, Endpoint } from '@trenskow/app';
14
+ import express from '@trenskow/app-express'
15
+
16
+ Application.plugin(express);
17
+
18
+ const app = new Application({ port: 8080 });
19
+
20
+ const root = new Endpoint()
21
+ .mount('ping', new Endpoint()
22
+ .express((req, res, /* next */) => {
23
+ res.json('pong')
24
+ }));
25
+
26
+ await app
27
+ .root(root)
28
+ .start();
29
+ ````
30
+
31
+ # LICENSE
32
+
33
+ See license in LICENSE.
34
+
package/index.js ADDED
@@ -0,0 +1,63 @@
1
+ //
2
+ // index.js
3
+ // @trenskow/app-express
4
+ //
5
+ // Created by Kristian Trenskow on 2022/07/26
6
+ // For license see LICENSE.
7
+ //
8
+
9
+ export default ({ Router, util: { resolveInlineImport } }) => {
10
+
11
+ Router.prototype.express = function(router) {
12
+ router = resolveInlineImport(router);
13
+ this._layers.push({
14
+ handler: (_, __, { ignore, request, response }, next) => {
15
+
16
+ return new Promise((resolve, reject) => {
17
+
18
+ const writeHeadEventHandler = () => {
19
+ ignore();
20
+ };
21
+
22
+ try {
23
+
24
+ response.on('writeHead', writeHeadEventHandler);
25
+
26
+ response.send = (data) => {
27
+ response.write(data);
28
+ response.end();
29
+ return response;
30
+ };
31
+
32
+ response.status = (statusCode) => {
33
+ response.statusCode = statusCode;
34
+ return response;
35
+ };
36
+
37
+ response.json = (json) => {
38
+ response.headers['contentType'] = 'application/json';
39
+ return response.send(JSON.stringify(json));
40
+ };
41
+
42
+ router(request, response, (error) => {
43
+
44
+ response.removeListener('writeHead', writeHeadEventHandler);
45
+
46
+ if (error) return reject(error);
47
+
48
+ resolve(next());
49
+
50
+ });
51
+
52
+ } catch (error) {
53
+ reject(error);
54
+ }
55
+
56
+ });
57
+
58
+ }
59
+ });
60
+ return this;
61
+ };
62
+
63
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@trenskow/app-express",
3
+ "version": "0.0.1",
4
+ "description": "A plugin for `@trenskow/app` that allows for using routes for express.`",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node ./node_modules/mocha/bin/mocha.js test.js"
8
+ },
9
+ "type": "module",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/trenskow/app-express.git"
13
+ },
14
+ "keywords": [
15
+ "express",
16
+ "router",
17
+ "http"
18
+ ],
19
+ "author": "Kristian Trenskow <trenskow@me.com>",
20
+ "license": "BSD-2-Clause",
21
+ "bugs": {
22
+ "url": "https://github.com/trenskow/app-express/issues"
23
+ },
24
+ "homepage": "https://github.com/trenskow/app-express#readme",
25
+ "devDependencies": {
26
+ "@trenskow/app": "^0.7.0",
27
+ "chai": "^4.3.6",
28
+ "mocha": "^10.0.0",
29
+ "supertest": "^6.2.4"
30
+ }
31
+ }
package/test.js ADDED
@@ -0,0 +1,80 @@
1
+ //
2
+ // index.js
3
+ // @trenskow/app-express
4
+ //
5
+ // Created by Kristian Trenskow on 2022/07/26
6
+ // For license see LICENSE.
7
+ //
8
+
9
+ import supertest from 'supertest';
10
+ import { expect } from 'chai';
11
+ import { Application, Endpoint } from '@trenskow/app';
12
+
13
+ import express from './index.js';
14
+
15
+ Application.plugin(express);
16
+
17
+ let app;
18
+ let request;
19
+
20
+ describe('app-express', () => {
21
+
22
+ before(async () => {
23
+
24
+ app = new Application();
25
+
26
+ const port = (await app
27
+ .open())
28
+ .port;
29
+
30
+ request = supertest(`http://localhost:${port}`);
31
+
32
+ });
33
+
34
+ it ('should call express route', async () => {
35
+
36
+ app.root(
37
+ new Endpoint()
38
+ .express((_, res) => {
39
+ res.status(201).json('pong');
40
+ })
41
+ );
42
+
43
+ let response = await request
44
+ .get('/')
45
+ .expect(201, '"pong"');
46
+
47
+ expect(response.headers).to.have.property('content-type').equal('application/json');
48
+
49
+ });
50
+
51
+ it ('should handle error from express route', async () => {
52
+ app.root(
53
+ new Endpoint()
54
+ .express((_, __, next) => {
55
+ next(new Error('An error'));
56
+ })
57
+ );
58
+ await request
59
+ .get('/')
60
+ .expect(500);
61
+ });
62
+
63
+ it ('should continue routing if express route called next', async () => {
64
+ app.root(
65
+ new Endpoint()
66
+ .express((_, __, next) => {
67
+ next();
68
+ })
69
+ .get(async () => 'pong')
70
+ );
71
+ await request
72
+ .get('/')
73
+ .expect(200, 'pong');
74
+ });
75
+
76
+ after(async () => {
77
+ await app.close({ awaitAllConnections: true });
78
+ });
79
+
80
+ });