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

Commit 0077f80

Browse files
author
dnphu
committed
[Core] Improve Interface for swagger documentation
1 parent e34698b commit 0077f80

File tree

3 files changed

+161
-15
lines changed

3 files changed

+161
-15
lines changed

src/api/core/document.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { OPEN_API_VERSION } from 'src/constants';
22
import { Validate } from 'src/utils/validate';
3-
import { InfoDoc, SwaggerDoc, TagDoc } from './interfaces/document.interface';
3+
import {
4+
ExternalDoc,
5+
InfoDoc,
6+
ServerDoc,
7+
SwaggerDoc,
8+
} from './interfaces/document.interface';
49

510
export class DocumentBuilder {
611
private document: SwaggerDoc = { openapi: OPEN_API_VERSION };
712

8-
public addTags(tags: TagDoc[]): DocumentBuilder {
9-
tags.forEach((tag: TagDoc) => {
10-
Validate.assertNotEmpty(
11-
tag.name,
12-
'Missing name when define tag in swagger',
13-
);
14-
Validate.assertString(tag.name, `Tag: ${tag.name} should be a string`);
15-
});
13+
public setExternalDocs(externalDoc: ExternalDoc): DocumentBuilder {
14+
this.document.externalDocs = externalDoc;
15+
return this;
16+
}
1617

17-
this.document.tags = tags;
18+
public setServers(...servers: ServerDoc[]) {
19+
this.document.servers = servers;
1820
return this;
1921
}
2022

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { SchemaDoc } from './schema-doc';
2+
13
export interface SwaggerDoc {
24
readonly openapi: string;
35
info?: InfoDoc;
46
externalDocs?: ExternalDoc;
57
servers?: ServerDoc[];
68
tags?: TagDoc[];
7-
paths?: any;
9+
paths?: PathDoc;
810
components?: ComponentDoc;
911
}
1012

@@ -52,11 +54,40 @@ export interface ExternalDoc {
5254
url?: string;
5355
}
5456

57+
// Component document
5558
export interface ComponentDoc {
56-
schemas: any;
57-
securitySchemes: {
58-
[authKey: string]: SecurityDoc | BearerAuthDoc | ApiKeyDoc;
59-
};
59+
schemas: Record<string, SchemaDoc> | RefDoc;
60+
securitySchemes:
61+
| Record<string, SecurityDoc | BearerAuthDoc | ApiKeyDoc>
62+
| RefDoc;
63+
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;
70+
}
71+
72+
export interface RefDoc {
73+
$ref: string;
74+
}
75+
76+
export interface ResponseDoc {
77+
description?: string;
78+
headers?: Record<string, any> | RefDoc;
79+
content?: Record<string, any>;
80+
links?: Record<string, any> | RefDoc;
81+
}
82+
83+
export type PathLocation = 'query' | 'header' | 'path' | 'cookie';
84+
export interface ParameterDoc {
85+
name: string;
86+
in: PathLocation;
87+
description?: string;
88+
required?: boolean;
89+
deprecated?: string;
90+
allowEmptyValue?: boolean;
6091
}
6192

6293
/**
@@ -98,3 +129,24 @@ export type ApiKeyDoc = {
98129
name: 'api_key';
99130
in: 'header';
100131
};
132+
133+
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
134+
135+
export interface PathDoc {
136+
[path: string]: HttpMethod;
137+
}
138+
139+
export interface OperationDoc {
140+
tags: string[];
141+
summary: string;
142+
description: string;
143+
externalDocs: ExternalDoc;
144+
operationId: string;
145+
parameters: ParameterDoc[];
146+
requestBody: any;
147+
responses: Record<string, ResponseDoc>;
148+
callbacks: any;
149+
deprecated: boolean;
150+
security: SecurityDoc[];
151+
servers: ServerDoc;
152+
}

src/api/core/interfaces/schema-doc.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { ExternalDoc } from './document.interface';
2+
3+
export interface SchemaRootDoc {
4+
type: string;
5+
properties?: Record<string, SchemaRootDoc>;
6+
required?: string[];
7+
allOf?: SchemaRootDoc;
8+
oneOf?: SchemaRootDoc;
9+
anyOf?: SchemaRootDoc;
10+
not?: SchemaRootDoc;
11+
items?: Record<string, any>; // Must contains when type is array
12+
additionalProperties?: boolean | SchemaRootDoc;
13+
description?: string;
14+
format?: string;
15+
default?: any;
16+
nullable: boolean;
17+
discriminator?: any;
18+
readOnly: boolean;
19+
writeOnly: boolean;
20+
xml?: any;
21+
externalDocs: ExternalDoc;
22+
example?: any;
23+
deprecated: boolean;
24+
}
25+
26+
export type IntType = {
27+
type: 'interger';
28+
format: 'int32';
29+
};
30+
31+
export type LongType = {
32+
type: 'interger';
33+
format: 'int64';
34+
};
35+
36+
export type FloatType = {
37+
type: 'number';
38+
format: 'float';
39+
};
40+
41+
export type DoubleType = {
42+
type: 'number';
43+
format: 'double';
44+
};
45+
46+
export type StringType = {
47+
type: 'string';
48+
};
49+
50+
export type ByteType = {
51+
type: 'string';
52+
format: 'byte';
53+
};
54+
55+
export type BinaryType = {
56+
type: 'string';
57+
format: 'binary';
58+
};
59+
60+
export type BooleanType = {
61+
type: 'boolean';
62+
};
63+
64+
export type DateType = {
65+
type: 'string';
66+
format: 'date';
67+
};
68+
69+
export type DateTimeType = {
70+
type: 'string';
71+
format: 'date-time';
72+
};
73+
74+
export type PasswordType = {
75+
type: 'string';
76+
format: 'password';
77+
};
78+
79+
export type DataTypes =
80+
| IntType
81+
| LongType
82+
| FloatType
83+
| DoubleType
84+
| StringType
85+
| ByteType
86+
| BinaryType
87+
| BooleanType
88+
| DateType
89+
| DateTimeType
90+
| PasswordType;
91+
92+
export type SchemaDoc = DataTypes & SchemaRootDoc;

0 commit comments

Comments
 (0)