-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
141 lines (117 loc) · 3.14 KB
/
helpers.js
File metadata and controls
141 lines (117 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function getNonEmptyObject(obj) {
// Check if the object is not null or undefined
if (!obj) {
return null;
}
// Check if the object has at least one own property
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return obj; // Return the object as it is non-empty
}
}
return null; // Return null if the object is empty
}
function tryPaths(data, firstKey) {
const pathData = data.paths[firstKey];
if (!pathData) {
return data.paths["/"] ?? null;
}
return pathData;
}
function tryWithProperty(pathData, property) {
console.log("respi", property, pathData?.post?.[property]);
const methods = ["get", "post", "put", "patch", "delete"];
for (const method of methods) {
const value = pathData?.[method]?.[property];
if (value) {
return value;
}
}
return null;
}
function getMethod(data, firstKey) {
if (typeof data !== "object" || data === null) {
throw new Error("data is not an object");
}
if (typeof firstKey !== "string") {
throw new Error("Path must be a string");
}
if (!data?.paths) {
return data;
}
const pathData = tryPaths(data, firstKey);
if (!pathData) {
return null;
}
const key = Object.keys(pathData).find((key) => pathData[key]);
if (!key) {
return null;
}
return key;
}
function transformObject(obj) {
const responses = [];
for (const [httpCode, details] of Object.entries(obj)) {
let status, httpMessage;
const code = parseInt(httpCode, 10);
if (code >= 200 && code < 300) {
status = "ok";
httpMessage = "OK";
} else if (code >= 300 && code < 400) {
status = "redirect";
httpMessage = "Redirection";
} else if (code >= 400 && code < 500) {
status = "clientError";
httpMessage = "Client Error";
} else if (code >= 500 && code < 600) {
status = "serverError";
httpMessage = "Server Error";
} else {
status = "unknown";
httpMessage = "Unknown Status";
}
const transformedObj = {
status: status,
httpCode: code,
httpMessage: httpMessage,
statusDescription: details.description,
schema: details.schema,
};
responses.push(transformedObj);
}
return { responses };
}
function getParameters(data, firstKey) {
if (!data?.paths) {
return ""; // or a default value, or throw an error, as appropriate
}
const pathData = tryPaths(data, firstKey);
if (!pathData) {
return null; // or a default value, or throw an error
}
const parameters = tryWithProperty(pathData, "parameters");
if (!parameters) {
return null;
}
return parameters.length > 0 ? parameters[0] : null;
}
function getResponses(data, firstKey) {
const pathData = tryPaths(data, firstKey);
if (!pathData) {
console.error(data.paths);
return null; // or a default value, or throw an error, as appropriate
}
if (!pathData) {
return null; // or a default value, or throw an error
}
const responses = tryWithProperty(pathData, "responses");
if (!responses) {
return null;
}
return transformObject(getNonEmptyObject(responses)).responses;
}
module.exports = {
getMethod,
getParameters,
getResponses,
};