@goast/typescript 0.6.0-beta.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/esm/src/generators/models/model-generator.d.ts.map +1 -1
- package/esm/src/generators/models/model-generator.js +2 -2
- package/esm/src/generators/services/angular-services/angular-service-generator.d.ts.map +1 -1
- package/esm/src/generators/services/angular-services/angular-service-generator.js +7 -2
- package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.d.ts.map +1 -1
- package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.js +7 -2
- package/esm/src/generators/services/fetch-clients/fetch-client-generator.d.ts.map +1 -1
- package/esm/src/generators/services/fetch-clients/fetch-client-generator.js +7 -2
- package/esm/src/generators/services/k6-clients/k6-client-generator.d.ts.map +1 -1
- package/esm/src/generators/services/k6-clients/k6-client-generator.js +7 -2
- package/package.json +2 -2
- package/script/src/generators/models/model-generator.d.ts.map +1 -1
- package/script/src/generators/models/model-generator.js +1 -1
- package/script/src/generators/services/angular-services/angular-service-generator.d.ts.map +1 -1
- package/script/src/generators/services/angular-services/angular-service-generator.js +6 -1
- package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.d.ts.map +1 -1
- package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.js +6 -1
- package/script/src/generators/services/fetch-clients/fetch-client-generator.d.ts.map +1 -1
- package/script/src/generators/services/fetch-clients/fetch-client-generator.js +6 -1
- package/script/src/generators/services/k6-clients/k6-client-generator.d.ts.map +1 -1
- package/script/src/generators/services/k6-clients/k6-client-generator.js +6 -1
- package/src/mod.ts +7 -0
- package/src/src/assets.ts +9 -0
- package/src/src/ast/_index.ts +54 -0
- package/src/src/ast/common.ts +1 -0
- package/src/src/ast/index.ts +1 -0
- package/src/src/ast/node.ts +10 -0
- package/src/src/ast/nodes/argument.ts +40 -0
- package/src/src/ast/nodes/array-type.ts +49 -0
- package/src/src/ast/nodes/arrow-function.ts +91 -0
- package/src/src/ast/nodes/call.ts +55 -0
- package/src/src/ast/nodes/class.ts +133 -0
- package/src/src/ast/nodes/constructor-parameter.ts +70 -0
- package/src/src/ast/nodes/constructor.ts +77 -0
- package/src/src/ast/nodes/decorator.ts +76 -0
- package/src/src/ast/nodes/doc-tag.ts +291 -0
- package/src/src/ast/nodes/doc.ts +102 -0
- package/src/src/ast/nodes/enum-value.ts +72 -0
- package/src/src/ast/nodes/enum.ts +77 -0
- package/src/src/ast/nodes/export.ts +54 -0
- package/src/src/ast/nodes/function-type.ts +60 -0
- package/src/src/ast/nodes/function.ts +107 -0
- package/src/src/ast/nodes/generic-parameter.ts +71 -0
- package/src/src/ast/nodes/indexer.ts +60 -0
- package/src/src/ast/nodes/interface.ts +117 -0
- package/src/src/ast/nodes/intersection-type.ts +67 -0
- package/src/src/ast/nodes/lookup-type.ts +53 -0
- package/src/src/ast/nodes/method.ts +138 -0
- package/src/src/ast/nodes/object-type.ts +52 -0
- package/src/src/ast/nodes/object.ts +48 -0
- package/src/src/ast/nodes/parameter.ts +85 -0
- package/src/src/ast/nodes/property-accessor.ts +173 -0
- package/src/src/ast/nodes/property.ts +192 -0
- package/src/src/ast/nodes/reference.ts +194 -0
- package/src/src/ast/nodes/string.ts +49 -0
- package/src/src/ast/nodes/tuple.ts +53 -0
- package/src/src/ast/nodes/type-alias.ts +75 -0
- package/src/src/ast/nodes/typeof.ts +42 -0
- package/src/src/ast/nodes/types.ts +40 -0
- package/src/src/ast/nodes/union-type.ts +62 -0
- package/src/src/ast/nodes/variable.ts +73 -0
- package/src/src/ast/references/angular.ts +24 -0
- package/src/src/ast/references/easy-network-stub.ts +21 -0
- package/src/src/ast/references/index.ts +5 -0
- package/src/src/ast/references/k6.ts +8 -0
- package/src/src/ast/references/rxjs.ts +7 -0
- package/src/src/ast/references/typescript.ts +35 -0
- package/src/src/ast/utils/get-type-script-builder-options.ts +19 -0
- package/src/src/ast/utils/to-ts-node.ts +27 -0
- package/src/src/ast/utils/write-ts-generic-parameters.ts +25 -0
- package/src/src/ast/utils/write-ts-members.ts +28 -0
- package/src/src/ast/utils/write-ts-nodes.ts +35 -0
- package/src/src/ast/utils/write-ts-parameters.ts +25 -0
- package/src/src/common-results.ts +26 -0
- package/src/src/config.ts +89 -0
- package/src/src/file-builder.ts +137 -0
- package/src/src/generators/file-generator.ts +15 -0
- package/src/src/generators/index.ts +6 -0
- package/src/src/generators/models/index.ts +3 -0
- package/src/src/generators/models/model-generator.ts +476 -0
- package/src/src/generators/models/models-generator.ts +108 -0
- package/src/src/generators/models/models.ts +121 -0
- package/src/src/generators/services/angular-services/angular-service-generator.ts +414 -0
- package/src/src/generators/services/angular-services/angular-services-generator.ts +295 -0
- package/src/src/generators/services/angular-services/index.ts +3 -0
- package/src/src/generators/services/angular-services/models.ts +277 -0
- package/src/src/generators/services/angular-services/refs.ts +54 -0
- package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +250 -0
- package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +211 -0
- package/src/src/generators/services/easy-network-stub/index.ts +3 -0
- package/src/src/generators/services/easy-network-stub/models.ts +124 -0
- package/src/src/generators/services/easy-network-stub/refs.ts +45 -0
- package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +285 -0
- package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +163 -0
- package/src/src/generators/services/fetch-clients/index.ts +3 -0
- package/src/src/generators/services/fetch-clients/models.ts +130 -0
- package/src/src/generators/services/fetch-clients/refs.ts +21 -0
- package/src/src/generators/services/k6-clients/index.ts +3 -0
- package/src/src/generators/services/k6-clients/k6-client-generator.ts +474 -0
- package/src/src/generators/services/k6-clients/k6-clients-generator.ts +171 -0
- package/src/src/generators/services/k6-clients/models.ts +247 -0
- package/src/src/generators/services/k6-clients/refs.ts +16 -0
- package/src/src/import-collection.ts +236 -0
- package/src/src/utils.ts +72 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ApiEndpoint,
|
|
5
|
+
type ApiResponse,
|
|
6
|
+
type ApiSchema,
|
|
7
|
+
type AppendValueGroup,
|
|
8
|
+
appendValueGroup,
|
|
9
|
+
builderTemplate as s,
|
|
10
|
+
getSourceDisplayName,
|
|
11
|
+
type MaybePromise,
|
|
12
|
+
type Nullable,
|
|
13
|
+
toCasing,
|
|
14
|
+
} from '@goast/core';
|
|
15
|
+
|
|
16
|
+
import { ts } from '../../../ast/index.js';
|
|
17
|
+
import type { TypeScriptExportOutput } from '../../../common-results.js';
|
|
18
|
+
import { TypeScriptFileBuilder } from '../../../file-builder.js';
|
|
19
|
+
import { TypeScriptFileGenerator } from '../../file-generator.js';
|
|
20
|
+
import type { TypeScriptAngularServiceGeneratorContext, TypeScriptAngularServiceGeneratorOutput } from './models.js';
|
|
21
|
+
|
|
22
|
+
type Context = TypeScriptAngularServiceGeneratorContext;
|
|
23
|
+
type Output = TypeScriptAngularServiceGeneratorOutput;
|
|
24
|
+
type Builder = TypeScriptFileBuilder;
|
|
25
|
+
|
|
26
|
+
export interface TypeScriptAngularServiceGenerator<TOutput extends Output = Output> {
|
|
27
|
+
generate(ctx: Context): MaybePromise<TOutput>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGenerator<Context, Output>
|
|
31
|
+
implements TypeScriptAngularServiceGenerator {
|
|
32
|
+
public generate(ctx: Context): MaybePromise<Output> {
|
|
33
|
+
const responseModels = this.generateResponseModels(ctx);
|
|
34
|
+
|
|
35
|
+
const filePath = this.getServiceFilePath(ctx);
|
|
36
|
+
const name = this.getServiceClassName(ctx);
|
|
37
|
+
|
|
38
|
+
TypeScriptFileBuilder.generate({
|
|
39
|
+
logName: `service ${name}`,
|
|
40
|
+
filePath,
|
|
41
|
+
options: ctx.config,
|
|
42
|
+
generator: (b) => {
|
|
43
|
+
ctx.service.endpoints.forEach((endpoint) => {
|
|
44
|
+
console.log(
|
|
45
|
+
` ${getSourceDisplayName(ctx.data, endpoint)} [${toCasing(endpoint.name, ctx.config.functionNameCasing)}]`,
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
b.append(this.getServiceFileContent(ctx));
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
filePath,
|
|
54
|
+
component: name,
|
|
55
|
+
responseModels,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// #region Response Model
|
|
60
|
+
protected generateResponseModels(ctx: Context): Record<string, TypeScriptExportOutput> {
|
|
61
|
+
const result: Record<string, TypeScriptExportOutput> = {};
|
|
62
|
+
const filePath = this.getResponseModelFilePath(ctx);
|
|
63
|
+
const builder = new TypeScriptFileBuilder(filePath, ctx.config);
|
|
64
|
+
|
|
65
|
+
for (const endpoint of ctx.service.endpoints) {
|
|
66
|
+
const name = this.getResponseModelName(ctx, endpoint);
|
|
67
|
+
builder.appendLine(this.getResponseModelFileContent(ctx, endpoint));
|
|
68
|
+
result[endpoint.id] = { component: name, filePath };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
builder.writeToFile();
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected getResponseModelFileContent(ctx: Context, endpoint: ApiEndpoint): AppendValueGroup<Builder> {
|
|
76
|
+
const result = appendValueGroup<Builder>([]);
|
|
77
|
+
const statusCodes = this.getEndpointStatusCodes(ctx, endpoint);
|
|
78
|
+
let statusCodeType: ts.Reference<Builder>;
|
|
79
|
+
|
|
80
|
+
const statusCodesTypeName = toCasing(`${endpoint.name}_StatusCodes`, ctx.config.typeNameCasing);
|
|
81
|
+
result.values.push(ts.typeAlias(statusCodesTypeName, ts.unionType(Object.keys(statusCodes))));
|
|
82
|
+
if (ctx.config.strictResponseTypes) {
|
|
83
|
+
statusCodeType = ts.reference(statusCodesTypeName);
|
|
84
|
+
} else {
|
|
85
|
+
statusCodeType = ctx.refs.httpStatusCode();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
result.values.push(
|
|
89
|
+
ts.typeAlias(
|
|
90
|
+
this.getResponseModelName(ctx, endpoint),
|
|
91
|
+
ts.intersectionType([
|
|
92
|
+
ts.unionType([
|
|
93
|
+
...Object.entries(statusCodes).map(([key, value]) => {
|
|
94
|
+
const isSuccess = key.startsWith('2');
|
|
95
|
+
return ts.intersectionType<Builder>([
|
|
96
|
+
isSuccess
|
|
97
|
+
? ts.refs.angular.httpResponse([value.type])
|
|
98
|
+
: ts.refs.omit([ts.refs.angular.httpErrorResponse(), ts.string('error')]),
|
|
99
|
+
ts.objectType({
|
|
100
|
+
members: [
|
|
101
|
+
isSuccess ? null : ts.property('error', { type: ts.unionType([value.type, ts.refs.null_()]) }),
|
|
102
|
+
ts.property('status', { type: key }),
|
|
103
|
+
ts.property('ok', { type: isSuccess ? 'true' : 'false' }),
|
|
104
|
+
],
|
|
105
|
+
}),
|
|
106
|
+
]);
|
|
107
|
+
}),
|
|
108
|
+
!ctx.config.strictResponseTypes
|
|
109
|
+
? ts.intersectionType([
|
|
110
|
+
ts.refs.angular.httpResponse([this.getAnyType(ctx)]),
|
|
111
|
+
ts.objectType({
|
|
112
|
+
members: [
|
|
113
|
+
ts.property('status', {
|
|
114
|
+
type: ts.refs.exclude([ctx.refs.httpStatusCode(), ts.reference(statusCodesTypeName)]),
|
|
115
|
+
}),
|
|
116
|
+
],
|
|
117
|
+
}),
|
|
118
|
+
])
|
|
119
|
+
: null,
|
|
120
|
+
]),
|
|
121
|
+
ts.objectType({ members: [ts.property('status', { type: 'TStatus' })] }),
|
|
122
|
+
]),
|
|
123
|
+
{
|
|
124
|
+
doc: ts.doc({
|
|
125
|
+
description: `Response model for operation ${this.getEndpointMethodName(ctx, endpoint)}`,
|
|
126
|
+
tags: [endpoint.deprecated ? ts.docTag('deprecated') : null],
|
|
127
|
+
}),
|
|
128
|
+
export: true,
|
|
129
|
+
generics: [
|
|
130
|
+
ts.genericParameter('TStatus', {
|
|
131
|
+
constraint: statusCodeType,
|
|
132
|
+
default: statusCodeType,
|
|
133
|
+
}),
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
protected getEndpointStatusCodes(
|
|
143
|
+
ctx: Context,
|
|
144
|
+
endpoint: ApiEndpoint,
|
|
145
|
+
): Record<number, { parser: 'text' | 'json'; type: ts.Type<Builder> }> {
|
|
146
|
+
return Object.fromEntries([
|
|
147
|
+
...Object.entries(ctx.config.defaultStatusCodeResponseTypes).map(([key, value]) => [
|
|
148
|
+
key,
|
|
149
|
+
{
|
|
150
|
+
parser: value?.parser ?? 'text',
|
|
151
|
+
type: typeof value?.type === 'function'
|
|
152
|
+
? this.getSchemaType(ctx, value.type(ctx.data.schemas))
|
|
153
|
+
: value ?? this.getAnyType(ctx),
|
|
154
|
+
},
|
|
155
|
+
]),
|
|
156
|
+
...endpoint.responses
|
|
157
|
+
.filter((x) => x.statusCode)
|
|
158
|
+
.map((x) => [
|
|
159
|
+
x.statusCode,
|
|
160
|
+
{
|
|
161
|
+
parser: this.contentTypeToResponseType(x.contentOptions[0]?.type),
|
|
162
|
+
type: this.getSchemaType(ctx, x.contentOptions[0]?.schema, this.getAnyType(ctx)),
|
|
163
|
+
},
|
|
164
|
+
]),
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
// #endregion
|
|
168
|
+
|
|
169
|
+
protected getServiceFileContent(ctx: Context): AppendValueGroup<Builder> {
|
|
170
|
+
const code = appendValueGroup<Builder>([], '\n');
|
|
171
|
+
for (const endpoint of ctx.service.endpoints) {
|
|
172
|
+
if (endpoint.parameters.length > 0 || endpoint.requestBody !== undefined) {
|
|
173
|
+
code.values.push(this.getEndpointParamsType(ctx, endpoint));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
code.values.push(this.getClass(ctx));
|
|
177
|
+
return code;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
protected getEndpointParamsType(ctx: Context, endpoint: ApiEndpoint): ts.TypeAlias<Builder> {
|
|
181
|
+
const type = ts.objectType<Builder>();
|
|
182
|
+
|
|
183
|
+
for (const parameter of endpoint.parameters) {
|
|
184
|
+
const schema = parameter.schema;
|
|
185
|
+
type.members.push(
|
|
186
|
+
ts.property(toCasing(parameter.name, ctx.config.propertyNameCasing), {
|
|
187
|
+
doc: ts.doc({
|
|
188
|
+
description: parameter.description,
|
|
189
|
+
tags: [parameter.deprecated ? ts.docTag('deprecated') : null],
|
|
190
|
+
}),
|
|
191
|
+
type: schema ? (b) => b.appendModelUsage(ctx.input.typescript.models[schema.id]) : this.getAnyType(ctx),
|
|
192
|
+
optional: !parameter.required,
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (endpoint.requestBody !== undefined) {
|
|
198
|
+
const body = endpoint.requestBody;
|
|
199
|
+
const schema = body.content[0].schema;
|
|
200
|
+
type.members.push(
|
|
201
|
+
ts.property('body', {
|
|
202
|
+
doc: ts.doc({ description: body.description }),
|
|
203
|
+
type: schema ? (b) => b.appendModelUsage(ctx.input.typescript.models[schema.id]) : this.getAnyType(ctx),
|
|
204
|
+
optional: !body.required,
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return ts.typeAlias(this.getEndpointParamsTypeName(ctx, endpoint), type, {
|
|
210
|
+
doc: ts.doc({ description: `Parameters for operation ${this.getEndpointMethodName(ctx, endpoint)}` }),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
protected getClass(ctx: Context): ts.Class<Builder> {
|
|
215
|
+
return ts.class<Builder>(this.getServiceClassName(ctx), {
|
|
216
|
+
doc: ts.doc({
|
|
217
|
+
description: ctx.service.description,
|
|
218
|
+
tags: [
|
|
219
|
+
ctx.service.endpoints.length === 0 || ctx.service.endpoints.some((x) => !x.deprecated)
|
|
220
|
+
? null
|
|
221
|
+
: ts.docTag('deprecated'),
|
|
222
|
+
],
|
|
223
|
+
}),
|
|
224
|
+
decorators: [
|
|
225
|
+
ts.decorator(
|
|
226
|
+
ts.refs.angular.injectable(),
|
|
227
|
+
ctx.config.provideKind === 'root' ? [ts.toNode({ providedIn: 'root' })] : [],
|
|
228
|
+
),
|
|
229
|
+
],
|
|
230
|
+
export: true,
|
|
231
|
+
extends: ctx.refs.apiBaseService(),
|
|
232
|
+
members: [
|
|
233
|
+
...ctx.service.endpoints.map((e) =>
|
|
234
|
+
ts.property(this.getEndpointPathPropertyName(ctx, e), {
|
|
235
|
+
accessModifier: 'private',
|
|
236
|
+
static: true,
|
|
237
|
+
readonly: true,
|
|
238
|
+
value: ts.string(e.path),
|
|
239
|
+
})
|
|
240
|
+
),
|
|
241
|
+
...ctx.service.endpoints.map((e) => this.getEndpointMethod(ctx, e)),
|
|
242
|
+
],
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
protected getEndpointMethod(ctx: Context, endpoint: ApiEndpoint): ts.Method<Builder> {
|
|
247
|
+
const hasParams = this.hasEndpointParams(ctx, endpoint);
|
|
248
|
+
const paramsOptional = !endpoint.parameters.some((p) => p.required) && !endpoint.requestBody?.required;
|
|
249
|
+
const responseModelType = ts.reference(
|
|
250
|
+
this.getResponseModelName(ctx, endpoint),
|
|
251
|
+
this.getResponseModelFilePath(ctx),
|
|
252
|
+
{
|
|
253
|
+
importType: 'type-import',
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
const returnType = ctx.refs.abortablePromise([responseModelType]);
|
|
257
|
+
const accept = this.getEndpointSuccessResponseType(ctx, endpoint);
|
|
258
|
+
const responseType = this.contentTypeToResponseType(accept);
|
|
259
|
+
return ts.method<Builder>(this.getEndpointMethodName(ctx, endpoint), {
|
|
260
|
+
accessModifier: 'public',
|
|
261
|
+
parameters: [
|
|
262
|
+
hasParams
|
|
263
|
+
? ts.parameter('params', {
|
|
264
|
+
optional: paramsOptional,
|
|
265
|
+
type: ts.reference(this.getEndpointParamsTypeName(ctx, endpoint)),
|
|
266
|
+
})
|
|
267
|
+
: null,
|
|
268
|
+
ts.parameter('context', { optional: true, type: ts.refs.angular.httpContext() }),
|
|
269
|
+
],
|
|
270
|
+
doc: ts.doc({
|
|
271
|
+
description: endpoint.description,
|
|
272
|
+
tags: [endpoint.deprecated ? ts.docTag('deprecated') : null],
|
|
273
|
+
}),
|
|
274
|
+
returnType,
|
|
275
|
+
body: appendValueGroup(
|
|
276
|
+
[
|
|
277
|
+
s`const rb = new ${ctx.refs.requestBuilder()}(this.rootUrl, ${
|
|
278
|
+
this.getServiceClassName(
|
|
279
|
+
ctx,
|
|
280
|
+
)
|
|
281
|
+
}.${this.getEndpointPathPropertyName(ctx, endpoint)}, '${endpoint.method}');`,
|
|
282
|
+
hasParams
|
|
283
|
+
? (b) =>
|
|
284
|
+
b.appendIf(paramsOptional, 'if (params) ').parenthesizeIf(
|
|
285
|
+
paramsOptional,
|
|
286
|
+
'{}',
|
|
287
|
+
appendValueGroup(
|
|
288
|
+
[
|
|
289
|
+
...endpoint.parameters
|
|
290
|
+
.filter((p) => p.target === 'path' || p.target === 'query' || p.target === 'header')
|
|
291
|
+
.map((p) => {
|
|
292
|
+
const options = ts.object({
|
|
293
|
+
members: [
|
|
294
|
+
p.style !== undefined ? ts.property('style', { value: ts.string(p.style) }) : null,
|
|
295
|
+
p.explode !== undefined ? ts.property('explode', { value: ts.toNode(p.explode) }) : null,
|
|
296
|
+
],
|
|
297
|
+
});
|
|
298
|
+
return s<Builder>`rb.${p.target}(${ts.string(p.name)}, params.${
|
|
299
|
+
toCasing(
|
|
300
|
+
p.name,
|
|
301
|
+
ctx.config.propertyNameCasing,
|
|
302
|
+
)
|
|
303
|
+
}, ${options});`;
|
|
304
|
+
}),
|
|
305
|
+
endpoint.requestBody
|
|
306
|
+
? s`rb.body(params.body, ${ts.string(this.getEndpointRequestContentType(ctx, endpoint))});`
|
|
307
|
+
: null,
|
|
308
|
+
],
|
|
309
|
+
'\n',
|
|
310
|
+
),
|
|
311
|
+
{ multiline: true },
|
|
312
|
+
)
|
|
313
|
+
: null,
|
|
314
|
+
'',
|
|
315
|
+
s`return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent`
|
|
316
|
+
this.http.request(rb.build({${s.indent`
|
|
317
|
+
responseType: ${ts.string(responseType)},
|
|
318
|
+
accept: ${ts.string(accept)},
|
|
319
|
+
context,`}
|
|
320
|
+
})),
|
|
321
|
+
{${s.indent`
|
|
322
|
+
errorResponseTypes: ${
|
|
323
|
+
ts.object({
|
|
324
|
+
members: [
|
|
325
|
+
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
326
|
+
.filter(([key]) => !key.startsWith('2'))
|
|
327
|
+
.map(([key, value]) => {
|
|
328
|
+
return ts.property(key, { value: ts.string(value.parser) });
|
|
329
|
+
}),
|
|
330
|
+
],
|
|
331
|
+
})
|
|
332
|
+
}`}
|
|
333
|
+
}`}
|
|
334
|
+
)`,
|
|
335
|
+
],
|
|
336
|
+
'\n',
|
|
337
|
+
),
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
protected getEndpointRequestContentType(ctx: Context, endpoint: ApiEndpoint): string {
|
|
342
|
+
return ctx.config.defaultRequestContentType ?? endpoint?.requestBody?.content[0].type ?? 'application/json';
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
protected getEndpointSuccessResponseType(ctx: Context, endpoint: ApiEndpoint): string {
|
|
346
|
+
const response = this.getEndpointSuccessResponse(ctx, endpoint);
|
|
347
|
+
if (!response) return '*/*';
|
|
348
|
+
|
|
349
|
+
if (!ctx.config.defaultSuccessResponseContentType) return response.contentOptions[0]?.type ?? '*/*';
|
|
350
|
+
|
|
351
|
+
return (
|
|
352
|
+
response.contentOptions.find((x) => x.type === ctx.config.defaultSuccessResponseContentType)?.type ??
|
|
353
|
+
response.contentOptions[0]?.type ??
|
|
354
|
+
'*/*'
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
protected getEndpointSuccessResponse(_ctx: Context, endpoint: ApiEndpoint): ApiResponse | undefined {
|
|
359
|
+
return (
|
|
360
|
+
endpoint.responses.find((x) => x.statusCode && x.statusCode >= 200 && x.statusCode < 300) ??
|
|
361
|
+
endpoint.responses.find((x) => x.statusCode === undefined)
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
protected getServiceClassName(ctx: Context): string {
|
|
366
|
+
return toCasing(ctx.service.name + '_Service', ctx.config.typeNameCasing);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
protected getResponseModelName(ctx: Context, endpoint: ApiEndpoint): string {
|
|
370
|
+
return toCasing(`${this.getEndpointMethodName(ctx, endpoint)}ApiResponse`, ctx.config.typeNameCasing);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
protected getEndpointParamsTypeName(ctx: Context, endpoint: ApiEndpoint): string {
|
|
374
|
+
return toCasing(this.getEndpointMethodName(ctx, endpoint) + '_Params', ctx.config.typeNameCasing);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
protected getEndpointPathPropertyName(ctx: Context, endpoint: ApiEndpoint): string {
|
|
378
|
+
return toCasing(this.getEndpointMethodName(ctx, endpoint) + '_Path', ctx.config.constantNameCasing);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
protected getEndpointMethodName(ctx: Context, endpoint: ApiEndpoint): string {
|
|
382
|
+
return toCasing(endpoint.name, ctx.config.functionNameCasing);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
protected getServiceFilePath(ctx: Context): string {
|
|
386
|
+
return resolve(
|
|
387
|
+
ctx.config.outputDir,
|
|
388
|
+
ctx.config.servicesDir,
|
|
389
|
+
`${toCasing(ctx.service.name, ctx.config.serviceFileNameCasing ?? ctx.config.fileNameCasing)}.ts`,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
protected getResponseModelFilePath(ctx: Context): string {
|
|
394
|
+
return resolve(
|
|
395
|
+
ctx.config.outputDir,
|
|
396
|
+
ctx.config.responseModelsDir,
|
|
397
|
+
`${toCasing(ctx.service.name, ctx.config.responseModelFileNameCasing ?? ctx.config.fileNameCasing)}.ts`,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
protected hasEndpointParams(_ctx: Context, endpoint: ApiEndpoint): boolean {
|
|
402
|
+
return endpoint.parameters.length > 0 || endpoint.requestBody !== undefined;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
protected getSchemaType(ctx: Context, schema: ApiSchema | undefined, fallback?: ts.Type<Builder>): ts.Type<Builder> {
|
|
406
|
+
const output = schema && ctx.input.typescript.models[schema.id];
|
|
407
|
+
if (!output) return fallback ?? this.getAnyType(ctx);
|
|
408
|
+
return (b) => b.appendModelUsage(output);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
private contentTypeToResponseType(contentType: Nullable<string>): 'text' | 'json' {
|
|
412
|
+
return contentType?.includes('json') ? 'json' : 'text';
|
|
413
|
+
}
|
|
414
|
+
}
|