diff --git a/e2e/fastify.e2e-spec.ts b/e2e/fastify.e2e-spec.ts index c7da7695e..cdf3bb4f2 100644 --- a/e2e/fastify.e2e-spec.ts +++ b/e2e/fastify.e2e-spec.ts @@ -27,8 +27,8 @@ describe('Fastify Swagger', () => { .addBearerAuth() .addOAuth2() .addApiKey() - .addApiKey({ type: 'apiKey' }, 'key1') - .addApiKey({ type: 'apiKey' }, 'key2') + .addApiKey({}, 'key1') + .addApiKey({}, 'key2') .addCookieAuth() .addSecurityRequirements('bearer') .addSecurityRequirements({ basic: [], cookie: [] }); diff --git a/e2e/validate-schema.e2e-spec.ts b/e2e/validate-schema.e2e-spec.ts index ca1ea0a08..18bd44626 100644 --- a/e2e/validate-schema.e2e-spec.ts +++ b/e2e/validate-schema.e2e-spec.ts @@ -35,8 +35,8 @@ describe('Validate OpenAPI schema', () => { .addBearerAuth() .addOAuth2() .addApiKey() - .addApiKey({ type: 'apiKey' }, 'key1') - .addApiKey({ type: 'apiKey' }, 'key2') + .addApiKey({}, 'key1') + .addApiKey({}, 'key2') .addCookieAuth() .addSecurityRequirements('bearer') .addSecurityRequirements({ basic: [], cookie: [] }) diff --git a/lib/document-builder.ts b/lib/document-builder.ts index 0be9a8c8f..7ac21447a 100644 --- a/lib/document-builder.ts +++ b/lib/document-builder.ts @@ -3,7 +3,10 @@ import { isString, isUndefined, negate, pickBy } from 'lodash'; import { buildDocumentBase } from './fixtures/document.base'; import { OpenAPIObject } from './interfaces'; import { + ApiKeySchemeObject, ExternalDocumentationObject, + HttpSchemeObject, + OAuth2SchemeObject, SecurityRequirementObject, SecuritySchemeObject, ServerVariableObject, @@ -110,12 +113,11 @@ export class DocumentBuilder { } public addBearerAuth( - options: SecuritySchemeObject = { - type: 'http' - }, + options: Partial> = {}, name = 'bearer' ): this { this.addSecurity(name, { + type: 'http', scheme: 'bearer', bearerFormat: 'JWT', ...options @@ -124,9 +126,7 @@ export class DocumentBuilder { } public addOAuth2( - options: SecuritySchemeObject = { - type: 'oauth2' - }, + options: Partial> = {}, name = 'oauth2' ): this { this.addSecurity(name, { @@ -137,30 +137,26 @@ export class DocumentBuilder { return this; } - public addApiKey( - options: SecuritySchemeObject = { - type: 'apiKey' - }, - name = 'api_key' + public addBasicAuth( + options: Partial> = {}, + name = 'basic' ): this { this.addSecurity(name, { - type: 'apiKey', - in: 'header', - name, + type: 'http', + scheme: 'basic', ...options }); return this; } - public addBasicAuth( - options: SecuritySchemeObject = { - type: 'http' - }, - name = 'basic' + public addApiKey( + options: Partial> = {}, + name = 'api_key' ): this { this.addSecurity(name, { - type: 'http', - scheme: 'basic', + type: 'apiKey', + in: 'header', + name, ...options }); return this; @@ -168,9 +164,7 @@ export class DocumentBuilder { public addCookieAuth( cookieName = 'connect.sid', - options: SecuritySchemeObject = { - type: 'apiKey' - }, + options: Partial> = {}, securityName = 'cookie' ): this { this.addSecurity(securityName, { diff --git a/lib/interfaces/open-api-spec.interface.ts b/lib/interfaces/open-api-spec.interface.ts index 277da4f9f..719dbc1f7 100644 --- a/lib/interfaces/open-api-spec.interface.ts +++ b/lib/interfaces/open-api-spec.interface.ts @@ -250,16 +250,37 @@ export interface XmlObject { } export type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect'; +export type ApiKeyLocation = 'query' | 'header' | 'cookie'; -export interface SecuritySchemeObject { - type: SecuritySchemeType; +export type SecuritySchemeObject = + | ApiKeySchemeObject + | HttpSchemeObject + | OAuth2SchemeObject + | OpenIdConnectSchemeObject; + +export interface ApiKeySchemeObject { + type: 'apiKey'; description?: string; - name?: string; - in?: string; - scheme?: string; + name: string; + in: ApiKeyLocation; +} + +export interface HttpSchemeObject { + type: 'http'; + description?: string; + scheme: string; bearerFormat?: string; - flows?: OAuthFlowsObject; - openIdConnectUrl?: string; +} +export interface OAuth2SchemeObject { + type: 'oauth2'; + description?: string; + flows: OAuthFlowsObject; +} + +export interface OpenIdConnectSchemeObject { + type: 'openIdConnect'; + description?: string; + openIdConnectUrl: string; } export interface OAuthFlowsObject {