Skip to content

Commit 28b49fd

Browse files
authored
Merge pull request #363 from pharindoko/feat/enableSwagger
feat(common): add option to enable/disable swagger interface
2 parents 260fa8d + 86cd6cf commit 28b49fd

File tree

7 files changed

+91
-43
lines changed

7 files changed

+91
-43
lines changed

packages/cli/src/commands/create-stack.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export class CreateStackCommand extends Command {
2222
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
2323
required: false, // default value if flag not passed (can be a function that returns a string or undefined)
2424
}),
25+
swagger: flags.boolean({
26+
char: 's', // shorter flag version
27+
description: 'enable or disable swagger interface support', // help description for flag
28+
hidden: false, // hide from help
29+
default: true, // default value if flag not passed (can be a function that returns a string or undefined)
30+
required: false, // make flag required (this is not common and you should probably use an argument instead)
31+
allowNo: true,
32+
}),
2533
apikeyauth: flags.boolean({
2634
char: 'a', // shorter flag version
2735
description: 'require api key authentication to access api', // help description for flag
@@ -127,6 +135,7 @@ export class CreateStackCommand extends Command {
127135
appconfig.jsonFile = filePath;
128136
appconfig.enableApiKeyAuth = flags.apikeyauth;
129137
appconfig.readOnly = flags.readonly;
138+
appconfig.enableSwagger = flags.swagger;
130139
appconfig.stackName = stackName;
131140
Helpers.createDir(stackFolder + '/config');
132141
fs.writeFileSync(

packages/cli/src/commands/run.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export class Run extends Command {
1818
default: 'local',
1919
required: false,
2020
}),
21+
swagger: flags.boolean({
22+
char: 's', // shorter flag version
23+
description: 'enable or disable swagger interface support', // help description for flag
24+
hidden: false, // hide from help
25+
default: true, // default value if flag not passed (can be a function that returns a string or undefined)
26+
required: false, // make flag required (this is not common and you should probably use an argument instead)
27+
allowNo: true,
28+
}),
2129
readonly: flags.boolean({
2230
char: 'r', // shorter flag version
2331
description: 'set api to readonly (true) or writeable (false)', // help description for flag
@@ -44,6 +52,7 @@ export class Run extends Command {
4452
const server = express();
4553
const defaultConfig = new AppConfig();
4654
defaultConfig.readOnly = flags.readonly;
55+
defaultConfig.enableSwagger = flags.swagger;
4756
defaultConfig.jsonFile = args.file;
4857
if (args.file && flags.env) {
4958
const promise = startServer(
@@ -55,28 +64,42 @@ export class Run extends Command {
5564
await promise;
5665
this.log();
5766
this.log();
58-
cli.table(
59-
[
60-
{
61-
text: `${chalk.blueBright('Swagger UI')}`,
62-
link: 'http://localhost:3000/ui',
63-
},
64-
{
65-
text: `${chalk.blueBright('GraphiQL')}`,
66-
link: 'http://localhost:3000/graphql',
67-
},
68-
{
69-
text: `${chalk.blueBright('Swagger Specification')}`,
70-
link: 'http://localhost:3000/api-spec',
71-
},
72-
{
73-
text: `${chalk.blueBright('API Routes')}`,
74-
link: 'http://localhost:3000/api/{routes}',
75-
},
76-
],
77-
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
78-
{ 'no-header': true }
79-
);
67+
68+
if (flags.swagger) {
69+
cli.table(
70+
[
71+
{
72+
text: `${chalk.blueBright('Swagger UI')}`,
73+
link: 'http://localhost:3000/ui',
74+
},
75+
{
76+
text: `${chalk.blueBright('GraphiQL')}`,
77+
link: 'http://localhost:3000/graphql',
78+
},
79+
{
80+
text: `${chalk.blueBright('Swagger Specification')}`,
81+
link: 'http://localhost:3000/api-spec',
82+
},
83+
{
84+
text: `${chalk.blueBright('API Routes')}`,
85+
link: 'http://localhost:3000/api/{routes}',
86+
},
87+
],
88+
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
89+
{ 'no-header': true }
90+
);
91+
} else {
92+
cli.table(
93+
[
94+
{
95+
text: `${chalk.blueBright('API Routes')}`,
96+
link: 'http://localhost:3000/api/{routes}',
97+
},
98+
],
99+
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
100+
{ 'no-header': true }
101+
);
102+
}
80103
this.log();
81104
this.log();
82105
}

packages/cli/src/commands/update-stack.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export class UpdateStackCommand extends Command {
2020
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
2121
required: false, // default value if flag not passed (can be a function that returns a string or undefined)
2222
}),
23+
swagger: flags.boolean({
24+
char: 's', // shorter flag version
25+
description: 'enable or disable swagger interface support', // help description for flag
26+
hidden: false, // hide from help
27+
default: true, // default value if flag not passed (can be a function that returns a string or undefined)
28+
required: false, // make flag required (this is not common and you should probably use an argument instead)
29+
allowNo: true,
30+
}),
2331
apikeyauth: flags.boolean({
2432
char: 'a', // shorter flag version
2533
description: 'require api key authentication to access api', // help description for flag
@@ -74,6 +82,12 @@ export class UpdateStackCommand extends Command {
7482
{
7583
title: 'Copy Template Files',
7684
task: async (task) => {
85+
if (process.env.NODE_ENV === 'local') {
86+
await fs.copy(
87+
templateFolder + '/node_modules',
88+
stackFolder + '/node_modules'
89+
);
90+
}
7791
await fs.copy(templateFolder + '/src', stackFolder + '/src');
7892
await fs.copy(
7993
templateFolder + '/package.json',
@@ -105,6 +119,7 @@ export class UpdateStackCommand extends Command {
105119
);
106120
appConfig.enableApiKeyAuth = flags.apikeyauth;
107121
appConfig.readOnly = flags.readonly;
122+
appConfig.enableSwagger = flags.swagger;
108123
fs.writeFileSync(
109124
path.normalize(stackFolder + '/config/appconfig.json'),
110125
JSON.stringify(appConfig, null, 2),
@@ -115,15 +130,17 @@ export class UpdateStackCommand extends Command {
115130
{
116131
title: 'Update Dependencies',
117132
task: async (task) => {
118-
task.output = 'INSTALL DEPENDENCIES';
119-
Helpers.removeDir(stackFolder + '/node_modules');
120-
await Helpers.executeChildProcess(
121-
'npm i',
122-
{
123-
cwd: stackFolder,
124-
},
125-
false
126-
);
133+
if (process.env.NODE_ENV != 'local') {
134+
task.output = 'INSTALL DEPENDENCIES';
135+
Helpers.removeDir(stackFolder + '/node_modules');
136+
await Helpers.executeChildProcess(
137+
'npm i',
138+
{
139+
cwd: stackFolder,
140+
},
141+
false
142+
);
143+
}
127144
},
128145
},
129146
{

packages/server/src/app/app.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export class AppConfig {
33
enableApiKeyAuth = false;
44
jsonFile = 'db.json';
55
enableJSONValidation = true;
6+
enableSwagger = true;
67
stackName = 'jsonsls';
78
static merge = <T, U>(t: T, u: U) => Object.assign({}, t, u);
89
}

packages/server/src/app/core.app.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class CoreApp {
4848
const json = await this.adapter.getState();
4949
if (this.validateJSON(json)) {
5050
const { middlewares, router } = this.initializeLayers();
51-
await this.setupRoutes(json, middlewares, router);
51+
await this.setupRoutes(json, middlewares, router, this.appConfig);
5252
} else {
5353
Output.setError('provided json is not valid - see validation checks');
5454
throw Error('provided json is not valid - see validation checks');
@@ -77,11 +77,16 @@ export class CoreApp {
7777
return isValid;
7878
}
7979

80-
protected async setupRoutes(db: {}, middlewares, router): Promise<void> {
80+
protected async setupRoutes(
81+
db: {},
82+
middlewares,
83+
router,
84+
appConfig: AppConfig
85+
): Promise<void> {
8186
middlewares.splice(middlewares.findIndex(x => x.name === 'serveStatic'), 1);
8287
this.server.use(middlewares);
8388
this.server.use('/api', router);
84-
if (!this.swaggerSpec) {
89+
if (!this.swaggerSpec && appConfig.enableSwagger) {
8590
this.swaggerSpec = this.apispec.generateSpecification(db, true);
8691
const swaggerSetupMiddleware = swaggerUi.setup(this.swaggerSpec);
8792
swaggerSetupMiddleware(

packages/server/tests/handler.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import request from 'supertest';
22
import express from 'express';
33
import { TestServer } from '../src/coreserver';
4-
import fs from 'fs';
54
import { Swagger } from '../src/specifications/swagger/swagger';
65
import { SwaggerConfig } from '../src/specifications/swagger/swagger.config';
76
import { FileStorageAdapter } from '../src/storage/file.storage';
87
import { Environment } from '../src/environment/environment';
98
import { CoreApp, AppConfig } from '../src/app';
109

11-
const appConfig: AppConfig = JSON.parse(
12-
fs.readFileSync('./tests/resources/appconfig.json', 'UTF-8')
13-
);
10+
const appConfig = new AppConfig();
1411
appConfig.jsonFile = './tests/resources/validate.json';
15-
const server = express();
1612

13+
const server = express();
1714
const environment = new Environment();
1815
const swagger = new Swagger(
1916
server,

packages/server/tests/resources/appconfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)