From dca78e1ba3241ed2a0e7067e00cc1afd001f0335 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 28 May 2020 08:55:15 -0700 Subject: [PATCH] feat: add `npm run openapi-spec` to export the openapi spec --- .../access-control-migration/package.json | 1 + .../access-control-migration/src/migrate.ts | 25 +++++++++++++++++ .../src/openapi-spec.ts | 28 +++++++++++++++++++ examples/greeting-app/package.json | 1 + examples/greeting-app/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/lb3-application/package.json | 1 + examples/lb3-application/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/multi-tenancy/package.json | 1 + examples/multi-tenancy/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/rest-crud/package.json | 1 + examples/rest-crud/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/todo-jwt/package.json | 1 + examples/todo-jwt/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/todo-list/package.json | 1 + examples/todo-list/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/todo/package.json | 1 + examples/todo/src/openapi-spec.ts | 28 +++++++++++++++++++ examples/validation-app/package.json | 1 + examples/validation-app/src/openapi-spec.ts | 28 +++++++++++++++++++ 19 files changed, 286 insertions(+) create mode 100644 examples/access-control-migration/src/migrate.ts create mode 100644 examples/access-control-migration/src/openapi-spec.ts create mode 100644 examples/greeting-app/src/openapi-spec.ts create mode 100644 examples/lb3-application/src/openapi-spec.ts create mode 100644 examples/multi-tenancy/src/openapi-spec.ts create mode 100644 examples/rest-crud/src/openapi-spec.ts create mode 100644 examples/todo-jwt/src/openapi-spec.ts create mode 100644 examples/todo-list/src/openapi-spec.ts create mode 100644 examples/todo/src/openapi-spec.ts create mode 100644 examples/validation-app/src/openapi-spec.ts diff --git a/examples/access-control-migration/package.json b/examples/access-control-migration/package.json index b6fd6a052056..7c611ff54fc8 100644 --- a/examples/access-control-migration/package.json +++ b/examples/access-control-migration/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js", "verify": "npm pack && tar xf loopback-access-control-migration*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/access-control-migration/src/migrate.ts b/examples/access-control-migration/src/migrate.ts new file mode 100644 index 000000000000..a709b9d2ebaa --- /dev/null +++ b/examples/access-control-migration/src/migrate.ts @@ -0,0 +1,25 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-access-control-migration +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {AccessControlApplication} from './application'; + +export async function migrate(args: string[]) { + const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter'; + console.log('Migrating schemas (%s existing schema)', existingSchema); + + const app = new AccessControlApplication(); + await app.boot(); + await app.migrateSchema({existingSchema}); + + // Connectors usually keep a pool of opened connections, + // this keeps the process running even after all work is done. + // We need to exit explicitly. + process.exit(0); +} + +migrate(process.argv).catch(err => { + console.error('Cannot migrate database schema', err); + process.exit(1); +}); diff --git a/examples/access-control-migration/src/openapi-spec.ts b/examples/access-control-migration/src/openapi-spec.ts new file mode 100644 index 000000000000..725350d88cf8 --- /dev/null +++ b/examples/access-control-migration/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-access-control-migration +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {AccessControlApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new AccessControlApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/greeting-app/package.json b/examples/greeting-app/package.json index 2171ec05334f..26772817c24c 100644 --- a/examples/greeting-app/package.json +++ b/examples/greeting-app/package.json @@ -22,6 +22,7 @@ "test": "lb-mocha \"dist/__tests__/**/*.js\"", "posttest": "npm run lint", "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node .", "verify": "npm pack && tar xf *example-greeting-app*.tgz && tree package && npm run clean" diff --git a/examples/greeting-app/src/openapi-spec.ts b/examples/greeting-app/src/openapi-spec.ts new file mode 100644 index 000000000000..1c8cb6ccb6b6 --- /dev/null +++ b/examples/greeting-app/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-greeting-app +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {GreetingApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new GreetingApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/lb3-application/package.json b/examples/lb3-application/package.json index fd5393a45163..3f22dfa6a930 100644 --- a/examples/lb3-application/package.json +++ b/examples/lb3-application/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", "verify": "npm pack && tar xf loopback-lb3-application*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/lb3-application/src/openapi-spec.ts b/examples/lb3-application/src/openapi-spec.ts new file mode 100644 index 000000000000..3bd58c7a7d0d --- /dev/null +++ b/examples/lb3-application/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-lb3-application +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {CoffeeShopApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new CoffeeShopApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/multi-tenancy/package.json b/examples/multi-tenancy/package.json index d2a39babdc5d..f4d0cf1ca1c1 100644 --- a/examples/multi-tenancy/package.json +++ b/examples/multi-tenancy/package.json @@ -28,6 +28,7 @@ "docker:build": "docker build -t @loopback/example-multi-tenancy .", "docker:run": "docker run -p 3000:3000 -d @loopback/example-multi-tenancy", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node -r source-map-support/register .", "clean": "lb-clean dist *.tsbuildinfo .eslintcache" diff --git a/examples/multi-tenancy/src/openapi-spec.ts b/examples/multi-tenancy/src/openapi-spec.ts new file mode 100644 index 000000000000..65df7ab893c0 --- /dev/null +++ b/examples/multi-tenancy/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-multi-tenancy +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {ExampleMultiTenancyApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new ExampleMultiTenancyApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/rest-crud/package.json b/examples/rest-crud/package.json index 666826f4e6c9..137fe06034f4 100644 --- a/examples/rest-crud/package.json +++ b/examples/rest-crud/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", "verify": "npm pack && tar xf loopback-rest-crud*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/rest-crud/src/openapi-spec.ts b/examples/rest-crud/src/openapi-spec.ts new file mode 100644 index 000000000000..641e349c589f --- /dev/null +++ b/examples/rest-crud/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-rest-crud +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {TodoApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new TodoApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/todo-jwt/package.json b/examples/todo-jwt/package.json index 0645d6952fdd..6ebf5cfc5cc7 100644 --- a/examples/todo-jwt/package.json +++ b/examples/todo-jwt/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", "verify": "npm pack && tar xf loopback-todo*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/todo-jwt/src/openapi-spec.ts b/examples/todo-jwt/src/openapi-spec.ts new file mode 100644 index 000000000000..9c9da05ee9a1 --- /dev/null +++ b/examples/todo-jwt/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-todo-jwt +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {TodoListApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new TodoListApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/todo-list/package.json b/examples/todo-list/package.json index a611050c24f1..b26ff5882eec 100644 --- a/examples/todo-list/package.json +++ b/examples/todo-list/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", "verify": "npm pack && tar xf loopback-todo-list*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/todo-list/src/openapi-spec.ts b/examples/todo-list/src/openapi-spec.ts new file mode 100644 index 000000000000..a17bf33d1920 --- /dev/null +++ b/examples/todo-list/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-todo-list +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {TodoListApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new TodoListApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/todo/package.json b/examples/todo/package.json index 755b87e8bf0c..147b88922051 100644 --- a/examples/todo/package.json +++ b/examples/todo/package.json @@ -24,6 +24,7 @@ "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest", "verify": "npm pack && tar xf loopback-todo*.tgz && tree package && npm run clean", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node ." }, diff --git a/examples/todo/src/openapi-spec.ts b/examples/todo/src/openapi-spec.ts new file mode 100644 index 000000000000..161966ef0cb6 --- /dev/null +++ b/examples/todo/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2018,2020. All Rights Reserved. +// Node module: @loopback/example-todo +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {TodoListApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new TodoListApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +}); diff --git a/examples/validation-app/package.json b/examples/validation-app/package.json index f525cb664ad5..02f5cb59dddc 100644 --- a/examples/validation-app/package.json +++ b/examples/validation-app/package.json @@ -26,6 +26,7 @@ "docker:build": "docker build -t validation-app .", "docker:run": "docker run -p 3000:3000 -d validation-app", "migrate": "node ./dist/migrate", + "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run build", "start": "node -r source-map-support/register ." }, diff --git a/examples/validation-app/src/openapi-spec.ts b/examples/validation-app/src/openapi-spec.ts new file mode 100644 index 000000000000..e5c351543766 --- /dev/null +++ b/examples/validation-app/src/openapi-spec.ts @@ -0,0 +1,28 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/example-validation-app +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {ApplicationConfig} from '@loopback/core'; +import {ValidationApplication} from './application'; + +/** + * Export the OpenAPI spec from the application + */ +async function exportOpenApiSpec(): Promise { + const config: ApplicationConfig = { + rest: { + port: +(process.env.PORT ?? 3000), + host: process.env.HOST ?? 'localhost', + }, + }; + const outFile = process.argv[2] ?? ''; + const app = new ValidationApplication(config); + await app.boot(); + await app.exportOpenApiSpec(outFile); +} + +exportOpenApiSpec().catch(err => { + console.error('Fail to export OpenAPI spec from the application.', err); + process.exit(1); +});