@kizunajs/kotlin 2.0.0-beta.3 → 2.0.0-beta.5

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
@@ -10,8 +10,25 @@ pnpm add -D @kizunajs/kotlin
10
10
 
11
11
  ## Usage
12
12
 
13
+ Name the client in your config, and `kizuna generate` writes it with the rest:
14
+
15
+ ```ts
16
+ import { kotlinClient } from '@kizunajs/kotlin';
17
+
18
+ export default defineConfig({
19
+ routes,
20
+ clients: [
21
+ kotlinClient({
22
+ output: '../android/app/src/main/kotlin/com/example/APIClient.kt',
23
+ namespace: 'API',
24
+ package: 'com.example',
25
+ }),
26
+ ],
27
+ });
28
+ ```
29
+
13
30
  ```sh
14
- kizuna-kotlin generate --config kizuna.config.ts --out android/app/src/main/kotlin/com/example/APIClient.kt --namespace-name API --package com.example
31
+ kizuna generate
15
32
  ```
16
33
 
17
34
  ## Documentation
package/dist/cli.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- const require_generator = require("./generator-DotGXjps.cjs");
2
+ const require_generator = require("./generator-DgTjvOZ-.cjs");
3
3
  let node_fs = require("node:fs");
4
4
  let node_path = require("node:path");
5
5
  let node_util = require("node:util");
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { t as generateKotlinClient } from "./generator-CWQYbiS9.mjs";
2
+ import { t as generateKotlinClient } from "./generator-CVPHq7oN.mjs";
3
3
  import { mkdirSync, writeFileSync } from "node:fs";
4
4
  import { dirname, resolve } from "node:path";
5
5
  import { parseArgs } from "node:util";
