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