-
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathparameters.ts
77 lines (70 loc) · 2.6 KB
/
parameters.ts
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
import { ParameterObject, ReferenceObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformSchemaObj } from "./schema";
export function transformParametersArray(
parameters: (ReferenceObject | ParameterObject)[],
{
globalParameters,
immutableTypes,
version,
}: {
globalParameters?: Record<string, ParameterObject>;
immutableTypes: boolean;
version: number;
}
): string {
const readonly = tsReadonly(immutableTypes);
let output = "";
// sort into map
let mappedParams: Record<string, Record<string, ParameterObject>> = {};
parameters.forEach((paramObj: any) => {
if (paramObj.$ref && globalParameters) {
const paramName = paramObj.$ref.split("/").pop(); // take last segment
if (globalParameters[paramName]) {
const reference = globalParameters[paramName] as any;
if (!mappedParams[reference.in]) mappedParams[reference.in] = {};
if (version === 2) {
mappedParams[reference.in][reference.name || paramName] = {
...reference,
$ref: paramObj.$ref,
};
} else if (version === 3) {
mappedParams[reference.in][reference.name || paramName] = {
...reference,
schema: { $ref: paramObj.$ref },
};
}
}
return;
}
if (!paramObj.in || !paramObj.name) return;
if (!mappedParams[paramObj.in]) mappedParams[paramObj.in] = {};
mappedParams[paramObj.in][paramObj.name] = paramObj;
});
// transform output
Object.entries(mappedParams).forEach(([paramIn, paramGroup]) => {
output += ` ${readonly}${paramIn}: {\n`; // open in
Object.entries(paramGroup).forEach(([paramName, paramObj]) => {
let paramComment = "";
if (paramObj.deprecated) paramComment += `@deprecated `;
if (paramObj.description) paramComment += paramObj.description;
if (paramComment) output += comment(paramComment);
const required = paramObj.required ? `` : `?`;
let paramType = ``;
if (version === 2) {
if (paramObj.in === "body" && paramObj.schema) {
paramType = transformSchemaObj(paramObj.schema, { immutableTypes });
} else if (paramObj.type) {
paramType = transformSchemaObj(paramObj, { immutableTypes });
} else {
paramType = "unknown";
}
} else if (version === 3) {
paramType = paramObj.schema ? transformSchemaObj(paramObj.schema, { immutableTypes }) : "unknown";
}
output += ` ${readonly}"${paramName}"${required}: ${paramType};\n`;
});
output += ` }\n`; // close in
});
return output;
}