@arrirpc/codegen-kotlin 0.67.0 → 0.68.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,8 +30,8 @@ export default defineConfig({
30
30
 
31
31
  The generated code relies on the following dependencies:
32
32
 
33
- - [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization)
34
- - [ktor client](https://ktor.io/docs/client-dependencies.html)
33
+ - [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization)
34
+ - [ktor client](https://ktor.io/docs/client-dependencies.html)
35
35
 
36
36
  ## Using the Generated Code
37
37
 
@@ -72,6 +72,15 @@ val service = MyClientUsersService(
72
72
  )
73
73
  ```
74
74
 
75
+ #### Client / Service Options
76
+
77
+ | Name | Type | Description |
78
+ | ------------------ | -------------------------------------- | ---------------------------------------------------------------- |
79
+ | httpClient | `HttpClient` | An instance of ktor HttpClient |
80
+ | baseUrl | `String` | The base URL of the API server |
81
+ | headers | `(() -> MutableMap<String, String>?)?` | A function that returns a map of http headers |
82
+ | onError (Optional) | `((err: Exception) -> Unit)` | A hook that fires whenever any exception is thrown by the client |
83
+
75
84
  ### Calling Procedures
76
85
 
77
86
  #### Standard HTTP Procedures
@@ -149,19 +158,19 @@ All generated models will be data classes. They will have access to the followin
149
158
 
150
159
  **Methods**:
151
160
 
152
- - `toJson(): String`
153
- - `toUrlQueryParams(): String`
161
+ - `toJson(): String`
162
+ - `toUrlQueryParams(): String`
154
163
 
155
164
  **Factory Methods**:
156
165
 
157
- - `new()`
158
- - `fromJson(input: String)`
159
- - `fromJsonElement(input: JsonElement, instancePath: String)`
166
+ - `new()`
167
+ - `fromJson(input: String)`
168
+ - `fromJsonElement(input: JsonElement, instancePath: String)`
160
169
 
161
170
  **Other Notes**
162
171
 
163
- - All Enums will have a `serialValue` property.
164
- - Discriminator schemas are converted to sealed classes
172
+ - All Enums will have a `serialValue` property.
173
+ - Discriminator schemas are converted to sealed classes
165
174
 
166
175
  ## Development
167
176
 
package/dist/index.cjs CHANGED
@@ -1163,6 +1163,7 @@ function kotlinHttpRpcFromSchema(schema, context) {
1163
1163
  bufferCapacity = bufferCapacity,
1164
1164
  onOpen = onOpen,
1165
1165
  onClose = onClose,
1166
+ onError = onError,
1166
1167
  onRequestError = onRequestError,
1167
1168
  onResponseError = onResponseError,
1168
1169
  onData = { str ->
@@ -1181,18 +1182,23 @@ function kotlinHttpRpcFromSchema(schema, context) {
1181
1182
  )
1182
1183
  }`;
1183
1184
  return `${codeComment}suspend fun ${name}(${params ? `params: ${params}` : ""}): ${response ?? "Unit"} {
1184
- val response = __prepareRequest(
1185
- client = httpClient,
1186
- url = "$baseUrl${schema.path}",
1187
- method = HttpMethod.${codegenUtils.pascalCase(schema.method, { normalize: true })},
1188
- params = ${params ? "params" : null},
1189
- headers = headers?.invoke(),
1190
- ).execute()
1191
- ${response ? headingCheck : ""}
1192
- if (response.status.value in 200..299) {
1193
- return ${response ? `${response}.fromJson(response.bodyAsText())` : ""}
1185
+ try {
1186
+ val response = __prepareRequest(
1187
+ client = httpClient,
1188
+ url = "$baseUrl${schema.path}",
1189
+ method = HttpMethod.${codegenUtils.pascalCase(schema.method, { normalize: true })},
1190
+ params = ${params ? "params" : null},
1191
+ headers = headers?.invoke(),
1192
+ ).execute()
1193
+ ${response ? headingCheck : ""}
1194
+ if (response.status.value in 200..299) {
1195
+ return ${response ? `${response}.fromJson(response.bodyAsText())` : ""}
1196
+ }
1197
+ throw ${context.clientName}Error.fromJson(response.bodyAsText())
1198
+ } catch (e: Exception) {
1199
+ onError(e)
1200
+ throw e
1194
1201
  }
1195
- throw ${context.clientName}Error.fromJson(response.bodyAsText())
1196
1202
  }`;
1197
1203
  }
1198
1204
  function kotlinWsRpcFromSchema(_schema, _context) {
@@ -1214,6 +1220,7 @@ function kotlinServiceFromSchema(schema, context) {
1214
1220
  httpClient = httpClient,
1215
1221
  baseUrl = baseUrl,
1216
1222
  headers = headers,
1223
+ onError = onError,
1217
1224
  )`);
1218
1225
  if (subService.content) {
1219
1226
  subServiceParts.push(subService.content);
@@ -1237,6 +1244,7 @@ function kotlinServiceFromSchema(schema, context) {
1237
1244
  private val httpClient: HttpClient,
1238
1245
  private val baseUrl: String,
1239
1246
  private val headers: headersFn,
1247
+ private val onError: ((err: Exception) -> Unit) = {},
1240
1248
  ) {
1241
1249
  ${procedureParts.join("\n\n ")}
1242
1250
  }
@@ -1334,6 +1342,7 @@ function kotlinClientFromAppDefinition(def, options) {
1334
1342
  httpClient = httpClient,
1335
1343
  baseUrl = baseUrl,
1336
1344
  headers = headers,
1345
+ onError = onError,
1337
1346
  )`);
1338
1347
  if (subService.content) {
1339
1348
  subServiceParts.push(subService.content);
@@ -1365,6 +1374,7 @@ class ${clientName}(
1365
1374
  private val httpClient: HttpClient,
1366
1375
  private val baseUrl: String,
1367
1376
  private val headers: headersFn,
1377
+ private val onError: ((err: Exception) -> Unit) = {},
1368
1378
  ) {
1369
1379
  ${procedureParts.join("\n\n ")}
1370
1380
  }
@@ -1734,8 +1744,9 @@ private suspend fun __handleSseRequest(
1734
1744
  onOpen: ((response: HttpResponse) -> Unit) = {},
1735
1745
  onClose: (() -> Unit) = {},
1736
1746
  onData: ((data: String) -> Unit) = {},
1737
- onRequestError: ((error: Exception) -> Unit) = {},
1738
- onResponseError: ((error: ${clientName}Error) -> Unit) = {},
1747
+ onError: ((err: Exception) -> Unit) = {},
1748
+ onRequestError: ((err: Exception) -> Unit) = {},
1749
+ onResponseError: ((err: ${clientName}Error) -> Unit) = {},
1739
1750
  bufferCapacity: Int,
1740
1751
  ) {
1741
1752
  val finalHeaders = headers?.invoke() ?: mutableMapOf()
@@ -1770,18 +1781,18 @@ private suspend fun __handleSseRequest(
1770
1781
  if (httpResponse.status.value !in 200..299) {
1771
1782
  try {
1772
1783
  if (httpResponse.headers["Content-Type"] == "application/json") {
1773
- onResponseError(
1774
- ${clientName}Error.fromJson(httpResponse.bodyAsText())
1775
- )
1784
+ val err = ${clientName}Error.fromJson(httpResponse.bodyAsText())
1785
+ onError(err)
1786
+ onResponseError(err)
1776
1787
  } else {
1777
- onResponseError(
1778
- ${clientName}Error(
1779
- code = httpResponse.status.value,
1780
- errorMessage = httpResponse.status.description,
1781
- data = JsonPrimitive(httpResponse.bodyAsText()),
1782
- stack = null,
1783
- )
1788
+ val err = ${clientName}Error(
1789
+ code = httpResponse.status.value,
1790
+ errorMessage = httpResponse.status.description,
1791
+ data = JsonPrimitive(httpResponse.bodyAsText()),
1792
+ stack = null,
1784
1793
  )
1794
+ onError(err)
1795
+ onResponseError(err)
1785
1796
  }
1786
1797
  } catch (e: CancellationException) {
1787
1798
  onClose()
@@ -1801,19 +1812,21 @@ private suspend fun __handleSseRequest(
1801
1812
  onOpen = onOpen,
1802
1813
  onClose = onClose,
1803
1814
  onData = onData,
1815
+ onError = onError,
1816
+ onRequestError = onRequestError,
1804
1817
  onResponseError = onResponseError,
1805
1818
  )
1806
1819
  }
1807
1820
  if (httpResponse.headers["Content-Type"] != "text/event-stream") {
1808
1821
  try {
1809
- onResponseError(
1810
- ${clientName}Error(
1811
- code = 0,
1812
- errorMessage = "Expected server to return Content-Type \\"text/event-stream\\". Got \\"\${httpResponse.headers["Content-Type"]}\\"",
1813
- data = JsonPrimitive(httpResponse.bodyAsText()),
1814
- stack = null,
1815
- )
1822
+ val err = ${clientName}Error(
1823
+ code = 0,
1824
+ errorMessage = "Expected server to return Content-Type \\"text/event-stream\\". Got \\"\${httpResponse.headers["Content-Type"]}\\"",
1825
+ data = JsonPrimitive(httpResponse.bodyAsText()),
1826
+ stack = null,
1816
1827
  )
1828
+ onError(err)
1829
+ onResponseError(err)
1817
1830
  } catch (e: CancellationException) {
1818
1831
  httpResponse.cancel()
1819
1832
  return@execute
@@ -1831,6 +1844,8 @@ private suspend fun __handleSseRequest(
1831
1844
  onOpen = onOpen,
1832
1845
  onClose = onClose,
1833
1846
  onData = onData,
1847
+ onError = onError,
1848
+ onRequestError = onRequestError,
1834
1849
  onResponseError = onResponseError,
1835
1850
  )
1836
1851
  }
@@ -1882,10 +1897,13 @@ private suspend fun __handleSseRequest(
1882
1897
  onOpen = onOpen,
1883
1898
  onClose = onClose,
1884
1899
  onData = onData,
1900
+ onError = onError,
1901
+ onRequestError = onRequestError,
1885
1902
  onResponseError = onResponseError,
1886
1903
  )
1887
1904
  }
1888
1905
  } catch (e: java.net.ConnectException) {
1906
+ onError(e)
1889
1907
  onRequestError(e)
1890
1908
  return __handleSseRequest(
1891
1909
  httpClient = httpClient,
@@ -1900,9 +1918,12 @@ private suspend fun __handleSseRequest(
1900
1918
  onOpen = onOpen,
1901
1919
  onClose = onClose,
1902
1920
  onData = onData,
1921
+ onError = onError,
1922
+ onRequestError = onRequestError,
1903
1923
  onResponseError = onResponseError,
1904
1924
  )
1905
1925
  } catch (e: Exception) {
1926
+ onError(e)
1906
1927
  onRequestError(e)
1907
1928
  return __handleSseRequest(
1908
1929
  httpClient = httpClient,
@@ -1917,6 +1938,8 @@ private suspend fun __handleSseRequest(
1917
1938
  onOpen = onOpen,
1918
1939
  onClose = onClose,
1919
1940
  onData = onData,
1941
+ onError = onError,
1942
+ onRequestError = onRequestError,
1920
1943
  onResponseError = onResponseError,
1921
1944
  )
1922
1945
  }
package/dist/index.mjs CHANGED
@@ -1157,6 +1157,7 @@ function kotlinHttpRpcFromSchema(schema, context) {
1157
1157
  bufferCapacity = bufferCapacity,
1158
1158
  onOpen = onOpen,
1159
1159
  onClose = onClose,
1160
+ onError = onError,
1160
1161
  onRequestError = onRequestError,
1161
1162
  onResponseError = onResponseError,
1162
1163
  onData = { str ->
@@ -1175,18 +1176,23 @@ function kotlinHttpRpcFromSchema(schema, context) {
1175
1176
  )
1176
1177
  }`;
1177
1178
  return `${codeComment}suspend fun ${name}(${params ? `params: ${params}` : ""}): ${response ?? "Unit"} {
1178
- val response = __prepareRequest(
1179
- client = httpClient,
1180
- url = "$baseUrl${schema.path}",
1181
- method = HttpMethod.${pascalCase(schema.method, { normalize: true })},
1182
- params = ${params ? "params" : null},
1183
- headers = headers?.invoke(),
1184
- ).execute()
1185
- ${response ? headingCheck : ""}
1186
- if (response.status.value in 200..299) {
1187
- return ${response ? `${response}.fromJson(response.bodyAsText())` : ""}
1179
+ try {
1180
+ val response = __prepareRequest(
1181
+ client = httpClient,
1182
+ url = "$baseUrl${schema.path}",
1183
+ method = HttpMethod.${pascalCase(schema.method, { normalize: true })},
1184
+ params = ${params ? "params" : null},
1185
+ headers = headers?.invoke(),
1186
+ ).execute()
1187
+ ${response ? headingCheck : ""}
1188
+ if (response.status.value in 200..299) {
1189
+ return ${response ? `${response}.fromJson(response.bodyAsText())` : ""}
1190
+ }
1191
+ throw ${context.clientName}Error.fromJson(response.bodyAsText())
1192
+ } catch (e: Exception) {
1193
+ onError(e)
1194
+ throw e
1188
1195
  }
1189
- throw ${context.clientName}Error.fromJson(response.bodyAsText())
1190
1196
  }`;
1191
1197
  }
1192
1198
  function kotlinWsRpcFromSchema(_schema, _context) {
@@ -1208,6 +1214,7 @@ function kotlinServiceFromSchema(schema, context) {
1208
1214
  httpClient = httpClient,
1209
1215
  baseUrl = baseUrl,
1210
1216
  headers = headers,
1217
+ onError = onError,
1211
1218
  )`);
1212
1219
  if (subService.content) {
1213
1220
  subServiceParts.push(subService.content);
@@ -1231,6 +1238,7 @@ function kotlinServiceFromSchema(schema, context) {
1231
1238
  private val httpClient: HttpClient,
1232
1239
  private val baseUrl: String,
1233
1240
  private val headers: headersFn,
1241
+ private val onError: ((err: Exception) -> Unit) = {},
1234
1242
  ) {
1235
1243
  ${procedureParts.join("\n\n ")}
1236
1244
  }
@@ -1328,6 +1336,7 @@ function kotlinClientFromAppDefinition(def, options) {
1328
1336
  httpClient = httpClient,
1329
1337
  baseUrl = baseUrl,
1330
1338
  headers = headers,
1339
+ onError = onError,
1331
1340
  )`);
1332
1341
  if (subService.content) {
1333
1342
  subServiceParts.push(subService.content);
@@ -1359,6 +1368,7 @@ class ${clientName}(
1359
1368
  private val httpClient: HttpClient,
1360
1369
  private val baseUrl: String,
1361
1370
  private val headers: headersFn,
1371
+ private val onError: ((err: Exception) -> Unit) = {},
1362
1372
  ) {
1363
1373
  ${procedureParts.join("\n\n ")}
1364
1374
  }
@@ -1728,8 +1738,9 @@ private suspend fun __handleSseRequest(
1728
1738
  onOpen: ((response: HttpResponse) -> Unit) = {},
1729
1739
  onClose: (() -> Unit) = {},
1730
1740
  onData: ((data: String) -> Unit) = {},
1731
- onRequestError: ((error: Exception) -> Unit) = {},
1732
- onResponseError: ((error: ${clientName}Error) -> Unit) = {},
1741
+ onError: ((err: Exception) -> Unit) = {},
1742
+ onRequestError: ((err: Exception) -> Unit) = {},
1743
+ onResponseError: ((err: ${clientName}Error) -> Unit) = {},
1733
1744
  bufferCapacity: Int,
1734
1745
  ) {
1735
1746
  val finalHeaders = headers?.invoke() ?: mutableMapOf()
@@ -1764,18 +1775,18 @@ private suspend fun __handleSseRequest(
1764
1775
  if (httpResponse.status.value !in 200..299) {
1765
1776
  try {
1766
1777
  if (httpResponse.headers["Content-Type"] == "application/json") {
1767
- onResponseError(
1768
- ${clientName}Error.fromJson(httpResponse.bodyAsText())
1769
- )
1778
+ val err = ${clientName}Error.fromJson(httpResponse.bodyAsText())
1779
+ onError(err)
1780
+ onResponseError(err)
1770
1781
  } else {
1771
- onResponseError(
1772
- ${clientName}Error(
1773
- code = httpResponse.status.value,
1774
- errorMessage = httpResponse.status.description,
1775
- data = JsonPrimitive(httpResponse.bodyAsText()),
1776
- stack = null,
1777
- )
1782
+ val err = ${clientName}Error(
1783
+ code = httpResponse.status.value,
1784
+ errorMessage = httpResponse.status.description,
1785
+ data = JsonPrimitive(httpResponse.bodyAsText()),
1786
+ stack = null,
1778
1787
  )
1788
+ onError(err)
1789
+ onResponseError(err)
1779
1790
  }
1780
1791
  } catch (e: CancellationException) {
1781
1792
  onClose()
@@ -1795,19 +1806,21 @@ private suspend fun __handleSseRequest(
1795
1806
  onOpen = onOpen,
1796
1807
  onClose = onClose,
1797
1808
  onData = onData,
1809
+ onError = onError,
1810
+ onRequestError = onRequestError,
1798
1811
  onResponseError = onResponseError,
1799
1812
  )
1800
1813
  }
1801
1814
  if (httpResponse.headers["Content-Type"] != "text/event-stream") {
1802
1815
  try {
1803
- onResponseError(
1804
- ${clientName}Error(
1805
- code = 0,
1806
- errorMessage = "Expected server to return Content-Type \\"text/event-stream\\". Got \\"\${httpResponse.headers["Content-Type"]}\\"",
1807
- data = JsonPrimitive(httpResponse.bodyAsText()),
1808
- stack = null,
1809
- )
1816
+ val err = ${clientName}Error(
1817
+ code = 0,
1818
+ errorMessage = "Expected server to return Content-Type \\"text/event-stream\\". Got \\"\${httpResponse.headers["Content-Type"]}\\"",
1819
+ data = JsonPrimitive(httpResponse.bodyAsText()),
1820
+ stack = null,
1810
1821
  )
1822
+ onError(err)
1823
+ onResponseError(err)
1811
1824
  } catch (e: CancellationException) {
1812
1825
  httpResponse.cancel()
1813
1826
  return@execute
@@ -1825,6 +1838,8 @@ private suspend fun __handleSseRequest(
1825
1838
  onOpen = onOpen,
1826
1839
  onClose = onClose,
1827
1840
  onData = onData,
1841
+ onError = onError,
1842
+ onRequestError = onRequestError,
1828
1843
  onResponseError = onResponseError,
1829
1844
  )
1830
1845
  }
@@ -1876,10 +1891,13 @@ private suspend fun __handleSseRequest(
1876
1891
  onOpen = onOpen,
1877
1892
  onClose = onClose,
1878
1893
  onData = onData,
1894
+ onError = onError,
1895
+ onRequestError = onRequestError,
1879
1896
  onResponseError = onResponseError,
1880
1897
  )
1881
1898
  }
1882
1899
  } catch (e: java.net.ConnectException) {
1900
+ onError(e)
1883
1901
  onRequestError(e)
1884
1902
  return __handleSseRequest(
1885
1903
  httpClient = httpClient,
@@ -1894,9 +1912,12 @@ private suspend fun __handleSseRequest(
1894
1912
  onOpen = onOpen,
1895
1913
  onClose = onClose,
1896
1914
  onData = onData,
1915
+ onError = onError,
1916
+ onRequestError = onRequestError,
1897
1917
  onResponseError = onResponseError,
1898
1918
  )
1899
1919
  } catch (e: Exception) {
1920
+ onError(e)
1900
1921
  onRequestError(e)
1901
1922
  return __handleSseRequest(
1902
1923
  httpClient = httpClient,
@@ -1911,6 +1932,8 @@ private suspend fun __handleSseRequest(
1911
1932
  onOpen = onOpen,
1912
1933
  onClose = onClose,
1913
1934
  onData = onData,
1935
+ onError = onError,
1936
+ onRequestError = onRequestError,
1914
1937
  onResponseError = onResponseError,
1915
1938
  )
1916
1939
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/codegen-kotlin",
3
- "version": "0.67.0",
3
+ "version": "0.68.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -22,6 +22,6 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@arrirpc/codegen-utils": "0.67.0"
25
+ "@arrirpc/codegen-utils": "0.68.0"
26
26
  }
27
27
  }