Skip to content

Commit 1ca6a19

Browse files
authored
Merge pull request #439 from pharindoko/api-path-config
Api path config
2 parents 3cf38c1 + 9d0de45 commit 1ca6a19

File tree

12 files changed

+91
-32
lines changed

12 files changed

+91
-32
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ export class CreateStackCommand extends Command {
7474
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
7575
required: false, // make flag required (this is not common and you should probably use an argument instead)
7676
}),
77+
apiRoute: flags.string({
78+
description: 'path to use for api route', // help description for flag
79+
hidden: false, // hide from help
80+
default: '/api',
81+
required: false, // make flag required (this is not common and you should probably use an argument instead)
82+
}),
7783
};
7884

7985
static args = [
@@ -195,7 +201,8 @@ export class CreateStackCommand extends Command {
195201
appconfig.readOnly = flags.readonly;
196202
appconfig.enableSwagger = flags.swagger;
197203
appconfig.stackName = stackName!;
198-
appconfig.logLevel = flags.loglevel as LogLevel;;
204+
appconfig.logLevel = flags.loglevel as LogLevel;
205+
appconfig.apiRoutePath = flags.apiRoute;
199206
Helpers.createDir(stackFolder + '/config');
200207
fs.writeFileSync(
201208
path.normalize(stackFolder + '/config/appconfig.json'),

packages/cli/src/commands/run.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export class Run extends Command {
4141
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
4242
required: false, // make flag required (this is not common and you should probably use an argument instead)
4343
}),
44+
apiRoute: flags.string({
45+
description: 'path to use for api route', // help description for flag
46+
hidden: false, // hide from help
47+
default: '/api',
48+
required: false, // make flag required (this is not common and you should probably use an argument instead)
49+
}),
4450
};
4551

4652
static args = [
@@ -62,6 +68,7 @@ export class Run extends Command {
6268
defaultConfig.readOnly = flags.readonly;
6369
defaultConfig.enableSwagger = flags.swagger;
6470
defaultConfig.logLevel = flags.loglevel as LogLevel;
71+
defaultConfig.apiRoutePath = flags.apiRoute;
6572
defaultConfig.jsonFile = args.file;
6673
if (args.file && flags.env) {
6774
const promise = startServer(
@@ -91,7 +98,7 @@ export class Run extends Command {
9198
},
9299
{
93100
text: `${chalk.blueBright('API Routes')}`,
94-
link: 'http://localhost:3000/api/{routes}',
101+
link: 'http://localhost:3000' + flags.apiRoute + '/{routes}',
95102
},
96103
],
97104
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
@@ -102,7 +109,7 @@ export class Run extends Command {
102109
[
103110
{
104111
text: `${chalk.blueBright('API Routes')}`,
105-
link: 'http://localhost:3000/api/{routes}',
112+
link: 'http://localhost:3000' + flags.apiRoute + '/{routes}',
106113
},
107114
],
108115
{ text: { minWidth: 30 }, link: { minWidth: 20 } },

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export class UpdateStackCommand extends Command {
5252
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
5353
required: false, // make flag required (this is not common and you should probably use an argument instead)
5454
}),
55+
apiRoute: flags.string({
56+
description: 'path to use for api route', // help description for flag
57+
hidden: false, // hide from help
58+
default: '/api',
59+
required: false, // make flag required (this is not common and you should probably use an argument instead)
60+
}),
5561
};
5662

5763
async run() {
@@ -132,6 +138,7 @@ export class UpdateStackCommand extends Command {
132138
appConfig.readOnly = flags.readonly;
133139
appConfig.enableSwagger = flags.swagger;
134140
appConfig.logLevel = flags.loglevel as LogLevel;
141+
appConfig.apiRoutePath = flags.apiRoute;
135142
fs.writeFileSync(
136143
path.normalize(stackFolder + '/config/appconfig.json'),
137144
JSON.stringify(appConfig, null, 2),

packages/server/package-lock.json

Lines changed: 48 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"nodemon": "2.0.4",
8080
"ts-jest": "26.1.0",
8181
"ts-loader": "7.0.5",
82+
"ts-node": "^8.10.2",
8283
"typescript": "3.9.3"
8384
},
8485
"config": {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class AppConfig {
66
enableSwagger = true;
77
logLevel = LogLevel.info;
88
stackName = 'jsonsls';
9+
apiRoutePath = '/api';
910
static merge = <T, U>(t: T, u: U) => Object.assign({}, t, u);
1011
}
1112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class CoreApp {
101101
1
102102
);
103103
this.server.use(middlewares);
104-
this.server.use('/api', router);
104+
this.server.use(appConfig.apiRoutePath, router);
105105
if (!this.swaggerSpec && appConfig.enableSwagger) {
106106
this.swaggerSpec = this.apispec.generateSpecification(db, true);
107107
const swaggerSetupMiddleware = swaggerUi.setup(this.swaggerSpec);

packages/server/src/factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export class ServerFactory {
8484
server,
8585
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
8686
env.basePath,
87+
appConfig.apiRoutePath,
8788
packageJsonFilePath
8889
);
8990
const core = new coreserver(

packages/server/src/specifications/swagger/swagger.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ export class Swagger implements ApiSpecification {
1212
private server: express.Express;
1313
private config: SwaggerConfig;
1414
private basePath: string;
15+
private apiRoutePath: string;
1516
constructor(
1617
server: express.Express,
1718
config: SwaggerConfig,
1819
basePath: string,
20+
apiRoutePath: string,
1921
packageJsonFilePath: string
2022
) {
2123
this.server = server;
2224
this.config = config;
2325
this.basePath = basePath;
26+
this.apiRoutePath = apiRoutePath;
2427
this.swaggerSpec = new SwaggerSpec(packageJsonFilePath);
2528
}
2629

@@ -33,7 +36,8 @@ export class Swagger implements ApiSpecification {
3336
this.server,
3437
{},
3538
this.config.readOnly,
36-
this.basePath
39+
this.basePath,
40+
this.apiRoutePath
3741
);
3842
const auth: ApiKeySecurity = {
3943
type: 'apiKey',

packages/server/src/specifications/swagger/swaggerspec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class SwaggerSpec {
7171
return sorted;
7272
}
7373

74-
initSpec(readOnly: boolean, basePath: string) {
74+
initSpec(readOnly: boolean, basePath: string, routePath: string) {
7575
const info = this.updateSpecFromPackage(basePath);
7676
let specification: Spec = {
7777
swagger: '2.0',
@@ -80,7 +80,10 @@ export class SwaggerSpec {
8080
};
8181
specification.swagger = '2.0';
8282
specification.paths = {};
83-
const excludedRoutes = ['/api/:resource/:id/:nested', '/api/db'];
83+
const excludedRoutes = [
84+
routePath + '/:resource/:id/:nested',
85+
routePath + '/db',
86+
];
8487
const endpoints = listEndpoints(this.app);
8588
endpoints.forEach((endpoint: EndPoint) => {
8689
if (readOnly) {
@@ -134,11 +137,12 @@ export class SwaggerSpec {
134137
app: express.Express,
135138
predefinedSpec: object,
136139
readOnly: boolean,
137-
basePath: string
140+
basePath: string,
141+
apiRoutePath: string
138142
) => {
139143
this.app = app;
140144
this.predefinedSpec = predefinedSpec;
141-
this.spec = this.initSpec(readOnly, basePath);
145+
this.spec = this.initSpec(readOnly, basePath, apiRoutePath);
142146
return this.spec;
143147
};
144148

0 commit comments

Comments
 (0)