Skip to content

Commit

Permalink
chore: swagger better configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslavirha committed Dec 1, 2023
1 parent 9a8ae45 commit 054a9ba
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 43 deletions.
13 changes: 11 additions & 2 deletions api/authentication/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion } from '@hikers-book/tsed-common/types';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { ConfigModel } from '../models/ConfigModel';

Expand All @@ -9,7 +9,16 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
service: `Hiker's Book Authentication API`,
port: 5501,
configModel: ConfigModel,
generateDocs: [SwaggerDocsVersion.AUTH, SwaggerDocsVersion.V1]
swagger: [
{
doc: SwaggerDocsVersion.AUTH,
security: [SwaggerSecurityScheme.BEARER_JWT, SwaggerSecurityScheme.BASIC]
},
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT]
}
]
};

constructor() {
Expand Down
9 changes: 7 additions & 2 deletions api/stages/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion } from '@hikers-book/tsed-common/types';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { ConfigModel } from '../models/ConfigModel';

Expand All @@ -9,7 +9,12 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
service: `Hiker's Book Stages API`,
port: 5503,
configModel: ConfigModel,
generateDocs: [SwaggerDocsVersion.V1]
swagger: [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT]
}
]
};

constructor() {
Expand Down
9 changes: 7 additions & 2 deletions api/trips/src/global/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigLoder } from '@hikers-book/tsed-common/server';
import { ConfigLoaderOptions, SwaggerDocsVersion } from '@hikers-book/tsed-common/types';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '@hikers-book/tsed-common/types';
import { Injectable } from '@tsed/di';
import { ConfigModel } from '../models/ConfigModel';

Expand All @@ -9,7 +9,12 @@ export class ConfigService extends ConfigLoder<ConfigModel> {
service: `Hiker's Book Trips API`,
port: 5504,
configModel: ConfigModel,
generateDocs: [SwaggerDocsVersion.V1]
swagger: [
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT]
}
]
};

constructor() {
Expand Down
16 changes: 16 additions & 0 deletions packages/tsed-common/src/models/SwaggerSecuritySchemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { OS3Security } from '@tsed/openspec';
import { EnumObject, SwaggerSecurityScheme } from '../types';

export const SWAGGER_SECURITY_SCHEMES: EnumObject<SwaggerSecurityScheme, OS3Security> = {
[SwaggerSecurityScheme.BEARER_JWT]: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Bearer JWT token'
},
[SwaggerSecurityScheme.BASIC]: {
type: 'http',
scheme: 'basic',
description: 'Basic authentication'
}
};
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 @@ -4,6 +4,7 @@

export * from './Base';
export * from './JWTAuthenticationResponse';
export * from './SwaggerSecuritySchemes';
export * from './config/MongoDB';
export * from './config/Redis';
export * from './config/Session';
24 changes: 11 additions & 13 deletions packages/tsed-common/src/server/ConfigLoader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { $log } from '@tsed/common';
import { Required } from '@tsed/schema';
import { ConfigLoder } from '.';
import { ConfigLoaderOptions, SwaggerDocsVersion } from '../types';
import { ConfigLoaderOptions, SwaggerDocsVersion, SwaggerSecurityScheme } from '../types';

