Skip to content

Commit defd523

Browse files
committed
create a decorator
1 parent 7dc4f2f commit defd523

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const OpenAPIDecorator = require("./src/OpenAPIDecorator");
2+
3+
module.exports = OpenAPIDecorator;

src/DocumentationParts.js

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"use strict";
2+
3+
class DocumentationParts {
4+
constructor(openAPI) {
5+
this.openAPI = openAPI;
6+
}
7+
8+
parse() {
9+
let parts = [];
10+
const apiPart = this.__createAPIPart();
11+
parts.push(apiPart);
12+
13+
const resourceParts = this.__createPathParts();
14+
parts = parts.concat(resourceParts);
15+
// parts = [...resourceParts, ...apiPart];
16+
return parts;
17+
}
18+
19+
__createAPIPart() {
20+
const info = this.openAPI.info;
21+
const part = {
22+
location: {
23+
type: "API",
24+
},
25+
properties: {
26+
...info,
27+
},
28+
};
29+
return part;
30+
}
31+
32+
__createPathParts() {
33+
let parts = [];
34+
35+
for (const [pathName, pathValue] of Object.entries(this.openAPI.paths)) {
36+
this.pathName = pathName;
37+
this.pathValue = pathValue;
38+
const pathPart = this.__createRESOURCEPart();
39+
parts.push(pathPart);
40+
41+
const methodParts = this.__createMETHODPart();
42+
parts = parts.concat(methodParts);
43+
44+
const paramParts = this.__createPARAMParts();
45+
parts = parts.concat(paramParts);
46+
47+
const bodyParts = this.__createREQUESTBODYParts();
48+
parts = parts.concat(bodyParts);
49+
50+
const responseParts = this.__createRESPONSEParts();
51+
parts = parts.concat(responseParts);
52+
}
53+
54+
return parts;
55+
}
56+
57+
__createRESOURCEPart() {
58+
return {
59+
location: { type: "RESOURCE", path: this.pathName },
60+
properties: {},
61+
};
62+
}
63+
64+
__createMETHODPart() {
65+
const parts = [];
66+
for (const method of Object.keys(this.pathValue)) {
67+
const description = this.pathValue[method]?.description;
68+
const summary = this.pathValue[method]?.summary;
69+
parts.push({
70+
location: { type: "METHOD", path: this.pathName, method: method },
71+
properties: {
72+
...(description && { description: description }),
73+
...(summary && { summary: summary }),
74+
},
75+
});
76+
}
77+
78+
return parts;
79+
}
80+
81+
__createPARAMParts() {
82+
const parts = [];
83+
for (const method of Object.keys(this.pathValue)) {
84+
for (const param of this.pathValue[method]?.parameters || []) {
85+
const description = param?.description;
86+
const part = {
87+
location: {
88+
path: this.pathName,
89+
name: param.name,
90+
method: method,
91+
},
92+
properties: {
93+
...(description && { description: description }),
94+
},
95+
};
96+
switch (param.in) {
97+
case "path":
98+
part.location.type = "PATH_PARAMETER";
99+
break;
100+
case "query":
101+
part.location.type = "QUERY_PARAMETER";
102+
break;
103+
case "header":
104+
part.location.type = "REQUEST_HEADER";
105+
break;
106+
}
107+
108+
parts.push(part);
109+
}
110+
}
111+
112+
return parts;
113+
}
114+
115+
__createREQUESTBODYParts() {
116+
const parts = [];
117+
for (const method of Object.keys(this.pathValue)) {
118+
if (this.pathValue[method]?.requestBody) {
119+
const description = this.pathValue[method]?.requestBody?.description;
120+
const part = {
121+
location: {
122+
type: "REQUEST_BODY",
123+
path: this.pathName,
124+
method: method,
125+
},
126+
properties: {
127+
...(description && { description: description }),
128+
},
129+
};
130+
parts.push(part);
131+
}
132+
}
133+
134+
return parts;
135+
}
136+
137+
__createRESPONSEParts() {
138+
const parts = [];
139+
140+
for (const method of Object.keys(this.pathValue)) {
141+
const responses = this.pathValue[method]?.responses;
142+
143+
for (const [statusCode, response] of Object.entries(responses)) {
144+
const description = response?.description;
145+
const part = {
146+
location: {
147+
path: this.pathName,
148+
method: method,
149+
statusCode: statusCode,
150+
type: "RESPONSE",
151+
},
152+
properties: {
153+
...(description && { description: description }),
154+
},
155+
};
156+
157+
parts.push(part);
158+
}
159+
}
160+
161+
return parts;
162+
}
163+
}
164+
165+
module.exports = DocumentationParts;

src/OpenAPIDecorator.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const DocumentationParts = require("./DocumentationParts");
4+
5+
class OpenAPIDecorator {
6+
constructor(openAPI) {
7+
this.openAPI = openAPI;
8+
}
9+
10+
decorate(type = "documentationPart") {
11+
if (type === "documentationPart") {
12+
this.decorateDocumentationParts();
13+
}
14+
}
15+
16+
decorateDocumentationParts() {
17+
const obj = {
18+
"x-amazon-apigateway-documentation": {
19+
version: this.openAPI.info.version,
20+
documentationParts: [],
21+
},
22+
};
23+
24+
const documentationParts = new DocumentationParts(this.openAPI);
25+
const parts = documentationParts.parse();
26+
27+
obj["x-amazon-apigateway-documentation"].documentationParts = parts;
28+
29+
Object.assign(this.openAPI, obj);
30+
}
31+
}
32+
33+
module.exports = OpenAPIDecorator;

0 commit comments

Comments
 (0)