@sohcah/openapi-generator 0.2.2 → 0.3.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/dist/{chunk-DYFKRlHt.js → chunk-BcJDCUAU.mjs} +24 -15
- package/dist/cli.d.mts +1 -0
- package/dist/{cli.js → cli.mjs} +8 -9
- package/dist/cli.mjs.map +1 -0
- package/dist/{config.d.ts → config.d.mts} +3 -3
- package/dist/config.d.mts.map +1 -0
- package/dist/{config.js → config.mjs} +1 -1
- package/dist/config.mjs.map +1 -0
- package/dist/generators/{index.d.ts → index.d.mts} +2 -2
- package/dist/generators/index.d.mts.map +1 -0
- package/dist/generators/index.mjs +739 -0
- package/dist/generators/index.mjs.map +1 -0
- package/dist/{helpers-CqzmoMek.js → helpers-BwQQbllq.mjs} +1944 -1901
- package/dist/helpers-BwQQbllq.mjs.map +1 -0
- package/dist/{index-nKXk7_RC.d.ts → index-D4xeswCF.d.mts} +4 -4
- package/dist/index-D4xeswCF.d.mts.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +4 -0
- package/dist/{src-BhVleMDu.js → src-9VHrwwX3.mjs} +1316 -1436
- package/dist/src-9VHrwwX3.mjs.map +1 -0
- package/dist/{types-CHhQKnbs.d.ts → types-C83IhMq9.d.mts} +2 -2
- package/dist/types-C83IhMq9.d.mts.map +1 -0
- package/package.json +29 -29
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js.map +0 -1
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/generators/index.d.ts.map +0 -1
- package/dist/generators/index.js +0 -748
- package/dist/generators/index.js.map +0 -1
- package/dist/helpers-CqzmoMek.js.map +0 -1
- package/dist/index-nKXk7_RC.d.ts.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -4
- package/dist/src-BhVleMDu.js.map +0 -1
- package/dist/types-CHhQKnbs.d.ts.map +0 -1
package/dist/generators/index.js
DELETED
|
@@ -1,748 +0,0 @@
|
|
|
1
|
-
import { __toESM } from "../chunk-DYFKRlHt.js";
|
|
2
|
-
import { DocumentContext, NotImplementedError, ensureImport, ensureNamespaceImport, getKey, helpers_exports, notImplementedStatement, require_lib } from "../helpers-CqzmoMek.js";
|
|
3
|
-
import { Context, Effect } from "effect";
|
|
4
|
-
import escapeRegex from "regex-escape";
|
|
5
|
-
|
|
6
|
-
//#region src/generators/requestFormats/json.ts
|
|
7
|
-
var import_lib$8 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
8
|
-
const generateJsonRequestCodec = Effect.fn(function* (options, schema, decoded) {
|
|
9
|
-
return options.transformer({
|
|
10
|
-
encoded: options.schema.string,
|
|
11
|
-
decoded,
|
|
12
|
-
decode: notImplementedStatement,
|
|
13
|
-
encode: import_lib$8.callExpression(import_lib$8.memberExpression(import_lib$8.identifier("JSON"), import_lib$8.identifier("stringify")), [import_lib$8.identifier("from")])
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region src/generators/requestFormats/formData.ts
|
|
19
|
-
var import_lib$7 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
20
|
-
const maybeStringify = Effect.fn(function* (expression, schema) {
|
|
21
|
-
const resolved = yield* resolveSchema(schema);
|
|
22
|
-
if (resolved.type === "object" || resolved.type === "array") return import_lib$7.callExpression(import_lib$7.memberExpression(import_lib$7.identifier("JSON"), import_lib$7.identifier("stringify")), [expression]);
|
|
23
|
-
if (resolved.type === "string") return expression;
|
|
24
|
-
return import_lib$7.callExpression(import_lib$7.identifier("String"), [expression]);
|
|
25
|
-
});
|
|
26
|
-
const generateFormDataRequestCodec = Effect.fn(function* (options, schema, decoded) {
|
|
27
|
-
const encode = import_lib$7.blockStatement([import_lib$7.variableDeclaration("const", [import_lib$7.variableDeclarator(import_lib$7.identifier("formData"), import_lib$7.newExpression(import_lib$7.identifier("FormData"), []))])]);
|
|
28
|
-
const properties = schema.properties ?? {};
|
|
29
|
-
for (const [propertyKey, rawProperty] of Object.entries(properties)) {
|
|
30
|
-
const property = yield* resolveSchema(rawProperty);
|
|
31
|
-
const propertyExpression = import_lib$7.memberExpression(import_lib$7.identifier("from"), import_lib$7.stringLiteral(propertyKey), true);
|
|
32
|
-
if (property.type === "array") {
|
|
33
|
-
encode.body.push(import_lib$7.forOfStatement(import_lib$7.variableDeclaration("const", [import_lib$7.variableDeclarator(import_lib$7.identifier("propertyValue"))]), propertyExpression, import_lib$7.blockStatement([import_lib$7.expressionStatement(import_lib$7.callExpression(import_lib$7.memberExpression(import_lib$7.identifier("formData"), import_lib$7.identifier("append")), [import_lib$7.stringLiteral(propertyKey), yield* maybeStringify(import_lib$7.identifier("propertyValue"), property.items)]))])));
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
encode.body.push(import_lib$7.expressionStatement(import_lib$7.callExpression(import_lib$7.memberExpression(import_lib$7.identifier("formData"), import_lib$7.identifier("append")), [import_lib$7.stringLiteral(propertyKey), yield* maybeStringify(propertyExpression, property)])));
|
|
37
|
-
}
|
|
38
|
-
encode.body.push(import_lib$7.returnStatement(import_lib$7.identifier("formData")));
|
|
39
|
-
return options.transformer({
|
|
40
|
-
encoded: import_lib$7.callExpression(options.schema.instanceOf, [import_lib$7.identifier("FormData")]),
|
|
41
|
-
decoded,
|
|
42
|
-
decode: notImplementedStatement,
|
|
43
|
-
encode
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/generators/generatePathExpression.ts
|
|
49
|
-
var import_lib$6 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
50
|
-
function generatePathExpression(path, pathParameters, queryParameter) {
|
|
51
|
-
let modifiedPath = path;
|
|
52
|
-
if (queryParameter) modifiedPath += "?";
|
|
53
|
-
const split = modifiedPath.split(new RegExp(`\\{(${Object.keys(pathParameters).map((i) => escapeRegex(i)).join("|")})\\}`, "g"));
|
|
54
|
-
const quasis = [];
|
|
55
|
-
const expressions = [];
|
|
56
|
-
for (const [i, part] of split.entries()) if (i % 2 === 0) quasis.push(import_lib$6.templateElement({
|
|
57
|
-
raw: part,
|
|
58
|
-
cooked: part
|
|
59
|
-
}, false));
|
|
60
|
-
else {
|
|
61
|
-
const paramExpression = pathParameters[part];
|
|
62
|
-
if (!paramExpression) throw new Error(`Path parameter ${part} not found`);
|
|
63
|
-
expressions.push(paramExpression);
|
|
64
|
-
}
|
|
65
|
-
if (queryParameter) {
|
|
66
|
-
expressions.push(queryParameter);
|
|
67
|
-
quasis.push(import_lib$6.templateElement({
|
|
68
|
-
raw: "",
|
|
69
|
-
cooked: ""
|
|
70
|
-
}, false));
|
|
71
|
-
}
|
|
72
|
-
return import_lib$6.templateLiteral(quasis, expressions);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
//#endregion
|
|
76
|
-
//#region src/generators/responseFormats/json.ts
|
|
77
|
-
var import_lib$5 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
78
|
-
const generateJsonResponseCodec = Effect.fn(function* (options, schema, decoded) {
|
|
79
|
-
return options.transformer({
|
|
80
|
-
encoded: import_lib$5.callExpression(options.schema.instanceOf, [import_lib$5.identifier("Response")]),
|
|
81
|
-
decoded,
|
|
82
|
-
decode: import_lib$5.blockStatement([import_lib$5.tryStatement(import_lib$5.blockStatement([import_lib$5.returnStatement(import_lib$5.tsAsExpression(import_lib$5.awaitExpression(import_lib$5.callExpression(import_lib$5.memberExpression(import_lib$5.identifier("from"), import_lib$5.identifier("json")), [])), import_lib$5.tsAnyKeyword()))]), import_lib$5.catchClause(Object.assign(import_lib$5.identifier("error"), { typeAnnotation: import_lib$5.tsTypeAnnotation(import_lib$5.tsUnknownKeyword()) }), options.transformerCatch(import_lib$5.identifier("error"))))]),
|
|
83
|
-
decodeAsync: true,
|
|
84
|
-
encode: notImplementedStatement
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
//#endregion
|
|
89
|
-
//#region src/generators/responseFormats/blob.ts
|
|
90
|
-
var import_lib$4 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
91
|
-
const generateBlobResponseCodec = Effect.fn(function* (options) {
|
|
92
|
-
return options.transformer({
|
|
93
|
-
encoded: import_lib$4.callExpression(options.schema.instanceOf, [import_lib$4.identifier("Response")]),
|
|
94
|
-
decoded: import_lib$4.callExpression(options.schema.instanceOf, [import_lib$4.identifier("Blob")]),
|
|
95
|
-
decode: import_lib$4.blockStatement([import_lib$4.tryStatement(import_lib$4.blockStatement([import_lib$4.returnStatement(import_lib$4.tsAsExpression(import_lib$4.awaitExpression(import_lib$4.callExpression(import_lib$4.memberExpression(import_lib$4.identifier("from"), import_lib$4.identifier("blob")), [])), import_lib$4.tsAnyKeyword()))]), import_lib$4.catchClause(Object.assign(import_lib$4.identifier("error"), { typeAnnotation: import_lib$4.tsTypeAnnotation(import_lib$4.tsUnknownKeyword()) }), options.transformerCatch(import_lib$4.identifier("error"))))]),
|
|
96
|
-
decodeAsync: true,
|
|
97
|
-
encode: notImplementedStatement
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
//#endregion
|
|
102
|
-
//#region src/generators/schema.ts
|
|
103
|
-
var import_lib$3 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
104
|
-
const equivalentType = (type) => ({
|
|
105
|
-
typeDecoded: type,
|
|
106
|
-
typeEncoded: type
|
|
107
|
-
});
|
|
108
|
-
const resolveSchema = Effect.fn(function* (schema) {
|
|
109
|
-
const ctx = yield* DocumentContext;
|
|
110
|
-
let resolvedSchema = schema;
|
|
111
|
-
for (let i = 0; i < 10; i++) {
|
|
112
|
-
if (!("$ref" in resolvedSchema)) return resolvedSchema;
|
|
113
|
-
const ref = resolvedSchema.$ref;
|
|
114
|
-
if (!ref.startsWith("#/components/schemas/")) return yield* new NotImplementedError({ message: `$ref ${ref}` });
|
|
115
|
-
const schemaName = ref.slice(21);
|
|
116
|
-
const nextSchema = ctx.document.components?.schemas?.[schemaName];
|
|
117
|
-
if (!nextSchema) return yield* new NotImplementedError({ message: `Missing $ref ${ref}` });
|
|
118
|
-
resolvedSchema = nextSchema;
|
|
119
|
-
}
|
|
120
|
-
return yield* new NotImplementedError({ message: `Too many $ref in schema` });
|
|
121
|
-
});
|
|
122
|
-
const stringLiteralOrIdentifier = (value) => {
|
|
123
|
-
if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) return import_lib$3.identifier(value);
|
|
124
|
-
return import_lib$3.stringLiteral(value);
|
|
125
|
-
};
|
|
126
|
-
const memberExpressionWithStringProperty = (object, property) => {
|
|
127
|
-
if (property.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) return import_lib$3.memberExpression(object, import_lib$3.identifier(property));
|
|
128
|
-
return import_lib$3.memberExpression(object, import_lib$3.stringLiteral(property), true);
|
|
129
|
-
};
|
|
130
|
-
function createSchemaGenerator(options) {
|
|
131
|
-
const ensureStandardParametersSchema = Effect.fn(function* () {
|
|
132
|
-
const ctx = yield* DocumentContext;
|
|
133
|
-
if (!ctx.schemas.has("ParametersSchema")) ctx.schemas.set("ParametersSchema", [import_lib$3.exportNamedDeclaration(import_lib$3.variableDeclaration("const", [import_lib$3.variableDeclarator(import_lib$3.identifier("ParametersSchema"), import_lib$3.callExpression(options.schema.object, [import_lib$3.objectExpression([
|
|
134
|
-
import_lib$3.objectProperty(import_lib$3.identifier("path"), options.schema.string),
|
|
135
|
-
import_lib$3.objectProperty(import_lib$3.identifier("headers"), options.modifiers.optional(import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("Headers")]))),
|
|
136
|
-
import_lib$3.objectProperty(import_lib$3.identifier("body"), options.modifiers.optional(options.schema.union([
|
|
137
|
-
options.schema.string,
|
|
138
|
-
import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("Blob")]),
|
|
139
|
-
import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("FormData")]),
|
|
140
|
-
import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("URLSearchParams")])
|
|
141
|
-
])))
|
|
142
|
-
])]))]))]);
|
|
143
|
-
return import_lib$3.identifier("ParametersSchema");
|
|
144
|
-
});
|
|
145
|
-
const getBaseEffectSchema = Effect.fn(function* (schema) {
|
|
146
|
-
const ctx = yield* DocumentContext;
|
|
147
|
-
const typeMeta = {};
|
|
148
|
-
if ("$ref" in schema) {
|
|
149
|
-
const ref = schema.$ref;
|
|
150
|
-
if (!ref.startsWith("#/components/schemas/")) return yield* new NotImplementedError({ message: `$ref ${ref}` });
|
|
151
|
-
const schemaName = ref.slice(21);
|
|
152
|
-
const schemaKey = yield* getKey(schemaName);
|
|
153
|
-
const resolvedSchema = ctx.document.components?.schemas?.[schemaName];
|
|
154
|
-
if (!resolvedSchema) return yield* new NotImplementedError({ message: `Missing $ref ${ref}` });
|
|
155
|
-
if (ctx.processingSchemas.has(schemaName)) {
|
|
156
|
-
const fn = import_lib$3.arrowFunctionExpression([], import_lib$3.identifier(schemaKey.upper));
|
|
157
|
-
if (ctx.processingSchemaTypes.has(schemaName)) return {
|
|
158
|
-
expression: import_lib$3.nullLiteral(),
|
|
159
|
-
typeDecoded: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
160
|
-
typeEncoded: import_lib$3.tsTypeReference(options.types.typeEncoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
161
|
-
typeMeta
|
|
162
|
-
};
|
|
163
|
-
ctx.processingSchemaTypes.add(schemaName);
|
|
164
|
-
const typeSchema = yield* ensureSchema(resolvedSchema);
|
|
165
|
-
fn.returnType = import_lib$3.tsTypeAnnotation(import_lib$3.tsTypeReference(options.types.schema, import_lib$3.tsTypeParameterInstantiation([typeSchema.typeDecoded, typeSchema.typeEncoded])));
|
|
166
|
-
ctx.processingSchemaTypes.delete(schemaName);
|
|
167
|
-
return {
|
|
168
|
-
expression: options.modifiers.lazy(fn),
|
|
169
|
-
typeDecoded: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
170
|
-
typeEncoded: import_lib$3.tsTypeReference(options.types.typeEncoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
171
|
-
typeMeta
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
if (!ctx.schemas.has(schemaName)) {
|
|
175
|
-
ctx.processingSchemas.add(schemaName);
|
|
176
|
-
const schemaExpression = yield* ensureSchema(resolvedSchema);
|
|
177
|
-
const schemaKeyIdentifier = import_lib$3.identifier(schemaKey.upper);
|
|
178
|
-
if (options.experimental_includeTypes) schemaKeyIdentifier.typeAnnotation = import_lib$3.tsTypeAnnotation(import_lib$3.tsTypeReference(options.types.schema, import_lib$3.tsTypeParameterInstantiation([schemaExpression.typeDecoded, schemaExpression.typeEncoded])));
|
|
179
|
-
ctx.schemas.set(schemaName, [import_lib$3.exportNamedDeclaration(import_lib$3.variableDeclaration("const", [import_lib$3.variableDeclarator(schemaKeyIdentifier, schemaExpression.expression)])), import_lib$3.exportNamedDeclaration(import_lib$3.tsTypeAliasDeclaration(import_lib$3.identifier(schemaKey.upper), null, import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))]))))]);
|
|
180
|
-
ctx.schemaTypeMeta.set(schemaName, schemaExpression.typeMeta);
|
|
181
|
-
ctx.processingSchemas.delete(schemaName);
|
|
182
|
-
}
|
|
183
|
-
return {
|
|
184
|
-
expression: import_lib$3.identifier(schemaKey.upper),
|
|
185
|
-
typeDecoded: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
186
|
-
typeEncoded: import_lib$3.tsTypeReference(options.types.typeEncoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(import_lib$3.identifier(schemaKey.upper))])),
|
|
187
|
-
typeMeta: ctx.schemaTypeMeta.get(schemaName)
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
if (schema.deprecated) if (options.deprecationHandling === "optional") typeMeta.optional = true;
|
|
191
|
-
else return {
|
|
192
|
-
expression: options.modifiers.optional(options.schema.unknown),
|
|
193
|
-
...equivalentType(import_lib$3.tsUnionType([import_lib$3.tsUnknownKeyword(), import_lib$3.tsUndefinedKeyword()])),
|
|
194
|
-
typeMeta
|
|
195
|
-
};
|
|
196
|
-
const customResult = options.custom?.(schema);
|
|
197
|
-
if (customResult) {
|
|
198
|
-
const identifier = yield* ensureImport(customResult.name, customResult.from);
|
|
199
|
-
const returnType = import_lib$3.tsTypeReference(import_lib$3.identifier("ReturnType"), import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(identifier)]));
|
|
200
|
-
return {
|
|
201
|
-
expression: import_lib$3.callExpression(identifier, []),
|
|
202
|
-
typeDecoded: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([returnType])),
|
|
203
|
-
typeEncoded: import_lib$3.tsTypeReference(options.types.typeEncoded, import_lib$3.tsTypeParameterInstantiation([returnType])),
|
|
204
|
-
typeMeta
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
if (schema.allOf) {
|
|
208
|
-
const results = yield* Effect.forEach((schema$1) => ensureSchema(schema$1))(schema.allOf);
|
|
209
|
-
let result;
|
|
210
|
-
if (results.length === 1) result = results[0];
|
|
211
|
-
else result = {
|
|
212
|
-
expression: options.schema[results.every((i) => i.typeMeta.isObject) ? "objectExtend" : "intersection"](results.map((result$1) => result$1.expression)),
|
|
213
|
-
typeDecoded: import_lib$3.tsIntersectionType(results.map((result$1) => result$1.typeDecoded)),
|
|
214
|
-
typeEncoded: import_lib$3.tsIntersectionType(results.map((result$1) => result$1.typeEncoded)),
|
|
215
|
-
typeMeta
|
|
216
|
-
};
|
|
217
|
-
if ("nullable" in schema && schema.nullable) {
|
|
218
|
-
result.expression = options.modifiers.nullable(result.expression);
|
|
219
|
-
result.typeDecoded = import_lib$3.tsUnionType([result.typeDecoded, import_lib$3.tsNullKeyword()]);
|
|
220
|
-
result.typeEncoded = import_lib$3.tsUnionType([result.typeEncoded, import_lib$3.tsNullKeyword()]);
|
|
221
|
-
}
|
|
222
|
-
return result;
|
|
223
|
-
}
|
|
224
|
-
if (schema.enum) {
|
|
225
|
-
const unsupportedEnumValue = schema.enum.find((i) => typeof i !== "string" && typeof i !== "number" && typeof i !== "boolean" && i !== null);
|
|
226
|
-
if (unsupportedEnumValue !== void 0) return yield* new NotImplementedError({ message: `Unsupported 'enum' value: ${JSON.stringify(unsupportedEnumValue)}` });
|
|
227
|
-
const getLiteral = (value) => {
|
|
228
|
-
if (typeof value === "number") return import_lib$3.numericLiteral(value);
|
|
229
|
-
if (typeof value === "boolean") return import_lib$3.booleanLiteral(value);
|
|
230
|
-
return import_lib$3.stringLiteral(value);
|
|
231
|
-
};
|
|
232
|
-
return {
|
|
233
|
-
expression: options.schema.enum(schema.enum.map((e) => e === null ? import_lib$3.nullLiteral() : getLiteral(e))),
|
|
234
|
-
...equivalentType(import_lib$3.tsUnionType(schema.enum.map((e) => e === null ? import_lib$3.tsNullKeyword() : import_lib$3.tsLiteralType(getLiteral(e))))),
|
|
235
|
-
typeMeta
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
switch (schema.type) {
|
|
239
|
-
case "boolean": return {
|
|
240
|
-
expression: options.schema.boolean,
|
|
241
|
-
...equivalentType(import_lib$3.tsBooleanKeyword()),
|
|
242
|
-
typeMeta
|
|
243
|
-
};
|
|
244
|
-
case "string": {
|
|
245
|
-
if (schema.format === "binary") return {
|
|
246
|
-
expression: import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("Blob")]),
|
|
247
|
-
...equivalentType(import_lib$3.tsTypeReference(import_lib$3.identifier("Blob"))),
|
|
248
|
-
typeMeta
|
|
249
|
-
};
|
|
250
|
-
let expression = {
|
|
251
|
-
expression: options.schema.string,
|
|
252
|
-
...equivalentType(import_lib$3.tsStringKeyword()),
|
|
253
|
-
typeMeta
|
|
254
|
-
};
|
|
255
|
-
if (schema.pattern) expression.expression = options.modifiers.regex(expression.expression, schema.pattern);
|
|
256
|
-
return expression;
|
|
257
|
-
}
|
|
258
|
-
case "number": return {
|
|
259
|
-
expression: options.schema.number,
|
|
260
|
-
...equivalentType(import_lib$3.tsNumberKeyword()),
|
|
261
|
-
typeMeta
|
|
262
|
-
};
|
|
263
|
-
case "integer": return {
|
|
264
|
-
expression: options.schema.integer,
|
|
265
|
-
...equivalentType(import_lib$3.tsNumberKeyword()),
|
|
266
|
-
typeMeta
|
|
267
|
-
};
|
|
268
|
-
case "object": {
|
|
269
|
-
typeMeta.isObject = true;
|
|
270
|
-
const object = import_lib$3.objectExpression([]);
|
|
271
|
-
const objectTypeDecoded = import_lib$3.tsTypeLiteral([]);
|
|
272
|
-
const objectTypeEncoded = import_lib$3.tsTypeLiteral([]);
|
|
273
|
-
for (const [propertyKey, property] of Object.entries(schema.properties ?? {})) {
|
|
274
|
-
let propertySchema = yield* ensureSchema(property);
|
|
275
|
-
const decodedMember = import_lib$3.tsPropertySignature(import_lib$3.identifier(propertyKey), import_lib$3.tsTypeAnnotation(propertySchema.typeDecoded));
|
|
276
|
-
const encodedMember = import_lib$3.tsPropertySignature(import_lib$3.identifier(propertyKey), import_lib$3.tsTypeAnnotation(propertySchema.typeEncoded));
|
|
277
|
-
decodedMember.readonly = !!propertySchema.typeMeta?.readonly;
|
|
278
|
-
encodedMember.readonly = !!propertySchema.typeMeta?.readonly;
|
|
279
|
-
if (propertySchema.typeMeta?.optional || !schema.required?.includes(propertyKey)) {
|
|
280
|
-
propertySchema.expression = options.modifiers.optional(propertySchema.expression);
|
|
281
|
-
decodedMember.optional = true;
|
|
282
|
-
encodedMember.optional = true;
|
|
283
|
-
}
|
|
284
|
-
const objectProperty = import_lib$3.objectProperty(stringLiteralOrIdentifier(propertyKey), propertySchema.expression);
|
|
285
|
-
if (property.description) import_lib$3.addComment(objectProperty, "leading", `* ${property.description}`);
|
|
286
|
-
object.properties.push(objectProperty);
|
|
287
|
-
objectTypeDecoded.members.push(decodedMember);
|
|
288
|
-
objectTypeEncoded.members.push(encodedMember);
|
|
289
|
-
}
|
|
290
|
-
if (schema.additionalProperties) {
|
|
291
|
-
const valueSchema = yield* ensureSchema(schema.additionalProperties === true ? { type: void 0 } : schema.additionalProperties);
|
|
292
|
-
if (object.properties.length === 0) return {
|
|
293
|
-
expression: options.schema.record(options.schema.string, valueSchema.expression),
|
|
294
|
-
typeDecoded: import_lib$3.tsTypeReference(import_lib$3.identifier("Record"), import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsStringKeyword(), valueSchema.typeDecoded])),
|
|
295
|
-
typeEncoded: import_lib$3.tsTypeReference(import_lib$3.identifier("Record"), import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsStringKeyword(), valueSchema.typeEncoded])),
|
|
296
|
-
typeMeta
|
|
297
|
-
};
|
|
298
|
-
const expression = options.schema.catchall(import_lib$3.callExpression(options.schema.object, [object]), valueSchema.expression);
|
|
299
|
-
const keyParam = import_lib$3.identifier("key");
|
|
300
|
-
keyParam.typeAnnotation = import_lib$3.tsTypeAnnotation(import_lib$3.tsStringKeyword());
|
|
301
|
-
const indexSignatureDecoded = import_lib$3.tsIndexSignature([keyParam], import_lib$3.tsTypeAnnotation(valueSchema.typeDecoded));
|
|
302
|
-
const indexSignatureEncoded = import_lib$3.tsIndexSignature([keyParam], import_lib$3.tsTypeAnnotation(valueSchema.typeEncoded));
|
|
303
|
-
return {
|
|
304
|
-
expression,
|
|
305
|
-
typeDecoded: import_lib$3.tsTypeLiteral([...objectTypeDecoded.members, indexSignatureDecoded]),
|
|
306
|
-
typeEncoded: import_lib$3.tsTypeLiteral([...objectTypeEncoded.members, indexSignatureEncoded]),
|
|
307
|
-
typeMeta
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
return {
|
|
311
|
-
expression: import_lib$3.callExpression(options.schema.object, [object]),
|
|
312
|
-
typeDecoded: objectTypeDecoded,
|
|
313
|
-
typeEncoded: objectTypeEncoded,
|
|
314
|
-
typeMeta
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
case "array": {
|
|
318
|
-
const itemsSchema = yield* ensureSchema(schema.items);
|
|
319
|
-
let expression = import_lib$3.callExpression(options.schema.array, [itemsSchema.expression]);
|
|
320
|
-
let typeDecoded = import_lib$3.tsArrayType(itemsSchema.typeDecoded);
|
|
321
|
-
let typeEncoded = import_lib$3.tsArrayType(itemsSchema.typeEncoded);
|
|
322
|
-
if (options.supportsImmutability) if (itemsSchema.typeMeta?.readonly) {
|
|
323
|
-
expression = options.modifiers.immutable(expression);
|
|
324
|
-
typeDecoded = import_lib$3.tsTypeOperator(typeDecoded, "readonly");
|
|
325
|
-
typeEncoded = import_lib$3.tsTypeOperator(typeEncoded, "readonly");
|
|
326
|
-
} else expression = options.modifiers.mutable(expression);
|
|
327
|
-
return {
|
|
328
|
-
expression,
|
|
329
|
-
typeDecoded,
|
|
330
|
-
typeEncoded,
|
|
331
|
-
typeMeta
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
case "null":
|
|
335
|
-
typeMeta.isNull = true;
|
|
336
|
-
return {
|
|
337
|
-
expression: options.schema.null,
|
|
338
|
-
...equivalentType(import_lib$3.tsNullKeyword()),
|
|
339
|
-
typeMeta
|
|
340
|
-
};
|
|
341
|
-
case void 0: return {
|
|
342
|
-
expression: options.schema.unknown,
|
|
343
|
-
...equivalentType(import_lib$3.tsUnknownKeyword()),
|
|
344
|
-
typeMeta
|
|
345
|
-
};
|
|
346
|
-
default:
|
|
347
|
-
console.info(schema);
|
|
348
|
-
return yield* new NotImplementedError({ message: `type ${schema.type}` });
|
|
349
|
-
}
|
|
350
|
-
});
|
|
351
|
-
const applyModifiers = Effect.fn(function* (expression, schema) {
|
|
352
|
-
if ("$ref" in schema) return expression;
|
|
353
|
-
const modified = { ...expression };
|
|
354
|
-
if (options.supportsImmutability) if (schema.readOnly) {
|
|
355
|
-
modified.expression = options.modifiers.immutable(modified.expression);
|
|
356
|
-
modified.typeMeta ??= {};
|
|
357
|
-
modified.typeMeta.readonly = true;
|
|
358
|
-
} else modified.expression = options.modifiers.mutable(modified.expression);
|
|
359
|
-
return modified;
|
|
360
|
-
});
|
|
361
|
-
const ensureSchema = Effect.fn(function* (schema) {
|
|
362
|
-
let schemas = [schema].flatMap((schema$1) => {
|
|
363
|
-
if ("$ref" in schema$1) return [schema$1];
|
|
364
|
-
if (schema$1.oneOf) return schema$1.oneOf;
|
|
365
|
-
return [schema$1];
|
|
366
|
-
}).flatMap((schema$1) => {
|
|
367
|
-
if (typeof schema$1 === "boolean") return [];
|
|
368
|
-
if ("$ref" in schema$1) return [schema$1];
|
|
369
|
-
if (Array.isArray(schema$1.type)) return schema$1.type.map((type) => ({
|
|
370
|
-
...schema$1,
|
|
371
|
-
type
|
|
372
|
-
}));
|
|
373
|
-
return [schema$1];
|
|
374
|
-
});
|
|
375
|
-
let expressions = [];
|
|
376
|
-
for (const schema$1 of schemas) expressions.push(yield* applyModifiers(yield* getBaseEffectSchema(schema$1), schema$1));
|
|
377
|
-
let isNullable = false;
|
|
378
|
-
if (expressions.some((e) => e.typeMeta?.isNull)) {
|
|
379
|
-
isNullable = true;
|
|
380
|
-
expressions = expressions.filter((e) => !e.typeMeta?.isNull);
|
|
381
|
-
}
|
|
382
|
-
let result;
|
|
383
|
-
if (expressions.length === 1) result = expressions[0];
|
|
384
|
-
else result = {
|
|
385
|
-
expression: options.schema.union(expressions.map((e) => e.expression)),
|
|
386
|
-
typeDecoded: import_lib$3.tsUnionType(expressions.map((e) => e.typeDecoded)),
|
|
387
|
-
typeEncoded: import_lib$3.tsUnionType(expressions.map((e) => e.typeEncoded)),
|
|
388
|
-
typeMeta: {}
|
|
389
|
-
};
|
|
390
|
-
if (isNullable) {
|
|
391
|
-
result.expression = options.modifiers.nullable(result.expression);
|
|
392
|
-
result.typeDecoded = import_lib$3.tsUnionType([result.typeDecoded, import_lib$3.tsNullKeyword()]);
|
|
393
|
-
result.typeEncoded = import_lib$3.tsUnionType([result.typeEncoded, import_lib$3.tsNullKeyword()]);
|
|
394
|
-
}
|
|
395
|
-
return result;
|
|
396
|
-
});
|
|
397
|
-
const ensureParametersSchema = Effect.fn(function* (operationKey, method, path) {
|
|
398
|
-
const ctx = yield* DocumentContext;
|
|
399
|
-
const identifier = import_lib$3.identifier(`${operationKey.upper}_Parameters`);
|
|
400
|
-
const object = import_lib$3.objectExpression([]);
|
|
401
|
-
const queryArray = import_lib$3.arrayExpression([]);
|
|
402
|
-
const pathParameters = {};
|
|
403
|
-
const headerArray = import_lib$3.arrayExpression([]);
|
|
404
|
-
let hasBody = false;
|
|
405
|
-
for (let parameter of method.parameters ?? []) {
|
|
406
|
-
if ("$ref" in parameter) {
|
|
407
|
-
const newParameter = ctx.document.components?.parameters?.[parameter.$ref.slice(24)];
|
|
408
|
-
if (newParameter) parameter = newParameter;
|
|
409
|
-
else return yield* new NotImplementedError({ message: "Unresolved $ref in parameter" });
|
|
410
|
-
if ("$ref" in parameter) return yield* new NotImplementedError({ message: "$ref in parameter" });
|
|
411
|
-
}
|
|
412
|
-
if (!parameter.schema) return yield* new NotImplementedError({ message: "parameter without schema" });
|
|
413
|
-
let expression = (yield* ensureSchema(parameter.schema)).expression;
|
|
414
|
-
if (!parameter.required) expression = options.modifiers.optional(expression);
|
|
415
|
-
const objectProperty = import_lib$3.objectProperty(stringLiteralOrIdentifier(parameter.name), expression);
|
|
416
|
-
if (parameter.description) import_lib$3.addComment(objectProperty, "leading", `* ${parameter.description}`);
|
|
417
|
-
object.properties.push(objectProperty);
|
|
418
|
-
if (parameter.in === "query") {
|
|
419
|
-
const param = memberExpressionWithStringProperty(import_lib$3.identifier("from"), parameter.name);
|
|
420
|
-
if ("type" in parameter.schema && parameter.schema.type === "array") queryArray.elements.push(import_lib$3.spreadElement(import_lib$3.logicalExpression("??", Object.assign(import_lib$3.optionalCallExpression(import_lib$3.optionalMemberExpression(param, import_lib$3.identifier("map"), false, true), [import_lib$3.arrowFunctionExpression([import_lib$3.identifier("value")], import_lib$3.arrayExpression([import_lib$3.stringLiteral(parameter.name), import_lib$3.callExpression(import_lib$3.identifier("String"), [import_lib$3.identifier("value")])]))], false), { typeParameters: import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTupleType([import_lib$3.tsStringKeyword(), import_lib$3.tsStringKeyword()])]) }), import_lib$3.arrayExpression([]))));
|
|
421
|
-
else queryArray.elements.push(import_lib$3.arrayExpression([import_lib$3.stringLiteral(parameter.name), import_lib$3.callExpression(import_lib$3.identifier("String"), [param])]));
|
|
422
|
-
} else if (parameter.in === "path") pathParameters[parameter.name] = memberExpressionWithStringProperty(import_lib$3.identifier("from"), parameter.name);
|
|
423
|
-
else if (parameter.in === "header") headerArray.elements.push(import_lib$3.arrayExpression([import_lib$3.stringLiteral(parameter.name), import_lib$3.callExpression(import_lib$3.identifier("String"), [memberExpressionWithStringProperty(import_lib$3.identifier("from"), parameter.name)])]));
|
|
424
|
-
else return yield* new NotImplementedError({ message: `parameter in ${parameter.in}` });
|
|
425
|
-
}
|
|
426
|
-
const body = yield* requestBodySchema(operationKey, method);
|
|
427
|
-
if (body) {
|
|
428
|
-
hasBody = true;
|
|
429
|
-
object.properties.push(import_lib$3.objectProperty(import_lib$3.identifier("data"), body.data));
|
|
430
|
-
headerArray.elements.push(...body.headers);
|
|
431
|
-
}
|
|
432
|
-
const decodedSchema = import_lib$3.callExpression(options.schema.object, [object]);
|
|
433
|
-
const transform = options.transformer({
|
|
434
|
-
encoded: yield* ensureStandardParametersSchema(),
|
|
435
|
-
decoded: decodedSchema,
|
|
436
|
-
decode: import_lib$3.blockStatement([import_lib$3.throwStatement(import_lib$3.newExpression(import_lib$3.identifier("Error"), [import_lib$3.stringLiteral("Not implemented")]))]),
|
|
437
|
-
encode: import_lib$3.objectExpression([
|
|
438
|
-
import_lib$3.objectProperty(import_lib$3.identifier("path"), generatePathExpression(path, pathParameters, queryArray.elements.length ? import_lib$3.newExpression(import_lib$3.identifier("URLSearchParams"), [queryArray]) : null)),
|
|
439
|
-
...headerArray.elements.length ? [import_lib$3.objectProperty(import_lib$3.identifier("headers"), import_lib$3.newExpression(import_lib$3.identifier("Headers"), [headerArray]))] : [],
|
|
440
|
-
...hasBody ? [import_lib$3.objectProperty(import_lib$3.identifier("body"), import_lib$3.memberExpression(import_lib$3.identifier("from"), import_lib$3.identifier("data")))] : []
|
|
441
|
-
])
|
|
442
|
-
});
|
|
443
|
-
ctx.schemas.set(identifier.name, [import_lib$3.exportNamedDeclaration(import_lib$3.variableDeclaration("const", [import_lib$3.variableDeclarator(identifier, transform)]))]);
|
|
444
|
-
return {
|
|
445
|
-
expression: identifier,
|
|
446
|
-
typeReference: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(identifier)]))
|
|
447
|
-
};
|
|
448
|
-
});
|
|
449
|
-
const requestBodySchema = Effect.fn(function* (operationKey, method) {
|
|
450
|
-
if (!method.requestBody) return null;
|
|
451
|
-
if ("$ref" in method.requestBody) return yield* new NotImplementedError({ message: "$ref in requestBody" });
|
|
452
|
-
let hasBody = false;
|
|
453
|
-
for (const contentType in method.requestBody.content) {
|
|
454
|
-
hasBody = true;
|
|
455
|
-
const schema = method.requestBody.content[contentType]?.schema;
|
|
456
|
-
if (contentType === "application/json") {
|
|
457
|
-
if (!schema) continue;
|
|
458
|
-
return {
|
|
459
|
-
headers: [import_lib$3.arrayExpression([import_lib$3.stringLiteral("Content-Type"), import_lib$3.stringLiteral("application/json")])],
|
|
460
|
-
data: yield* generateJsonRequestCodec(options, yield* resolveSchema(schema), (yield* ensureSchema(schema)).expression)
|
|
461
|
-
};
|
|
462
|
-
} else if (contentType === "multipart/form-data") {
|
|
463
|
-
if (!schema) continue;
|
|
464
|
-
return {
|
|
465
|
-
headers: [],
|
|
466
|
-
data: yield* generateFormDataRequestCodec(options, yield* resolveSchema(schema), (yield* ensureSchema(schema)).expression)
|
|
467
|
-
};
|
|
468
|
-
} else {
|
|
469
|
-
if (schema && ("$ref" in schema || schema.type !== "string" && schema.format !== "binary")) continue;
|
|
470
|
-
return {
|
|
471
|
-
headers: [import_lib$3.arrayExpression([import_lib$3.stringLiteral("Content-Type"), import_lib$3.stringLiteral(contentType)])],
|
|
472
|
-
data: import_lib$3.callExpression(options.schema.instanceOf, [import_lib$3.identifier("Blob")])
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
if (!hasBody) return null;
|
|
477
|
-
return yield* new NotImplementedError({ message: `No supported requestBody type (${Object.keys(method.requestBody.content).join(", ")}) in ${operationKey.upper}` });
|
|
478
|
-
});
|
|
479
|
-
const responseSchema = Effect.fn(function* (content) {
|
|
480
|
-
if (content) {
|
|
481
|
-
for (const [format, formatContent] of Object.entries(content)) if (format === "application/json") {
|
|
482
|
-
if (!formatContent.schema) continue;
|
|
483
|
-
const schema = formatContent.schema;
|
|
484
|
-
const decodedSchema = (yield* ensureSchema(schema)).expression;
|
|
485
|
-
return yield* generateJsonResponseCodec(options, yield* resolveSchema(schema), decodedSchema);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
return yield* generateBlobResponseCodec(options);
|
|
489
|
-
});
|
|
490
|
-
const ensureResponseSchema = Effect.fn(function* (operationKey, method) {
|
|
491
|
-
const ctx = yield* DocumentContext;
|
|
492
|
-
let transform;
|
|
493
|
-
if (!method.responses?.["200"]) transform = yield* responseSchema(void 0);
|
|
494
|
-
else {
|
|
495
|
-
let response;
|
|
496
|
-
if ("$ref" in method.responses["200"]) {
|
|
497
|
-
const ref = method.responses["200"].$ref;
|
|
498
|
-
if (!ref.startsWith("#/components/responses/")) return yield* new NotImplementedError({ message: `$ref ${ref} in response in ${operationKey.upper}` });
|
|
499
|
-
const responseName = ref.slice(23);
|
|
500
|
-
const newResponse = ctx.document.components?.responses?.[responseName];
|
|
501
|
-
if (!newResponse) return yield* new NotImplementedError({ message: `Missing $ref ${ref} in response in ${operationKey.upper}` });
|
|
502
|
-
if ("$ref" in newResponse) return yield* new NotImplementedError({ message: `$ref in $ref in response in ${operationKey.upper}` });
|
|
503
|
-
response = newResponse;
|
|
504
|
-
} else response = method.responses["200"];
|
|
505
|
-
transform = yield* responseSchema(response.content);
|
|
506
|
-
}
|
|
507
|
-
const identifier = import_lib$3.identifier(`${operationKey.upper}_Response`);
|
|
508
|
-
ctx.schemas.set(identifier.name, [import_lib$3.exportNamedDeclaration(import_lib$3.variableDeclaration("const", [import_lib$3.variableDeclarator(identifier, transform)]))]);
|
|
509
|
-
return {
|
|
510
|
-
expression: identifier,
|
|
511
|
-
typeReference: import_lib$3.tsTypeReference(options.types.typeDecoded, import_lib$3.tsTypeParameterInstantiation([import_lib$3.tsTypeQuery(identifier)]))
|
|
512
|
-
};
|
|
513
|
-
});
|
|
514
|
-
const processSchema = Effect.fn(function* (schema) {
|
|
515
|
-
if (options.includeSchemas) yield* ensureSchema(schema);
|
|
516
|
-
});
|
|
517
|
-
const processOperation = Effect.fn(function* (operationKey, path, _method, operation) {
|
|
518
|
-
if (options.includeOperations) {
|
|
519
|
-
yield* ensureParametersSchema(operationKey, operation, path);
|
|
520
|
-
yield* ensureResponseSchema(operationKey, operation);
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
const decodeResponse = Effect.fn(function* (schema, response) {
|
|
524
|
-
return options.methods.decode(schema, response);
|
|
525
|
-
});
|
|
526
|
-
const encodeParameters = Effect.fn(function* (schema, response) {
|
|
527
|
-
return options.methods.encode(schema, response);
|
|
528
|
-
});
|
|
529
|
-
return {
|
|
530
|
-
processSchema,
|
|
531
|
-
processOperation,
|
|
532
|
-
ensureParametersSchema,
|
|
533
|
-
ensureResponseSchema,
|
|
534
|
-
decodeResponse,
|
|
535
|
-
encodeParameters,
|
|
536
|
-
get schemaType() {
|
|
537
|
-
return options.types.schema;
|
|
538
|
-
}
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
//#endregion
|
|
543
|
-
//#region src/generators/effect.ts
|
|
544
|
-
var import_lib$2 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
545
|
-
const createEffectSchemaGenerator = (options) => ({
|
|
546
|
-
...createSchemaGenerator({
|
|
547
|
-
...options,
|
|
548
|
-
modifiers: {
|
|
549
|
-
lazy: (expression) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("suspend")), [expression]),
|
|
550
|
-
optional: (expression) => import_lib$2.callExpression(import_lib$2.memberExpression(expression, import_lib$2.identifier("pipe")), [import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("optional"))]),
|
|
551
|
-
regex: (expression, pattern) => import_lib$2.callExpression(import_lib$2.memberExpression(expression, import_lib$2.identifier("pipe")), [import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("pattern")), [import_lib$2.regExpLiteral(pattern)])]),
|
|
552
|
-
immutable: (expression) => expression,
|
|
553
|
-
mutable: (expression) => import_lib$2.callExpression(import_lib$2.memberExpression(expression, import_lib$2.identifier("pipe")), [import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("mutable"))]),
|
|
554
|
-
nullable: (expression) => import_lib$2.callExpression(import_lib$2.memberExpression(expression, import_lib$2.identifier("pipe")), [import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("NullOr"))])
|
|
555
|
-
},
|
|
556
|
-
types: {
|
|
557
|
-
schema: import_lib$2.tsQualifiedName(import_lib$2.identifier("Schema"), import_lib$2.identifier("Schema")),
|
|
558
|
-
typeDecoded: import_lib$2.tsQualifiedName(import_lib$2.tsQualifiedName(import_lib$2.identifier("Schema"), import_lib$2.identifier("Schema")), import_lib$2.identifier("Type")),
|
|
559
|
-
typeEncoded: import_lib$2.tsQualifiedName(import_lib$2.tsQualifiedName(import_lib$2.identifier("Schema"), import_lib$2.identifier("Schema")), import_lib$2.identifier("Encoded"))
|
|
560
|
-
},
|
|
561
|
-
schema: {
|
|
562
|
-
record: (key, value) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Record")), [import_lib$2.objectExpression([import_lib$2.objectProperty(import_lib$2.identifier("key"), key), import_lib$2.objectProperty(import_lib$2.identifier("value"), value)])]),
|
|
563
|
-
catchall: (object, value) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("extend")), [object, import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Record")), [import_lib$2.objectExpression([import_lib$2.objectProperty(import_lib$2.identifier("key"), import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("String"))), import_lib$2.objectProperty(import_lib$2.identifier("value"), value)])])]),
|
|
564
|
-
union: (expressions) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Union")), expressions),
|
|
565
|
-
intersection: (expressions) => expressions.reduce((a, b) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("extend")), [a, b])),
|
|
566
|
-
objectExtend: (expressions) => expressions.reduce((a, b) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("extend")), [a, b])),
|
|
567
|
-
enum: (expressions) => import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Literal")), expressions),
|
|
568
|
-
instanceOf: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("instanceOf")),
|
|
569
|
-
boolean: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Boolean")),
|
|
570
|
-
string: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("String")),
|
|
571
|
-
number: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Number")),
|
|
572
|
-
integer: import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Number")), import_lib$2.identifier("pipe")), [import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("int")), [])]),
|
|
573
|
-
array: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Array")),
|
|
574
|
-
object: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Struct")),
|
|
575
|
-
null: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Null")),
|
|
576
|
-
unknown: import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("Unknown"))
|
|
577
|
-
},
|
|
578
|
-
transformer: ({ encoded, decoded, decode, decodeAsync, encode, encodeAsync }) => {
|
|
579
|
-
let decodeFn;
|
|
580
|
-
let encodeFn;
|
|
581
|
-
if (decodeAsync || encodeAsync) {
|
|
582
|
-
decodeFn = import_lib$2.arrowFunctionExpression([import_lib$2.identifier("from"), import_lib$2.identifier("ctx")], import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Effect"), import_lib$2.identifier("promise")), [import_lib$2.arrowFunctionExpression([], decode, true)]));
|
|
583
|
-
encodeFn = import_lib$2.arrowFunctionExpression([import_lib$2.identifier("from"), import_lib$2.identifier("ctx")], import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Effect"), import_lib$2.identifier("promise")), [import_lib$2.arrowFunctionExpression([], encode, true)]));
|
|
584
|
-
} else {
|
|
585
|
-
decodeFn = import_lib$2.arrowFunctionExpression([import_lib$2.identifier("from"), import_lib$2.identifier("ctx")], decode);
|
|
586
|
-
encodeFn = import_lib$2.arrowFunctionExpression([import_lib$2.identifier("from"), import_lib$2.identifier("ctx")], encode);
|
|
587
|
-
}
|
|
588
|
-
return import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier(decodeAsync || encodeAsync ? "transformOrFail" : "transform")), [
|
|
589
|
-
encoded,
|
|
590
|
-
decoded,
|
|
591
|
-
import_lib$2.objectExpression([
|
|
592
|
-
import_lib$2.objectProperty(import_lib$2.identifier("strict"), import_lib$2.booleanLiteral(true)),
|
|
593
|
-
import_lib$2.objectProperty(import_lib$2.identifier("decode"), decodeFn),
|
|
594
|
-
import_lib$2.objectProperty(import_lib$2.identifier("encode"), encodeFn)
|
|
595
|
-
])
|
|
596
|
-
]);
|
|
597
|
-
},
|
|
598
|
-
transformerCatch: (expression) => import_lib$2.blockStatement([import_lib$2.throwStatement(expression)]),
|
|
599
|
-
methods: {
|
|
600
|
-
encode: (schema, value) => import_lib$2.callExpression(import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("encodePromise")), [schema]), [value]),
|
|
601
|
-
decode: (schema, value) => import_lib$2.callExpression(import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("decodePromise")), [schema]), [value]),
|
|
602
|
-
parse: (schema, value) => import_lib$2.callExpression(import_lib$2.callExpression(import_lib$2.memberExpression(import_lib$2.identifier("Schema"), import_lib$2.identifier("decodeUnknownPromise")), [schema]), [value])
|
|
603
|
-
},
|
|
604
|
-
supportsImmutability: true
|
|
605
|
-
}),
|
|
606
|
-
initialize: Effect.fn(function* () {
|
|
607
|
-
yield* ensureNamespaceImport("Schema", "effect/Schema");
|
|
608
|
-
yield* ensureImport("Effect", "effect");
|
|
609
|
-
})
|
|
610
|
-
});
|
|
611
|
-
|
|
612
|
-
//#endregion
|
|
613
|
-
//#region src/generators/zod.ts
|
|
614
|
-
var import_lib$1 = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
615
|
-
const z = import_lib$1.identifier("z");
|
|
616
|
-
function getObjectShape(expression) {
|
|
617
|
-
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && expression.callee.object.type === "Identifier" && expression.callee.object.name === "z" && expression.callee.property.type === "Identifier" && expression.callee.property.name === "object" && expression.arguments.length === 1 && expression.arguments[0]?.type === "ObjectExpression") return expression.arguments[0];
|
|
618
|
-
return import_lib$1.memberExpression(expression, import_lib$1.identifier("shape"));
|
|
619
|
-
}
|
|
620
|
-
const createZodSchemaGenerator = ({ mini = false,...options }) => {
|
|
621
|
-
const maybeExtension = (expression, extension, args) => mini ? import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier(extension)), [expression, ...args]) : import_lib$1.callExpression(import_lib$1.memberExpression(expression, import_lib$1.identifier(extension)), args);
|
|
622
|
-
return {
|
|
623
|
-
...createSchemaGenerator({
|
|
624
|
-
...options,
|
|
625
|
-
modifiers: {
|
|
626
|
-
lazy: (expression) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("lazy")), [expression]),
|
|
627
|
-
optional: (expression) => maybeExtension(expression, "optional", []),
|
|
628
|
-
regex: (expression, pattern) => maybeExtension(expression, "regex", [import_lib$1.regExpLiteral(pattern)]),
|
|
629
|
-
immutable: (expression) => expression,
|
|
630
|
-
mutable: (expression) => expression,
|
|
631
|
-
nullable: (expression) => maybeExtension(expression, "nullable", [])
|
|
632
|
-
},
|
|
633
|
-
types: {
|
|
634
|
-
schema: import_lib$1.tsQualifiedName(z, import_lib$1.identifier(mini ? "ZodMiniType" : "Schema")),
|
|
635
|
-
typeDecoded: import_lib$1.tsQualifiedName(z, import_lib$1.identifier("output")),
|
|
636
|
-
typeEncoded: import_lib$1.tsQualifiedName(z, import_lib$1.identifier("input"))
|
|
637
|
-
},
|
|
638
|
-
schema: {
|
|
639
|
-
record: (key, value) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("record")), [key, value]),
|
|
640
|
-
catchall: (object, value) => import_lib$1.callExpression(import_lib$1.memberExpression(mini ? z : object, import_lib$1.identifier("catchall")), [...mini ? [object] : [], value]),
|
|
641
|
-
union: (expressions) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("union")), [import_lib$1.arrayExpression(expressions)]),
|
|
642
|
-
intersection: (expressions) => expressions.reduce((a, b) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("intersection")), [a, b])),
|
|
643
|
-
objectExtend: (expressions) => expressions.reduce((a, b) => import_lib$1.callExpression(import_lib$1.memberExpression(mini ? z : a, import_lib$1.identifier("extend")), [...mini ? [a] : [], getObjectShape(b)])),
|
|
644
|
-
enum: (expressions) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("enum")), [import_lib$1.arrayExpression(expressions)]),
|
|
645
|
-
instanceOf: import_lib$1.memberExpression(z, import_lib$1.identifier("instanceof")),
|
|
646
|
-
boolean: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("boolean")), []),
|
|
647
|
-
string: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("string")), []),
|
|
648
|
-
number: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("number")), []),
|
|
649
|
-
integer: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("int")), []),
|
|
650
|
-
array: import_lib$1.memberExpression(z, import_lib$1.identifier("array")),
|
|
651
|
-
object: import_lib$1.memberExpression(z, import_lib$1.identifier("object")),
|
|
652
|
-
null: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("null")), []),
|
|
653
|
-
unknown: import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("unknown")), [])
|
|
654
|
-
},
|
|
655
|
-
transformer: ({ encoded, decoded, decode, decodeAsync, encode, encodeAsync }) => import_lib$1.callExpression(import_lib$1.memberExpression(z, import_lib$1.identifier("codec")), [
|
|
656
|
-
encoded,
|
|
657
|
-
decoded,
|
|
658
|
-
import_lib$1.objectExpression([import_lib$1.objectProperty(import_lib$1.identifier("decode"), import_lib$1.arrowFunctionExpression([import_lib$1.identifier("from"), import_lib$1.identifier("ctx")], decode, decodeAsync)), import_lib$1.objectProperty(import_lib$1.identifier("encode"), import_lib$1.arrowFunctionExpression([import_lib$1.identifier("from"), import_lib$1.identifier("ctx")], encode, encodeAsync))])
|
|
659
|
-
]),
|
|
660
|
-
transformerCatch: (expression) => import_lib$1.blockStatement([import_lib$1.expressionStatement(import_lib$1.callExpression(import_lib$1.memberExpression(import_lib$1.memberExpression(import_lib$1.identifier("ctx"), import_lib$1.identifier("issues")), import_lib$1.identifier("push")), [import_lib$1.objectExpression([
|
|
661
|
-
import_lib$1.objectProperty(import_lib$1.identifier("code"), import_lib$1.stringLiteral("custom")),
|
|
662
|
-
import_lib$1.objectProperty(import_lib$1.identifier("input"), import_lib$1.identifier("from")),
|
|
663
|
-
import_lib$1.objectProperty(import_lib$1.identifier("message"), import_lib$1.memberExpression(import_lib$1.tsAsExpression(expression, import_lib$1.tsTypeReference(import_lib$1.identifier("Error"))), import_lib$1.identifier("message")))
|
|
664
|
-
])])), import_lib$1.returnStatement(import_lib$1.memberExpression(import_lib$1.identifier("z"), import_lib$1.identifier("NEVER")))]),
|
|
665
|
-
methods: {
|
|
666
|
-
encode: (schema, value) => import_lib$1.callExpression(import_lib$1.memberExpression(import_lib$1.identifier("z"), import_lib$1.identifier("encodeAsync")), [schema, value]),
|
|
667
|
-
decode: (schema, value) => import_lib$1.callExpression(import_lib$1.memberExpression(import_lib$1.identifier("z"), import_lib$1.identifier("decodeAsync")), [schema, value]),
|
|
668
|
-
parse: (schema, value) => import_lib$1.callExpression(import_lib$1.memberExpression(import_lib$1.identifier("z"), import_lib$1.identifier("parseAsync")), [schema, value])
|
|
669
|
-
},
|
|
670
|
-
supportsImmutability: false
|
|
671
|
-
}),
|
|
672
|
-
initialize: Effect.fn(function* () {
|
|
673
|
-
yield* ensureImport("z", mini ? "zod/mini" : "zod");
|
|
674
|
-
})
|
|
675
|
-
};
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
//#endregion
|
|
679
|
-
//#region src/generators/client.ts
|
|
680
|
-
var import_lib = /* @__PURE__ */ __toESM(require_lib(), 1);
|
|
681
|
-
function createReactQueryClientGenerator(options) {
|
|
682
|
-
const ensureApi = Effect.fn(function* () {
|
|
683
|
-
const ctx = yield* DocumentContext;
|
|
684
|
-
if (!ctx.schemas.has("Api")) {
|
|
685
|
-
const fetch = import_lib.identifier("fetch");
|
|
686
|
-
const fetchPath = import_lib.identifier("path");
|
|
687
|
-
fetchPath.typeAnnotation = import_lib.tsTypeAnnotation(import_lib.tsStringKeyword());
|
|
688
|
-
const fetchOptions = import_lib.identifier("options");
|
|
689
|
-
fetchOptions.typeAnnotation = import_lib.tsTypeAnnotation(import_lib.tsTypeLiteral([
|
|
690
|
-
import_lib.tsPropertySignature(import_lib.identifier("method"), import_lib.tsTypeAnnotation(import_lib.tsStringKeyword())),
|
|
691
|
-
Object.assign(import_lib.tsPropertySignature(import_lib.identifier("headers"), import_lib.tsTypeAnnotation(import_lib.tsTypeReference(import_lib.identifier("Headers")))), { optional: true }),
|
|
692
|
-
Object.assign(import_lib.tsPropertySignature(import_lib.identifier("body"), import_lib.tsTypeAnnotation(import_lib.tsUnionType([
|
|
693
|
-
import_lib.tsTypeReference(import_lib.identifier("string")),
|
|
694
|
-
import_lib.tsTypeReference(import_lib.identifier("Blob")),
|
|
695
|
-
import_lib.tsTypeReference(import_lib.identifier("FormData")),
|
|
696
|
-
import_lib.tsTypeReference(import_lib.identifier("URLSearchParams"))
|
|
697
|
-
]))), { optional: true })
|
|
698
|
-
]));
|
|
699
|
-
fetch.typeAnnotation = import_lib.tsTypeAnnotation(import_lib.tsFunctionType(null, [fetchPath, fetchOptions], import_lib.tsTypeAnnotation(import_lib.tsTypeReference(import_lib.identifier("Promise"), import_lib.tsTypeParameterInstantiation([import_lib.tsTypeReference(import_lib.identifier("Response"))])))));
|
|
700
|
-
ctx.schemas.set("Api", [import_lib.exportNamedDeclaration(import_lib.classDeclaration(import_lib.identifier("Api"), null, import_lib.classBody([import_lib.classPrivateProperty(import_lib.privateName(fetch)), import_lib.classMethod("constructor", import_lib.identifier("constructor"), [fetch], import_lib.blockStatement([import_lib.expressionStatement(import_lib.assignmentExpression("=", import_lib.memberExpression(import_lib.thisExpression(), import_lib.privateName(fetch)), fetch))]))])))]);
|
|
701
|
-
}
|
|
702
|
-
return ctx.schemas.get("Api")[0].declaration.body;
|
|
703
|
-
});
|
|
704
|
-
return {
|
|
705
|
-
initialize: Effect.fn(function* () {
|
|
706
|
-
yield* ensureApi();
|
|
707
|
-
if (options.schema.initialize) yield* options.schema.initialize();
|
|
708
|
-
}),
|
|
709
|
-
processSchema: Effect.fn(function* (schema) {
|
|
710
|
-
yield* ensureApi();
|
|
711
|
-
yield* options.schema.processSchema(schema);
|
|
712
|
-
}),
|
|
713
|
-
processOperation: Effect.fn(function* (operationKey, path, method, operation) {
|
|
714
|
-
const parametersSchema = yield* options.schema.ensureParametersSchema(operationKey, operation, path);
|
|
715
|
-
const responseSchema = yield* options.schema.ensureResponseSchema(operationKey, operation);
|
|
716
|
-
const parameters = import_lib.identifier("parameters");
|
|
717
|
-
parameters.typeAnnotation = import_lib.tsTypeAnnotation(parametersSchema.typeReference);
|
|
718
|
-
const parametersWithSkipToken = import_lib.identifier("parameters");
|
|
719
|
-
parametersWithSkipToken.typeAnnotation = import_lib.tsTypeAnnotation(import_lib.tsUnionType([parametersSchema.typeReference, import_lib.tsTypeReference(yield* ensureImport("SkipToken", "@tanstack/react-query", true))]));
|
|
720
|
-
const isMutation = method !== "get" && method !== "head" && method !== "options" && !operation.tags?.some((i) => i.toLowerCase() === "query");
|
|
721
|
-
const fetchCall = import_lib.awaitExpression(import_lib.callExpression(import_lib.memberExpression(import_lib.thisExpression(), import_lib.privateName(import_lib.identifier("fetch"))), [import_lib.memberExpression(import_lib.identifier("params"), import_lib.identifier("path")), import_lib.objectExpression([
|
|
722
|
-
import_lib.objectProperty(import_lib.identifier("method"), import_lib.stringLiteral(method.toUpperCase())),
|
|
723
|
-
import_lib.objectProperty(import_lib.identifier("headers"), import_lib.memberExpression(import_lib.identifier("params"), import_lib.identifier("headers"))),
|
|
724
|
-
import_lib.objectProperty(import_lib.identifier("body"), import_lib.memberExpression(import_lib.identifier("params"), import_lib.identifier("body")))
|
|
725
|
-
])]));
|
|
726
|
-
const doMethod = import_lib.classMethod("method", import_lib.identifier(operationKey.lower), [parameters], import_lib.blockStatement([
|
|
727
|
-
import_lib.variableDeclaration("const", [import_lib.variableDeclarator(import_lib.identifier("params"), import_lib.awaitExpression(yield* options.schema.encodeParameters(parametersSchema.expression, import_lib.identifier("parameters"))))]),
|
|
728
|
-
import_lib.variableDeclaration("const", [import_lib.variableDeclarator(import_lib.identifier("response"), fetchCall)]),
|
|
729
|
-
import_lib.returnStatement(import_lib.awaitExpression(yield* options.schema.decodeResponse(responseSchema.expression, import_lib.identifier("response"))))
|
|
730
|
-
]), false, false, false, true);
|
|
731
|
-
const queryMethod = import_lib.classMethod("method", import_lib.identifier(operationKey.lower + (isMutation ? "Mutation" : "Query")), isMutation ? [] : [parametersWithSkipToken], import_lib.blockStatement([import_lib.returnStatement(import_lib.callExpression(yield* ensureImport(isMutation ? "mutationOptions" : "queryOptions", "@tanstack/react-query"), [import_lib.objectExpression([...isMutation ? [] : [import_lib.objectProperty(import_lib.identifier("queryKey"), import_lib.tsAsExpression(import_lib.arrayExpression([import_lib.stringLiteral(operationKey.upper), parametersWithSkipToken]), import_lib.tsTypeReference(yield* ensureImport("QueryKey", "@tanstack/react-query", true))))], isMutation ? import_lib.objectProperty(import_lib.identifier("mutationFn"), import_lib.arrowFunctionExpression([parameters], import_lib.callExpression(import_lib.memberExpression(import_lib.thisExpression(), import_lib.identifier(operationKey.lower)), [parameters]), true)) : import_lib.objectProperty(import_lib.identifier("queryFn"), import_lib.conditionalExpression(import_lib.binaryExpression("===", parametersWithSkipToken, yield* ensureImport("skipToken", "@tanstack/react-query")), yield* ensureImport("skipToken", "@tanstack/react-query"), import_lib.arrowFunctionExpression([], import_lib.callExpression(import_lib.memberExpression(import_lib.thisExpression(), import_lib.identifier(operationKey.lower)), [parametersWithSkipToken]), true)))])]))]));
|
|
732
|
-
const commentLines = [];
|
|
733
|
-
if (operation.summary) commentLines.push(`### ${operation.summary}`);
|
|
734
|
-
if (operation.description) commentLines.push(`${operation.description}`);
|
|
735
|
-
if (commentLines.length) {
|
|
736
|
-
import_lib.addComment(queryMethod, "leading", `*\n${commentLines.join("\n")}\n*`);
|
|
737
|
-
import_lib.addComment(doMethod, "leading", `*\n${commentLines.join("\n")}\n*`);
|
|
738
|
-
}
|
|
739
|
-
const api = yield* ensureApi();
|
|
740
|
-
api.body.push(doMethod);
|
|
741
|
-
api.body.push(queryMethod);
|
|
742
|
-
})
|
|
743
|
-
};
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
//#endregion
|
|
747
|
-
export { createEffectSchemaGenerator, createReactQueryClientGenerator, createSchemaGenerator, createZodSchemaGenerator, helpers_exports as generatorHelpers };
|
|
748
|
-
//# sourceMappingURL=index.js.map
|