// Must match the config file in config/test.json
class ConfigModel {
Expand All @@ -18,7 +18,16 @@ const options: ConfigLoaderOptions = {
service: 'test',
port: 4000,
configModel: ConfigModel,
generateDocs: [SwaggerDocsVersion.GLOBAL, SwaggerDocsVersion.V1]
swagger: [
{
doc: SwaggerDocsVersion.GLOBAL,
security: [SwaggerSecurityScheme.BASIC]
},
{
doc: SwaggerDocsVersion.V1,
security: [SwaggerSecurityScheme.BEARER_JWT]
}
]
};

describe('ConfigLoder', () => {
Expand Down Expand Up @@ -53,12 +62,6 @@ describe('ConfigLoder', () => {
}),
components: expect.objectContaining({
securitySchemes: {
BEARER_JWT: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Bearer JWT token'
},
BASIC: {
type: 'http',
scheme: 'basic',
Expand All @@ -84,11 +87,6 @@ describe('ConfigLoder', () => {
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Bearer JWT token'
},
BASIC: {
type: 'http',
scheme: 'basic',
description: 'Basic authentication'
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/tsed-common/src/server/ConfigLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ConfigLoder<T> {
title: options.service,
version: this.packageJson.version,
description: this.packageJson.description ?? '',
generateDocs: options.generateDocs ?? []
swagger: options.swagger ?? []
}).settings;
}
}
38 changes: 18 additions & 20 deletions packages/tsed-common/src/server/ConfigSwagger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OpenSpec3, OpenSpecInfo } from '@tsed/openspec';
import { OS3Security, OpenSpec3, OpenSpecHash, OpenSpecInfo } from '@tsed/openspec';
import { SwaggerSettings } from '@tsed/swagger';
import { SWAGGER_SECURITY_SCHEMES } from '../models/SwaggerSecuritySchemes';
import { SwaggerSecurityScheme } from '../types';
import { ConfigSwaggerOptions } from '../types/ConfigSwaggerOptions';
import { SwaggerDocsVersion } from '../types/SwaggerDocsVersion.enum';
import { ConfigSwaggerOptions, SwaggerDocConfig } from '../types/ConfigSwaggerOptions';

export class ConfigSwagger {
readonly _settings: SwaggerSettings[];
Expand All @@ -12,7 +12,7 @@ export class ConfigSwagger {
}

constructor(options: ConfigSwaggerOptions) {
this._settings = options.generateDocs.map((docsVersion) =>
this._settings = options.swagger.map((docsVersion) =>
this.generateSettings(options.title, options.version, options.description, docsVersion)
);
}
Expand All @@ -22,11 +22,11 @@ export class ConfigSwagger {
title: string,
version: string,
description: string,
docsVersion: SwaggerDocsVersion
settings: SwaggerDocConfig
): SwaggerSettings {
return {
path: `/${docsVersion}/docs`,
doc: docsVersion,
path: `/${settings.doc}/docs`,
doc: settings.doc,
specVersion: '3.0.3',
spec: <Partial<OpenSpec3>>{
info: <OpenSpecInfo>{
Expand All @@ -35,21 +35,19 @@ export class ConfigSwagger {
description
},
components: {
securitySchemes: {
[SwaggerSecurityScheme.BEARER_JWT]: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Bearer JWT token'
},
[SwaggerSecurityScheme.BASIC]: {
type: 'http',
scheme: 'basic',
description: 'Basic authentication'
}
}
securitySchemes: this.getSecuritySchemes(settings.security)
}
}
};
}

private getSecuritySchemes(security: SwaggerSecurityScheme[]): OpenSpecHash<OS3Security> {
const schemes: OpenSpecHash<OS3Security> = {};

for (const scheme of security) {
schemes[scheme] = SWAGGER_SECURITY_SCHEMES[scheme];
}

return schemes;
}
}
4 changes: 2 additions & 2 deletions packages/tsed-common/src/types/ConfigLoaderOptions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Type } from '@tsed/core';
import { SwaggerDocsVersion } from './SwaggerDocsVersion.enum';
import { SwaggerDocConfig } from './ConfigSwaggerOptions';

export type ConfigLoaderOptions<T = object> = {
service: string;
port: number;
configModel: Type<T>;
generateDocs?: SwaggerDocsVersion[];
swagger?: SwaggerDocConfig[];
};
8 changes: 7 additions & 1 deletion packages/tsed-common/src/types/ConfigSwaggerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { SwaggerDocsVersion } from './SwaggerDocsVersion.enum';
import { SwaggerSecurityScheme } from './SwaggerSecurityScheme.enum';

export type SwaggerDocConfig = {
doc: SwaggerDocsVersion;
security: SwaggerSecurityScheme[];
};

export type ConfigSwaggerOptions = {
title: string;
version: string;
description: string;
generateDocs: SwaggerDocsVersion[];
swagger: SwaggerDocConfig[];
};
1 change: 1 addition & 0 deletions packages/tsed-common/src/types/EnumObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type EnumObject<TKey extends string | symbol | number, TType> = { [key in TKey]: TType };
1 change: 1 addition & 0 deletions packages/tsed-common/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

export * from './ConfigLoaderOptions';
export * from './ConfigSwaggerOptions';
export * from './EnumObject';
export * from './JWTPayload';
export * from './SwaggerDocsVersion.enum';
export * from './SwaggerSecurityScheme.enum';
Expand Down

0 comments on commit 054a9ba

Please sign in to comment.