@ir-kit/openapi-ts-orpc 0.1.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/LICENSE.md +21 -0
- package/README.md +234 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +101 -0
- package/dist/config.js.map +1 -0
- package/dist/generators/client.d.ts +7 -0
- package/dist/generators/client.d.ts.map +1 -0
- package/dist/generators/client.js +113 -0
- package/dist/generators/client.js.map +1 -0
- package/dist/generators/contract-validator.d.ts +21 -0
- package/dist/generators/contract-validator.d.ts.map +1 -0
- package/dist/generators/contract-validator.js +250 -0
- package/dist/generators/contract-validator.js.map +1 -0
- package/dist/generators/contract.d.ts +9 -0
- package/dist/generators/contract.d.ts.map +1 -0
- package/dist/generators/contract.js +159 -0
- package/dist/generators/contract.js.map +1 -0
- package/dist/generators/faker.d.ts +15 -0
- package/dist/generators/faker.d.ts.map +1 -0
- package/dist/generators/faker.js +61 -0
- package/dist/generators/faker.js.map +1 -0
- package/dist/generators/handlers/ast.d.ts +65 -0
- package/dist/generators/handlers/ast.d.ts.map +1 -0
- package/dist/generators/handlers/ast.js +262 -0
- package/dist/generators/handlers/ast.js.map +1 -0
- package/dist/generators/handlers/index.d.ts +37 -0
- package/dist/generators/handlers/index.d.ts.map +1 -0
- package/dist/generators/handlers/index.js +48 -0
- package/dist/generators/handlers/index.js.map +1 -0
- package/dist/generators/index.d.ts +14 -0
- package/dist/generators/index.d.ts.map +1 -0
- package/dist/generators/index.js +13 -0
- package/dist/generators/index.js.map +1 -0
- package/dist/generators/router.d.ts +7 -0
- package/dist/generators/router.d.ts.map +1 -0
- package/dist/generators/router.js +21 -0
- package/dist/generators/router.js.map +1 -0
- package/dist/generators/server.d.ts +7 -0
- package/dist/generators/server.d.ts.map +1 -0
- package/dist/generators/server.js +22 -0
- package/dist/generators/server.js.map +1 -0
- package/dist/generators/tanstack.d.ts +7 -0
- package/dist/generators/tanstack.d.ts.map +1 -0
- package/dist/generators/tanstack.js +27 -0
- package/dist/generators/tanstack.js.map +1 -0
- package/dist/generators/types.d.ts +74 -0
- package/dist/generators/types.d.ts.map +1 -0
- package/dist/generators/types.js +2 -0
- package/dist/generators/types.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +7 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +124 -0
- package/dist/plugin.js.map +1 -0
- package/dist/router-organizer.d.ts +13 -0
- package/dist/router-organizer.d.ts.map +1 -0
- package/dist/router-organizer.js +42 -0
- package/dist/router-organizer.js.map +1 -0
- package/dist/schema/extractor.d.ts +16 -0
- package/dist/schema/extractor.d.ts.map +1 -0
- package/dist/schema/extractor.js +133 -0
- package/dist/schema/extractor.js.map +1 -0
- package/dist/schema/index.d.ts +2 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +2 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/symbols/external.d.ts +8 -0
- package/dist/symbols/external.d.ts.map +1 -0
- package/dist/symbols/external.js +67 -0
- package/dist/symbols/external.js.map +1 -0
- package/dist/types.d.ts +238 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +13 -0
- package/dist/utils.js.map +1 -0
- package/docs/typia.md +152 -0
- package/package.json +101 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard validator API contract builders.
|
|
3
|
+
* Works with any hey-api validator plugin (zod, valibot, arktype)
|
|
4
|
+
* that exposes the `createRequestSchema` API.
|
|
5
|
+
*/
|
|
6
|
+
import { $ } from "@hey-api/openapi-ts";
|
|
7
|
+
import { operationResponsesMap } from "@hey-api/shared";
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Public API
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
/** Classify the body media-type so callers don't pass loose booleans. */
|
|
12
|
+
export function classifyBody(mediaType) {
|
|
13
|
+
switch (mediaType) {
|
|
14
|
+
case "application/json":
|
|
15
|
+
return "json";
|
|
16
|
+
case "application/octet-stream":
|
|
17
|
+
return "raw-file";
|
|
18
|
+
case "multipart/form-data":
|
|
19
|
+
return "multipart";
|
|
20
|
+
default:
|
|
21
|
+
return "other";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Build a validator-backed `.input()` expression for a contract. */
|
|
25
|
+
export function buildValidatorInput(plugin, operation, bodyKind) {
|
|
26
|
+
const validatorName = plugin.config.validator.input;
|
|
27
|
+
if (!validatorName)
|
|
28
|
+
return null;
|
|
29
|
+
const requestSchema = callCreateRequestSchema(plugin, validatorName, operation, bodyKind);
|
|
30
|
+
// Typia validators are Symbol references to pre-emitted `createValidate<T>()`
|
|
31
|
+
// calls with the full request shape baked into the TS type argument. Body
|
|
32
|
+
// kind-specific patching (e.g. replacing a body property with `z.file()`)
|
|
33
|
+
// doesn't apply — the TS type already models File/Blob where appropriate.
|
|
34
|
+
if (validatorName === "@ir-kit/openapi-ts-typia") {
|
|
35
|
+
return requestSchema
|
|
36
|
+
? { expr: requestSchema, useDetailedMode: true }
|
|
37
|
+
: null;
|
|
38
|
+
}
|
|
39
|
+
switch (bodyKind) {
|
|
40
|
+
case "raw-file":
|
|
41
|
+
return patchRawFileBody(plugin, validatorName, operation, requestSchema);
|
|
42
|
+
case "multipart":
|
|
43
|
+
return patchMultipartBody(plugin, validatorName, operation, requestSchema);
|
|
44
|
+
default:
|
|
45
|
+
return requestSchema
|
|
46
|
+
? { expr: requestSchema, useDetailedMode: true }
|
|
47
|
+
: null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function buildValidatorOutput(plugin, operationId) {
|
|
51
|
+
const validatorName = plugin.config.validator.output;
|
|
52
|
+
if (!validatorName)
|
|
53
|
+
return null;
|
|
54
|
+
// Typia plugin emits a flattened `role: 'response'` symbol per operation.
|
|
55
|
+
// Zod/valibot/arktype emit the unflattened `role: 'responses'`.
|
|
56
|
+
const role = validatorName === "@ir-kit/openapi-ts-typia" ? "response" : "responses";
|
|
57
|
+
return plugin.referenceSymbol({
|
|
58
|
+
category: "schema",
|
|
59
|
+
resource: "operation",
|
|
60
|
+
resourceId: operationId,
|
|
61
|
+
role,
|
|
62
|
+
tool: validatorName,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export function buildValidatorErrorMap(plugin, operation) {
|
|
66
|
+
const validatorName = plugin.config.validator.input;
|
|
67
|
+
if (!validatorName)
|
|
68
|
+
return null;
|
|
69
|
+
return validatorName === "@ir-kit/openapi-ts-typia"
|
|
70
|
+
? buildTypiaErrorMap(plugin, operation)
|
|
71
|
+
: buildComponentErrorMap(plugin, validatorName, operation);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Typia plugin emits one `error-<code>` validator per error status per
|
|
75
|
+
* operation (no per-$ref symbols). Look up each status's symbol and
|
|
76
|
+
* assemble `.errors({ 404: { data: tFooError404 }, ... })`.
|
|
77
|
+
*/
|
|
78
|
+
function buildTypiaErrorMap(plugin, operation) {
|
|
79
|
+
const responses = operation.responses;
|
|
80
|
+
if (!responses)
|
|
81
|
+
return null;
|
|
82
|
+
let errorMapObj = $.object().pretty();
|
|
83
|
+
let hasErrors = false;
|
|
84
|
+
for (const statusCode in responses) {
|
|
85
|
+
const status = Number.parseInt(statusCode, 10);
|
|
86
|
+
if (!Number.isFinite(status))
|
|
87
|
+
continue;
|
|
88
|
+
if (status < 400 || status > 599)
|
|
89
|
+
continue;
|
|
90
|
+
const errorSymbol = plugin.querySymbol({
|
|
91
|
+
category: "schema",
|
|
92
|
+
resource: "operation",
|
|
93
|
+
resourceId: operation.id,
|
|
94
|
+
role: `error-${status}`,
|
|
95
|
+
tool: "@ir-kit/openapi-ts-typia",
|
|
96
|
+
});
|
|
97
|
+
if (!errorSymbol)
|
|
98
|
+
continue;
|
|
99
|
+
errorMapObj = errorMapObj.prop(statusCode, $.object().prop("data", $(errorSymbol)));
|
|
100
|
+
hasErrors = true;
|
|
101
|
+
}
|
|
102
|
+
return hasErrors ? errorMapObj : null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Standard validators (zod/valibot/arktype) emit per-component symbols
|
|
106
|
+
* at each `$ref`. Map each error response to its component symbol.
|
|
107
|
+
*/
|
|
108
|
+
function buildComponentErrorMap(plugin, validatorName, operation) {
|
|
109
|
+
const { errors: errorsSchema } = operationResponsesMap(operation);
|
|
110
|
+
if (!errorsSchema?.properties)
|
|
111
|
+
return null;
|
|
112
|
+
let errorMapObj = $.object().pretty();
|
|
113
|
+
let hasErrors = false;
|
|
114
|
+
for (const statusCode in errorsSchema.properties) {
|
|
115
|
+
const errorResponseSchema = errorsSchema.properties[statusCode];
|
|
116
|
+
if (!errorResponseSchema?.$ref)
|
|
117
|
+
continue;
|
|
118
|
+
const errorSchema = plugin.querySymbol({
|
|
119
|
+
resource: "definition",
|
|
120
|
+
resourceId: errorResponseSchema.$ref,
|
|
121
|
+
tool: validatorName,
|
|
122
|
+
});
|
|
123
|
+
if (errorSchema) {
|
|
124
|
+
errorMapObj = errorMapObj.prop(statusCode, $.object().prop("data", $(errorSchema)));
|
|
125
|
+
hasErrors = true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return hasErrors ? errorMapObj : null;
|
|
129
|
+
}
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
// createRequestSchema wrapper
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
/**
|
|
134
|
+
* Call the validator plugin's `createRequestSchema` API when available.
|
|
135
|
+
* For raw-file bodies the body layer is skipped (replaced later with z.file()).
|
|
136
|
+
* For multipart the body layer is kept so field names are preserved.
|
|
137
|
+
*/
|
|
138
|
+
function callCreateRequestSchema(plugin, validatorName, operation, bodyKind) {
|
|
139
|
+
const validatorPlugin = plugin.getPlugin(validatorName);
|
|
140
|
+
const api = validatorPlugin?.api;
|
|
141
|
+
if (!api || !("createRequestSchema" in api))
|
|
142
|
+
return null;
|
|
143
|
+
return api.createRequestSchema({
|
|
144
|
+
layers: {
|
|
145
|
+
body: bodyKind === "raw-file" ? false : { whenEmpty: "omit" },
|
|
146
|
+
headers: { whenEmpty: "omit" },
|
|
147
|
+
path: { whenEmpty: "omit", as: "params" },
|
|
148
|
+
query: { whenEmpty: "omit" },
|
|
149
|
+
},
|
|
150
|
+
operation,
|
|
151
|
+
plugin: validatorPlugin,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// File schema helpers
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
/**
|
|
158
|
+
* Produce the validator-native file schema expression oRPC can detect.
|
|
159
|
+
*
|
|
160
|
+
* zod v4 → z.file() (native)
|
|
161
|
+
* zod v3 → oz.file() (@orpc/zod)
|
|
162
|
+
* valibot → v.file() (native)
|
|
163
|
+
*
|
|
164
|
+
* Returns null for unknown validators.
|
|
165
|
+
*/
|
|
166
|
+
function fileSchemaExpr(plugin, validatorName) {
|
|
167
|
+
if (validatorName === "zod") {
|
|
168
|
+
const zodPlugin = plugin.getPlugin("zod");
|
|
169
|
+
const compat = zodPlugin?.config?.compatibilityVersion;
|
|
170
|
+
if (compat === 4) {
|
|
171
|
+
return $(plugin.external("zod.z")).attr("file").call();
|
|
172
|
+
}
|
|
173
|
+
return $(plugin.external("@orpc/zod.oz")).attr("file").call();
|
|
174
|
+
}
|
|
175
|
+
if (validatorName === "valibot") {
|
|
176
|
+
return $(plugin.external("valibot.*")).attr("file").call();
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
/** Wrap a file expression with `.optional()` when the field isn't required. */
|
|
181
|
+
function optionalFile(fileExpr, required) {
|
|
182
|
+
return required ? fileExpr : fileExpr.attr("optional").call();
|
|
183
|
+
}
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
// Body patching strategies
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
/**
|
|
188
|
+
* application/octet-stream — body IS the file.
|
|
189
|
+
* Replace the entire body layer with z.file() / oz.file().
|
|
190
|
+
*/
|
|
191
|
+
function patchRawFileBody(plugin, validatorName, operation, requestSchema) {
|
|
192
|
+
const file = fileSchemaExpr(plugin, validatorName);
|
|
193
|
+
if (!file)
|
|
194
|
+
return null;
|
|
195
|
+
const body = optionalFile(file, !!operation.body?.required);
|
|
196
|
+
if (requestSchema) {
|
|
197
|
+
return {
|
|
198
|
+
expr: $(requestSchema).attr("extend").call($.object().prop("body", body)),
|
|
199
|
+
useDetailedMode: true,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
// createRequestSchema unavailable — compact mode with just the file.
|
|
203
|
+
return { expr: body, useDetailedMode: false };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* multipart/form-data — body is an object with named fields.
|
|
207
|
+
* Only the `format: "binary"` properties are patched to z.file();
|
|
208
|
+
* every other field and the object wrapper are preserved.
|
|
209
|
+
*/
|
|
210
|
+
function patchMultipartBody(plugin, validatorName, operation, requestSchema) {
|
|
211
|
+
if (!requestSchema)
|
|
212
|
+
return null;
|
|
213
|
+
const bodyProps = operation.body?.schema?.properties;
|
|
214
|
+
if (!bodyProps)
|
|
215
|
+
return { expr: requestSchema, useDetailedMode: true };
|
|
216
|
+
const file = fileSchemaExpr(plugin, validatorName);
|
|
217
|
+
if (!file)
|
|
218
|
+
return { expr: requestSchema, useDetailedMode: true };
|
|
219
|
+
// Build an .extend() object with only the binary fields replaced.
|
|
220
|
+
const requiredFields = operation.body?.schema?.required;
|
|
221
|
+
let overrides = $.object();
|
|
222
|
+
let count = 0;
|
|
223
|
+
for (const [name, schema] of Object.entries(bodyProps)) {
|
|
224
|
+
if (schema.format !== "binary")
|
|
225
|
+
continue;
|
|
226
|
+
const isRequired = Array.isArray(requiredFields) && requiredFields.includes(name);
|
|
227
|
+
overrides = overrides.prop(name, optionalFile(file, isRequired));
|
|
228
|
+
count++;
|
|
229
|
+
}
|
|
230
|
+
if (count === 0)
|
|
231
|
+
return { expr: requestSchema, useDetailedMode: true };
|
|
232
|
+
// Reference the body schema symbol, then .extend() only the binary fields.
|
|
233
|
+
const bodySym = plugin.referenceSymbol({
|
|
234
|
+
category: "schema",
|
|
235
|
+
resource: "operation",
|
|
236
|
+
resourceId: operation.id,
|
|
237
|
+
role: "request-body",
|
|
238
|
+
tool: validatorName,
|
|
239
|
+
});
|
|
240
|
+
if (!bodySym)
|
|
241
|
+
return { expr: requestSchema, useDetailedMode: true };
|
|
242
|
+
const patchedBody = $(bodySym).attr("extend").call(overrides);
|
|
243
|
+
return {
|
|
244
|
+
expr: $(requestSchema)
|
|
245
|
+
.attr("extend")
|
|
246
|
+
.call($.object().prop("body", patchedBody)),
|
|
247
|
+
useDetailedMode: true,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=contract-validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract-validator.js","sourceRoot":"","sources":["../../src/generators/contract-validator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAC;AAExC,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAaxD,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E,yEAAyE;AACzE,MAAM,UAAU,YAAY,CAAC,SAA6B;IACxD,QAAQ,SAAS,EAAE,CAAC;QAClB,KAAK,kBAAkB;YACrB,OAAO,MAAM,CAAC;QAChB,KAAK,0BAA0B;YAC7B,OAAO,UAAU,CAAC;QACpB,KAAK,qBAAqB;YACxB,OAAO,WAAW,CAAC;QACrB;YACE,OAAO,OAAO,CAAC;IACnB,CAAC;A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,OAAO,GAAG,MAAM,CAAC,eAAe,CAAC;QACrC,QAAQ,EAAE,QAAQ;QAClB,QAAQ,EAAE,WAAW;QACrB,UAAU,EAAE,SAAS,CAAC,EAAG;QACzB,IAAI,EAAE,cAAc;QACpB,IAAI,EAAE,aAAa;KACpB,CAAC,CAAC;IAEH,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC;IAEpE,MAAM,WAAW,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC9D,OAAO;QACL,IAAI,EAAE,CAAC,CAAC,aAAa,CAAC;aACnB,IAAI,CAAC,QAAQ,CAAC;aACd,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAC7C,eAAe,EAAE,IAAI;KACtB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates oRPC contracts from OpenAPI operations.
|
|
3
|
+
* Input/output schemas come from the configured validator plugin via its
|
|
4
|
+
* standard `createRequestSchema` / symbol-lookup API — identical code path
|
|
5
|
+
* for zod, valibot, arktype, and typia.
|
|
6
|
+
*/
|
|
7
|
+
import type { ContractGeneratorInput, ContractGeneratorOutput } from "./types";
|
|
8
|
+
export declare const generateContracts: ({ contractFile, plugin, }: ContractGeneratorInput) => ContractGeneratorOutput;
|
|
9
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/generators/contract.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAE/E,eAAO,MAAM,iBAAiB,GAAI,2BAG/B,sBAAsB,KAAG,uBA6K3B,CAAC"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates oRPC contracts from OpenAPI operations.
|
|
3
|
+
* Input/output schemas come from the configured validator plugin via its
|
|
4
|
+
* standard `createRequestSchema` / symbol-lookup API — identical code path
|
|
5
|
+
* for zod, valibot, arktype, and typia.
|
|
6
|
+
*/
|
|
7
|
+
import { $ } from "@hey-api/openapi-ts";
|
|
8
|
+
import { escapeComment } from "@hey-api/shared";
|
|
9
|
+
import { getGroupKey } from "../router-organizer";
|
|
10
|
+
import { contractName as toContractName } from "../utils";
|
|
11
|
+
import { buildValidatorErrorMap, buildValidatorInput, buildValidatorOutput, classifyBody, } from "./contract-validator";
|
|
12
|
+
export const generateContracts = ({ contractFile, plugin, }) => {
|
|
13
|
+
const routerStructure = new Map();
|
|
14
|
+
const oc = plugin.external("@orpc/contract.oc");
|
|
15
|
+
const { input: inputValidator, output: outputValidator } = plugin.config.validator;
|
|
16
|
+
plugin.forEach("operation", (event) => {
|
|
17
|
+
const { operation } = event;
|
|
18
|
+
if (!operation.id)
|
|
19
|
+
return;
|
|
20
|
+
const operationId = operation.id;
|
|
21
|
+
const method = operation.method.toUpperCase();
|
|
22
|
+
const path = operation.path;
|
|
23
|
+
const contractName = `${toContractName(operationId, plugin.config.naming.contract)}Contract`;
|
|
24
|
+
const hasInput = !!operation.body || !!operation.parameters;
|
|
25
|
+
let hasOutput = false;
|
|
26
|
+
if (operation.responses) {
|
|
27
|
+
for (const [statusCode, response] of Object.entries(operation.responses)) {
|
|
28
|
+
const code = Number.parseInt(statusCode, 10);
|
|
29
|
+
if (code >= 200 &&
|
|
30
|
+
code <= 399 &&
|
|
31
|
+
response?.mediaType &&
|
|
32
|
+
response?.schema) {
|
|
33
|
+
hasOutput = true;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
let successStatus;
|
|
39
|
+
let successDescription;
|
|
40
|
+
if (operation.responses) {
|
|
41
|
+
const pathItem = plugin.context.spec.paths?.[operation.path];
|
|
42
|
+
const openApiOperation = pathItem?.[operation.method];
|
|
43
|
+
const responses = openApiOperation?.responses;
|
|
44
|
+
for (const statusCode in operation.responses) {
|
|
45
|
+
const numericStatus = Number.parseInt(statusCode, 10);
|
|
46
|
+
if (numericStatus >= 200 && numericStatus < 300) {
|
|
47
|
+
successStatus = numericStatus;
|
|
48
|
+
if (responses) {
|
|
49
|
+
const response = responses[statusCode];
|
|
50
|
+
if (response && "$ref" in response) {
|
|
51
|
+
const resolved = plugin.context.resolveRef(response.$ref);
|
|
52
|
+
successDescription = resolved?.description;
|
|
53
|
+
}
|
|
54
|
+
else if (response && "description" in response) {
|
|
55
|
+
successDescription = response.description;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const groupMode = plugin.config.group;
|
|
62
|
+
const groupKey = getGroupKey(operation, groupMode);
|
|
63
|
+
const contractFilePath = groupMode === "flat"
|
|
64
|
+
? contractFile
|
|
65
|
+
: `${plugin.name}/${groupKey}/contract`;
|
|
66
|
+
const symbol = plugin.symbol(contractName, {
|
|
67
|
+
getFilePath: () => contractFilePath,
|
|
68
|
+
meta: {
|
|
69
|
+
category: "contract",
|
|
70
|
+
resource: "operation",
|
|
71
|
+
resourceId: operationId,
|
|
72
|
+
tool: "orpc",
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
let routeObj = $.object()
|
|
76
|
+
.pretty()
|
|
77
|
+
.prop("method", $.literal(method))
|
|
78
|
+
.prop("path", $.literal(path));
|
|
79
|
+
if (operation.operationId)
|
|
80
|
+
routeObj = routeObj.prop("operationId", $.literal(operation.operationId));
|
|
81
|
+
if (operation.summary)
|
|
82
|
+
routeObj = routeObj.prop("summary", $.literal(operation.summary));
|
|
83
|
+
if (operation.description)
|
|
84
|
+
routeObj = routeObj.prop("description", $.literal(operation.description));
|
|
85
|
+
if (operation.deprecated)
|
|
86
|
+
routeObj = routeObj.prop("deprecated", $.literal(true));
|
|
87
|
+
if (operation.tags)
|
|
88
|
+
routeObj = routeObj.prop("tags", $.fromValue([...new Set(operation.tags)]));
|
|
89
|
+
if (successStatus !== undefined)
|
|
90
|
+
routeObj = routeObj.prop("successStatus", $.literal(successStatus));
|
|
91
|
+
if (successDescription)
|
|
92
|
+
routeObj = routeObj.prop("successDescription", $.literal(successDescription));
|
|
93
|
+
const mode = plugin.config.mode;
|
|
94
|
+
const isGetOrDelete = operation.method === "get" || operation.method === "delete";
|
|
95
|
+
const hasBody = !!(operation.body && !isGetOrDelete);
|
|
96
|
+
const bodyKind = hasBody
|
|
97
|
+
? classifyBody(operation.body?.mediaType)
|
|
98
|
+
: "other";
|
|
99
|
+
let useDetailedMode = mode === "detailed";
|
|
100
|
+
let inputExpr;
|
|
101
|
+
if (hasInput && inputValidator) {
|
|
102
|
+
const result = buildValidatorInput(plugin, operation, bodyKind);
|
|
103
|
+
if (result) {
|
|
104
|
+
inputExpr = result.expr;
|
|
105
|
+
useDetailedMode = result.useDetailedMode;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (useDetailedMode && hasInput) {
|
|
109
|
+
routeObj = routeObj.prop("inputStructure", $.literal("detailed"));
|
|
110
|
+
}
|
|
111
|
+
let contractExpr = $(oc).attr("route").call(routeObj);
|
|
112
|
+
if (inputExpr)
|
|
113
|
+
contractExpr = contractExpr.attr("input").call(inputExpr);
|
|
114
|
+
if (hasOutput && outputValidator) {
|
|
115
|
+
const outputExpr = buildValidatorOutput(plugin, operationId);
|
|
116
|
+
if (outputExpr)
|
|
117
|
+
contractExpr = contractExpr.attr("output").call(outputExpr);
|
|
118
|
+
}
|
|
119
|
+
if (inputValidator) {
|
|
120
|
+
const errorMap = buildValidatorErrorMap(plugin, operation);
|
|
121
|
+
if (errorMap)
|
|
122
|
+
contractExpr = contractExpr.attr("errors").call(errorMap);
|
|
123
|
+
}
|
|
124
|
+
const comment = plugin.config.comments
|
|
125
|
+
? createOperationComment(operation)
|
|
126
|
+
: undefined;
|
|
127
|
+
plugin.node($.const(symbol)
|
|
128
|
+
.export()
|
|
129
|
+
.$if(comment, (n, v) => n.doc(v))
|
|
130
|
+
.assign(contractExpr));
|
|
131
|
+
const operationName = operation.operationId || operationId;
|
|
132
|
+
if (!routerStructure.has(groupKey))
|
|
133
|
+
routerStructure.set(groupKey, []);
|
|
134
|
+
routerStructure.get(groupKey).push({
|
|
135
|
+
contractName,
|
|
136
|
+
contractSymbol: symbol,
|
|
137
|
+
operation,
|
|
138
|
+
operationName,
|
|
139
|
+
});
|
|
140
|
+
}, { order: "declarations" });
|
|
141
|
+
return { routerStructure };
|
|
142
|
+
};
|
|
143
|
+
function createOperationComment(operation) {
|
|
144
|
+
const comments = [];
|
|
145
|
+
if (operation.summary)
|
|
146
|
+
comments.push(escapeComment(operation.summary));
|
|
147
|
+
if (operation.description) {
|
|
148
|
+
if (comments.length)
|
|
149
|
+
comments.push("");
|
|
150
|
+
comments.push(escapeComment(operation.description));
|
|
151
|
+
}
|
|
152
|
+
if (operation.deprecated) {
|
|
153
|
+
if (comments.length)
|
|
154
|
+
comments.push("");
|
|
155
|
+
comments.push("@deprecated");
|
|
156
|
+
}
|
|
157
|
+
return comments.length ? comments : undefined;
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/generators/contract.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAC;AAExC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,YAAY,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EACL,sBAAsB,EACtB,mBAAmB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,WAAW,CAAC;QAC3D,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC;YAAE,eAAe,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QACtE,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC,IAAI,CAAC;YAClC,YAAY;YACZ,cAAc,EAAE,MAAM;YACtB,SAAS;YACT,aAAa;SACd,CAAC,CAAC;IACL,CAAC,EACD,EAAE,KAAK,EAAE,cAAc,EAAE,CAC1B,CAAC;IAEF,OAAO,EAAE,eAAe,EAAE,CAAC;AAC7B,CAAC,CAAC;AAEF,SAAS,sBAAsB,CAC7B,SAA6B;IAE7B,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,SAAS,CAAC,OAAO;QAAE,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IACvE,IAAI,SAAS,CAAC,WAAW,EAAE,CAAC;QAC1B,IAAI,QAAQ,CAAC,MAAM;YAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACvC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;IACtD,CAAC;IACD,IAAI,SAAS,CAAC,UAAU,EAAE,CAAC;QACzB,IAAI,QAAQ,CAAC,MAAM;YAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACvC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ResponseSchemaInfo } from "@ir-kit/openapi-ts-faker/core";
|
|
2
|
+
import type { RouterNode } from "../router-organizer";
|
|
3
|
+
import type { FakerHandlerConfig } from "../types";
|
|
4
|
+
import type { GeneratorContext } from "./types";
|
|
5
|
+
export interface FakerGeneratorInput {
|
|
6
|
+
plugin: GeneratorContext["plugin"];
|
|
7
|
+
routerStructure: Map<string, RouterNode[]>;
|
|
8
|
+
responseSchemas: Map<string, ResponseSchemaInfo>;
|
|
9
|
+
config?: FakerHandlerConfig;
|
|
10
|
+
}
|
|
11
|
+
export interface FakerGeneratorOutput {
|
|
12
|
+
factoryNames: Map<string, string>;
|
|
13
|
+
}
|
|
14
|
+
export declare const generateFakerFactories: ({ plugin, routerStructure, responseSchemas, config, }: FakerGeneratorInput) => FakerGeneratorOutput;
|
|
15
|
+
//# sourceMappingURL=faker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"faker.d.ts","sourceRoot":"","sources":["../../src/generators/faker.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,kBAAkB,EACxB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IACnC,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAC3C,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACjD,MAAM,CAAC,EAAE,kBAAkB,CAAC;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACnC,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC;AAmBD,eAAO,MAAM,sBAAsB,GAAI,uDAKpC,mBAAmB,KAAG,oBAgDxB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { $ } from "@hey-api/openapi-ts";
|
|
2
|
+
import { buildFakerExpression, } from "@ir-kit/openapi-ts-faker/core";
|
|
3
|
+
import { operationName } from "../utils";
|
|
4
|
+
/**
|
|
5
|
+
* Match the faker plugin's hint-key normalization (case- and separator-
|
|
6
|
+
* insensitive) so users can write `firstName`, `first_name`, `FirstName`, etc.
|
|
7
|
+
* The faker plugin does this in its `resolveConfig` hook; we apply it here
|
|
8
|
+
* because we feed the lib directly without going through that hook.
|
|
9
|
+
*/
|
|
10
|
+
const normalizeFieldNameHints = (hints) => {
|
|
11
|
+
if (!hints)
|
|
12
|
+
return undefined;
|
|
13
|
+
const out = {};
|
|
14
|
+
for (const [key, value] of Object.entries(hints)) {
|
|
15
|
+
out[key.toLowerCase().replace(/[_-]/g, "")] = value;
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
};
|
|
19
|
+
export const generateFakerFactories = ({ plugin, routerStructure, responseSchemas, config, }) => {
|
|
20
|
+
const faker = plugin.external("@faker-js/faker.faker");
|
|
21
|
+
const factoryNames = new Map();
|
|
22
|
+
const buildOpts = {
|
|
23
|
+
fieldHints: normalizeFieldNameHints(config?.fieldNameHints),
|
|
24
|
+
formatHints: config?.formatMapping,
|
|
25
|
+
respectConstraints: config?.respectConstraints ?? true,
|
|
26
|
+
};
|
|
27
|
+
for (const [group, nodes] of routerStructure) {
|
|
28
|
+
const fakerFile = `${plugin.name}/${group}/faker.gen`;
|
|
29
|
+
for (const node of nodes) {
|
|
30
|
+
const opName = operationName(node.operationName);
|
|
31
|
+
const schema = responseSchemas.get(opName);
|
|
32
|
+
const factoryName = `mock${opName.charAt(0).toUpperCase()}${opName.slice(1)}`;
|
|
33
|
+
const symbol = plugin.symbol(factoryName, {
|
|
34
|
+
getFilePath: () => fakerFile,
|
|
35
|
+
meta: {
|
|
36
|
+
category: "faker",
|
|
37
|
+
resource: "factory",
|
|
38
|
+
resourceId: opName,
|
|
39
|
+
tool: "orpc",
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
factoryNames.set(opName, factoryName);
|
|
43
|
+
let bodyExpr;
|
|
44
|
+
if (schema && Object.keys(schema.properties).length > 0) {
|
|
45
|
+
let obj = $.object().pretty();
|
|
46
|
+
for (const [key, info] of Object.entries(schema.properties)) {
|
|
47
|
+
obj = obj.prop(key, buildFakerExpression(faker, info, buildOpts));
|
|
48
|
+
}
|
|
49
|
+
bodyExpr = obj;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
bodyExpr = $.object();
|
|
53
|
+
}
|
|
54
|
+
const factoryFn = $.func().do($.return(bodyExpr));
|
|
55
|
+
const statement = $.const(symbol).export().assign(factoryFn);
|
|
56
|
+
plugin.node(statement);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { factoryNames };
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=faker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"faker.js","sourceRoot":"","sources":["../../src/generators/faker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAC;AACxC,OAAO,EAEL,oBAAoB,GAGrB,MAAM,+BAA+B,CAAC;AAIvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAczC;;;;;GAKG;AACH,MAAM,uBAAuB,GAAG,CAC9B,KAAiC,EACL,EAAE;IAC9B,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,GAAG,GAAmB,EAAE,CAAC;IAC/B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACjD,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC;IACtD,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,EAC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AU,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACxD,IAAI,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC;gBAC9B,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;oBAC5D,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,oBAAoB,CAAC,KAAK,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;gBACpE,CAAC;gBACD,QAAQ,GAAG,GAAG,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACN,QAAQ,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;YACxB,CAAC;YAED,MAAM,SAAS,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;YAClD,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAC7D,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,CAAC;AAC1B,CAAC,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript AST builders for handler file generation.
|
|
3
|
+
* Constructs import declarations, handler property assignments, and
|
|
4
|
+
* complete handler file ASTs using the TypeScript compiler API.
|
|
5
|
+
*/
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
import type { HandlerMode, ProxyHandlerConfig } from "../../types";
|
|
8
|
+
export type { PropertyInfo, ResponseSchemaInfo, } from "@ir-kit/openapi-ts-faker/core";
|
|
9
|
+
export interface FileGenContext {
|
|
10
|
+
tag: string;
|
|
11
|
+
procedures: string[];
|
|
12
|
+
implementer: {
|
|
13
|
+
name: string;
|
|
14
|
+
from: string;
|
|
15
|
+
};
|
|
16
|
+
mode: HandlerMode;
|
|
17
|
+
clientName: string;
|
|
18
|
+
proxyConfig?: ProxyHandlerConfig;
|
|
19
|
+
fakerFactoryNames?: Map<string, string>;
|
|
20
|
+
fakerGenImport?: string;
|
|
21
|
+
}
|
|
22
|
+
/** Returns a safe variable/export name — appends `Handlers` when tag is reserved. */
|
|
23
|
+
export declare function safeVarName(tag: string): string;
|
|
24
|
+
export declare function makeImport(names: string[], from: string): ts.ImportDeclaration;
|
|
25
|
+
interface HandlerPropertyOpts {
|
|
26
|
+
tag: string;
|
|
27
|
+
procedure: string;
|
|
28
|
+
implementerName: string;
|
|
29
|
+
mode: HandlerMode;
|
|
30
|
+
clientName: string;
|
|
31
|
+
factoryName?: string;
|
|
32
|
+
}
|
|
33
|
+
export declare function makeHandlerProperty({ tag, procedure, implementerName, mode, clientName, factoryName, }: HandlerPropertyOpts): ts.PropertyAssignment;
|
|
34
|
+
export declare function getImports(ctx: FileGenContext): ts.ImportDeclaration[];
|
|
35
|
+
/**
|
|
36
|
+
* Generates a complete handler file.
|
|
37
|
+
* The full source file is constructed as a TypeScript AST and printed with
|
|
38
|
+
* `ts.createPrinter` — no template strings involved.
|
|
39
|
+
*/
|
|
40
|
+
export declare function buildFile(ctx: FileGenContext): string;
|
|
41
|
+
export interface HandlerObjectInfo {
|
|
42
|
+
/** Property names already declared in the handler object. */
|
|
43
|
+
existingNames: Set<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Character position of the closing `}` of the handler object literal.
|
|
46
|
+
* New properties are inserted just before this position.
|
|
47
|
+
*/
|
|
48
|
+
closingBracePos: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parses the source file and locates the top-level handler object
|
|
52
|
+
* (`const <tag> = { ... }`), returning the existing property names
|
|
53
|
+
* and the insertion position for new properties.
|
|
54
|
+
*/
|
|
55
|
+
export declare function analyzeHandlerFile(source: string, varName: string): HandlerObjectInfo | null;
|
|
56
|
+
/**
|
|
57
|
+
* Inserts missing procedure stubs into an existing handler file.
|
|
58
|
+
*
|
|
59
|
+
* Detection is AST-based (TypeScript compiler API) — property names are
|
|
60
|
+
* extracted from the parsed object literal, not by string search.
|
|
61
|
+
* The original file content is preserved for all existing code; only the
|
|
62
|
+
* new stubs are spliced in at the exact character position of the closing `}`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function patchFile(source: string, ctx: FileGenContext, missing: string[]): string;
|
|
65
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/generators/handlers/ast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEnE,YAAY,EACV,YAAY,EACZ,kBAAkB,GACnB,MAAM,+BAA+B,CAAC;AAMvC,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,WAAW,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,IAAI,EAAE,WAAW,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAuDD,qFAAqF;AACrF,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAoBD,wBAAgB,UAAU,CACxB,KAAK,EAAE,MAAM,EAAE,EACf,IAAI,EAAE,MAAM,GACX,EAAE,CAAC,iBAAiB,CAmBtB;AAkDD,UAA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