Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit 465fcdb

Browse files
author
dnphu
committed
[Core]: Provide auth in document
1 parent 0077f80 commit 465fcdb

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

src/api/core/document.ts

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import { OPEN_API_VERSION } from 'src/constants';
1+
import { OPEN_API_VERSION, SECURITY_KEY } from 'src/constants';
22
import { Validate } from 'src/utils/validate';
33
import {
4+
ApiKeyDoc,
5+
BearerAuthDoc,
46
ExternalDoc,
57
InfoDoc,
8+
OAth2Doc,
69
ServerDoc,
710
SwaggerDoc,
811
} from './interfaces/document.interface';
912

1013
export class DocumentBuilder {
11-
private document: SwaggerDoc = { openapi: OPEN_API_VERSION };
14+
private document: SwaggerDoc = {
15+
openapi: OPEN_API_VERSION,
16+
components: {
17+
schemas: {},
18+
securitySchemes: {},
19+
responses: {},
20+
},
21+
paths: {},
22+
};
1223

1324
public setExternalDocs(externalDoc: ExternalDoc): DocumentBuilder {
1425
this.document.externalDocs = externalDoc;
1526
return this;
1627
}
1728

1829
public setServers(...servers: ServerDoc[]) {
30+
servers.forEach((server: ServerDoc) => {
31+
Validate.assertString(server.url, 'url must be declared in server');
32+
});
33+
1934
this.document.servers = servers;
2035
return this;
2136
}
@@ -33,6 +48,30 @@ export class DocumentBuilder {
3348
return this;
3449
}
3550

51+
public setBearerAuth() {
52+
this.document.components.securitySchemes[SECURITY_KEY.BEARER_KEY] = {
53+
type: 'http',
54+
bearerFormat: 'JWT',
55+
scheme: 'bearer',
56+
} as BearerAuthDoc;
57+
58+
return DocumentBuilder;
59+
}
60+
61+
public setApiKey() {
62+
this.document.components.securitySchemes[SECURITY_KEY.API_KEY] = {
63+
type: 'apiKey',
64+
name: 'api_key',
65+
in: 'header',
66+
} as ApiKeyDoc;
67+
return DocumentBuilder;
68+
}
69+
70+
public setOauth2(key: string, oauth2Flow: OAth2Doc) {
71+
this.document.components.securitySchemes[key] = oauth2Flow;
72+
return DocumentBuilder;
73+
}
74+
3675
public build(): SwaggerDoc {
3776
return this.document;
3877
}
File renamed without changes.

src/api/core/interfaces/document.interface.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ export interface ExternalDoc {
5858
export interface ComponentDoc {
5959
schemas: Record<string, SchemaDoc> | RefDoc;
6060
securitySchemes:
61-
| Record<string, SecurityDoc | BearerAuthDoc | ApiKeyDoc>
61+
| Record<string, SecurityDoc | BearerAuthDoc | ApiKeyDoc | OAth2Doc>
6262
| RefDoc;
6363
responses: Record<string, ResponseDoc> | RefDoc;
64-
parameters: Record<string, ParameterDoc> | RefDoc;
65-
examples: any | RefDoc;
66-
requestBodies: any | RefDoc;
67-
headers: any | RefDoc;
68-
links: any | RefDoc;
69-
callbacks: any | RefDoc;
64+
parameters?: Record<string, ParameterDoc> | RefDoc;
65+
examples?: any | RefDoc;
66+
requestBodies?: any | RefDoc;
67+
headers?: any | RefDoc;
68+
links?: any | RefDoc;
69+
callbacks?: any | RefDoc;
7070
}
7171

7272
export interface RefDoc {
@@ -130,6 +130,11 @@ export type ApiKeyDoc = {
130130
in: 'header';
131131
};
132132

133+
export type OAth2Doc = {
134+
type: 'oauth2';
135+
flows: OAuthFlows;
136+
};
137+
133138
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
134139

135140
export interface PathDoc {

src/api/core/model.ts

Whitespace-only changes.

src/constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export const OPEN_API_VERSION = '3.0.1';
2+
export enum SECURITY_KEY {
3+
BEARER_KEY = 'Bearer',
4+
API_KEY = 'ApiKey',
5+
}

0 commit comments

Comments
 (0)