@loopback/example-lb3-application 3.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.
Files changed (59) hide show
  1. package/.prettierignore +2 -0
  2. package/.prettierrc +7 -0
  3. package/.vscode/settings.json +20 -0
  4. package/.vscode/tasks.json +29 -0
  5. package/CHANGELOG.md +626 -0
  6. package/LICENSE +25 -0
  7. package/README.md +560 -0
  8. package/dist/__tests__/acceptance/home-page.acceptance.d.ts +1 -0
  9. package/dist/__tests__/acceptance/home-page.acceptance.js +38 -0
  10. package/dist/__tests__/acceptance/home-page.acceptance.js.map +1 -0
  11. package/dist/__tests__/acceptance/lb3app.acceptance.d.ts +1 -0
  12. package/dist/__tests__/acceptance/lb3app.acceptance.js +200 -0
  13. package/dist/__tests__/acceptance/lb3app.acceptance.js.map +1 -0
  14. package/dist/__tests__/acceptance/test-helper.d.ts +11 -0
  15. package/dist/__tests__/acceptance/test-helper.js +33 -0
  16. package/dist/__tests__/acceptance/test-helper.js.map +1 -0
  17. package/dist/application.d.ts +187 -0
  18. package/dist/application.js +41 -0
  19. package/dist/application.js.map +1 -0
  20. package/dist/index.d.ts +3 -0
  21. package/dist/index.js +38 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/migrate.d.ts +1 -0
  24. package/dist/migrate.js +25 -0
  25. package/dist/migrate.js.map +1 -0
  26. package/dist/openapi-spec.d.ts +1 -0
  27. package/dist/openapi-spec.js +28 -0
  28. package/dist/openapi-spec.js.map +1 -0
  29. package/dist/sequence.d.ts +3 -0
  30. package/dist/sequence.js +12 -0
  31. package/dist/sequence.js.map +1 -0
  32. package/dist/server.d.ts +15 -0
  33. package/dist/server.js +58 -0
  34. package/dist/server.js.map +1 -0
  35. package/lb3app/common/models/coffee-shop.js +44 -0
  36. package/lb3app/common/models/coffee-shop.json +30 -0
  37. package/lb3app/server/boot/authentication.js +11 -0
  38. package/lb3app/server/boot/create-sample-models.js +28 -0
  39. package/lb3app/server/config.json +21 -0
  40. package/lb3app/server/datasources.json +6 -0
  41. package/lb3app/server/middleware.json +9 -0
  42. package/lb3app/server/model-config.json +39 -0
  43. package/lb3app/server/server.js +17 -0
  44. package/lb3app/test/acceptance.js +107 -0
  45. package/lb3app/test/authentication.js +135 -0
  46. package/lb3app/test/integration.js +75 -0
  47. package/package.json +78 -0
  48. package/public/index.html +74 -0
  49. package/public/lb3-index.html +5 -0
  50. package/src/__tests__/acceptance/home-page.acceptance.ts +44 -0
  51. package/src/__tests__/acceptance/lb3app.acceptance.ts +244 -0
  52. package/src/__tests__/acceptance/test-helper.ts +40 -0
  53. package/src/application.ts +44 -0
  54. package/src/index.ts +35 -0
  55. package/src/migrate.ts +25 -0
  56. package/src/openapi-spec.ts +28 -0
  57. package/src/sequence.ts +8 -0
  58. package/src/server.ts +71 -0
  59. package/tsconfig.json +36 -0
