Skip to content

Commit

Permalink
chore: some swagger config
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslavirha committed Dec 19, 2023
1 parent 0dc1b92 commit e1fda6d
Show file tree
Hide file tree
Showing 29 changed files with 230 additions and 127 deletions.
4 changes: 2 additions & 2 deletions api/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"bootstrap": "pnpm run config:api && pnpm run keys",
"build": "pnpm run barrels && tsc --project tsconfig.compile.json",
"barrels": "barrelsby --config .barrelsby.json",
"start": "pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "cross-env NODE_ENV=production node dist/index.js",
"start": "pnpm run copy:views && pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "pnpm run copy:views && cross-env NODE_ENV=production node dist/index.js",
"format": "eslint --ext .ts --fix src",
"lint": "eslint --ext .ts src",
"test": "pnpm run config:test && cross-env NODE_ENV=test jest",
Expand Down
7 changes: 5 additions & 2 deletions api/authentication/src/global/models/ConfigModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoDBConfigModel, RedisConfigModel } from '@hikers-book/tsed-common/models';
import { Required } from '@tsed/schema';
import { MongoDBConfigModel, RedisConfigModel, SwaggerConfigModel } from '@hikers-book/tsed-common/models';
import { Property, Required } from '@tsed/schema';
import { AuthConfigModel } from './config/AuthModel';
import { FrontendConfigModel } from './config/FrontendConfigModel';
import { JWTConfigModel } from './config/JWTConfigModel';
Expand All @@ -23,4 +23,7 @@ export class ConfigModel {

@Required()
redis!: RedisConfigModel;

@Property(SwaggerConfigModel)
swagger?: SwaggerConfigModel;
}
34 changes: 20 additions & 14 deletions api/authentication/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import {
ConfigLoaderOptions,
SwaggerDocConfig,
SwaggerDocsVersion,
SwaggerSecurityScheme
} from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { resolve } from 'path';
import { ConfigModel } from '../models/ConfigModel';
Expand All @@ -9,21 +14,22 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
public static readonly options: ConfigLoaderOptions<ConfigModel> = {
service: `Hiker's Book Authentication API`,
port: 5501,
configModel: ConfigModel,
swagger: [
{
doc: SwaggerDocsVersion.AUTH,
security: [SwaggerSecurityScheme.BEARER_JWT, SwaggerSecurityScheme.BASIC],
outFile: resolve(__dirname, '../../auth/swagger.json')
},
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
]
configModel: ConfigModel
};

public static readonly swagger: SwaggerDocConfig[] = [
{
doc: SwaggerDocsVersion.AUTH,
security: [SwaggerSecurityScheme.BEARER_JWT, SwaggerSecurityScheme.BASIC],
outFile: resolve(__dirname, '../../auth/swagger.json')
},
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
];

constructor() {
super(ConfigService.options);
}
Expand Down
9 changes: 8 additions & 1 deletion api/authentication/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { $log } from '@tsed/common';
import { PlatformExpress } from '@tsed/platform-express';
import { SwaggerSettings } from '@tsed/swagger';
import { Server } from './Server';
import { ConfigService } from './global/services/ConfigService';

