@goast/kotlin 0.0.2 → 0.0.4
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/assets/client/okhttp3/ApiAbstractions.kt +30 -0
- package/assets/client/okhttp3/ApiClient.kt +248 -0
- package/assets/client/okhttp3/ApiResponse.kt +43 -0
- package/assets/client/okhttp3/Errors.kt +21 -0
- package/assets/client/okhttp3/PartConfig.kt +11 -0
- package/assets/client/okhttp3/RequestConfig.kt +18 -0
- package/assets/client/okhttp3/RequestMethod.kt +8 -0
- package/assets/client/okhttp3/ResponseExtensions.kt +24 -0
- package/assets/client/okhttp3/Serializer.kt +14 -0
- package/cjs/lib/generators/services/okhttp3-clients/models.js +1 -1
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +43 -36
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +22 -3
- package/cjs/lib/generators/services/spring-controllers/spring-controller-generator.js +10 -3
- package/cjs/lib/utils.js +16 -1
- package/esm/lib/generators/services/okhttp3-clients/models.js +1 -1
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +44 -37
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +22 -3
- package/esm/lib/generators/services/spring-controllers/spring-controller-generator.js +10 -3
- package/esm/lib/utils.js +14 -0
- package/package.json +1 -1
- package/types/lib/generators/services/okhttp3-clients/models.d.ts +11 -3
- package/types/lib/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +4 -3
- package/types/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.d.ts +2 -0
- package/types/lib/generators/services/spring-controllers/models.d.ts +3 -1
- package/types/lib/generators/services/spring-controllers/spring-controller-generator.d.ts +2 -0
- package/types/lib/utils.d.ts +1 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
typealias MultiValueMap = MutableMap<String, List<String>>
|
|
4
|
+
|
|
5
|
+
fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
|
|
6
|
+
"csv" -> ","
|
|
7
|
+
"tsv" -> "\t"
|
|
8
|
+
"pipe" -> "|"
|
|
9
|
+
"space" -> " "
|
|
10
|
+
else -> ""
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
|
|
14
|
+
|
|
15
|
+
fun <T : Any?> toMultiValue(
|
|
16
|
+
items: Array<T>,
|
|
17
|
+
collectionFormat: String,
|
|
18
|
+
map: (item: T) -> String = defaultMultiValueConverter
|
|
19
|
+
) = toMultiValue(items.asIterable(), collectionFormat, map)
|
|
20
|
+
|
|
21
|
+
fun <T : Any?> toMultiValue(
|
|
22
|
+
items: Iterable<T>,
|
|
23
|
+
collectionFormat: String,
|
|
24
|
+
map: (item: T) -> String = defaultMultiValueConverter
|
|
25
|
+
): List<String> {
|
|
26
|
+
return when (collectionFormat) {
|
|
27
|
+
"multi" -> items.map(map)
|
|
28
|
+
else -> listOf(items.joinToString(separator = collectionDelimiter(collectionFormat), transform = map))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
import com.fasterxml.jackson.core.type.TypeReference
|
|
4
|
+
import okhttp3.FormBody
|
|
5
|
+
import okhttp3.Headers.Companion.toHeaders
|
|
6
|
+
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
7
|
+
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
8
|
+
import okhttp3.MultipartBody
|
|
9
|
+
import okhttp3.OkHttpClient
|
|
10
|
+
import okhttp3.Request
|
|
11
|
+
import okhttp3.RequestBody
|
|
12
|
+
import okhttp3.RequestBody.Companion.asRequestBody
|
|
13
|
+
import okhttp3.RequestBody.Companion.toRequestBody
|
|
14
|
+
import okhttp3.ResponseBody
|
|
15
|
+
import okhttp3.internal.EMPTY_REQUEST
|
|
16
|
+
import java.io.File
|
|
17
|
+
import java.net.URLConnection
|
|
18
|
+
import java.time.LocalDate
|
|
19
|
+
import java.time.LocalDateTime
|
|
20
|
+
import java.time.LocalTime
|
|
21
|
+
import java.time.OffsetDateTime
|
|
22
|
+
import java.time.OffsetTime
|
|
23
|
+
import java.util.Locale
|
|
24
|
+
|
|
25
|
+
open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClient) {
|
|
26
|
+
companion object {
|
|
27
|
+
protected const val ContentType = "Content-Type"
|
|
28
|
+
protected const val Accept = "Accept"
|
|
29
|
+
protected const val Authorization = "Authorization"
|
|
30
|
+
protected const val JsonMediaType = "application/json"
|
|
31
|
+
protected const val FormDataMediaType = "multipart/form-data"
|
|
32
|
+
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
|
|
33
|
+
protected const val XmlMediaType = "application/xml"
|
|
34
|
+
|
|
35
|
+
val apiKey: MutableMap<String, String> = mutableMapOf()
|
|
36
|
+
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
|
|
37
|
+
var username: String? = null
|
|
38
|
+
var password: String? = null
|
|
39
|
+
var accessToken: String? = null
|
|
40
|
+
const val baseUrlKey = "org.openapitools.client.baseUrl"
|
|
41
|
+
|
|
42
|
+
@JvmStatic
|
|
43
|
+
val defaultClient: OkHttpClient by lazy {
|
|
44
|
+
builder.build()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@JvmStatic
|
|
48
|
+
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
|
|
53
|
+
*
|
|
54
|
+
* @param file The given file
|
|
55
|
+
* @return The guessed Content-Type
|
|
56
|
+
*/
|
|
57
|
+
protected fun guessContentTypeFromFile(file: File): String {
|
|
58
|
+
val contentType = URLConnection.guessContentTypeFromName(file.name)
|
|
59
|
+
return contentType ?: "application/octet-stream"
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
|
|
63
|
+
when {
|
|
64
|
+
content is File -> content.asRequestBody(
|
|
65
|
+
(mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
mediaType == FormDataMediaType ->
|
|
69
|
+
MultipartBody.Builder()
|
|
70
|
+
.setType(MultipartBody.FORM)
|
|
71
|
+
.apply {
|
|
72
|
+
// content's type *must* be Map<String, PartConfig<*>>
|
|
73
|
+
@Suppress("UNCHECKED_CAST")
|
|
74
|
+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
75
|
+
if (part.body is File) {
|
|
76
|
+
val partHeaders = part.headers.toMutableMap() +
|
|
77
|
+
("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"")
|
|
78
|
+
val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull()
|
|
79
|
+
addPart(
|
|
80
|
+
partHeaders.toHeaders(),
|
|
81
|
+
part.body.asRequestBody(fileMediaType)
|
|
82
|
+
)
|
|
83
|
+
} else {
|
|
84
|
+
val partHeaders = part.headers.toMutableMap() +
|
|
85
|
+
("Content-Disposition" to "form-data; name=\"$name\"")
|
|
86
|
+
addPart(
|
|
87
|
+
partHeaders.toHeaders(),
|
|
88
|
+
parameterToString(part.body).toRequestBody(null)
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}.build()
|
|
93
|
+
|
|
94
|
+
mediaType == FormUrlEncMediaType -> {
|
|
95
|
+
FormBody.Builder().apply {
|
|
96
|
+
// content's type *must* be Map<String, PartConfig<*>>
|
|
97
|
+
@Suppress("UNCHECKED_CAST")
|
|
98
|
+
(content as Map<String, PartConfig<*>>).forEach { (name, part) ->
|
|
99
|
+
add(name, parameterToString(part.body))
|
|
100
|
+
}
|
|
101
|
+
}.build()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
mediaType == null || mediaType.startsWith("application/") && mediaType.endsWith("json") ->
|
|
105
|
+
if (content == null) {
|
|
106
|
+
EMPTY_REQUEST
|
|
107
|
+
} else {
|
|
108
|
+
Serializer.jacksonObjectMapper.writeValueAsString(content)
|
|
109
|
+
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
|
|
113
|
+
// TODO: this should be extended with other serializers
|
|
114
|
+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
|
|
118
|
+
if (body == null) {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
if (T::class.java == File::class.java) {
|
|
122
|
+
// return tempFile
|
|
123
|
+
// Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options
|
|
124
|
+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
|
|
125
|
+
tempFile.deleteOnExit()
|
|
126
|
+
body.byteStream().use { inputStream ->
|
|
127
|
+
tempFile.outputStream().use { tempFileOutputStream ->
|
|
128
|
+
inputStream.copyTo(tempFileOutputStream)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return tempFile as T
|
|
132
|
+
}
|
|
133
|
+
val bodyContent = body.string()
|
|
134
|
+
if (bodyContent.isEmpty()) {
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
return when {
|
|
138
|
+
mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
|
|
139
|
+
Serializer.jacksonObjectMapper.readValue(bodyContent, object : TypeReference<T>() {})
|
|
140
|
+
|
|
141
|
+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
protected inline fun <reified I, reified T : Any?> request(requestConfig: RequestConfig<I>): ApiResponse<T?> {
|
|
146
|
+
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
|
|
147
|
+
|
|
148
|
+
val url = httpUrl.newBuilder()
|
|
149
|
+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
|
|
150
|
+
.apply {
|
|
151
|
+
requestConfig.query.forEach { query ->
|
|
152
|
+
query.value.forEach { queryValue ->
|
|
153
|
+
addQueryParameter(query.key, queryValue)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}.build()
|
|
157
|
+
|
|
158
|
+
// take content-type/accept from spec or set to default (application/json) if not defined
|
|
159
|
+
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
|
|
160
|
+
requestConfig.headers[ContentType] = JsonMediaType
|
|
161
|
+
}
|
|
162
|
+
if (requestConfig.headers[Accept].isNullOrEmpty()) {
|
|
163
|
+
requestConfig.headers[Accept] = JsonMediaType
|
|
164
|
+
}
|
|
165
|
+
val headers = requestConfig.headers
|
|
166
|
+
|
|
167
|
+
if (headers[Accept].isNullOrEmpty()) {
|
|
168
|
+
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
val contentType = if (headers[ContentType] != null) {
|
|
172
|
+
// TODO: support multiple contentType options here.
|
|
173
|
+
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
|
|
174
|
+
} else {
|
|
175
|
+
null
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
val request = when (requestConfig.method) {
|
|
179
|
+
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))
|
|
180
|
+
RequestMethod.GET -> Request.Builder().url(url)
|
|
181
|
+
RequestMethod.HEAD -> Request.Builder().url(url).head()
|
|
182
|
+
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(requestConfig.body, contentType))
|
|
183
|
+
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(requestConfig.body, contentType))
|
|
184
|
+
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(requestConfig.body, contentType))
|
|
185
|
+
RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
|
|
186
|
+
}.apply {
|
|
187
|
+
headers.forEach { header -> addHeader(header.key, header.value) }
|
|
188
|
+
}.build()
|
|
189
|
+
|
|
190
|
+
val response = client.newCall(request).execute()
|
|
191
|
+
|
|
192
|
+
val accept = response.header(ContentType)?.substringBefore(";")?.lowercase(Locale.getDefault())
|
|
193
|
+
|
|
194
|
+
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
|
|
195
|
+
return when {
|
|
196
|
+
response.isRedirect -> Redirection(
|
|
197
|
+
response.code,
|
|
198
|
+
response.headers.toMultimap()
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
response.isInformational -> Informational(
|
|
202
|
+
response.message,
|
|
203
|
+
response.code,
|
|
204
|
+
response.headers.toMultimap()
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
response.isSuccessful -> Success(
|
|
208
|
+
responseBody(response.body, accept),
|
|
209
|
+
response.code,
|
|
210
|
+
response.headers.toMultimap()
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
response.isClientError -> ClientError(
|
|
214
|
+
response.message,
|
|
215
|
+
response.body?.string(),
|
|
216
|
+
response.code,
|
|
217
|
+
response.headers.toMultimap()
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
else -> ServerError(
|
|
221
|
+
response.message,
|
|
222
|
+
response.body?.string(),
|
|
223
|
+
response.code,
|
|
224
|
+
response.headers.toMultimap()
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
protected fun parameterToString(value: Any?): String = when (value) {
|
|
230
|
+
null -> ""
|
|
231
|
+
is Array<*> -> toMultiValue(value, "csv").toString()
|
|
232
|
+
is Iterable<*> -> toMultiValue(value, "csv").toString()
|
|
233
|
+
is OffsetDateTime, is OffsetTime, is LocalDateTime, is LocalDate, is LocalTime ->
|
|
234
|
+
parseDateToQueryString(value)
|
|
235
|
+
|
|
236
|
+
else -> value.toString()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
protected inline fun <reified T : Any> parseDateToQueryString(value: T): String {
|
|
240
|
+
/*
|
|
241
|
+
.replace("\"", "") converts the json object string to an actual string for the query parameter.
|
|
242
|
+
The moshi or gson adapter allows a more generic solution instead of trying to use a native
|
|
243
|
+
formatter. It also easily allows to provide a simple way to define a custom date format pattern
|
|
244
|
+
inside a gson/moshi adapter.
|
|
245
|
+
*/
|
|
246
|
+
return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "")
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
enum class ResponseType {
|
|
4
|
+
Success, Informational, Redirection, ClientError, ServerError
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface Response
|
|
8
|
+
|
|
9
|
+
abstract class ApiResponse<T>(val responseType: ResponseType) : Response {
|
|
10
|
+
abstract val statusCode: Int
|
|
11
|
+
abstract val headers: Map<String, List<String>>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class Success<T>(
|
|
15
|
+
val data: T,
|
|
16
|
+
override val statusCode: Int = -1,
|
|
17
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
18
|
+
) : ApiResponse<T>(ResponseType.Success)
|
|
19
|
+
|
|
20
|
+
class Informational<T>(
|
|
21
|
+
val statusText: String,
|
|
22
|
+
override val statusCode: Int = -1,
|
|
23
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
24
|
+
) : ApiResponse<T>(ResponseType.Informational)
|
|
25
|
+
|
|
26
|
+
class Redirection<T>(
|
|
27
|
+
override val statusCode: Int = -1,
|
|
28
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
29
|
+
) : ApiResponse<T>(ResponseType.Redirection)
|
|
30
|
+
|
|
31
|
+
class ClientError<T>(
|
|
32
|
+
val message: String? = null,
|
|
33
|
+
val body: Any? = null,
|
|
34
|
+
override val statusCode: Int = -1,
|
|
35
|
+
override val headers: Map<String, List<String>> = mapOf()
|
|
36
|
+
) : ApiResponse<T>(ResponseType.ClientError)
|
|
37
|
+
|
|
38
|
+
class ServerError<T>(
|
|
39
|
+
val message: String? = null,
|
|
40
|
+
val body: Any? = null,
|
|
41
|
+
override val statusCode: Int = -1,
|
|
42
|
+
override val headers: Map<String, List<String>>
|
|
43
|
+
) : ApiResponse<T>(ResponseType.ServerError)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
@file:Suppress("unused")
|
|
2
|
+
|
|
3
|
+
package @PACKAGE_NAME@
|
|
4
|
+
|
|
5
|
+
import java.lang.RuntimeException
|
|
6
|
+
|
|
7
|
+
open class ClientException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
8
|
+
RuntimeException(message) {
|
|
9
|
+
|
|
10
|
+
companion object {
|
|
11
|
+
private const val serialVersionUID: Long = 123L
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
open class ServerException(message: kotlin.String? = null, val statusCode: Int = -1, val response: Response? = null) :
|
|
16
|
+
RuntimeException(message) {
|
|
17
|
+
|
|
18
|
+
companion object {
|
|
19
|
+
private const val serialVersionUID: Long = 456L
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defines a config object for a given part of a multi-part request.
|
|
5
|
+
* NOTE: Headers is a Map<String,String> because rfc2616 defines
|
|
6
|
+
* multi-valued headers as csv-only.
|
|
7
|
+
*/
|
|
8
|
+
data class PartConfig<T>(
|
|
9
|
+
val headers: MutableMap<String, String> = mutableMapOf(),
|
|
10
|
+
val body: T? = null
|
|
11
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defines a config object for a given request.
|
|
5
|
+
* NOTE: This object doesn't include 'body' because it
|
|
6
|
+
* allows for caching of the constructed object
|
|
7
|
+
* for many request definitions.
|
|
8
|
+
* NOTE: Headers is a Map<String,String> because rfc2616 defines
|
|
9
|
+
* multi-valued headers as csv-only.
|
|
10
|
+
*/
|
|
11
|
+
data class RequestConfig<T>(
|
|
12
|
+
val method: RequestMethod,
|
|
13
|
+
val path: String,
|
|
14
|
+
val headers: MutableMap<String, String> = mutableMapOf(),
|
|
15
|
+
val query: MutableMap<String, List<String>> = mutableMapOf(),
|
|
16
|
+
val requiresAuthentication: Boolean,
|
|
17
|
+
val body: T? = null
|
|
18
|
+
)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
import okhttp3.Response
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Provides an extension to evaluation whether the response is a 1xx code
|
|
7
|
+
*/
|
|
8
|
+
val Response.isInformational: Boolean get() = this.code in 100..199
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Provides an extension to evaluation whether the response is a 3xx code
|
|
12
|
+
*/
|
|
13
|
+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
|
14
|
+
val Response.isRedirect: Boolean get() = this.code in 300..399
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides an extension to evaluation whether the response is a 4xx code
|
|
18
|
+
*/
|
|
19
|
+
val Response.isClientError: Boolean get() = this.code in 400..499
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
|
|
23
|
+
*/
|
|
24
|
+
val Response.isServerError: Boolean get() = this.code in 500..999
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
package @PACKAGE_NAME@
|
|
2
|
+
|
|
3
|
+
import com.fasterxml.jackson.annotation.JsonInclude
|
|
4
|
+
import com.fasterxml.jackson.databind.ObjectMapper
|
|
5
|
+
import com.fasterxml.jackson.databind.SerializationFeature
|
|
6
|
+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
|
7
|
+
|
|
8
|
+
object Serializer {
|
|
9
|
+
@JvmStatic
|
|
10
|
+
val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
|
|
11
|
+
.findAndRegisterModules()
|
|
12
|
+
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
|
|
13
|
+
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
|
|
14
|
+
}
|
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defaultKotlinOkHttp3ClientsGeneratorConfig = void 0;
|
|
4
4
|
const config_1 = require("../../../config");
|
|
5
|
-
exports.defaultKotlinOkHttp3ClientsGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.client', infrastructurePackageName: '
|
|
5
|
+
exports.defaultKotlinOkHttp3ClientsGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.api.client', infrastructurePackageName: { mode: 'append-full-package-name', value: '.infrastructure' } });
|
|
@@ -10,15 +10,14 @@ const utils_1 = require("../../../utils");
|
|
|
10
10
|
const file_generator_1 = require("../../file-generator");
|
|
11
11
|
class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator {
|
|
12
12
|
generate(ctx) {
|
|
13
|
-
const packageName = this.getPackageName(ctx);
|
|
14
13
|
const typeName = this.getApiClientName(ctx);
|
|
15
|
-
const filePath = this.getFilePath(ctx
|
|
14
|
+
const filePath = this.getFilePath(ctx);
|
|
16
15
|
(0, fs_extra_1.ensureDirSync)((0, path_1.dirname)(filePath));
|
|
17
16
|
console.log(`Generating client for service ${ctx.service.name} to ${filePath}...`);
|
|
18
|
-
const builder = new file_builder_1.KotlinFileBuilder(packageName, ctx.config);
|
|
17
|
+
const builder = new file_builder_1.KotlinFileBuilder(ctx.packageName, ctx.config);
|
|
19
18
|
this.generateApiClientFileContent(ctx, builder);
|
|
20
19
|
(0, fs_1.writeFileSync)(filePath, builder.toString());
|
|
21
|
-
return { typeName, packageName };
|
|
20
|
+
return { typeName, packageName: ctx.packageName };
|
|
22
21
|
}
|
|
23
22
|
generateApiClientFileContent(ctx, builder) {
|
|
24
23
|
builder
|
|
@@ -39,7 +38,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
39
38
|
.appendLine('basePath: String = defaultBasePath,')
|
|
40
39
|
.appendLine('client: OkHttpClient = ApiClient.defaultClient')
|
|
41
40
|
.addImport('OkHttpClient', 'okhttp3')
|
|
42
|
-
.addImport('ApiClient', ctx.
|
|
41
|
+
.addImport('ApiClient', ctx.infrastructurePackageName))
|
|
43
42
|
.append(' : ')
|
|
44
43
|
.append('ApiClient(basePath, client)');
|
|
45
44
|
}
|
|
@@ -70,7 +69,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
70
69
|
.append('val defaultBasePath: String by lazy ')
|
|
71
70
|
.parenthesizeMultiline('{}', (builder) => builder
|
|
72
71
|
.appendLine(`System.getProperties().getProperty(ApiClient.baseUrlKey, ${this.toStringLiteral(ctx, this.getBasePath(ctx))})`)
|
|
73
|
-
.addImport('ApiClient', ctx.
|
|
72
|
+
.addImport('ApiClient', ctx.infrastructurePackageName));
|
|
74
73
|
}
|
|
75
74
|
generateApiClientMethod(ctx, builder, endpoint) {
|
|
76
75
|
builder
|
|
@@ -102,9 +101,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
102
101
|
.appendLine(' */');
|
|
103
102
|
}
|
|
104
103
|
generateApiClientMethodAnnotations(ctx, builder, endpoint) {
|
|
105
|
-
const responseSchema = this.getResponseSchema(ctx, endpoint);
|
|
106
104
|
builder
|
|
107
|
-
.applyIf(responseSchema !== undefined, (builder) => builder.appendAnnotation('Supress', undefined, ['"UNCHECKED_CAST"']))
|
|
108
105
|
.appendAnnotation('Throws', undefined, [
|
|
109
106
|
'IllegalStateException::class',
|
|
110
107
|
'IOException::class',
|
|
@@ -113,8 +110,8 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
113
110
|
'ServerException::class',
|
|
114
111
|
])
|
|
115
112
|
.addImport('IOException', 'java.io')
|
|
116
|
-
.addImport('ClientException', ctx.
|
|
117
|
-
.addImport('ServerException', ctx.
|
|
113
|
+
.addImport('ClientException', ctx.infrastructurePackageName)
|
|
114
|
+
.addImport('ServerException', ctx.infrastructurePackageName);
|
|
118
115
|
}
|
|
119
116
|
generateApiClientMethodSignature(ctx, builder, endpoint) {
|
|
120
117
|
builder
|
|
@@ -133,7 +130,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
133
130
|
generateApiClientMethodContent(ctx, builder, endpoint) {
|
|
134
131
|
const responseSchema = this.getResponseSchema(ctx, endpoint);
|
|
135
132
|
builder
|
|
136
|
-
.append(`val localVarResponse = ${(0, core_1.toCasing)(endpoint.name, 'camel')}
|
|
133
|
+
.append(`val localVarResponse = ${(0, core_1.toCasing)(endpoint.name, 'camel')}WithHttpInfo`)
|
|
137
134
|
.parenthesize('()', (builder) => this.generateParams(ctx, builder, endpoint, false))
|
|
138
135
|
.appendLine()
|
|
139
136
|
.appendLine()
|
|
@@ -142,13 +139,13 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
142
139
|
.append('ResponseType.Success -> ')
|
|
143
140
|
.applyIfElse(responseSchema === undefined, (builder) => builder.append('Unit'), (builder) => builder
|
|
144
141
|
.append('(localVarResponse as Success<*>).data as ')
|
|
145
|
-
.addImport('Success', ctx.
|
|
142
|
+
.addImport('Success', ctx.infrastructurePackageName)
|
|
146
143
|
.apply((builder) => this.generateTypeUsage(ctx, builder, responseSchema)))
|
|
147
144
|
.ensureCurrentLineEmpty()
|
|
148
145
|
.appendLine(responseErrorHandlingCode)
|
|
149
|
-
.addImport('ClientError', ctx.
|
|
150
|
-
.addImport('ServerError', ctx.
|
|
151
|
-
.addImport('ResponseType', ctx.
|
|
146
|
+
.addImport('ClientError', ctx.infrastructurePackageName)
|
|
147
|
+
.addImport('ServerError', ctx.infrastructurePackageName)
|
|
148
|
+
.addImport('ResponseType', ctx.infrastructurePackageName));
|
|
152
149
|
}
|
|
153
150
|
generateApiClientHttpInfoMethod(ctx, builder, endpoint) {
|
|
154
151
|
builder
|
|
@@ -187,7 +184,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
187
184
|
generateApiClientHttpInfoMethodSignature(ctx, builder, endpoint) {
|
|
188
185
|
builder
|
|
189
186
|
.append('fun ')
|
|
190
|
-
.append((0, core_1.toCasing)(endpoint.name, 'camel'), '
|
|
187
|
+
.append((0, core_1.toCasing)(endpoint.name, 'camel'), 'WithHttpInfo')
|
|
191
188
|
.parenthesize('()', (builder) => this.generateApiClientHttpInfoMethodSignatureParameters(ctx, builder, endpoint))
|
|
192
189
|
.append(': ')
|
|
193
190
|
.apply((builder) => this.generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint));
|
|
@@ -198,8 +195,8 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
198
195
|
generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint) {
|
|
199
196
|
builder
|
|
200
197
|
.append('ApiResponse')
|
|
201
|
-
.addImport('ApiResponse', ctx.
|
|
202
|
-
.parenthesize('<>', (builder) => this.generateTypeUsage(ctx, builder, this.getResponseSchema(ctx, endpoint), 'Unit'));
|
|
198
|
+
.addImport('ApiResponse', ctx.infrastructurePackageName)
|
|
199
|
+
.parenthesize('<>', (builder) => this.generateTypeUsage(ctx, builder, this.getResponseSchema(ctx, endpoint), 'Unit', true));
|
|
203
200
|
}
|
|
204
201
|
generateApiClientHttpInfoMethodContent(ctx, builder, endpoint) {
|
|
205
202
|
builder
|
|
@@ -250,29 +247,34 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
250
247
|
generateApiClientRequestConfigMethodReturnType(ctx, builder, endpoint) {
|
|
251
248
|
builder
|
|
252
249
|
.append('RequestConfig')
|
|
253
|
-
.addImport('RequestConfig', ctx.
|
|
250
|
+
.addImport('RequestConfig', ctx.infrastructurePackageName)
|
|
254
251
|
.parenthesize('<>', (builder) => { var _a; return this.generateTypeUsage(ctx, builder, (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0].schema, 'Unit'); });
|
|
255
252
|
}
|
|
256
253
|
generateApiClientRequestConfigMethodContent(ctx, builder, endpoint) {
|
|
257
254
|
var _a, _b;
|
|
255
|
+
const queryParameters = endpoint.parameters.filter((x) => x.target === 'query');
|
|
258
256
|
builder
|
|
259
|
-
.
|
|
260
|
-
.appendLine('val localVariableQuery: MultiValueMap = mutableMapOf()')
|
|
261
|
-
.addImport('MultiValueMap', ctx.
|
|
262
|
-
.
|
|
257
|
+
.appendLineIf(!!endpoint.requestBody, `val localVariableBody = ${(0, core_1.toCasing)(this.getRequestBodyParamName(ctx, endpoint), 'camel')}`)
|
|
258
|
+
.appendLine('val localVariableQuery: MultiValueMap = mutableMapOf<String, List<String>>()')
|
|
259
|
+
.addImport('MultiValueMap', ctx.infrastructurePackageName)
|
|
260
|
+
.applyIf(queryParameters.length > 0, (builder) => builder.indent((builder) => builder.append('.apply ').parenthesizeMultiline('{}', (builder) => builder.forEach(queryParameters, (builder, param) => builder
|
|
261
|
+
.appendIf(!param.required, `if (${(0, core_1.toCasing)(param.name, 'camel')} != null) `)
|
|
262
|
+
.parenthesizeMultilineIf(!param.required, '{}', (builder) => builder.appendLine(`put(${this.toStringLiteral(ctx, (0, core_1.toCasing)(param.name, 'camel'))}, listOf(${(0, core_1.toCasing)(param.name, 'camel')}.toString()))`))
|
|
263
|
+
.appendLine()))))
|
|
264
|
+
.ensureCurrentLineEmpty()
|
|
263
265
|
.appendLine('val localVariableHeaders: MutableMap<String, String> = mutableMapOf()')
|
|
264
266
|
.appendLineIf(((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) !== undefined, `localVariableHeaders["Content-Type"] = "${(_b = endpoint.requestBody) === null || _b === void 0 ? void 0 : _b.content[0].type}"`)
|
|
265
267
|
.appendLine()
|
|
266
268
|
.append('return RequestConfig')
|
|
267
|
-
.addImport('RequestConfig', ctx.
|
|
269
|
+
.addImport('RequestConfig', ctx.infrastructurePackageName)
|
|
268
270
|
.parenthesizeMultiline('()', (builder) => builder
|
|
269
271
|
.appendLine(`method = RequestMethod.${endpoint.method.toUpperCase()},`)
|
|
270
|
-
.addImport('RequestMethod', ctx.
|
|
272
|
+
.addImport('RequestMethod', ctx.infrastructurePackageName)
|
|
271
273
|
.appendLine(`path = "${this.getPathWithInterpolation(ctx, endpoint)}",`)
|
|
272
274
|
.appendLine('query = localVariableQuery,')
|
|
273
275
|
.appendLine('headers = localVariableHeaders,')
|
|
274
276
|
.appendLine('requiresAuthentication = false,')
|
|
275
|
-
.
|
|
277
|
+
.appendLineIf(!!endpoint.requestBody, 'body = localVariableBody'));
|
|
276
278
|
}
|
|
277
279
|
generateAdditionalMethods(ctx, builder) {
|
|
278
280
|
this.generateEncodeUriComponentMethod(ctx, builder);
|
|
@@ -294,21 +296,26 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
294
296
|
.apply((builder) => this.generateTypeUsage(ctx, builder, parameter.schema))
|
|
295
297
|
.appendIf(!parameter.required, '? = ', this.getDefaultValue(ctx, parameter.schema))));
|
|
296
298
|
}
|
|
297
|
-
generateTypeUsage(ctx, builder, schema, fallback) {
|
|
299
|
+
generateTypeUsage(ctx, builder, schema, fallback, nullable) {
|
|
298
300
|
if (schema && schema.kind === 'array') {
|
|
299
301
|
const schemaInfo = this.getSchemaInfo(ctx, schema.items);
|
|
300
|
-
builder.append(`List<${schemaInfo.typeName}
|
|
302
|
+
builder.append(this.getTypeNameWithNullability(`List<${schemaInfo.typeName}>`, nullable));
|
|
301
303
|
builder.imports.addImports([schemaInfo, ...schemaInfo.additionalImports]);
|
|
302
304
|
}
|
|
303
305
|
else if (schema || !fallback) {
|
|
304
306
|
const schemaInfo = this.getSchemaInfo(ctx, schema);
|
|
305
|
-
builder.append(schemaInfo.typeName);
|
|
307
|
+
builder.append(this.getTypeNameWithNullability(schemaInfo.typeName, nullable));
|
|
306
308
|
builder.imports.addImports([schemaInfo, ...schemaInfo.additionalImports]);
|
|
307
309
|
}
|
|
308
310
|
else {
|
|
309
|
-
builder.append(fallback);
|
|
311
|
+
builder.append(this.getTypeNameWithNullability(fallback, nullable));
|
|
310
312
|
}
|
|
311
313
|
}
|
|
314
|
+
getTypeNameWithNullability(typeName, nullable) {
|
|
315
|
+
if (nullable === undefined)
|
|
316
|
+
return typeName;
|
|
317
|
+
return nullable ? `${typeName}?` : typeName.match(/^(.*?)\??$/)[1];
|
|
318
|
+
}
|
|
312
319
|
getDefaultValue(ctx, schema) {
|
|
313
320
|
if (!(schema === null || schema === void 0 ? void 0 : schema.default)) {
|
|
314
321
|
return 'null';
|
|
@@ -324,7 +331,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
324
331
|
}
|
|
325
332
|
}
|
|
326
333
|
getPathWithInterpolation(ctx, endpoint) {
|
|
327
|
-
let path = endpoint
|
|
334
|
+
let path = this.getEndpointPath(ctx, endpoint);
|
|
328
335
|
endpoint.parameters
|
|
329
336
|
.filter((x) => x.target === 'path')
|
|
330
337
|
.forEach((parameter) => {
|
|
@@ -370,13 +377,13 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
370
377
|
}
|
|
371
378
|
getBasePath(ctx) {
|
|
372
379
|
var _a, _b, _c, _d, _e, _f;
|
|
373
|
-
return (_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/';
|
|
380
|
+
return (0, utils_1.modifyString)((_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/', ctx.config.basePath, ctx.service);
|
|
374
381
|
}
|
|
375
|
-
|
|
376
|
-
return
|
|
382
|
+
getEndpointPath(ctx, endpoint) {
|
|
383
|
+
return (0, utils_1.modifyString)(endpoint.path, ctx.config.pathModifier, endpoint);
|
|
377
384
|
}
|
|
378
|
-
|
|
379
|
-
return ctx.config.packageName
|
|
385
|
+
getFilePath(ctx) {
|
|
386
|
+
return `${ctx.config.outputDir}/${ctx.packageName.replace(/\./g, '/')}/${this.getApiClientName(ctx)}.kt`;
|
|
380
387
|
}
|
|
381
388
|
getApiClientName(ctx) {
|
|
382
389
|
return (0, core_1.toCasing)(ctx.service.name, 'pascal') + 'ApiClient';
|
|
@@ -30,17 +30,36 @@ class KotlinOkHttp3ClientsGenerator extends core_1.OpenApiServicesGenerationProv
|
|
|
30
30
|
}
|
|
31
31
|
buildContext(context, config) {
|
|
32
32
|
context.data.services = context.data.services.filter((x) => x.name !== 'exclude-from-generation');
|
|
33
|
-
|
|
33
|
+
const providerContext = this.getProviderContext(context, config, models_1.defaultKotlinOkHttp3ClientsGeneratorConfig);
|
|
34
|
+
return Object.assign(providerContext, {
|
|
35
|
+
packageName: this.getPackageName(providerContext.config),
|
|
36
|
+
infrastructurePackageName: this.getInfrastructurePackageName(providerContext.config),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
getInfrastructurePackageName(config) {
|
|
40
|
+
if (typeof config.infrastructurePackageName === 'string') {
|
|
41
|
+
return config.infrastructurePackageName;
|
|
42
|
+
}
|
|
43
|
+
if (config.infrastructurePackageName.mode === 'append-package-name') {
|
|
44
|
+
return config.packageName + config.infrastructurePackageName.value;
|
|
45
|
+
}
|
|
46
|
+
if (config.infrastructurePackageName.mode === 'append-full-package-name') {
|
|
47
|
+
return this.getPackageName(config) + config.infrastructurePackageName.value;
|
|
48
|
+
}
|
|
49
|
+
return config.infrastructurePackageName.value;
|
|
50
|
+
}
|
|
51
|
+
getPackageName(config) {
|
|
52
|
+
return config.packageName + config.packageSuffix;
|
|
34
53
|
}
|
|
35
54
|
copyInfrastructureFiles(ctx) {
|
|
36
55
|
const sourceDir = (0, path_1.resolve)((0, path_1.dirname)(require.resolve('@goast/kotlin')), '../assets/client/okhttp3');
|
|
37
|
-
const targetDir = (0, path_1.resolve)(ctx.config.outputDir, ctx.
|
|
56
|
+
const targetDir = (0, path_1.resolve)(ctx.config.outputDir, ctx.infrastructurePackageName.replace(/\./g, '/'));
|
|
38
57
|
(0, fs_extra_1.ensureDirSync)(targetDir);
|
|
39
58
|
const files = (0, fs_extra_1.readdirSync)(sourceDir);
|
|
40
59
|
for (const file of files) {
|
|
41
60
|
const fileContent = (0, fs_extra_1.readFileSync)((0, path_1.resolve)(sourceDir, file))
|
|
42
61
|
.toString()
|
|
43
|
-
.replace(/@PACKAGE_NAME@/g, ctx.
|
|
62
|
+
.replace(/@PACKAGE_NAME@/g, ctx.infrastructurePackageName);
|
|
44
63
|
(0, fs_1.writeFileSync)((0, path_1.resolve)(targetDir, file), fileContent);
|
|
45
64
|
}
|
|
46
65
|
}
|
|
@@ -4,6 +4,7 @@ exports.DefaultKotlinSpringControllerGenerator = void 0;
|
|
|
4
4
|
const fs_extra_1 = require("fs-extra");
|
|
5
5
|
const core_1 = require("@goast/core");
|
|
6
6
|
const file_builder_1 = require("../../../file-builder");
|
|
7
|
+
const utils_1 = require("../../../utils");
|
|
7
8
|
const file_generator_1 = require("../../file-generator");
|
|
8
9
|
class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFileGenerator {
|
|
9
10
|
generate(ctx) {
|
|
@@ -88,7 +89,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
88
89
|
])
|
|
89
90
|
.appendAnnotation('RequestMapping', 'org.springframework.web.bind.annotation', [
|
|
90
91
|
['method', '[RequestMethod.' + endpoint.method.toUpperCase() + ']'],
|
|
91
|
-
['value', '[' + this.toStringLiteral(ctx, endpoint
|
|
92
|
+
['value', '[' + this.toStringLiteral(ctx, this.getEndpointPath(ctx, endpoint)) + ']'],
|
|
92
93
|
[
|
|
93
94
|
'consumes',
|
|
94
95
|
'[' + ((_c = endpoint.requestBody) === null || _c === void 0 ? void 0 : _c.content.map((x) => this.toStringLiteral(ctx, x.type)).join(', ')) + ']',
|
|
@@ -330,11 +331,17 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
330
331
|
}
|
|
331
332
|
}
|
|
332
333
|
getControllerRequestMapping(ctx, prefix) {
|
|
333
|
-
|
|
334
|
-
const basePath = (_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/';
|
|
334
|
+
const basePath = this.getBasePath(ctx);
|
|
335
335
|
prefix !== null && prefix !== void 0 ? prefix : (prefix = `openapi.${(0, core_1.toCasing)(ctx.service.name, 'camel')}`);
|
|
336
336
|
return this.toStringLiteral(ctx, `\${${prefix}.base-path:${basePath}}`);
|
|
337
337
|
}
|
|
338
|
+
getBasePath(ctx) {
|
|
339
|
+
var _a, _b, _c, _d, _e, _f;
|
|
340
|
+
return (0, utils_1.modifyString)((_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/', ctx.config.basePath, ctx.service);
|
|
341
|
+
}
|
|
342
|
+
getEndpointPath(ctx, endpoint) {
|
|
343
|
+
return (0, utils_1.modifyString)(endpoint.path, ctx.config.pathModifier, endpoint);
|
|
344
|
+
}
|
|
338
345
|
getDirectoryPath(ctx, packageName) {
|
|
339
346
|
return `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}`;
|
|
340
347
|
}
|
package/cjs/lib/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toKotlinPropertyName = exports.toKotlinStringLiteral = void 0;
|
|
3
|
+
exports.modifyString = exports.toKotlinPropertyName = exports.toKotlinStringLiteral = void 0;
|
|
4
4
|
function toKotlinStringLiteral(value) {
|
|
5
5
|
if (!value) {
|
|
6
6
|
return '""';
|
|
@@ -20,3 +20,18 @@ function toKotlinPropertyName(value) {
|
|
|
20
20
|
return `\`${value}\``;
|
|
21
21
|
}
|
|
22
22
|
exports.toKotlinPropertyName = toKotlinPropertyName;
|
|
23
|
+
function modifyString(value, modifier, ...args) {
|
|
24
|
+
var _a;
|
|
25
|
+
if (typeof modifier === 'string') {
|
|
26
|
+
return modifier;
|
|
27
|
+
}
|
|
28
|
+
if (modifier instanceof RegExp) {
|
|
29
|
+
const match = value.match(modifier);
|
|
30
|
+
return (_a = match === null || match === void 0 ? void 0 : match[0]) !== null && _a !== void 0 ? _a : value;
|
|
31
|
+
}
|
|
32
|
+
if (typeof modifier === 'function') {
|
|
33
|
+
return modifier(value, ...args);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
exports.modifyString = modifyString;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { defaultKotlinGeneratorConfig } from '../../../config';
|
|
2
|
-
export const defaultKotlinOkHttp3ClientsGeneratorConfig = Object.assign(Object.assign({}, defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.client', infrastructurePackageName: '
|
|
2
|
+
export const defaultKotlinOkHttp3ClientsGeneratorConfig = Object.assign(Object.assign({}, defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.api.client', infrastructurePackageName: { mode: 'append-full-package-name', value: '.infrastructure' } });
|
|
@@ -3,19 +3,18 @@ import { dirname } from 'path';
|
|
|
3
3
|
import { ensureDirSync } from 'fs-extra';
|
|
4
4
|
import { toCasing } from '@goast/core';
|
|
5
5
|
import { KotlinFileBuilder } from '../../../file-builder';
|
|
6
|
-
import { toKotlinStringLiteral } from '../../../utils';
|
|
6
|
+
import { modifyString, toKotlinStringLiteral } from '../../../utils';
|
|
7
7
|
import { KotlinFileGenerator } from '../../file-generator';
|
|
8
8
|
export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
9
9
|
generate(ctx) {
|
|
10
|
-
const packageName = this.getPackageName(ctx);
|
|
11
10
|
const typeName = this.getApiClientName(ctx);
|
|
12
|
-
const filePath = this.getFilePath(ctx
|
|
11
|
+
const filePath = this.getFilePath(ctx);
|
|
13
12
|
ensureDirSync(dirname(filePath));
|
|
14
13
|
console.log(`Generating client for service ${ctx.service.name} to ${filePath}...`);
|
|
15
|
-
const builder = new KotlinFileBuilder(packageName, ctx.config);
|
|
14
|
+
const builder = new KotlinFileBuilder(ctx.packageName, ctx.config);
|
|
16
15
|
this.generateApiClientFileContent(ctx, builder);
|
|
17
16
|
writeFileSync(filePath, builder.toString());
|
|
18
|
-
return { typeName, packageName };
|
|
17
|
+
return { typeName, packageName: ctx.packageName };
|
|
19
18
|
}
|
|
20
19
|
generateApiClientFileContent(ctx, builder) {
|
|
21
20
|
builder
|
|
@@ -36,7 +35,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
36
35
|
.appendLine('basePath: String = defaultBasePath,')
|
|
37
36
|
.appendLine('client: OkHttpClient = ApiClient.defaultClient')
|
|
38
37
|
.addImport('OkHttpClient', 'okhttp3')
|
|
39
|
-
.addImport('ApiClient', ctx.
|
|
38
|
+
.addImport('ApiClient', ctx.infrastructurePackageName))
|
|
40
39
|
.append(' : ')
|
|
41
40
|
.append('ApiClient(basePath, client)');
|
|
42
41
|
}
|
|
@@ -67,7 +66,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
67
66
|
.append('val defaultBasePath: String by lazy ')
|
|
68
67
|
.parenthesizeMultiline('{}', (builder) => builder
|
|
69
68
|
.appendLine(`System.getProperties().getProperty(ApiClient.baseUrlKey, ${this.toStringLiteral(ctx, this.getBasePath(ctx))})`)
|
|
70
|
-
.addImport('ApiClient', ctx.
|
|
69
|
+
.addImport('ApiClient', ctx.infrastructurePackageName));
|
|
71
70
|
}
|
|
72
71
|
generateApiClientMethod(ctx, builder, endpoint) {
|
|
73
72
|
builder
|
|
@@ -99,9 +98,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
99
98
|
.appendLine(' */');
|
|
100
99
|
}
|
|
101
100
|
generateApiClientMethodAnnotations(ctx, builder, endpoint) {
|
|
102
|
-
const responseSchema = this.getResponseSchema(ctx, endpoint);
|
|
103
101
|
builder
|
|
104
|
-
.applyIf(responseSchema !== undefined, (builder) => builder.appendAnnotation('Supress', undefined, ['"UNCHECKED_CAST"']))
|
|
105
102
|
.appendAnnotation('Throws', undefined, [
|
|
106
103
|
'IllegalStateException::class',
|
|
107
104
|
'IOException::class',
|
|
@@ -110,8 +107,8 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
110
107
|
'ServerException::class',
|
|
111
108
|
])
|
|
112
109
|
.addImport('IOException', 'java.io')
|
|
113
|
-
.addImport('ClientException', ctx.
|
|
114
|
-
.addImport('ServerException', ctx.
|
|
110
|
+
.addImport('ClientException', ctx.infrastructurePackageName)
|
|
111
|
+
.addImport('ServerException', ctx.infrastructurePackageName);
|
|
115
112
|
}
|
|
116
113
|
generateApiClientMethodSignature(ctx, builder, endpoint) {
|
|
117
114
|
builder
|
|
@@ -130,7 +127,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
130
127
|
generateApiClientMethodContent(ctx, builder, endpoint) {
|
|
131
128
|
const responseSchema = this.getResponseSchema(ctx, endpoint);
|
|
132
129
|
builder
|
|
133
|
-
.append(`val localVarResponse = ${toCasing(endpoint.name, 'camel')}
|
|
130
|
+
.append(`val localVarResponse = ${toCasing(endpoint.name, 'camel')}WithHttpInfo`)
|
|
134
131
|
.parenthesize('()', (builder) => this.generateParams(ctx, builder, endpoint, false))
|
|
135
132
|
.appendLine()
|
|
136
133
|
.appendLine()
|
|
@@ -139,13 +136,13 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
139
136
|
.append('ResponseType.Success -> ')
|
|
140
137
|
.applyIfElse(responseSchema === undefined, (builder) => builder.append('Unit'), (builder) => builder
|
|
141
138
|
.append('(localVarResponse as Success<*>).data as ')
|
|
142
|
-
.addImport('Success', ctx.
|
|
139
|
+
.addImport('Success', ctx.infrastructurePackageName)
|
|
143
140
|
.apply((builder) => this.generateTypeUsage(ctx, builder, responseSchema)))
|
|
144
141
|
.ensureCurrentLineEmpty()
|
|
145
142
|
.appendLine(responseErrorHandlingCode)
|
|
146
|
-
.addImport('ClientError', ctx.
|
|
147
|
-
.addImport('ServerError', ctx.
|
|
148
|
-
.addImport('ResponseType', ctx.
|
|
143
|
+
.addImport('ClientError', ctx.infrastructurePackageName)
|
|
144
|
+
.addImport('ServerError', ctx.infrastructurePackageName)
|
|
145
|
+
.addImport('ResponseType', ctx.infrastructurePackageName));
|
|
149
146
|
}
|
|
150
147
|
generateApiClientHttpInfoMethod(ctx, builder, endpoint) {
|
|
151
148
|
builder
|
|
@@ -184,7 +181,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
184
181
|
generateApiClientHttpInfoMethodSignature(ctx, builder, endpoint) {
|
|
185
182
|
builder
|
|
186
183
|
.append('fun ')
|
|
187
|
-
.append(toCasing(endpoint.name, 'camel'), '
|
|
184
|
+
.append(toCasing(endpoint.name, 'camel'), 'WithHttpInfo')
|
|
188
185
|
.parenthesize('()', (builder) => this.generateApiClientHttpInfoMethodSignatureParameters(ctx, builder, endpoint))
|
|
189
186
|
.append(': ')
|
|
190
187
|
.apply((builder) => this.generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint));
|
|
@@ -195,8 +192,8 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
195
192
|
generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint) {
|
|
196
193
|
builder
|
|
197
194
|
.append('ApiResponse')
|
|
198
|
-
.addImport('ApiResponse', ctx.
|
|
199
|
-
.parenthesize('<>', (builder) => this.generateTypeUsage(ctx, builder, this.getResponseSchema(ctx, endpoint), 'Unit'));
|
|
195
|
+
.addImport('ApiResponse', ctx.infrastructurePackageName)
|
|
196
|
+
.parenthesize('<>', (builder) => this.generateTypeUsage(ctx, builder, this.getResponseSchema(ctx, endpoint), 'Unit', true));
|
|
200
197
|
}
|
|
201
198
|
generateApiClientHttpInfoMethodContent(ctx, builder, endpoint) {
|
|
202
199
|
builder
|
|
@@ -247,29 +244,34 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
247
244
|
generateApiClientRequestConfigMethodReturnType(ctx, builder, endpoint) {
|
|
248
245
|
builder
|
|
249
246
|
.append('RequestConfig')
|
|
250
|
-
.addImport('RequestConfig', ctx.
|
|
247
|
+
.addImport('RequestConfig', ctx.infrastructurePackageName)
|
|
251
248
|
.parenthesize('<>', (builder) => { var _a; return this.generateTypeUsage(ctx, builder, (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0].schema, 'Unit'); });
|
|
252
249
|
}
|
|
253
250
|
generateApiClientRequestConfigMethodContent(ctx, builder, endpoint) {
|
|
254
251
|
var _a, _b;
|
|
252
|
+
const queryParameters = endpoint.parameters.filter((x) => x.target === 'query');
|
|
255
253
|
builder
|
|
256
|
-
.
|
|
257
|
-
.appendLine('val localVariableQuery: MultiValueMap = mutableMapOf()')
|
|
258
|
-
.addImport('MultiValueMap', ctx.
|
|
259
|
-
.
|
|
254
|
+
.appendLineIf(!!endpoint.requestBody, `val localVariableBody = ${toCasing(this.getRequestBodyParamName(ctx, endpoint), 'camel')}`)
|
|
255
|
+
.appendLine('val localVariableQuery: MultiValueMap = mutableMapOf<String, List<String>>()')
|
|
256
|
+
.addImport('MultiValueMap', ctx.infrastructurePackageName)
|
|
257
|
+
.applyIf(queryParameters.length > 0, (builder) => builder.indent((builder) => builder.append('.apply ').parenthesizeMultiline('{}', (builder) => builder.forEach(queryParameters, (builder, param) => builder
|
|
258
|
+
.appendIf(!param.required, `if (${toCasing(param.name, 'camel')} != null) `)
|
|
259
|
+
.parenthesizeMultilineIf(!param.required, '{}', (builder) => builder.appendLine(`put(${this.toStringLiteral(ctx, toCasing(param.name, 'camel'))}, listOf(${toCasing(param.name, 'camel')}.toString()))`))
|
|
260
|
+
.appendLine()))))
|
|
261
|
+
.ensureCurrentLineEmpty()
|
|
260
262
|
.appendLine('val localVariableHeaders: MutableMap<String, String> = mutableMapOf()')
|
|
261
263
|
.appendLineIf(((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) !== undefined, `localVariableHeaders["Content-Type"] = "${(_b = endpoint.requestBody) === null || _b === void 0 ? void 0 : _b.content[0].type}"`)
|
|
262
264
|
.appendLine()
|
|
263
265
|
.append('return RequestConfig')
|
|
264
|
-
.addImport('RequestConfig', ctx.
|
|
266
|
+
.addImport('RequestConfig', ctx.infrastructurePackageName)
|
|
265
267
|
.parenthesizeMultiline('()', (builder) => builder
|
|
266
268
|
.appendLine(`method = RequestMethod.${endpoint.method.toUpperCase()},`)
|
|
267
|
-
.addImport('RequestMethod', ctx.
|
|
269
|
+
.addImport('RequestMethod', ctx.infrastructurePackageName)
|
|
268
270
|
.appendLine(`path = "${this.getPathWithInterpolation(ctx, endpoint)}",`)
|
|
269
271
|
.appendLine('query = localVariableQuery,')
|
|
270
272
|
.appendLine('headers = localVariableHeaders,')
|
|
271
273
|
.appendLine('requiresAuthentication = false,')
|
|
272
|
-
.
|
|
274
|
+
.appendLineIf(!!endpoint.requestBody, 'body = localVariableBody'));
|
|
273
275
|
}
|
|
274
276
|
generateAdditionalMethods(ctx, builder) {
|
|
275
277
|
this.generateEncodeUriComponentMethod(ctx, builder);
|
|
@@ -291,21 +293,26 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
291
293
|
.apply((builder) => this.generateTypeUsage(ctx, builder, parameter.schema))
|
|
292
294
|
.appendIf(!parameter.required, '? = ', this.getDefaultValue(ctx, parameter.schema))));
|
|
293
295
|
}
|
|
294
|
-
generateTypeUsage(ctx, builder, schema, fallback) {
|
|
296
|
+
generateTypeUsage(ctx, builder, schema, fallback, nullable) {
|
|
295
297
|
if (schema && schema.kind === 'array') {
|
|
296
298
|
const schemaInfo = this.getSchemaInfo(ctx, schema.items);
|
|
297
|
-
builder.append(`List<${schemaInfo.typeName}
|
|
299
|
+
builder.append(this.getTypeNameWithNullability(`List<${schemaInfo.typeName}>`, nullable));
|
|
298
300
|
builder.imports.addImports([schemaInfo, ...schemaInfo.additionalImports]);
|
|
299
301
|
}
|
|
300
302
|
else if (schema || !fallback) {
|
|
301
303
|
const schemaInfo = this.getSchemaInfo(ctx, schema);
|
|
302
|
-
builder.append(schemaInfo.typeName);
|
|
304
|
+
builder.append(this.getTypeNameWithNullability(schemaInfo.typeName, nullable));
|
|
303
305
|
builder.imports.addImports([schemaInfo, ...schemaInfo.additionalImports]);
|
|
304
306
|
}
|
|
305
307
|
else {
|
|
306
|
-
builder.append(fallback);
|
|
308
|
+
builder.append(this.getTypeNameWithNullability(fallback, nullable));
|
|
307
309
|
}
|
|
308
310
|
}
|
|
311
|
+
getTypeNameWithNullability(typeName, nullable) {
|
|
312
|
+
if (nullable === undefined)
|
|
313
|
+
return typeName;
|
|
314
|
+
return nullable ? `${typeName}?` : typeName.match(/^(.*?)\??$/)[1];
|
|
315
|
+
}
|
|
309
316
|
getDefaultValue(ctx, schema) {
|
|
310
317
|
if (!(schema === null || schema === void 0 ? void 0 : schema.default)) {
|
|
311
318
|
return 'null';
|
|
@@ -321,7 +328,7 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
321
328
|
}
|
|
322
329
|
}
|
|
323
330
|
getPathWithInterpolation(ctx, endpoint) {
|
|
324
|
-
let path = endpoint
|
|
331
|
+
let path = this.getEndpointPath(ctx, endpoint);
|
|
325
332
|
endpoint.parameters
|
|
326
333
|
.filter((x) => x.target === 'path')
|
|
327
334
|
.forEach((parameter) => {
|
|
@@ -367,13 +374,13 @@ export class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator {
|
|
|
367
374
|
}
|
|
368
375
|
getBasePath(ctx) {
|
|
369
376
|
var _a, _b, _c, _d, _e, _f;
|
|
370
|
-
return (_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/';
|
|
377
|
+
return modifyString((_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/', ctx.config.basePath, ctx.service);
|
|
371
378
|
}
|
|
372
|
-
|
|
373
|
-
return
|
|
379
|
+
getEndpointPath(ctx, endpoint) {
|
|
380
|
+
return modifyString(endpoint.path, ctx.config.pathModifier, endpoint);
|
|
374
381
|
}
|
|
375
|
-
|
|
376
|
-
return ctx.config.packageName
|
|
382
|
+
getFilePath(ctx) {
|
|
383
|
+
return `${ctx.config.outputDir}/${ctx.packageName.replace(/\./g, '/')}/${this.getApiClientName(ctx)}.kt`;
|
|
377
384
|
}
|
|
378
385
|
getApiClientName(ctx) {
|
|
379
386
|
return toCasing(ctx.service.name, 'pascal') + 'ApiClient';
|
|
@@ -27,17 +27,36 @@ export class KotlinOkHttp3ClientsGenerator extends OpenApiServicesGenerationProv
|
|
|
27
27
|
}
|
|
28
28
|
buildContext(context, config) {
|
|
29
29
|
context.data.services = context.data.services.filter((x) => x.name !== 'exclude-from-generation');
|
|
30
|
-
|
|
30
|
+
const providerContext = this.getProviderContext(context, config, defaultKotlinOkHttp3ClientsGeneratorConfig);
|
|
31
|
+
return Object.assign(providerContext, {
|
|
32
|
+
packageName: this.getPackageName(providerContext.config),
|
|
33
|
+
infrastructurePackageName: this.getInfrastructurePackageName(providerContext.config),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getInfrastructurePackageName(config) {
|
|
37
|
+
if (typeof config.infrastructurePackageName === 'string') {
|
|
38
|
+
return config.infrastructurePackageName;
|
|
39
|
+
}
|
|
40
|
+
if (config.infrastructurePackageName.mode === 'append-package-name') {
|
|
41
|
+
return config.packageName + config.infrastructurePackageName.value;
|
|
42
|
+
}
|
|
43
|
+
if (config.infrastructurePackageName.mode === 'append-full-package-name') {
|
|
44
|
+
return this.getPackageName(config) + config.infrastructurePackageName.value;
|
|
45
|
+
}
|
|
46
|
+
return config.infrastructurePackageName.value;
|
|
47
|
+
}
|
|
48
|
+
getPackageName(config) {
|
|
49
|
+
return config.packageName + config.packageSuffix;
|
|
31
50
|
}
|
|
32
51
|
copyInfrastructureFiles(ctx) {
|
|
33
52
|
const sourceDir = resolve(dirname(require.resolve('@goast/kotlin')), '../assets/client/okhttp3');
|
|
34
|
-
const targetDir = resolve(ctx.config.outputDir, ctx.
|
|
53
|
+
const targetDir = resolve(ctx.config.outputDir, ctx.infrastructurePackageName.replace(/\./g, '/'));
|
|
35
54
|
ensureDirSync(targetDir);
|
|
36
55
|
const files = readdirSync(sourceDir);
|
|
37
56
|
for (const file of files) {
|
|
38
57
|
const fileContent = readFileSync(resolve(sourceDir, file))
|
|
39
58
|
.toString()
|
|
40
|
-
.replace(/@PACKAGE_NAME@/g, ctx.
|
|
59
|
+
.replace(/@PACKAGE_NAME@/g, ctx.infrastructurePackageName);
|
|
41
60
|
writeFileSync(resolve(targetDir, file), fileContent);
|
|
42
61
|
}
|
|
43
62
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ensureDirSync, writeFileSync } from 'fs-extra';
|
|
2
2
|
import { toCasing } from '@goast/core';
|
|
3
3
|
import { KotlinFileBuilder } from '../../../file-builder';
|
|
4
|
+
import { modifyString } from '../../../utils';
|
|
4
5
|
import { KotlinFileGenerator } from '../../file-generator';
|
|
5
6
|
export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator {
|
|
6
7
|
generate(ctx) {
|
|
@@ -85,7 +86,7 @@ export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator
|
|
|
85
86
|
])
|
|
86
87
|
.appendAnnotation('RequestMapping', 'org.springframework.web.bind.annotation', [
|
|
87
88
|
['method', '[RequestMethod.' + endpoint.method.toUpperCase() + ']'],
|
|
88
|
-
['value', '[' + this.toStringLiteral(ctx, endpoint
|
|
89
|
+
['value', '[' + this.toStringLiteral(ctx, this.getEndpointPath(ctx, endpoint)) + ']'],
|
|
89
90
|
[
|
|
90
91
|
'consumes',
|
|
91
92
|
'[' + ((_c = endpoint.requestBody) === null || _c === void 0 ? void 0 : _c.content.map((x) => this.toStringLiteral(ctx, x.type)).join(', ')) + ']',
|
|
@@ -327,11 +328,17 @@ export class DefaultKotlinSpringControllerGenerator extends KotlinFileGenerator
|
|
|
327
328
|
}
|
|
328
329
|
}
|
|
329
330
|
getControllerRequestMapping(ctx, prefix) {
|
|
330
|
-
|
|
331
|
-
const basePath = (_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/';
|
|
331
|
+
const basePath = this.getBasePath(ctx);
|
|
332
332
|
prefix !== null && prefix !== void 0 ? prefix : (prefix = `openapi.${toCasing(ctx.service.name, 'camel')}`);
|
|
333
333
|
return this.toStringLiteral(ctx, `\${${prefix}.base-path:${basePath}}`);
|
|
334
334
|
}
|
|
335
|
+
getBasePath(ctx) {
|
|
336
|
+
var _a, _b, _c, _d, _e, _f;
|
|
337
|
+
return modifyString((_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/', ctx.config.basePath, ctx.service);
|
|
338
|
+
}
|
|
339
|
+
getEndpointPath(ctx, endpoint) {
|
|
340
|
+
return modifyString(endpoint.path, ctx.config.pathModifier, endpoint);
|
|
341
|
+
}
|
|
335
342
|
getDirectoryPath(ctx, packageName) {
|
|
336
343
|
return `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}`;
|
|
337
344
|
}
|
package/esm/lib/utils.js
CHANGED
|
@@ -15,3 +15,17 @@ export function toKotlinPropertyName(value) {
|
|
|
15
15
|
}
|
|
16
16
|
return `\`${value}\``;
|
|
17
17
|
}
|
|
18
|
+
export function modifyString(value, modifier, ...args) {
|
|
19
|
+
var _a;
|
|
20
|
+
if (typeof modifier === 'string') {
|
|
21
|
+
return modifier;
|
|
22
|
+
}
|
|
23
|
+
if (modifier instanceof RegExp) {
|
|
24
|
+
const match = value.match(modifier);
|
|
25
|
+
return (_a = match === null || match === void 0 ? void 0 : match[0]) !== null && _a !== void 0 ? _a : value;
|
|
26
|
+
}
|
|
27
|
+
if (typeof modifier === 'function') {
|
|
28
|
+
return modifier(value, ...args);
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { ApiService, DefaultGenerationProviderConfig, OpenApiServicesGenerationProviderContext } from '@goast/core';
|
|
1
|
+
import { ApiEndpoint, ApiService, DefaultGenerationProviderConfig, OpenApiServicesGenerationProviderContext } from '@goast/core';
|
|
2
2
|
import { KotlinImport } from '../../../common-results';
|
|
3
3
|
import { KotlinGeneratorConfig } from '../../../config';
|
|
4
4
|
import { KotlinModelsGeneratorOutput } from '../../models';
|
|
5
5
|
export type KotlinOkHttp3ClientsGeneratorConfig = KotlinGeneratorConfig & {
|
|
6
6
|
packageName: string;
|
|
7
7
|
packageSuffix: string;
|
|
8
|
-
infrastructurePackageName: string
|
|
8
|
+
infrastructurePackageName: string | {
|
|
9
|
+
mode: 'append-package-name' | 'append-full-package-name' | 'replace';
|
|
10
|
+
value: string;
|
|
11
|
+
};
|
|
12
|
+
basePath?: string | RegExp | ((basePath: string, service: ApiService) => string);
|
|
13
|
+
pathModifier?: RegExp | ((path: string, endpoint: ApiEndpoint) => string);
|
|
9
14
|
};
|
|
10
15
|
export declare const defaultKotlinOkHttp3ClientsGeneratorConfig: DefaultGenerationProviderConfig<KotlinOkHttp3ClientsGeneratorConfig>;
|
|
11
16
|
export type KotlinOkHttp3ClientsGeneratorInput = KotlinModelsGeneratorOutput;
|
|
@@ -15,7 +20,10 @@ export type KotlinOkHttp3ClientsGeneratorOutput = {
|
|
|
15
20
|
};
|
|
16
21
|
};
|
|
17
22
|
export type KotlinOkHttp3ClientGeneratorOutput = KotlinImport;
|
|
18
|
-
export type KotlinOkHttp3ClientsGeneratorContext = OpenApiServicesGenerationProviderContext<KotlinOkHttp3ClientsGeneratorInput, KotlinOkHttp3ClientsGeneratorOutput, KotlinOkHttp3ClientsGeneratorConfig, KotlinOkHttp3ClientGeneratorOutput
|
|
23
|
+
export type KotlinOkHttp3ClientsGeneratorContext = OpenApiServicesGenerationProviderContext<KotlinOkHttp3ClientsGeneratorInput, KotlinOkHttp3ClientsGeneratorOutput, KotlinOkHttp3ClientsGeneratorConfig, KotlinOkHttp3ClientGeneratorOutput> & {
|
|
24
|
+
packageName: string;
|
|
25
|
+
infrastructurePackageName: string;
|
|
26
|
+
};
|
|
19
27
|
export type KotlinOkHttp3ClientGeneratorContext = KotlinOkHttp3ClientsGeneratorContext & {
|
|
20
28
|
service: ApiService;
|
|
21
29
|
};
|
|
@@ -44,7 +44,8 @@ export declare class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator<C
|
|
|
44
44
|
protected generateEncodeUriComponentMethod(ctx: Context, builder: Builder): void;
|
|
45
45
|
protected generateParamDocEntries(ctx: Context, builder: Builder, endpoint: ApiEndpoint): void;
|
|
46
46
|
protected generateParams(ctx: Context, builder: Builder, endpoint: ApiEndpoint, includeTypeDefinition: boolean): void;
|
|
47
|
-
protected generateTypeUsage(ctx: Context, builder: Builder, schema: ApiSchema | undefined, fallback?: string): void;
|
|
47
|
+
protected generateTypeUsage(ctx: Context, builder: Builder, schema: ApiSchema | undefined, fallback?: string, nullable?: boolean): void;
|
|
48
|
+
protected getTypeNameWithNullability(typeName: string, nullable: boolean | undefined): string;
|
|
48
49
|
protected getDefaultValue(ctx: Context, schema: ApiSchema | undefined): string;
|
|
49
50
|
protected getPathWithInterpolation(ctx: Context, endpoint: ApiEndpoint): string;
|
|
50
51
|
protected getResponseSchema(ctx: Context, endpoint: ApiEndpoint): ApiSchema | undefined;
|
|
@@ -52,8 +53,8 @@ export declare class DefaultKotlinOkHttp3Generator extends KotlinFileGenerator<C
|
|
|
52
53
|
protected getAllParameters(ctx: Context, endpoint: ApiEndpoint): ApiParameter[];
|
|
53
54
|
protected getRequestBodyParamName(ctx: Context, endpoint: ApiEndpoint): string;
|
|
54
55
|
protected getBasePath(ctx: Context): string;
|
|
55
|
-
protected
|
|
56
|
-
protected
|
|
56
|
+
protected getEndpointPath(ctx: Context, endpoint: ApiEndpoint): string;
|
|
57
|
+
protected getFilePath(ctx: Context): string;
|
|
57
58
|
protected getApiClientName(ctx: Context): string;
|
|
58
59
|
}
|
|
59
60
|
export {};
|
|
@@ -15,6 +15,8 @@ export declare class KotlinOkHttp3ClientsGenerator extends OpenApiServicesGenera
|
|
|
15
15
|
protected generateService(ctx: Context, service: ApiService): ServiceOutput;
|
|
16
16
|
protected addServiceResult(ctx: Context, service: ApiService, result: ServiceOutput): void;
|
|
17
17
|
protected buildContext(context: OpenApiGeneratorContext<KotlinServicesGeneratorInput>, config?: Partial<Config> | undefined): Context;
|
|
18
|
+
protected getInfrastructurePackageName(config: Config): string;
|
|
19
|
+
protected getPackageName(config: Config): string;
|
|
18
20
|
private copyInfrastructureFiles;
|
|
19
21
|
}
|
|
20
22
|
export {};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { OpenApiServicesGenerationProviderContext, ApiService, DefaultGenerationProviderConfig } from '@goast/core';
|
|
1
|
+
import { OpenApiServicesGenerationProviderContext, ApiService, DefaultGenerationProviderConfig, ApiEndpoint } from '@goast/core';
|
|
2
2
|
import { KotlinImport } from '../../../common-results';
|
|
3
3
|
import { KotlinGeneratorConfig } from '../../../config';
|
|
4
4
|
import { KotlinModelsGeneratorOutput } from '../../models';
|
|
5
5
|
export type KotlinServicesGeneratorConfig = KotlinGeneratorConfig & {
|
|
6
6
|
packageName: string;
|
|
7
7
|
packageSuffix: string;
|
|
8
|
+
basePath?: string | RegExp | ((basePath: string, service: ApiService) => string);
|
|
9
|
+
pathModifier?: RegExp | ((path: string, endpoint: ApiEndpoint) => string);
|
|
8
10
|
};
|
|
9
11
|
export declare const defaultKotlinServicesGeneratorConfig: DefaultGenerationProviderConfig<KotlinServicesGeneratorConfig>;
|
|
10
12
|
export type KotlinServicesGeneratorInput = KotlinModelsGeneratorOutput;
|
|
@@ -51,6 +51,8 @@ export declare class DefaultKotlinSpringControllerGenerator extends KotlinFileGe
|
|
|
51
51
|
protected generateResponseEntityType(ctx: Context, builder: Builder, schema: ApiSchema | undefined): void;
|
|
52
52
|
protected generateTypeUsage(ctx: Context, builder: Builder, schema: ApiSchema | undefined, fallback?: string): void;
|
|
53
53
|
protected getControllerRequestMapping(ctx: Context, prefix?: string): string;
|
|
54
|
+
protected getBasePath(ctx: Context): string;
|
|
55
|
+
protected getEndpointPath(ctx: Context, endpoint: ApiEndpoint): string;
|
|
54
56
|
protected getDirectoryPath(ctx: Context, packageName: string): string;
|
|
55
57
|
protected getPackageName(ctx: Context): string;
|
|
56
58
|
protected getApiInterfaceName(ctx: Context): string;
|
package/types/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Nullable } from '@goast/core';
|
|
2
2
|
export declare function toKotlinStringLiteral(value: Nullable<string>): string;
|
|
3
3
|
export declare function toKotlinPropertyName(value: string): string;
|
|
4
|
+
export declare function modifyString<TArgs extends any[]>(value: string, modifier: string | RegExp | ((value: string, ...args: TArgs) => string) | undefined, ...args: TArgs): string;
|