-
Notifications
You must be signed in to change notification settings - Fork 12
/
operation-type-coder.js
117 lines (97 loc) · 3.51 KB
/
operation-type-coder.js
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
import nodePath from "node:path";
import { Coder } from "./coder.js";
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
import { ParametersTypeCoder } from "./parameters-type-coder.js";
import { ResponseTypeCoder } from "./response-type-coder.js";
import { SchemaTypeCoder } from "./schema-type-coder.js";
export class OperationTypeCoder extends Coder {
names() {
return super.names(
`HTTP_${this.requirement.url.split("/").at(-1).toUpperCase()}`,
);
}
responseTypes(script) {
return this.requirement
.get("responses")
.flatMap((response, responseCode) => {
const status =
responseCode === "default"
? "number | undefined"
: Number.parseInt(responseCode, 10);
if (response.has("content")) {
return response.get("content").map(
(content, contentType) => `{
status: ${status},
contentType?: "${contentType}",
body?: ${new SchemaTypeCoder(content.get("schema")).write(script)}
}`,
);
}
if (response.has("schema")) {
const produces =
this.requirement?.get("produces")?.data ??
this.requirement.specification.rootRequirement.get("produces").data;
return produces
.map(
(contentType) => `{
status: ${status},
contentType?: "${contentType}",
body?: ${new SchemaTypeCoder(response.get("schema")).write(script)}
}`,
)
.join(" | ");
}
return `{
status: ${status}
}`;
})
.join(" | ");
}
modulePath() {
const pathString = this.requirement.url
.split("/")
.at(-2)
.replaceAll("~1", "/");
return `${nodePath
.join("path-types", pathString)
.replaceAll("\\", "/")}.types.ts`;
}
write(script) {
const contextTypeImportName = script.importExternalType(
"Context",
CONTEXT_FILE_TOKEN,
);
const parameters = this.requirement.get("parameters");
const queryType =
parameters === undefined
? "never"
: new ParametersTypeCoder(parameters, "query").write(script);
const pathType =
parameters === undefined
? "never"
: new ParametersTypeCoder(parameters, "path").write(script);
const headerType =
parameters === undefined
? "never"
: new ParametersTypeCoder(parameters, "header").write(script);
const bodyRequirement = this.requirement.get("consumes")
? parameters
.find((parameter) =>
["body", "formData"].includes(parameter.get("in").data),
)
.get("schema")
: this.requirement.select("requestBody/content/application~1json/schema");
const bodyType = bodyRequirement
? new SchemaTypeCoder(bodyRequirement).write(script)
: "undefined";
const responseType = new ResponseTypeCoder(
this.requirement.get("responses"),
this.requirement.get("produces")?.data ??
this.requirement.specification?.rootRequirement?.get("produces")?.data,
).write(script);
const proxyType = "(url: string) => { proxyUrl: string }";
return `({ query, path, header, body, context, proxy }: { query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, proxy: ${proxyType} }) => ${this.responseTypes(
script,
)} | { status: 415, contentType: "text/plain", body: string } | { }`;
}
}