@@ -1,4 +1,4 @@
1
- import { createGenerator, isDiscriminatedUnionSchema, isFileSchema, isHintPrefix, isIntegerSchema, isNamedStream, isObjectSchema, isStreamResponse, isSuccessStatus, localTypeName, mergeHeaderFields, parsePath, readDef, readDefType, readDeprecation, readDiscriminatedUnion, readDiscriminatorStringLiteral, readMetaDescription, readMetaId, readObjectShape, resolveResponseBody, resolveResponseHeaders, routeStreams, sanitizeFieldName, sanitizeIdentifier, shortTypeName, soleStreamResponse, statusToCamelCase, streamMode, toCamelCase, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
1
+ import { createGenerator, isBinarySchema, isDiscriminatedUnionSchema, isFileSchema, isHintPrefix, isIntegerSchema, isNamedStream, isObjectSchema, isStreamResponse, isSuccessStatus, localTypeName, mergeHeaderFields, parsePath, readDef, readDefType, readDeprecation, readDiscriminatedUnion, readDiscriminatorStringLiteral, readMetaDescription, readMetaId, readObjectShape, resolveResponseBody, resolveResponseHeaders, routeStreams, sanitizeFieldName, sanitizeIdentifier, shortTypeName, soleStreamResponse, statusToCamelCase, streamMode, toCamelCase, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
2
2
  //#region src/emit.ts
3
3
  var KotlinWriter = class {
4
4
  lines = [];
@@ -117,8 +117,7 @@ const propertyName = (key, camelCase) => {
117
117
  if (!camel) return "field";
118
118
  return /^[0-9]/.test(camel) ? `_${camel}` : camel;
119
119
  };
120
- const objectFields = (schema, registry, hint) => {
121
- const shape = readObjectShape(schema) ?? {};
120
+ const objectFields = (schema, registry, hint, shape = readObjectShape(schema) ?? {}) => {
122
121
  const fields = [];
123
122
  const seen = /* @__PURE__ */ new Map();
124
123
  for (const [key, value] of Object.entries(shape)) {
@@ -148,6 +147,10 @@ const mapType = (schema, registry, hint) => {
148
147
  optional: false,
149
148
  isFile: true
150
149
  };
150
+ if (isBinarySchema(schema)) return {
151
+ expression: "ByteArray",
152
+ optional: false
153
+ };
151
154
  const id = readMetaId(schema);
152
155
  const def = readDef(schema);
153
156
  const discriminated = readDiscriminatedUnion(schema);
@@ -277,8 +280,9 @@ const mapType = (schema, registry, hint) => {
277
280
  optional: false
278
281
  };
279
282
  }
283
+ const elementType = mapType(element, registry, `${hint}Item`).expression.replace(/\?$/, "");
280
284
  return {
281
- expression: `List<${mapType(element, registry, `${hint}Item`).expression.replace(/\?$/, "")}>`,
285
+ expression: `List<${unwrapOptionalWrappers(element).nullable ? `${elementType}?` : elementType}>`,
282
286
  optional: false
283
287
  };
284
288
  }
@@ -653,8 +657,13 @@ const KOTLIN_PRIMITIVE_TYPES = new Set([
653
657
  "Long",
654
658
  "Boolean",
655
659
  "Instant",
656
- "Unit"
660
+ "Unit",
661
+ "ByteArray"
657
662
  ]);
663
+ /**
664
+ * How a response body is read. Raw bytes are the body already.
665
+ */
666
+ const decodeBody = (resolved) => resolved === "ByteArray" ? "data" : `json.decodeFromString<${resolved}>(data.decodeToString())`;
658
667
  const resolveType = (typeName, currentOperation, context, scope = "client") => {
659
668
  const { operationTypeMap, namespaceName, clientName, fileLevelTypeNames } = context;
660
669
  const optional = typeName.endsWith("?");
@@ -1344,7 +1353,7 @@ const emitMethodBody = (writer, method, context) => {
1344
1353
  writer.indent(() => {
1345
1354
  const resolved = resolveType(successResponse.type, method.operationName, context);
1346
1355
  writer.block("try", () => {
1347
- writer.line(`val payload = json.decodeFromString<${resolved}>(data.decodeToString())`);
1356
+ writer.line(`val payload = ${decodeBody(resolved)}`);
1348
1357
  const bodyExpr = isMultiSuccess ? `${operationRef}.Success.Status${successResponse.status}(payload)` : "payload";
1349
1358
  if (hasHeaders) {
1350
1359
  for (const field of successResponse.responseHeaders) writer.line(`val ${escapeKeyword(field.name)} = httpResponse.header(${stringLiteral(field.wireName)})`);
@@ -1381,7 +1390,7 @@ const emitErrorBranches = (writer, method, context) => {
1381
1390
  const resolved = resolveType(errorCase.type, method.operationName, context);
1382
1391
  writer.line("val payload = try {");
1383
1392
  writer.indent(() => {
1384
- writer.line(`json.decodeFromString<${resolved}>(data.decodeToString())`);
1393
+ writer.line(decodeBody(resolved));
1385
1394
  });
1386
1395
  writer.line(`} catch (error: Exception) { throw ${failureRef}.Decoding(error, statusCode, data) }`);
1387
1396
  writer.line(`throw ${failureRef}.${caseName}(body = payload)`);
@@ -1394,7 +1403,7 @@ const emitErrorBranches = (writer, method, context) => {
1394
1403
  const resolved = resolveType(errorCase.type, method.operationName, context);
1395
1404
  writer.line(`val ${errorCase.caseName} = try {`);
1396
1405
  writer.indent(() => {
1397
- writer.line(`json.decodeFromString<${resolved}>(data.decodeToString())`);
1406
+ writer.line(decodeBody(resolved));
1398
1407
  });
1399
1408
  writer.line("} catch (_: Exception) { null }");
1400
1409
  writer.line(`if (${errorCase.caseName} != null) throw ${failureRef}.${caseName}(body = ${errorCase.caseName})`);
@@ -117,8 +117,7 @@ const propertyName = (key, camelCase) => {
117
117
  if (!camel) return "field";
118
118
  return /^[0-9]/.test(camel) ? `_${camel}` : camel;
119
119
  };
120
- const objectFields = (schema, registry, hint) => {
121
- const shape = (0, kizunajs_generator.readObjectShape)(schema) ?? {};
120
+ const objectFields = (schema, registry, hint, shape = (0, kizunajs_generator.readObjectShape)(schema) ?? {}) => {
122
121
  const fields = [];
123
122
  const seen = /* @__PURE__ */ new Map();
124
123
  for (const [key, value] of Object.entries(shape)) {
@@ -148,6 +147,10 @@ const mapType = (schema, registry, hint) => {
148
147
  optional: false,
149
148
  isFile: true
150
149
  };
150
+ if ((0, kizunajs_generator.isBinarySchema)(schema)) return {
151
+ expression: "ByteArray",
152
+ optional: false
153
+ };
151
154
  const id = (0, kizunajs_generator.readMetaId)(schema);
152
155
  const def = (0, kizunajs_generator.readDef)(schema);
153
156
  const discriminated = (0, kizunajs_generator.readDiscriminatedUnion)(schema);
@@ -277,8 +280,9 @@ const mapType = (schema, registry, hint) => {
277
280
  optional: false
278
281
  };
279
282
  }
283
+ const elementType = mapType(element, registry, `${hint}Item`).expression.replace(/\?$/, "");
280
284
  return {
281
- expression: `List<${mapType(element, registry, `${hint}Item`).expression.replace(/\?$/, "")}>`,
285
+ expression: `List<${(0, kizunajs_generator.unwrapOptionalWrappers)(element).nullable ? `${elementType}?` : elementType}>`,
282
286
  optional: false
283
287
  };
284
288
  }
@@ -653,8 +657,13 @@ const KOTLIN_PRIMITIVE_TYPES = new Set([
653
657
  "Long",
654
658
  "Boolean",
655
659
  "Instant",
656
- "Unit"
660
+ "Unit",
661
+ "ByteArray"
657
662
  ]);
663
+ /**
664
+ * How a response body is read. Raw bytes are the body already.
665
+ */
666
+ const decodeBody = (resolved) => resolved === "ByteArray" ? "data" : `json.decodeFromString<${resolved}>(data.decodeToString())`;
658
667
  const resolveType = (typeName, currentOperation, context, scope = "client") => {
659
668
  const { operationTypeMap, namespaceName, clientName, fileLevelTypeNames } = context;
660
669
  const optional = typeName.endsWith("?");
@@ -1344,7 +1353,7 @@ const emitMethodBody = (writer, method, context) => {
1344
1353
  writer.indent(() => {
1345
1354
  const resolved = resolveType(successResponse.type, method.operationName, context);
1346
1355
  writer.block("try", () => {
1347
- writer.line(`val payload = json.decodeFromString<${resolved}>(data.decodeToString())`);
1356
+ writer.line(`val payload = ${decodeBody(resolved)}`);
1348
1357
  const bodyExpr = isMultiSuccess ? `${operationRef}.Success.Status${successResponse.status}(payload)` : "payload";
1349
1358
  if (hasHeaders) {
1350
1359
  for (const field of successResponse.responseHeaders) writer.line(`val ${escapeKeyword(field.name)} = httpResponse.header(${stringLiteral(field.wireName)})`);
@@ -1381,7 +1390,7 @@ const emitErrorBranches = (writer, method, context) => {
1381
1390
  const resolved = resolveType(errorCase.type, method.operationName, context);
1382
1391
  writer.line("val payload = try {");
1383
1392
  writer.indent(() => {
1384
- writer.line(`json.decodeFromString<${resolved}>(data.decodeToString())`);
1393
+ writer.line(decodeBody(resolved));
1385
1394
  });
1386
1395
  writer.line(`} catch (error: Exception) { throw ${failureRef}.Decoding(error, statusCode, data) }`);
1387
1396
  writer.line(`throw ${failureRef}.${caseName}(body = payload)`);
@@ -1394,7 +1403,7 @@ const emitErrorBranches = (writer, method, context) => {
1394
1403
  const resolved = resolveType(errorCase.type, method.operationName, context);
1395
1404
  writer.line(`val ${errorCase.caseName} = try {`);
1396
1405
  writer.indent(() => {
1397
- writer.line(`json.decodeFromString<${resolved}>(data.decodeToString())`);
1406
+ writer.line(decodeBody(resolved));
1398
1407
  });
1399
1408
  writer.line("} catch (_: Exception) { null }");
1400
1409
  writer.line(`if (${errorCase.caseName} != null) throw ${failureRef}.${caseName}(body = ${errorCase.caseName})`);
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_generator = require("./generator-DotGXjps.cjs");
2
+ const require_generator = require("./generator-DgTjvOZ-.cjs");
3
3
  //#region src/client-target.ts
4
4
  /**
5
5
  * A Kotlin client target for `kizuna.config.ts`.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as generateKotlinClient } from "./generator-CWQYbiS9.mjs";
1
+ import { t as generateKotlinClient } from "./generator-CVPHq7oN.mjs";
2
2
  //#region src/client-target.ts
3
3
  /**
4
4
  * A Kotlin client target for `kizuna.config.ts`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizunajs/kotlin",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-beta.5",
4
4
  "description": "Kotlin client generation for Kizuna.js APIs",
5
5
  "keywords": [
6
6
  "kizunajs",
@@ -60,18 +60,18 @@
60
60
  "access": "public"
61
61
  },
62
62
  "dependencies": {
63
- "@kizunajs/cli": "2.0.0-beta.3"
63
+ "@kizunajs/cli": "2.0.0-beta.5"
64
64
  },
65
65
  "peerDependencies": {
66
66
  "zod": "^4.0.0",
67
- "kizunajs": "2.0.0-beta.3"
67
+ "kizunajs": "2.0.0-beta.5"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@types/node": "^22.9.0",
71
71
  "tsdown": "^0.21.0",
72
72
  "typescript": "^6.0.3",
73
73
  "zod": "^4.0.0",
74
- "kizunajs": "2.0.0-beta.3"
74
+ "kizunajs": "2.0.0-beta.5"
75
75
  },
76
76
  "scripts": {
77
77
  "build": "tsdown src/index.ts src/cli.ts --format esm,cjs --dts --clean --external kizunajs --external @kizunajs/cli --external zod",