@kizunajs/swift 2.0.0-beta.12 → 2.0.0-beta.14
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/dist/cli.cjs
CHANGED
package/dist/cli.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as generateSwiftClient } from "./generator-
|
|
2
|
+
import { t as generateSwiftClient } from "./generator-CxUzLmbK.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 { isBinarySchema, isDiscriminatedUnionSchema, isFileSchema, isHintPrefix, isIntegerSchema, isJsonMediaType, isNamedStream, isObjectSchema, isStreamResponse, isSuccessStatus, isVoidSchema, localTypeName, mergeHeaderFields, parsePath, readDef, readDefType, readDeprecation, readDiscriminatedUnion, readDiscriminatorStringLiteral, readMetaDescription, readMetaId, readObjectShape, resolveResponseBody, resolveResponseContentType, resolveResponseHeaders, routeStreams, sanitizeFieldName, sanitizeIdentifier, shortTypeName, soleStreamResponse, statusToCamelCase, streamMode, toCamelCase, toPascalCase, unwrapOptionalWrappers, walkApi } from "kizunajs/generator";
|
|
1
|
+
import { isBinarySchema, isDiscriminatedUnionSchema, isFileSchema, isHintPrefix, isIntegerSchema, isJsonMediaType, isNamedStream, isObjectSchema, isStreamResponse, isSuccessStatus, isVoidSchema, localTypeName, mergeHeaderFields, parsePath, readDef, readDefType, readDeprecation, readDiscriminatedUnion, readDiscriminatorStringLiteral, readMetaBrand, readMetaDescription, readMetaId, readObjectShape, resolveResponseBody, resolveResponseContentType, resolveResponseHeaders, routeStreams, sanitizeFieldName, sanitizeIdentifier, shortTypeName, soleStreamResponse, statusToCamelCase, streamMode, toCamelCase, toPascalCase, unwrapOptionalWrappers, walkApi } from "kizunajs/generator";
|
|
2
2
|
//#region src/emit.ts
|
|
3
3
|
var SwiftWriter = class {
|
|
4
4
|
lines = [];
|
|
@@ -45,6 +45,7 @@ var TypeRegistry = class {
|
|
|
45
45
|
warningSet = /* @__PURE__ */ new Set();
|
|
46
46
|
explicitIds = /* @__PURE__ */ new Set();
|
|
47
47
|
unionVariantOwners = /* @__PURE__ */ new Map();
|
|
48
|
+
brands = /* @__PURE__ */ new Map();
|
|
48
49
|
usesAnyCodable = false;
|
|
49
50
|
constructor(camelCaseProperties = false, unknownEnumCase = false) {
|
|
50
51
|
this.camelCaseProperties = camelCaseProperties;
|
|
@@ -82,6 +83,15 @@ var TypeRegistry = class {
|
|
|
82
83
|
unionVariantOwner(payloadName) {
|
|
83
84
|
return this.unionVariantOwners.get(payloadName);
|
|
84
85
|
}
|
|
86
|
+
addBrand(brand) {
|
|
87
|
+
const existing = this.brands.get(brand.name);
|
|
88
|
+
if (existing !== void 0 && existing.rawType !== brand.rawType) throw new Error(`@kizunajs/swift: brand ${stringify(brand.name)} wraps both ${existing.rawType} and ${brand.rawType}. Give each its own name.`);
|
|
89
|
+
if (this.types.has(brand.name)) throw new Error(`@kizunajs/swift: brand ${stringify(brand.name)} has the same name as a model. Give each its own name.`);
|
|
90
|
+
this.brands.set(brand.name, brand);
|
|
91
|
+
}
|
|
92
|
+
allBrands() {
|
|
93
|
+
return Array.from(this.brands.values());
|
|
94
|
+
}
|
|
85
95
|
warnAnyCodable(hint, reason) {
|
|
86
96
|
this.usesAnyCodable = true;
|
|
87
97
|
this.warningSet.add(`${hint}: ${reason}`);
|
|
@@ -124,7 +134,29 @@ const objectFields = (schema, registry, hint, shape = readObjectShape(schema) ??
|
|
|
124
134
|
return fields;
|
|
125
135
|
};
|
|
126
136
|
const stringify = (value) => JSON.stringify(value);
|
|
137
|
+
const BRANDABLE_TYPES = new Set([
|
|
138
|
+
"String",
|
|
139
|
+
"Int",
|
|
140
|
+
"Int64",
|
|
141
|
+
"Double",
|
|
142
|
+
"Bool",
|
|
143
|
+
"Date"
|
|
144
|
+
]);
|
|
127
145
|
const mapType = (schema, registry, hint) => {
|
|
146
|
+
const result = mapUnbrandedType(schema, registry, hint);
|
|
147
|
+
const brand = readMetaBrand(schema);
|
|
148
|
+
if (brand === void 0 || !BRANDABLE_TYPES.has(result.expression)) return result;
|
|
149
|
+
const name = sanitizeIdentifier(brand);
|
|
150
|
+
registry.addBrand({
|
|
151
|
+
name,
|
|
152
|
+
rawType: result.expression
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
expression: name,
|
|
156
|
+
optional: false
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
const mapUnbrandedType = (schema, registry, hint) => {
|
|
128
160
|
if (isFileSchema(schema)) return {
|
|
129
161
|
expression: "MultipartFile",
|
|
130
162
|
optional: false,
|
|
@@ -862,6 +894,31 @@ const emitStruct = (writer, type, context, ownedTypeMap, ownedTypeLookup, regist
|
|
|
862
894
|
emitMemberwiseInit(writer, adjustedFields, (field) => field.deprecated ? deprecatedStorageName(field, type.fields) : escapeKeyword(field.name));
|
|
863
895
|
});
|
|
864
896
|
};
|
|
897
|
+
/**
|
|
898
|
+
* A brand as a struct around its value, encoded as the bare value.
|
|
899
|
+
*/
|
|
900
|
+
const emitBrand = (writer, brand) => {
|
|
901
|
+
writer.block(`public struct ${brand.name}: RawRepresentable, Codable, Hashable, Sendable`, () => {
|
|
902
|
+
writer.line(`public let rawValue: ${brand.rawType}`);
|
|
903
|
+
writer.blank();
|
|
904
|
+
writer.block(`public init(rawValue: ${brand.rawType})`, () => {
|
|
905
|
+
writer.line("self.rawValue = rawValue");
|
|
906
|
+
});
|
|
907
|
+
writer.blank();
|
|
908
|
+
writer.block(`public init(_ rawValue: ${brand.rawType})`, () => {
|
|
909
|
+
writer.line("self.rawValue = rawValue");
|
|
910
|
+
});
|
|
911
|
+
writer.blank();
|
|
912
|
+
writer.block("public init(from decoder: Decoder) throws", () => {
|
|
913
|
+
writer.line(`rawValue = try decoder.singleValueContainer().decode(${brand.rawType}.self)`);
|
|
914
|
+
});
|
|
915
|
+
writer.blank();
|
|
916
|
+
writer.block("public func encode(to encoder: Encoder) throws", () => {
|
|
917
|
+
writer.line("var container = encoder.singleValueContainer()");
|
|
918
|
+
writer.line("try container.encode(rawValue)");
|
|
919
|
+
});
|
|
920
|
+
});
|
|
921
|
+
};
|
|
865
922
|
const emitDiscriminatedEnum = (writer, type, context, registryName, ownedTypeMap, ownedTypeLookup) => {
|
|
866
923
|
const lookupName = registryName ?? type.name;
|
|
867
924
|
writer.block(`public enum ${type.name}: Codable, Sendable, Equatable`, () => {
|
|
@@ -2058,6 +2115,10 @@ const renderSwiftClient = (api, partition, registry, options) => {
|
|
|
2058
2115
|
};
|
|
2059
2116
|
writer.block(`public enum ${namespaceName}`, () => {
|
|
2060
2117
|
emitTypes(writer, topLevelSharedTypes, context, ownedTypeMap, ownedTypeLookup);
|
|
2118
|
+
for (const brand of registry.allBrands()) {
|
|
2119
|
+
writer.blank();
|
|
2120
|
+
emitBrand(writer, brand);
|
|
2121
|
+
}
|
|
2061
2122
|
});
|
|
2062
2123
|
emitClient(writer, {
|
|
2063
2124
|
clientName,
|
|
@@ -45,6 +45,7 @@ var TypeRegistry = class {
|
|
|
45
45
|
warningSet = /* @__PURE__ */ new Set();
|
|
46
46
|
explicitIds = /* @__PURE__ */ new Set();
|
|
47
47
|
unionVariantOwners = /* @__PURE__ */ new Map();
|
|
48
|
+
brands = /* @__PURE__ */ new Map();
|
|
48
49
|
usesAnyCodable = false;
|
|
49
50
|
constructor(camelCaseProperties = false, unknownEnumCase = false) {
|
|
50
51
|
this.camelCaseProperties = camelCaseProperties;
|
|
@@ -82,6 +83,15 @@ var TypeRegistry = class {
|
|
|
82
83
|
unionVariantOwner(payloadName) {
|
|
83
84
|
return this.unionVariantOwners.get(payloadName);
|
|
84
85
|
}
|
|
86
|
+
addBrand(brand) {
|
|
87
|
+
const existing = this.brands.get(brand.name);
|
|
88
|
+
if (existing !== void 0 && existing.rawType !== brand.rawType) throw new Error(`@kizunajs/swift: brand ${stringify(brand.name)} wraps both ${existing.rawType} and ${brand.rawType}. Give each its own name.`);
|
|
89
|
+
if (this.types.has(brand.name)) throw new Error(`@kizunajs/swift: brand ${stringify(brand.name)} has the same name as a model. Give each its own name.`);
|
|
90
|
+
this.brands.set(brand.name, brand);
|
|
91
|
+
}
|
|
92
|
+
allBrands() {
|
|
93
|
+
return Array.from(this.brands.values());
|
|
94
|
+
}
|
|
85
95
|
warnAnyCodable(hint, reason) {
|
|
86
96
|
this.usesAnyCodable = true;
|
|
87
97
|
this.warningSet.add(`${hint}: ${reason}`);
|
|
@@ -124,7 +134,29 @@ const objectFields = (schema, registry, hint, shape = (0, kizunajs_generator.rea
|
|
|
124
134
|
return fields;
|
|
125
135
|
};
|
|
126
136
|
const stringify = (value) => JSON.stringify(value);
|
|
137
|
+
const BRANDABLE_TYPES = new Set([
|
|
138
|
+
"String",
|
|
139
|
+
"Int",
|
|
140
|
+
"Int64",
|
|
141
|
+
"Double",
|
|
142
|
+
"Bool",
|
|
143
|
+
"Date"
|
|
144
|
+
]);
|
|
127
145
|
const mapType = (schema, registry, hint) => {
|
|
146
|
+
const result = mapUnbrandedType(schema, registry, hint);
|
|
147
|
+
const brand = (0, kizunajs_generator.readMetaBrand)(schema);
|
|
148
|
+
if (brand === void 0 || !BRANDABLE_TYPES.has(result.expression)) return result;
|
|
149
|
+
const name = (0, kizunajs_generator.sanitizeIdentifier)(brand);
|
|
150
|
+
registry.addBrand({
|
|
151
|
+
name,
|
|
152
|
+
rawType: result.expression
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
expression: name,
|
|
156
|
+
optional: false
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
const mapUnbrandedType = (schema, registry, hint) => {
|
|
128
160
|
if ((0, kizunajs_generator.isFileSchema)(schema)) return {
|
|
129
161
|
expression: "MultipartFile",
|
|
130
162
|
optional: false,
|
|
@@ -862,6 +894,31 @@ const emitStruct = (writer, type, context, ownedTypeMap, ownedTypeLookup, regist
|
|
|
862
894
|
emitMemberwiseInit(writer, adjustedFields, (field) => field.deprecated ? deprecatedStorageName(field, type.fields) : escapeKeyword(field.name));
|
|
863
895
|
});
|
|
864
896
|
};
|
|
897
|
+
/**
|
|
898
|
+
* A brand as a struct around its value, encoded as the bare value.
|
|
899
|
+
*/
|
|
900
|
+
const emitBrand = (writer, brand) => {
|
|
901
|
+
writer.block(`public struct ${brand.name}: RawRepresentable, Codable, Hashable, Sendable`, () => {
|
|
902
|
+
writer.line(`public let rawValue: ${brand.rawType}`);
|
|
903
|
+
writer.blank();
|
|
904
|
+
writer.block(`public init(rawValue: ${brand.rawType})`, () => {
|
|
905
|
+
writer.line("self.rawValue = rawValue");
|
|
906
|
+
});
|
|
907
|
+
writer.blank();
|
|
908
|
+
writer.block(`public init(_ rawValue: ${brand.rawType})`, () => {
|
|
909
|
+
writer.line("self.rawValue = rawValue");
|
|
910
|
+
});
|
|
911
|
+
writer.blank();
|
|
912
|
+
writer.block("public init(from decoder: Decoder) throws", () => {
|
|
913
|
+
writer.line(`rawValue = try decoder.singleValueContainer().decode(${brand.rawType}.self)`);
|
|
914
|
+
});
|
|
915
|
+
writer.blank();
|
|
916
|
+
writer.block("public func encode(to encoder: Encoder) throws", () => {
|
|
917
|
+
writer.line("var container = encoder.singleValueContainer()");
|
|
918
|
+
writer.line("try container.encode(rawValue)");
|
|
919
|
+
});
|
|
920
|
+
});
|
|
921
|
+
};
|
|
865
922
|
const emitDiscriminatedEnum = (writer, type, context, registryName, ownedTypeMap, ownedTypeLookup) => {
|
|
866
923
|
const lookupName = registryName ?? type.name;
|
|
867
924
|
writer.block(`public enum ${type.name}: Codable, Sendable, Equatable`, () => {
|
|
@@ -2058,6 +2115,10 @@ const renderSwiftClient = (api, partition, registry, options) => {
|
|
|
2058
2115
|
};
|
|
2059
2116
|
writer.block(`public enum ${namespaceName}`, () => {
|
|
2060
2117
|
emitTypes(writer, topLevelSharedTypes, context, ownedTypeMap, ownedTypeLookup);
|
|
2118
|
+
for (const brand of registry.allBrands()) {
|
|
2119
|
+
writer.blank();
|
|
2120
|
+
emitBrand(writer, brand);
|
|
2121
|
+
}
|
|
2061
2122
|
});
|
|
2062
2123
|
emitClient(writer, {
|
|
2063
2124
|
clientName,
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_generator = require("./generator-
|
|
2
|
+
const require_generator = require("./generator-E0WQFf92.cjs");
|
|
3
3
|
let kizunajs_generator = require("kizunajs/generator");
|
|
4
4
|
let zod = require("zod");
|
|
5
5
|
//#region src/client-target.ts
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/swift",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.14",
|
|
4
4
|
"description": "Swift client generation for Kizuna.js APIs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -59,18 +59,18 @@
|
|
|
59
59
|
"access": "public"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@kizunajs/cli": "2.0.0-beta.
|
|
62
|
+
"@kizunajs/cli": "2.0.0-beta.14"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"zod": "^4.0.0",
|
|
66
|
-
"kizunajs": "2.0.0-beta.
|
|
66
|
+
"kizunajs": "2.0.0-beta.14"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/node": "^24",
|
|
70
70
|
"tsdown": "^0.21.0",
|
|
71
71
|
"typescript": "^6.0.3",
|
|
72
72
|
"zod": "^4.0.0",
|
|
73
|
-
"kizunajs": "2.0.0-beta.
|
|
73
|
+
"kizunajs": "2.0.0-beta.14"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"build": "tsdown src/index.ts src/cli.ts --format esm,cjs --dts --clean --external kizunajs --external @kizunajs/cli --external zod",
|