Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('Fastify Swagger', () => {
.addBearerAuth()
.addOAuth2()
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
Comment on lines -30 to -31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make it a breaking change, but would it make more sense to bring the name to be the first parameter instead of the options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmcdo29 , yep, you're right. Fixed

.addApiKey({}, 'key1')
.addApiKey({}, 'key2')
.addCookieAuth()
.addSecurityRequirements('bearer')
.addSecurityRequirements({ basic: [], cookie: [] });
Expand Down
4 changes: 2 additions & 2 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] })
Expand Down
42 changes: 18 additions & 24 deletions lib/document-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -110,12 +113,11 @@ export class DocumentBuilder {
}

public addBearerAuth(
options: SecuritySchemeObject = {
type: 'http'
},
options: Partial<Omit<HttpSchemeObject, 'type'>> = {},
name = 'bearer'
): this {
this.addSecurity(name, {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
...options
Expand All @@ -124,9 +126,7 @@ export class DocumentBuilder {
}

public addOAuth2(
options: SecuritySchemeObject = {
type: 'oauth2'
},
options: Partial<Omit<OAuth2SchemeObject, 'type'>> = {},
name = 'oauth2'
): this {
this.addSecurity(name, {
Expand All @@ -137,40 +137,34 @@ export class DocumentBuilder {
return this;
}

public addApiKey(
options: SecuritySchemeObject = {
type: 'apiKey'
},
name = 'api_key'
public addBasicAuth(
options: Partial<Omit<HttpSchemeObject, 'type'>> = {},
name = 'basic'
Comment on lines +141 to +142
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the change I mentioned above here so it would be

Suggested change
options: Partial<Omit<HttpSchemeObject, 'type'>> = {},
name = 'basic'
name = 'basic',
options: Partial<Omit<HttpSchemeObject, 'type'>> = {}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmcdo29, Yes, this can also be added, but is it better in a minor update?..

): 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<Omit<ApiKeySchemeObject, 'type'>> = {},
name = 'api_key'
): this {
this.addSecurity(name, {
type: 'http',
scheme: 'basic',
type: 'apiKey',
in: 'header',
name,
...options
});
return this;
}

public addCookieAuth(
cookieName = 'connect.sid',
options: SecuritySchemeObject = {
type: 'apiKey'
},
options: Partial<Omit<ApiKeySchemeObject, 'type'>> = {},
securityName = 'cookie'
): this {
this.addSecurity(securityName, {
Expand Down
35 changes: 28 additions & 7 deletions lib/interfaces/open-api-spec.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down