async function bootstrap() {
try {
const config = new ConfigLoder(ConfigService.options);

let swagger: SwaggerSettings[] = [];

if (config.config.swagger?.enabled) {
swagger = config.buildSwagger(ConfigService.swagger, config.config.swagger.swaggerUIOptions);
}

const configuration: Partial<TsED.Configuration> = {
...config.server,
api: config.api,
swagger: config.swagger,
swagger,
mongoose: [
{
id: 'authentication',
Expand Down
4 changes: 2 additions & 2 deletions api/stages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"build": "pnpm run barrels && tsc --project tsconfig.compile.json",
"barrels": "barrelsby --config .barrelsby.json",
"start": "pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "cross-env NODE_ENV=production node dist/index.js",
"start": "pnpm run copy:views && pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "pnpm run copy:views && cross-env NODE_ENV=production node dist/index.js",
"format": "eslint --ext .ts --fix src",
"lint": "eslint --ext .ts src",
"test": "pnpm run config:test && cross-env NODE_ENV=test jest",
Expand Down
7 changes: 5 additions & 2 deletions api/stages/src/global/models/ConfigModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoDBConfigModel, RedisConfigModel } from '@hikers-book/tsed-common/models';
import { Required } from '@tsed/schema';
import { MongoDBConfigModel, RedisConfigModel, SwaggerConfigModel } from '@hikers-book/tsed-common/models';
import { Property, Required } from '@tsed/schema';

class ConfigModelAPIsHikersBook {
@Required()
Expand All @@ -20,4 +20,7 @@ export class ConfigModel {

@Required()
redis!: RedisConfigModel;

@Property(SwaggerConfigModel)
swagger?: SwaggerConfigModel;
}
24 changes: 15 additions & 9 deletions api/stages/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import {
ConfigLoaderOptions,
SwaggerDocConfig,
SwaggerDocsVersion,
SwaggerSecurityScheme
} from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { resolve } from 'path';
import { ConfigModel } from '../models/ConfigModel';
Expand All @@ -9,16 +14,17 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
public static readonly options: ConfigLoaderOptions<ConfigModel> = {
service: `Hiker's Book Stages API`,
port: 5503,
configModel: ConfigModel,
swagger: [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
]
configModel: ConfigModel
};

public static readonly swagger: SwaggerDocConfig[] = [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
];

constructor() {
super(ConfigService.options);
}
Expand Down
9 changes: 8 additions & 1 deletion api/stages/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { $log } from '@tsed/common';
import { PlatformExpress } from '@tsed/platform-express';
import { SwaggerSettings } from '@tsed/swagger';
import { Server } from './Server';
import { ConfigService } from './global/services/ConfigService';

async function bootstrap() {
try {
const config = new ConfigLoder(ConfigService.options);

let swagger: SwaggerSettings[] = [];

if (config.config.swagger?.enabled) {
swagger = config.buildSwagger(ConfigService.swagger, config.config.swagger.swaggerUIOptions);
}

const configuration: Partial<TsED.Configuration> = {
...config.server,
api: config.api,
swagger: config.swagger,
swagger,
mongoose: [
{
id: 'stages',
Expand Down
4 changes: 2 additions & 2 deletions api/trips/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"build": "pnpm run barrels && tsc --project tsconfig.compile.json",
"barrels": "barrelsby --config .barrelsby.json",
"start": "pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "cross-env NODE_ENV=production node dist/index.js",
"start": "pnpm run copy:views && pnpm run barrels && NODE_ENV=localhost tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
"start:prod": "pnpm run copy:views && cross-env NODE_ENV=production node dist/index.js",
"format": "eslint --ext .ts --fix src",
"lint": "eslint --ext .ts src",
"test": "pnpm run config:test && cross-env NODE_ENV=test jest",
Expand Down
7 changes: 5 additions & 2 deletions api/trips/src/global/models/ConfigModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoDBConfigModel, RedisConfigModel } from '@hikers-book/tsed-common/models';
import { Required } from '@tsed/schema';
import { MongoDBConfigModel, RedisConfigModel, SwaggerConfigModel } from '@hikers-book/tsed-common/models';
import { Property, Required } from '@tsed/schema';

class ConfigModelAPIsHikersBook {
@Required()
Expand All @@ -20,4 +20,7 @@ export class ConfigModel {

@Required()
redis!: RedisConfigModel;

@Property(SwaggerConfigModel)
swagger?: SwaggerConfigModel;
}
24 changes: 15 additions & 9 deletions api/trips/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import {
ConfigLoaderOptions,
SwaggerDocConfig,
SwaggerDocsVersion,
SwaggerSecurityScheme
} from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { resolve } from 'path';
import { ConfigModel } from '../models/ConfigModel';
Expand All @@ -9,16 +14,17 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
public static readonly options: ConfigLoaderOptions<ConfigModel> = {
service: `Hiker's Book Trips API`,
port: 5504,
configModel: ConfigModel,
swagger: [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
]
configModel: ConfigModel
};

public static readonly swagger: SwaggerDocConfig[] = [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT],
outFile: resolve(__dirname, '../../v1/swagger.json')
}
];

constructor() {
super(ConfigService.options);
}
Expand Down
9 changes: 8 additions & 1 deletion api/trips/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { $log } from '@tsed/common';
import { PlatformExpress } from '@tsed/platform-express';
import { SwaggerSettings } from '@tsed/swagger';
import { Server } from './Server';
import { ConfigService } from './global/services/ConfigService';

async function bootstrap() {
try {
const config = new ConfigLoder(ConfigService.options);

let swagger: SwaggerSettings[] = [];

if (config.config.swagger?.enabled) {
swagger = config.buildSwagger(ConfigService.swagger, config.config.swagger.swaggerUIOptions);
}

const configuration: Partial<TsED.Configuration> = {
...config.server,
api: config.api,
swagger: config.swagger,
swagger,
mongoose: [
{
id: 'trips',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
1 change: 1 addition & 0 deletions infra/helm/hikers-book/templates/ingress-srv-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ metadata:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
# nginx.ingress.kubernetes.io/x-forwarded-prefix: /$1
spec:
rules:
- host: api.hikers-book.dev.info
Expand Down
3 changes: 3 additions & 0 deletions packages/config/config/api.authentication.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions packages/config/config/api.stages.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions packages/config/config/api.trips.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"username": "",
"password": ""
}
},
"swagger": {
"enabled": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('SwaggerController', () => {

expect(minified).toContain(`<title>Test API 1.0.0</title>`);
expect(minified).toContain(`<h1>Test API 1.0.0</h1>`);
expect(minified).toContain(`<li><a href="/v1"><span>API v1</span> <span>OpenSpec 3.0.3</span></a></li>`);
expect(minified).toContain(`<li><a href="/v2"><span>API v2</span> <span>OpenSpec 3.0.3</span></a></li>`);
expect(minified).toContain(`/v1"><span>API v1</span> <span>OpenSpec 3.0.3</span></a></li>`);
expect(minified).toContain(`/v2"><span>API v2</span> <span>OpenSpec 3.0.3</span></a></li>`);
expect(response.status).toEqual(200);
});
});
16 changes: 16 additions & 0 deletions packages/tsed-common/src/models/config/Swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AdditionalProperties, Property } from '@tsed/schema';
import { SwaggerUIOptions } from '@tsed/swagger';

@AdditionalProperties(true)
export class SwaggerUIOptionsConfigModel implements SwaggerUIOptions {
[key: string]: unknown;
}

@AdditionalProperties(false)
export class SwaggerConfigModel {
@Property()
enabled: boolean = true;

@Property(SwaggerUIOptionsConfigModel)
swaggerUIOptions: SwaggerUIOptionsConfigModel = {};
}
1 change: 1 addition & 0 deletions packages/tsed-common/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './JWTAuthenticationResponse';
export * from './SwaggerSecuritySchemes';
export * from './config/MongoDB';
export * from './config/Redis';
export * from './config/Swagger';
2 changes: 1 addition & 1 deletion packages/tsed-common/src/server/ConfigFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ajv = new Ajv({ allErrors: true });
export class ConfigFileLoader<T> {
readonly _config: T;

public get config() {
public get config(): T {
return Object.assign({}, this._config);
}

Expand Down
Loading

0 comments on commit e1fda6d

Please sign in to comment.