-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operation.ts
138 lines (122 loc) · 3.76 KB
/
Operation.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
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
import { RequestParameters } from "./RequestParameters.js";
import { Path } from "../Path.js";
import { Name } from "../../global/Name.js";
import { Responses } from "./responses/Responses.js";
import { Tag } from "../../tags/Tag.js";
import { TypeCompilationOptions } from "../../CodeGenerationModel.js";
import { OpenAPIV3 } from "openapi-types";
export class Operation {
public readonly path: Path;
public readonly id: Name;
public readonly httpMethod: Name;
public readonly parameters: RequestParameters;
public readonly responses: Responses;
public readonly summary?: string;
public readonly tags: Tag[];
private constructor(
path: Path,
httpMethod: Name,
doc: OpenAPIV3.OperationObject,
) {
const fallbackId = `${httpMethod.raw}-${path.name.raw}`;
this.id = new Name(doc.operationId ?? fallbackId);
this.path = path;
this.httpMethod = httpMethod;
this.parameters = RequestParameters.fromDoc(this, doc);
this.responses = new Responses(this, doc.responses);
this.summary = doc.summary;
this.tags =
doc.tags
?.map((name) => path.paths.model.tags.find((t) => t.name.raw === name))
.filter((t): t is Tag => !!t) ?? [];
}
public static fromDoc(
path: Path,
httpMethod: string,
doc: OpenAPIV3.OperationObject,
): Operation {
return new Operation(path, new Name(httpMethod, path.name), doc);
}
public async compileTypes(options: TypeCompilationOptions): Promise<string> {
const t = {
ns: this.httpMethod.tsType,
parameters: await this.parameters.compileTypes(options),
responses: await this.responses.compileTypes(options),
};
return `\
namespace ${t.ns} {
${t.parameters};
${t.responses};
}
`;
}
public compileDescriptor(): string {
const t = {
constName: this.id.tsVar,
requestType: this.parameters.compileDescriptorRequestType(),
responseTypes: this.responses.compileDescriptorTypes(),
path: this.path.name.raw,
method: this.httpMethod.raw.toUpperCase(),
operationId: this.id.raw,
};
return `\
/** ${this.summary} */
export const ${t.constName}: OpenAPIOperation<
${t.requestType},
${t.responseTypes}
> = {
path: "${t.path}",
method: "${t.method}",
operationId: "${t.operationId}",
};
`;
}
public compileRequestResponseTypes(): string {
const t = {
ns: this.id.tsType,
descriptor: this.id.tsVar,
};
return `\
namespace ${t.ns} {
type RequestData = InferredRequestData<typeof descriptors.${t.descriptor}>;
type ResponseData<TStatus extends HttpStatus = 200> = InferredResponseData<typeof descriptors.${t.descriptor}, TStatus>;
}
`;
}
private getMethodName(tag: Tag): string {
const methodName = this.id.tsVar;
return new Name(
methodName.startsWith(tag.name.tsVar)
? methodName.substring(tag.name.tsVar.length)
: methodName,
).tsVar;
}
public compileClientMethod(tag: Tag): string {
const methodName = this.getMethodName(tag);
const t = {
methodName,
descriptorName: this.id.tsVar,
};
return `\
/** ${this.summary} */
${t.methodName}: this.requestFunctionFactory(
descriptors.${t.descriptorName}
)
`;
}
public compileReactClientMethod(tag: Tag): string {
const methodName = this.getMethodName(tag);
const t = {
methodName,
descriptorName: this.id.tsVar,
tag: tag.name.tsVar,
};
return `\
/** ${this.summary} */
${t.methodName}: new ApiCallAsyncResourceFactory(
descriptors.${t.descriptorName},
baseClient.${t.tag}.${t.methodName}
).getApiResource
`;
}
}