@goast/kotlin 0.5.9-beta.2 → 0.5.10

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.
Files changed (100) hide show
  1. package/LICENSE +21 -21
  2. package/assets/client/okhttp3/ApiAbstractions.kt +30 -30
  3. package/assets/client/okhttp3/ApiClient.kt +252 -252
  4. package/assets/client/okhttp3/ApiResponse.kt +43 -43
  5. package/assets/client/okhttp3/Errors.kt +21 -21
  6. package/assets/client/okhttp3/PartConfig.kt +11 -11
  7. package/assets/client/okhttp3/RequestConfig.kt +18 -18
  8. package/assets/client/okhttp3/RequestMethod.kt +8 -8
  9. package/assets/client/okhttp3/ResponseExtensions.kt +24 -24
  10. package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
  11. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +28 -28
  12. package/esm/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +9 -9
  13. package/esm/src/generators/services/spring-controllers/spring-controller-generator.d.ts +11 -11
  14. package/esm/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  15. package/esm/src/generators/services/spring-controllers/spring-controller-generator.js +15 -13
  16. package/package.json +1 -1
  17. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +28 -28
  18. package/script/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +9 -9
  19. package/script/src/generators/services/spring-controllers/spring-controller-generator.d.ts +11 -11
  20. package/script/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  21. package/script/src/generators/services/spring-controllers/spring-controller-generator.js +15 -13
  22. package/src/mod.ts +8 -0
  23. package/src/src/assets.ts +9 -0
  24. package/src/src/ast/_index.ts +66 -0
  25. package/src/src/ast/common.ts +1 -0
  26. package/src/src/ast/index.ts +1 -0
  27. package/src/src/ast/node.ts +10 -0
  28. package/src/src/ast/nodes/annotation.ts +79 -0
  29. package/src/src/ast/nodes/argument.ts +62 -0
  30. package/src/src/ast/nodes/call.ts +75 -0
  31. package/src/src/ast/nodes/class.ts +178 -0
  32. package/src/src/ast/nodes/collection-literal.ts +49 -0
  33. package/src/src/ast/nodes/constructor.ts +126 -0
  34. package/src/src/ast/nodes/doc-tag.ts +138 -0
  35. package/src/src/ast/nodes/doc.ts +111 -0
  36. package/src/src/ast/nodes/enum-value.ts +100 -0
  37. package/src/src/ast/nodes/enum.ts +163 -0
  38. package/src/src/ast/nodes/function.ts +178 -0
  39. package/src/src/ast/nodes/generic-parameter.ts +54 -0
  40. package/src/src/ast/nodes/init-block.ts +38 -0
  41. package/src/src/ast/nodes/interface.ts +133 -0
  42. package/src/src/ast/nodes/lambda-type.ts +73 -0
  43. package/src/src/ast/nodes/lambda.ts +74 -0
  44. package/src/src/ast/nodes/object.ts +102 -0
  45. package/src/src/ast/nodes/parameter.ts +118 -0
  46. package/src/src/ast/nodes/property.ts +225 -0
  47. package/src/src/ast/nodes/reference.ts +178 -0
  48. package/src/src/ast/nodes/string.ts +114 -0
  49. package/src/src/ast/nodes/types.ts +23 -0
  50. package/src/src/ast/references/index.ts +10 -0
  51. package/src/src/ast/references/jackson.ts +59 -0
  52. package/src/src/ast/references/jakarta.ts +14 -0
  53. package/src/src/ast/references/java.ts +27 -0
  54. package/src/src/ast/references/kotlin.ts +41 -0
  55. package/src/src/ast/references/kotlinx.ts +14 -0
  56. package/src/src/ast/references/okhttp3.ts +5 -0
  57. package/src/src/ast/references/reactor.ts +5 -0
  58. package/src/src/ast/references/spring-reactive.ts +33 -0
  59. package/src/src/ast/references/spring.ts +86 -0
  60. package/src/src/ast/references/swagger.ts +23 -0
  61. package/src/src/ast/utils/get-kotlin-builder-options.ts +19 -0
  62. package/src/src/ast/utils/to-kt-node.ts +31 -0
  63. package/src/src/ast/utils/write-kt-annotations.ts +15 -0
  64. package/src/src/ast/utils/write-kt-arguments.ts +45 -0
  65. package/src/src/ast/utils/write-kt-enum-values.ts +27 -0
  66. package/src/src/ast/utils/write-kt-generic-parameters.ts +12 -0
  67. package/src/src/ast/utils/write-kt-members.ts +25 -0
  68. package/src/src/ast/utils/write-kt-node.ts +37 -0
  69. package/src/src/ast/utils/write-kt-parameters.ts +25 -0
  70. package/src/src/common-results.ts +4 -0
  71. package/src/src/config.ts +61 -0
  72. package/src/src/file-builder.ts +112 -0
  73. package/src/src/generators/file-generator.ts +29 -0
  74. package/src/src/generators/index.ts +5 -0
  75. package/src/src/generators/models/args.ts +136 -0
  76. package/src/src/generators/models/index.ts +4 -0
  77. package/src/src/generators/models/model-generator.ts +725 -0
  78. package/src/src/generators/models/models-generator.ts +65 -0
  79. package/src/src/generators/models/models.ts +95 -0
  80. package/src/src/generators/services/okhttp3-clients/args.ts +90 -0
  81. package/src/src/generators/services/okhttp3-clients/index.ts +4 -0
  82. package/src/src/generators/services/okhttp3-clients/models.ts +73 -0
  83. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +613 -0
  84. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +188 -0
  85. package/src/src/generators/services/okhttp3-clients/refs.ts +59 -0
  86. package/src/src/generators/services/spring-controllers/args.ts +95 -0
  87. package/src/src/generators/services/spring-controllers/index.ts +4 -0
  88. package/src/src/generators/services/spring-controllers/models.ts +76 -0
  89. package/src/src/generators/services/spring-controllers/refs.ts +17 -0
  90. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +1109 -0
  91. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +140 -0
  92. package/src/src/generators/services/spring-reactive-web-clients/args.ts +103 -0
  93. package/src/src/generators/services/spring-reactive-web-clients/index.ts +4 -0
  94. package/src/src/generators/services/spring-reactive-web-clients/models.ts +62 -0
  95. package/src/src/generators/services/spring-reactive-web-clients/refs.ts +11 -0
  96. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +586 -0
  97. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +125 -0
  98. package/src/src/import-collection.ts +98 -0
  99. package/src/src/types.ts +3 -0
  100. package/src/src/utils.ts +47 -0