@@ -0,0 +1,17 @@
1
+ // Copyright IBM Corp. 2019,2020. All Rights Reserved.
2
+ // Node module: @loopback/example-lb3-application
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ 'use strict';
7
+
8
+ const loopback = require('loopback');
9
+ const boot = require('loopback-boot');
10
+
11
+ const app = (module.exports = loopback());
12
+
13
+ // Bootstrap the application, configure models, datasources and middleware.
14
+ // Sub-apps like REST API are mounted via boot scripts.
15
+ boot(app, __dirname, function (err) {
16
+ if (err) throw err;
17
+ });
@@ -0,0 +1,107 @@
1
+ // Copyright IBM Corp. 2020. All Rights Reserved.
2
+ // Node module: @loopback/example-lb3-application
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ 'use strict';
7
+ const {supertest} = require('@loopback/testlab');
8
+ const assert = require('assert');
9
+ const {ExpressServer} = require('../../dist/server');
10
+ const {CoffeeShopApplication} = require('../../dist/application');
11
+ require('should');
12
+
13
+ let app;
14
+
15
+ function jsonForLB4(verb, url) {
16
+ // use the lb4 app's rest server
17
+ return supertest(app.restServer.url)
18
+ [verb](url)
19
+ .set('Content-Type', 'application/json')
20
+ .set('Accept', 'application/json')
21
+ .expect('Content-Type', /json/);
22
+ }
23
+
24
+ function jsonForExpressApp(verb, url) {
25
+ // use the express server, it mounts LoopBack 3 apis to
26
+ // base path '/api'
27
+ return supertest(app.server)
28
+ [verb]('/api' + url)
29
+ .set('Content-Type', 'application/json')
30
+ .set('Accept', 'application/json')
31
+ .expect('Content-Type', /json/);
32
+ }
33
+
34
+ function jsonForExternal(verb, url) {
35
+ // use the express server, its external apis doesn't have base path
36
+ return supertest(app.server)
37
+ [verb](url)
38
+ .set('Content-Type', 'application/json')
39
+ .set('Accept', 'application/json')
40
+ .expect('Content-Type', /json/);
41
+ }
42
+
43
+ describe('LoopBack 3 style acceptance tests - boot from Express server', function () {
44
+ before(async function () {
45
+ app = new ExpressServer();
46
+ await app.boot();
47
+ await app.start();
48
+ });
49
+
50
+ after(async () => {
51
+ await app.stop();
52
+ });
53
+
54
+ it('gets external route in application', function (done) {
55
+ jsonForExternal('get', '/ping').expect(200, function (err, res) {
56
+ assert.equal(res.text, 'pong');
57
+ done();
58
+ });
59
+ });
60
+
61
+ runTests(jsonForExpressApp);
62
+ });
63
+
64
+ describe('LoopBack 3 style acceptance tests - boot from LB4 app', function () {
65
+ before(async function () {
66
+ app = new CoffeeShopApplication();
67
+ await app.boot();
68
+ await app.start();
69
+ });
70
+
71
+ after(async () => {
72
+ await app.stop();
73
+ });
74
+
75
+ runTests(jsonForLB4);
76
+ });
77
+
78
+ function runTests(request) {
79
+ context('basic REST calls for LoopBack 3 application', () => {
80
+ it('creates and finds a CoffeeShop', function (done) {
81
+ request('post', '/CoffeeShops')
82
+ .send({
83
+ name: 'Coffee Shop',
84
+ city: 'Toronto',
85
+ })
86
+ .expect(200)
87
+ .end(function (err, res) {
88
+ assert(typeof res.body === 'object');
89
+ assert(res.body.name);
90
+ assert(res.body.city);
91
+ assert.equal(res.body.name, 'Coffee Shop');
92
+ assert.equal(res.body.city, 'Toronto');
93
+ done();
94
+ });
95
+ });
96
+
97
+ it("gets the CoffeeShop's status", function (done) {
98
+ request('get', '/CoffeeShops/status').expect(200, function (err, res) {
99
+ res.body.status.should.be.equalOneOf(
100
+ 'We are open for business.',
101
+ 'Sorry, we are closed. Open daily from 6am to 8pm.',
102
+ );
103
+ done();
104
+ });
105
+ });
106
+ });
107
+ }
@@ -0,0 +1,135 @@
1
+ // Copyright IBM Corp. 2020. All Rights Reserved.
2
+ // Node module: @loopback/example-lb3-application
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ 'use strict';
7
+ const lb3App = require('../server/server');
8
+ const {supertest} = require('@loopback/testlab');
9
+ const assert = require('assert');
10
+ const {ExpressServer} = require('../../dist/server');
11
+ const {CoffeeShopApplication} = require('../../dist/application');
12
+ require('should');
13
+
14
+ let app, User;
15
+
16
+ function jsonForLB4(verb, url) {
17
+ // use the lb4 app's rest server
18
+ return supertest(app.restServer.url)
19
+ [verb](url)
20
+ .set('Content-Type', 'application/json')
21
+ .set('Accept', 'application/json')
22
+ .expect('Content-Type', /json/);
23
+ }
24
+
25
+ function jsonForExpressApp(verb, url) {
26
+ // use the express server, it mounts apis to base path '/api'
27
+ return supertest(app.server)
28
+ [verb]('/api' + url)
29
+ .set('Content-Type', 'application/json')
30
+ .set('Accept', 'application/json')
31
+ .expect('Content-Type', /json/);
32
+ }
33
+
34
+ /**
35
+ * The tests show running LoopBack 3 authentication tests mounted to
36
+ * an Express server.
37
+ * Make sure you start the express server first and stop it after tests done.
38
+ */
39
+ describe('LoopBack 3 authentication - Express server', function () {
40
+ before(async function () {
41
+ User = lb3App.models.User;
42
+ app = new ExpressServer();
43
+ await app.boot();
44
+ await app.start();
45
+ });
46
+
47
+ after(async () => {
48
+ await User.destroyAll();
49
+ await app.stop();
50
+ });
51
+
52
+ runTests(jsonForExpressApp);
53
+ });
54
+
55
+ /**
56
+ * The tests show running LoopBack 3 authentication tests mounted to
57
+ * a LoopBack 4 application.
58
+ * Make sure you start the LoopBack 4 application first and stop it
59
+ * after tests done.
60
+ */
61
+ describe('Loopback 3 authentication - LoopBack 4 app', function () {
62
+ before(async function () {
63
+ User = lb3App.models.User;
64
+ app = new CoffeeShopApplication();
65
+ await app.boot();
66
+ await app.start();
67
+ });
68
+
69
+ after(async () => {
70
+ await User.destroyAll();
71
+ await app.stop();
72
+ });
73
+
74
+ runTests(jsonForLB4);
75
+ });
76
+
77
+ function runTests(request) {
78
+ it('creates a User and logs them in and out', function (done) {
79
+ // create user
80
+ request('post', '/users')
81
+ .send({email: 'new@email.com', password: 'L00pBack!'})
82
+ .expect(200, function (err, user) {
83
+ assert.equal(user.body.email, 'new@email.com');
84
+ // login
85
+ request('post', '/users/login')
86
+ .send({
87
+ email: 'new@email.com',
88
+ password: 'L00pBack!',
89
+ })
90
+ .expect(200, function (err2, token) {
91
+ token.body.should.have.properties('ttl', 'userId', 'created', 'id');
92
+ assert.equal(token.body.userId, user.body.id);
93
+ // logout
94
+ request(
95
+ 'post',
96
+ `/users/logout?access_token=${token.body.id}`,
97
+ ).expect(204);
98
+ done();
99
+ });
100
+ });
101
+ });
102
+
103
+ it('rejects anonymous requests to protected endpoints', function (done) {
104
+ request('get', '/CoffeeShops/greet').expect(401, function (err, res) {
105
+ assert.equal(res.body.error.code, 'AUTHORIZATION_REQUIRED');
106
+ });
107
+ done();
108
+ });
109
+
110
+ it('makes an authenticated request', function (done) {
111
+ // create user
112
+ User.create(
113
+ {email: 'new@gmail.com', password: 'L00pBack!'},
114
+ function (err, user) {
115
+ user.email.should.be.equal('new@gmail.com');
116
+ // login
117
+ User.login(
118
+ {email: 'new@gmail.com', password: 'L00pBack!'},
119
+ function (err2, token) {
120
+ assert.equal(typeof token, 'object');
121
+ assert.equal(token.userId, user.id);
122
+ // authenticate user with token
123
+ request(
124
+ 'get',
125
+ `/CoffeeShops/greet?access_token=${token.id}`,
126
+ ).expect(200, function (err3, res) {
127
+ res.body.greeting.should.be.equal('Hello from this Coffee Shop');
128
+ done();
129
+ });
130
+ },
131
+ );
132
+ },
133
+ );
134
+ });
135
+ }
@@ -0,0 +1,75 @@
1
+ // Copyright IBM Corp. 2020. All Rights Reserved.
2
+ // Node module: @loopback/example-lb3-application
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ 'use strict';
7
+
8
+ const assert = require('assert');
9
+ const ExpressServer = require('../../dist/server').ExpressServer;
10
+ const CoffeeShopApp = require('../../dist/application').CoffeeShopApplication;
11
+ require('should');
12
+
13
+ let CoffeeShop, app;
14
+
15
+ describe('LoopBack 3 style integration tests - boot from LB4 app', function () {
16
+ before(async function () {
17
+ app = new CoffeeShopApp();
18
+ await app.boot();
19
+ await app.start();
20
+ CoffeeShop = await app.get('lb3-models.CoffeeShop');
21
+ });
22
+
23
+ after(async () => {
24
+ await CoffeeShop.destroyAll({name: 'Nook Shop'});
25
+ await app.stop();
26
+ });
27
+
28
+ runTests();
29
+ });
30
+
31
+ describe('LoopBack 3 style integration tests - boot from express', function () {
32
+ before(async () => {
33
+ app = new ExpressServer();
34
+ await app.boot();
35
+ await app.start();
36
+ CoffeeShop = await app.lbApp.get('lb3-models.CoffeeShop');
37
+ });
38
+
39
+ after(async () => {
40
+ await app.stop();
41
+ });
42
+
43
+ runTests();
44
+ });
45
+
46
+ function runTests() {
47
+ it('CoffeeShop.find', function (done) {
48
+ CoffeeShop.find({where: {name: 'Bel Cafe'}}, function (err, shop) {
49
+ shop[0].__data.name.should.be.equal('Bel Cafe');
50
+ shop[0].__data.city.should.be.equal('Vancouver');
51
+ });
52
+ done();
53
+ });
54
+
55
+ it('CoffeeShop.count', function (done) {
56
+ CoffeeShop.count({}, function (err, count) {
57
+ assert.equal(count, 6);
58
+ });
59
+ done();
60
+ });
61
+
62
+ it('CoffeeShop.create', function (done) {
63
+ CoffeeShop.create(
64
+ {
65
+ name: 'Nook Shop',
66
+ city: 'Toronto',
67
+ },
68
+ function (err, shop) {
69
+ shop.__data.name.should.be.equal('Nook Shop');
70
+ shop.__data.city.should.be.equal('Toronto');
71
+ },
72
+ );
73
+ done();
74
+ });
75
+ }
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@loopback/example-lb3-application",
3
+ "description": "Tutorial example on how to add existing an LB3 application to a LB4 project",
4
+ "version": "3.0.1",
5
+ "keywords": [
6
+ "loopback",
7
+ "LoopBack",
8
+ "example"
9
+ ],
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "types": "dist/index.d.ts",
13
+ "author": "IBM Corp.",
14
+ "copyright.owner": "IBM Corp.",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/loopbackio/loopback-next.git",
18
+ "directory": "examples/lb3-application"
19
+ },
20
+ "engines": {
21
+ "node": "^10.16 || 12 || 14 || 16"
22
+ },
23
+ "scripts": {
24
+ "build": "lb-tsc",
25
+ "build:watch": "lb-tsc --watch",
26
+ "clean": "lb-clean *example-lb3-application*.tgz dist *.tsbuildinfo package",
27
+ "lint": "npm run prettier:check && npm run eslint",
28
+ "lint:fix": "npm run eslint:fix && npm run prettier:fix",
29
+ "prettier:cli": "lb-prettier \"**/*.ts\"",
30
+ "prettier:check": "npm run prettier:cli -- -l",
31
+ "prettier:fix": "npm run prettier:cli -- --write",
32
+ "eslint": "lb-eslint --report-unused-disable-directives .",
33
+ "eslint:fix": "npm run eslint -- --fix",
34
+ "pretest": "npm run rebuild",
35
+ "test": "lb-mocha \"dist/__tests__/**/*.js\" \"lb3app/test/*.js\"",
36
+ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
37
+ "verify": "npm pack && tar xf loopback-lb3-application*.tgz && tree package && npm run clean",
38
+ "premigrate": "npm run build ",
39
+ "migrate": "node ./dist/migrate",
40
+ "preopenapi-spec": "npm run build",
41
+ "openapi-spec": "node ./dist/openapi-spec",
42
+ "rebuild": "npm run clean && npm run build",
43
+ "prestart": "npm run rebuild",
44
+ "start": "node ."
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "dependencies": {
50
+ "@loopback/boot": "^3.4.3",
51
+ "@loopback/booter-lb3app": "^2.4.3",
52
+ "@loopback/core": "^2.17.0",
53
+ "@loopback/repository": "^3.7.2",
54
+ "@loopback/rest": "^10.0.1",
55
+ "@loopback/rest-explorer": "^3.3.3",
56
+ "compression": "^1.7.4",
57
+ "cors": "^2.8.5",
58
+ "debug": "^4.3.2",
59
+ "express": "^4.17.1",
60
+ "helmet": "^4.6.0",
61
+ "loopback": "^3.28.0",
62
+ "loopback-boot": "^3.3.1",
63
+ "tslib": "^2.3.1"
64
+ },
65
+ "devDependencies": {
66
+ "@loopback/build": "^7.0.1",
67
+ "@loopback/eslint-config": "^11.0.1",
68
+ "@loopback/rest": "^10.0.1",
69
+ "@loopback/testlab": "^3.4.3",
70
+ "@types/lodash": "^4.14.173",
71
+ "@types/node": "^10.17.60",
72
+ "eslint": "^7.32.0",
73
+ "lodash": "^4.17.21",
74
+ "should": "^13.2.3",
75
+ "typescript": "~4.3.5"
76
+ },
77
+ "gitHead": "1df36bb1ee2e513d9e197bd6010c4cfb296d50b8"
78
+ }
@@ -0,0 +1,74 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <title>@loopback/example-lb3-application</title>
6
+
7
+ <meta charset="utf-8">
8
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ <link rel="shortcut icon" type="image/x-icon" href="//loopback.io/favicon.ico">
11
+
12
+ <style>
13
+ h3 {
14
+ margin-left: 25px;
15
+ text-align: center;
16
+ }
17
+
18
+ a, a:visited {
19
+ color: #3f5dff;
20
+ }
21
+
22
+ h3 a {
23
+ margin-left: 10px;
24
+ }
25
+
26
+ a:hover, a:focus, a:active {
27
+ color: #001956;
28
+ }
29
+
30
+ .power {
31
+ position: absolute;
32
+ bottom: 25px;
33
+ left: 50%;
34
+ transform: translateX(-50%);
35
+ }
36
+
37
+ .info {
38
+ position: absolute;
39
+ top: 50%;
40
+ left: 50%;
41
+ transform: translate(-50%, -50%)
42
+ }
43
+
44
+ .info h1 {
45
+ text-align: center;
46
+ margin-bottom: 0
47
+ }
48
+
49
+ .info p {
50
+ text-align: center;
51
+ margin-bottom: 3em;
52
+ margin-top: 1em;
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <div class="info">
59
+ <h1>@loopback/example-lb3-application</h1>
60
+
61
+ <h3>OpenAPI spec: <a href="/api/openapi.json">/openapi.json</a></h3>
62
+ <h3>API Explorer: <a href="/api/explorer">/explorer</a></h3>
63
+ <h3><a href="/lb3-index.html">Home page from the LB3 app</a></h3>
64
+ </div>
65
+
66
+ <footer class="power">
67
+ <a href="https://loopback.io" target="_blank">
68
+ <img src="https://loopback.io/images/branding/powered-by-loopback/blue/powered-by-loopback-sm.png" />
69
+ </a>
70
+ </footer>
71
+ </body>
72
+
73
+ </html>
74
+