@@ -0,0 +1,586 @@
1
+ import {
2
+ type ApiParameter,
3
+ type ApiSchema,
4
+ type AppendValueGroup,
5
+ appendValueGroup,
6
+ type BasicAppendValue,
7
+ builderTemplate as s,
8
+ createOverwriteProxy,
9
+ getSourceDisplayName,
10
+ type MaybePromise,
11
+ notNullish,
12
+ resolveAnyOfAndAllOf,
13
+ SourceBuilder,
14
+ toCasing,
15
+ } from '@goast/core';
16
+
17
+ import { kt } from '../../../ast/index.js';
18
+ import type { KtValue } from '../../../ast/nodes/types.js';
19
+ import { KotlinFileBuilder } from '../../../file-builder.js';
20
+ import type { ApiParameterWithMultipartInfo } from '../../../types.js';
21
+ import { getSourceDocLine, modifyString } from '../../../utils.js';
22
+ import { KotlinFileGenerator } from '../../file-generator.js';
23
+ import type { DefaultKotlinSpringReactiveWebClientGeneratorArgs as Args } from './index.js';
24
+ import type {
25
+ KotlinSpringReactiveWebClientGeneratorContext,
26
+ KotlinSpringReactiveWebClientGeneratorOutput,
27
+ } from './models.js';
28
+
29
+ type Context = KotlinSpringReactiveWebClientGeneratorContext;
30
+ type Output = KotlinSpringReactiveWebClientGeneratorOutput;
31
+ type Builder = KotlinFileBuilder;
32
+
33
+ export interface KotlinSpringReactiveWebClientGenerator<TOutput extends Output = Output> {
34
+ generate(ctx: Context): MaybePromise<TOutput>;
35
+ }
36
+
37
+ export class DefaultKotlinSpringReactiveWebClientGenerator extends KotlinFileGenerator<Context, Output>
38
+ implements KotlinSpringReactiveWebClientGenerator {
39
+ public generate(ctx: Context): MaybePromise<Output> {
40
+ const typeName = this.getRequestsObjectName(ctx, {});
41
+ const packageName = this.getPackageName(ctx, {});
42
+ const filePath = this.getFilePath(ctx, { packageName });
43
+
44
+ console.log(`Generating client for service ${ctx.service.name} to ${filePath}...`);
45
+ ctx.service.endpoints.forEach((endpoint) => {
46
+ console.log(
47
+ ` ${getSourceDisplayName(ctx.data, endpoint)} [${toCasing(endpoint.name, ctx.config.functionNameCasing)}]`,
48
+ );
49
+ });
50
+
51
+ const builder = new KotlinFileBuilder(packageName, ctx.config);
52
+ builder.append(this.getClientFileContent(ctx, {}));
53
+ builder.writeToFile(filePath);
54
+
55
+ return { typeName, packageName };
56
+ }
57
+
58
+ // deno-lint-ignore no-unused-vars
59
+ protected getClientFileContent(ctx: Context, args: Args.GetClientFileContent): AppendValueGroup<Builder> {
60
+ return appendValueGroup([this.getRequestsObject(ctx, {})], '\n\n');
61
+ }
62
+
63
+ // deno-lint-ignore no-unused-vars
64
+ protected getRequestsObject(ctx: Context, args: Args.GetRequestsObject): kt.Object<Builder> {
65
+ return kt.object({
66
+ name: this.getRequestsObjectName(ctx, {}),
67
+ members: ctx.service.endpoints.flatMap((endpoint) =>
68
+ this.getEndpointMembers(ctx, { endpoint, parameters: this.getAllParameters(ctx, { endpoint }) })
69
+ ),
70
+ });
71
+ }
72
+
73
+ protected getEndpointMembers(ctx: Context, args: Args.GetEndpointMembers): kt.ObjectMember<Builder>[] {
74
+ const { endpoint, parameters } = args;
75
+ const responseSchema = this.getResponseSchema(ctx, { endpoint });
76
+
77
+ return [
78
+ this.getEndpointFunction(ctx, { endpoint, parameters, responseSchema }),
79
+ this.getEndpointFunctionWithHandler(ctx, { endpoint, parameters, responseSchema }),
80
+ this.getEndpointUriFunction(ctx, { endpoint, parameters }),
81
+ this.getEndpointRequestFunction(ctx, { endpoint, parameters, responseSchema }),
82
+ ];
83
+ }
84
+
85
+ protected getEndpointFunctionName(ctx: Context, args: Args.GetEndpointFunctionName): string {
86
+ const { endpoint } = args;
87
+ return toCasing(endpoint.name, ctx.config.functionNameCasing);
88
+ }
89
+
90
+ protected getEndpointFunction(ctx: Context, args: Args.GetEndpointFunction): kt.Function<Builder> {
91
+ const { endpoint, parameters, responseSchema } = args;
92
+ const functionName = this.getEndpointFunctionName(ctx, { endpoint });
93
+
94
+ return kt.function(functionName, {
95
+ doc: kt.doc(this.getEndpointDocDescription(ctx, { endpoint })),
96
+ suspend: true,
97
+ receiverType: kt.refs.springReactive.webClient(),
98
+ parameters: parameters.map((parameter) =>
99
+ kt.parameter(
100
+ toCasing(parameter.name, ctx.config.parameterNameCasing),
101
+ this.getParameterType(ctx, { endpoint, parameter }),
102
+ { default: this.getParameterDefaultValue(ctx, { endpoint, parameter }) },
103
+ )
104
+ ),
105
+ returnType: this.getTypeUsage(ctx, { schema: responseSchema, fallback: kt.refs.unit() }),
106
+ body: this.getEndpointFunctionBody(ctx, { endpoint, parameters, responseSchema }),
107
+ });
108
+ }
109
+
110
+ protected getEndpointFunctionBody(ctx: Context, args: Args.GetEndpointFunctionBody): AppendValueGroup<Builder> {
111
+ const { endpoint, parameters, responseSchema } = args;
112
+ const result = appendValueGroup<Builder>([], '\n');
113
+
114
+ const requestFunctionName = this.getEndpointRequestFunctionName(ctx, { endpoint });
115
+ const parameterNames = parameters.map((p) => toCasing(p.name, ctx.config.parameterNameCasing));
116
+ const call = kt.call([
117
+ 'this',
118
+ kt.call(requestFunctionName, parameterNames),
119
+ kt.call('retrieve', []),
120
+ responseSchema
121
+ ? kt.call(
122
+ kt.refs.springReactive.awaitBody([
123
+ this.getTypeUsage(ctx, { schema: responseSchema, fallback: kt.refs.unit() }),
124
+ ]),
125
+ [],
126
+ )
127
+ : kt.call(kt.refs.springReactive.awaitBodilessEntity(), []),
128
+ ]);
129
+
130
+ if (responseSchema) {
131
+ result.values.push(s`return ${call}`);
132
+ } else {
133
+ result.values.push(call);
134
+ }
135
+
136
+ return result;
137
+ }
138
+
139
+ protected getEndpointFunctionWithHandler(
140
+ ctx: Context,
141
+ args: Args.GetEndpointFunctionWithHandler,
142
+ ): kt.Function<Builder> {
143
+ const { endpoint, parameters, responseSchema } = args;
144
+ const functionName = this.getEndpointFunctionName(ctx, { endpoint });
145
+
146
+ return kt.function(functionName, {
147
+ doc: kt.doc(this.getEndpointDocDescription(ctx, { endpoint })),
148
+ suspend: true,
149
+ // Spring 7's `WebClient.exchangeToMono` is `<V : Any>`, so the `<T>` overloads need an `Any` bound to infer.
150
+ generics: [
151
+ kt.genericParameter('T', ctx.config.springBootVersion === 4 ? { constraint: kt.refs.any() } : undefined),
152
+ ],
153
+ receiverType: kt.refs.springReactive.webClient(),
154
+ parameters: [
155
+ ...parameters.map((parameter) =>
156
+ kt.parameter(
157
+ toCasing(parameter.name, ctx.config.parameterNameCasing),
158
+ this.getParameterType(ctx, { endpoint, parameter }),
159
+ { default: this.getParameterDefaultValue(ctx, { endpoint, parameter }) },
160
+ )
161
+ ),
162
+ kt.parameter(
163
+ 'responseHandler',
164
+ kt.lambdaType([kt.refs.springReactive.clientResponse()], 'T', { suspend: true }),
165
+ ),
166
+ ],
167
+ returnType: 'T',
168
+ body: this.getEndpointFunctionWithHandlerBody(ctx, { endpoint, parameters, responseSchema }),
169
+ });
170
+ }
171
+
172
+ protected getEndpointFunctionWithHandlerBody(
173
+ ctx: Context,
174
+ args: Args.GetEndpointFunctionWithHandlerBody,
175
+ ): AppendValueGroup<Builder> {
176
+ const { endpoint, parameters } = args;
177
+ const result = appendValueGroup<Builder>([], '\n');
178
+
179
+ const requestFunctionName = this.getEndpointRequestFunctionName(ctx, { endpoint });
180
+ const parameterNames = parameters.map((p) => toCasing(p.name, ctx.config.parameterNameCasing));
181
+
182
+ result.values.push(s`return ${
183
+ kt.call([
184
+ kt.call(['this', requestFunctionName], parameterNames),
185
+ kt.call(['exchangeToMono'], [
186
+ kt.lambda(
187
+ [],
188
+ kt.call(kt.refs.kotlinx.mono.infer(), [kt.lambda([], 'responseHandler(it)', { singleline: true })]),
189
+ { singleline: true },
190
+ ),
191
+ ]),
192
+ kt.call([kt.refs.kotlinx.awaitFirstOrNull.infer()], []),
193
+ ])
194
+ } as T`);
195
+
196
+ return result;
197
+ }
198
+
199
+ protected getEndpointRequestFunctionName(ctx: Context, args: Args.GetEndpointRequestFunctionName): string {
200
+ const { endpoint } = args;
201
+ return toCasing(`${this.getEndpointFunctionName(ctx, { endpoint })}_request`, ctx.config.functionNameCasing);
202
+ }
203
+
204
+ protected getEndpointRequestFunction(ctx: Context, args: Args.GetEndpointRequestFunction): kt.Function<Builder> {
205
+ const { endpoint, parameters, responseSchema } = args;
206
+ const functionName = this.getEndpointRequestFunctionName(ctx, { endpoint });
207
+
208
+ return kt.function(functionName, {
209
+ doc: kt.doc(this.getEndpointDocDescription(ctx, { endpoint })),
210
+ receiverType: kt.refs.springReactive.webClient(),
211
+ parameters: parameters.map((parameter) =>
212
+ kt.parameter(
213
+ toCasing(parameter.name, ctx.config.parameterNameCasing),
214
+ this.getParameterType(ctx, { endpoint, parameter }),
215
+ { default: this.getParameterDefaultValue(ctx, { endpoint, parameter }) },
216
+ )
217
+ ),
218
+ returnType: kt.refs.springReactive.requestHeadersSpec(['*']),
219
+ body: this.getEndpointRequestFunctionBody(ctx, { endpoint, parameters, responseSchema }),
220
+ });
221
+ }
222
+
223
+ protected getEndpointRequestFunctionBody(
224
+ ctx: Context,
225
+ args: Args.GetEndpointRequestFunctionBody,
226
+ ): AppendValueGroup<Builder> {
227
+ const { endpoint, parameters, responseSchema } = args;
228
+ const result = appendValueGroup<Builder>([], '\n');
229
+
230
+ const callChain: KtValue<Builder>[] = [];
231
+ callChain.push(
232
+ s`return this.method(${kt.refs.spring.httpMethod()}.${endpoint.method.toUpperCase()})`,
233
+ kt.call('uri', [kt.call(
234
+ [this.getEndpointUriFunctionName(ctx, { endpoint })],
235
+ parameters.filter((p) => p.target === 'path' || p.target === 'query').map((p) =>
236
+ toCasing(p.name, ctx.config.parameterNameCasing)
237
+ ),
238
+ )]),
239
+ );
240
+
241
+ if (responseSchema) {
242
+ callChain.push(s`accept(${kt.refs.spring.mediaType()}.APPLICATION_JSON)`);
243
+ }
244
+
245
+ if (endpoint.requestBody?.content[0] !== undefined) {
246
+ callChain.push(
247
+ s`contentType(${kt.refs.spring.mediaType()}.parseMediaType(${
248
+ kt.string(endpoint.requestBody.content[0].type)
249
+ }))`,
250
+ );
251
+ }
252
+
253
+ if (endpoint.requestBody?.content[0]) {
254
+ if (endpoint.requestBody.content[0].type === 'multipart/form-data') {
255
+ callChain.push(
256
+ kt.call('body', [
257
+ kt.call([kt.refs.springReactive.bodyInserters(), 'fromMultipartData'], [
258
+ kt.call([
259
+ kt.call(kt.refs.spring.multipartBodyBuilder(), []),
260
+ kt.call('apply', [
261
+ kt.lambda(
262
+ [],
263
+ appendValueGroup(
264
+ parameters.filter((x) => x.multipart).map((p) => {
265
+ const parameterName = toCasing(p.name, ctx.config.parameterNameCasing);
266
+ if (p.multipart?.isFile) {
267
+ return kt.call([
268
+ p.required && !p.schema?.nullable ? parameterName : `${parameterName}?`,
269
+ 'addToBuilder',
270
+ ], ['this']);
271
+ } else {
272
+ const call = kt.call([
273
+ kt.call('part', [kt.string(p.multipart!.name), parameterName]),
274
+ kt.call('contentType', [
275
+ kt.call([
276
+ kt.refs.spring.mediaType(),
277
+ 'APPLICATION_JSON',
278
+ ]),
279
+ ]),
280
+ ]);
281
+ return p.required && !p.schema?.nullable ? call : kt.call([`${parameterName}?`, 'also'], [
282
+ kt.lambda([parameterName], call, { singleline: true }),
283
+ ]);
284
+ }
285
+ }),
286
+ '\n',
287
+ ),
288
+ ),
289
+ ]),
290
+ 'build',
291
+ ], []),
292
+ ]),
293
+ ]),
294
+ );
295
+ } else {
296
+ const bodySchema = endpoint.requestBody.content[0].schema;
297
+ const parameterName = this.getRequestBodyParamName(ctx, { endpoint });
298
+ const call = kt.call('bodyValue', [
299
+ toCasing(parameterName, ctx.config.parameterNameCasing),
300
+ ]);
301
+ callChain.push(
302
+ endpoint.requestBody.required && !bodySchema?.nullable ? call : kt.call('apply', [
303
+ kt.lambda(
304
+ [],
305
+ kt.call([`${parameterName}?`, 'also'], [kt.lambda([parameterName], call, { singleline: true })]),
306
+ { singleline: true },
307
+ ),
308
+ ]),
309
+ );
310
+ }
311
+ }
312
+
313
+ if (parameters.some((x) => x.target === 'header')) {
314
+ callChain.push(
315
+ kt.call('headers', [
316
+ kt.lambda(
317
+ ['headers'],
318
+ appendValueGroup(
319
+ parameters.filter((x) => x.target === 'header').map((p) => {
320
+ const parameterName = toCasing(p.name, ctx.config.parameterNameCasing);
321
+ const toString = this.getParameterToString(ctx, { endpoint, parameter: p });
322
+ return p.required && !p.schema?.nullable
323
+ ? kt.call(['headers', 'add'], [kt.string(p.name), parameterName + toString])
324
+ : kt.call([`${parameterName}?`, 'also'], [
325
+ kt.lambda([], kt.call(['headers', 'add'], [kt.string(p.name), 'it' + toString]), {
326
+ singleline: true,
327
+ }),
328
+ ]);
329
+ }),
330
+ '\n',
331
+ ),
332
+ ),
333
+ ]),
334
+ );
335
+ }
336
+
337
+ result.values.push(kt.call(callChain));
338
+ return result;
339
+ }
340
+
341
+ protected getEndpointUriFunctionName(ctx: Context, args: Args.GetEndpointUriFunctionName): string {
342
+ const { endpoint } = args;
343
+ return toCasing(`${this.getEndpointFunctionName(ctx, { endpoint })}_uri`, ctx.config.functionNameCasing);
344
+ }
345
+
346
+ protected getEndpointUriFunction(ctx: Context, args: Args.GetEndpointUriFunction): kt.Function<Builder> {
347
+ const { endpoint, parameters } = args;
348
+ const functionName = this.getEndpointUriFunctionName(ctx, { endpoint });
349
+
350
+ return kt.function(functionName, {
351
+ parameters: parameters.filter((p) => p.target === 'path' || p.target === 'query').map((parameter) =>
352
+ kt.parameter(
353
+ toCasing(parameter.name, ctx.config.parameterNameCasing),
354
+ this.getParameterType(ctx, { endpoint, parameter }),
355
+ { default: this.getParameterDefaultValue(ctx, { endpoint, parameter }) },
356
+ )
357
+ ),
358
+ returnType: kt.refs.string(),
359
+ body: this.getEndpointUriFunctionBody(ctx, { endpoint, parameters }),
360
+ });
361
+ }
362
+
363
+ protected getEndpointUriFunctionBody(ctx: Context, args: Args.GetEndpointUriFunctionBody): AppendValueGroup<Builder> {
364
+ const { endpoint } = args;
365
+ const result = appendValueGroup<Builder>([], '\n');
366
+
367
+ const callChain: KtValue<Builder>[] = [];
368
+ callChain.push(
369
+ s`${kt.refs.spring.uriComponentsBuilder()}.fromPath(${kt.string(this.getEndpointPath(ctx, { endpoint }))})`,
370
+ );
371
+
372
+ if (endpoint.parameters.some((x) => x.target === 'query')) {
373
+ callChain.push(
374
+ kt.call('apply', [kt.lambda(
375
+ [],
376
+ appendValueGroup(
377
+ endpoint.parameters.filter((x) => x.target === 'query').map((p) => {
378
+ const parameterName = toCasing(p.name, ctx.config.parameterNameCasing);
379
+ const toString = this.getParameterToString(ctx, { endpoint, parameter: p });
380
+ return p.required && !p.schema?.nullable
381
+ ? kt.call('queryParam', [kt.string(p.name), parameterName + toString])
382
+ : kt.call([`${parameterName}?`, 'also'], [
383
+ kt.lambda([], kt.call('queryParam', [kt.string(p.name), 'it' + toString]), { singleline: true }),
384
+ ]);
385
+ }),
386
+ '\n',
387
+ ),
388
+ )]),
389
+ );
390
+ }
391
+
392
+ if (endpoint.parameters.some((x) => x.target === 'path')) {
393
+ callChain.push(kt.call('buildAndExpand', [
394
+ kt.call(
395
+ [kt.refs.mapOf.infer()],
396
+ endpoint.parameters.filter((p) => p.target === 'path').map((p) =>
397
+ s`${kt.string(p.name)} to ${toCasing(p.name, ctx.config.parameterNameCasing)}${
398
+ this.getParameterToString(ctx, { endpoint, parameter: p })
399
+ }`
400
+ ),
401
+ ),
402
+ ]));
403
+ } else {
404
+ callChain.push(kt.call('build', []));
405
+ }
406
+
407
+ callChain.push('toUriString');
408
+ result.values.push(s`return ${kt.call(callChain, [])}`);
409
+ return result;
410
+ }
411
+
412
+ protected getParameterToString(ctx: Context, args: Args.GetParameterToString): kt.Value<Builder> {
413
+ const { parameter } = args;
414
+ if (parameter.schema?.kind === 'array') {
415
+ return '.joinToString()';
416
+ } else if (
417
+ parameter.schema?.kind === 'string' && parameter.schema.enum?.length &&
418
+ this.getSchemaType(ctx, { schema: parameter.schema })
419
+ ) {
420
+ return '.value';
421
+ } else {
422
+ return '.toString()';
423
+ }
424
+ }
425
+
426
+ protected getParameterType(ctx: Context, args: Args.GetParameterType): kt.Type<Builder> {
427
+ const { parameter } = args;
428
+ if (parameter.multipart?.isFile) {
429
+ return ctx.refs.apiRequestFile();
430
+ }
431
+ return this.getTypeUsage(ctx, {
432
+ schema: parameter.schema,
433
+ nullable: !parameter.required,
434
+ });
435
+ }
436
+
437
+ protected getParameterDefaultValue(ctx: Context, args: Args.GetParameterDefaultValue): kt.Value<Builder> | null {
438
+ const { parameter } = args;
439
+
440
+ return !parameter.required
441
+ ? parameter.schema?.kind === 'string' && parameter.schema.enum && parameter.schema.default
442
+ ? s`${this.getTypeUsage(ctx, { schema: parameter.schema, nullable: false })}.${
443
+ toCasing(String(parameter.schema.default), ctx.config.enumValueNameCasing)
444
+ }`
445
+ : kt.toNode(parameter.schema?.default)
446
+ : null;
447
+ }
448
+
449
+ protected getTypeUsage(ctx: Context, args: Args.GetTypeUsage<Builder>): kt.Type<Builder> {
450
+ const { schema, nullable, fallback } = args;
451
+ const type = this.getSchemaType(ctx, { schema });
452
+ return type
453
+ ? createOverwriteProxy(type, { nullable: nullable ?? type.nullable })
454
+ : (fallback ?? kt.refs.any({ nullable }));
455
+ }
456
+
457
+ // deno-lint-ignore no-unused-vars
458
+ protected getPackageName(ctx: Context, args: Args.GetPackageName): string {
459
+ const packageSuffix = typeof ctx.config.packageSuffix === 'string'
460
+ ? ctx.config.packageSuffix
461
+ : ctx.config.packageSuffix(ctx.service);
462
+ return ctx.config.packageName + packageSuffix;
463
+ }
464
+
465
+ protected getResponseSchema(_ctx: Context, args: Args.GetResponseSchema): ApiSchema | undefined {
466
+ const { endpoint } = args;
467
+ return endpoint.responses.find((x) => !x.statusCode || (x.statusCode >= 200 && x.statusCode < 300))
468
+ ?.contentOptions[0]?.schema;
469
+ }
470
+
471
+ protected getSchemaType(ctx: Context, args: Args.GetSchemaType): kt.Reference<SourceBuilder> | undefined {
472
+ const { schema } = args;
473
+ return schema && ctx.input.kotlin.models[schema.id].type;
474
+ }
475
+
476
+ protected getAllParameters(ctx: Context, args: Args.GetAllParameters): ApiParameterWithMultipartInfo[] {
477
+ const { endpoint } = args;
478
+ const parameters = endpoint.parameters.filter(
479
+ (parameter) => parameter.target === 'query' || parameter.target === 'path' || parameter.target === 'header',
480
+ );
481
+ if (endpoint.requestBody) {
482
+ const content = endpoint.requestBody.content[0];
483
+ let schema = content.schema;
484
+
485
+ if (content.type === 'multipart/form-data') {
486
+ if (schema && schema.kind === 'object') {
487
+ schema = resolveAnyOfAndAllOf(schema, true) ?? schema;
488
+ const properties = schema.properties ?? {};
489
+ for (const [name, property] of properties.entries()) {
490
+ parameters.push(
491
+ Object.assign(
492
+ this.createApiParameter({
493
+ id: `multipart-${name}`,
494
+ name,
495
+ target: 'body',
496
+ schema: property.schema,
497
+ required: schema.required.has(name),
498
+ description: property.schema.description,
499
+ }),
500
+ {
501
+ multipart: {
502
+ name,
503
+ isFile: property.schema.kind === 'string' && property.schema.format === 'binary',
504
+ },
505
+ },
506
+ ),
507
+ );
508
+ }
509
+ }
510
+ } else {
511
+ parameters.push(
512
+ this.createApiParameter({
513
+ id: 'body',
514
+ name: this.getRequestBodyParamName(ctx, { endpoint }),
515
+ target: 'body',
516
+ schema,
517
+ required: endpoint.requestBody.required,
518
+ description: endpoint.requestBody.description,
519
+ }),
520
+ );
521
+ }
522
+ }
523
+
524
+ return parameters.sort((a, b) => (a.required === b.required ? 0 : a.required ? -1 : 1));
525
+ }
526
+
527
+ protected getRequestBodyParamName(ctx: Context, args: Args.GetRequestBodyParamName): string {
528
+ const { endpoint } = args;
529
+ const schema = endpoint.requestBody?.content[0].schema;
530
+ const schemaInfo = this.getSchemaType(ctx, { schema });
531
+ return toCasing(
532
+ schemaInfo && schemaInfo.name !== 'Any' ? SourceBuilder.build((b) => kt.reference.write(b, schemaInfo)) : 'body',
533
+ ctx.config.parameterNameCasing,
534
+ );
535
+ }
536
+
537
+ protected getEndpointDocDescription(ctx: Context, args: Args.GetEndpointDocDescription): BasicAppendValue<Builder> {
538
+ const docSegments = [args.endpoint.description?.trim()];
539
+
540
+ if (ctx.config.includeSourceInDocs) {
541
+ docSegments.push(getSourceDocLine(args.endpoint));
542
+ }
543
+
544
+ return docSegments.filter(notNullish).join('\n\n');
545
+ }
546
+
547
+ protected getBasePath(ctx: Context, _args: Args.GetBasePath): string {
548
+ return modifyString(
549
+ (ctx.service.$src ?? ctx.service.endpoints[0]?.$src)?.document.servers?.[0]?.url ?? '/',
550
+ ctx.config.basePath,
551
+ ctx.service,
552
+ );
553
+ }
554
+
555
+ protected getEndpointPath(ctx: Context, args: Args.GetEndpointPath): string {
556
+ const { endpoint } = args;
557
+ return modifyString(endpoint.path.replace(/^\/*/, ''), ctx.config.pathModifier, endpoint);
558
+ }
559
+
560
+ protected getFilePath(ctx: Context, args: Args.GetFilePath): string {
561
+ const { packageName } = args;
562
+ return `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}/${this.getRequestsObjectName(ctx, {})}.kt`;
563
+ }
564
+
565
+ // deno-lint-ignore no-unused-vars
566
+ protected getRequestsObjectName(ctx: Context, args: Args.GetRequestsObjectName): string {
567
+ return toCasing(`${ctx.service.name}_Requests`, ctx.config.typeNameCasing);
568
+ }
569
+
570
+ private createApiParameter(data: Partial<ApiParameter> & Pick<ApiParameter, 'id' | 'name' | 'target'>): ApiParameter {
571
+ return {
572
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
573
+ $src: undefined!,
574
+ $ref: undefined,
575
+ schema: undefined,
576
+ required: false,
577
+ description: undefined,
578
+ allowEmptyValue: undefined,
579
+ allowReserved: undefined,
580
+ deprecated: false,
581
+ explode: undefined,
582
+ style: undefined,
583
+ ...data,
584
+ };
585
+ }
586
+ }