@7nohe/openapi-react-query-codegen 3.1.0 → 4.0.0-beta.2
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/AGENTS.md +207 -0
- package/README.md +7 -3
- package/dist/check.d.mts +59 -0
- package/dist/check.mjs +162 -0
- package/dist/cli.d.mts +6 -1
- package/dist/cli.mjs +4 -31
- package/dist/client/emitSchemas.d.mts +9 -0
- package/dist/client/emitSchemas.mjs +130 -0
- package/dist/client/emitSdk.d.mts +18 -0
- package/dist/client/emitSdk.mjs +253 -0
- package/dist/client/emitTransformers.d.mts +19 -0
- package/dist/client/emitTransformers.mjs +556 -0
- package/dist/client/emitTypes.d.mts +16 -0
- package/dist/client/emitTypes.mjs +204 -0
- package/dist/client/index.d.mts +44 -0
- package/dist/client/index.mjs +126 -0
- package/dist/client/naming.d.mts +41 -0
- package/dist/client/naming.mjs +175 -0
- package/dist/client/operations.d.mts +103 -0
- package/dist/client/operations.mjs +352 -0
- package/dist/client/runtime/axios.runtime.ts +706 -0
- package/dist/client/runtime/fetch.runtime.ts +795 -0
- package/dist/client/schema.d.mts +50 -0
- package/dist/client/schema.mjs +648 -0
- package/dist/client/spec.d.mts +40 -0
- package/dist/client/spec.mjs +387 -0
- package/dist/client/tsconfig.d.mts +13 -0
- package/dist/client/tsconfig.mjs +56 -0
- package/dist/common.d.mts +0 -38
- package/dist/common.mjs +0 -87
- package/dist/constants.d.mts +0 -2
- package/dist/constants.mjs +0 -2
- package/dist/createSource.d.mts +8 -7
- package/dist/createSource.mjs +14 -17
- package/dist/errors.d.mts +43 -0
- package/dist/errors.mjs +61 -0
- package/dist/format.mjs +0 -1
- package/dist/generate.d.mts +25 -3
- package/dist/generate.mjs +83 -83
- package/dist/runCli.d.mts +20 -0
- package/dist/runCli.mjs +105 -0
- package/dist/tsmorph/buildQueryHooks.mjs +19 -11
- package/dist/tsmorph/generateFiles.mjs +105 -161
- package/dist/tsmorph/operationNames.d.mts +2 -13
- package/dist/tsmorph/operationNames.mjs +4 -24
- package/dist/tsmorph/projectFactory.d.mts +7 -2
- package/dist/tsmorph/projectFactory.mjs +16 -9
- package/dist/types.d.mts +14 -26
- package/package.json +8 -8
- package/dist/parseOperations.d.mts +0 -10
- package/dist/parseOperations.mjs +0 -164
- package/dist/service.d.mts +0 -8
- package/dist/service.mjs +0 -98
- package/dist/util.d.mts +0 -2
- package/dist/util.mjs +0 -16
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { HEADER } from "./emitSdk.mjs";
|
|
2
|
+
import { propertyKey, uniqueName } from "./naming.mjs";
|
|
3
|
+
import { componentSchemaKey, hasFlag } from "./schema.mjs";
|
|
4
|
+
const DOCUMENTATION_KEYWORDS = [
|
|
5
|
+
"description",
|
|
6
|
+
"title",
|
|
7
|
+
"x-enum-descriptions",
|
|
8
|
+
"x-enum-varnames",
|
|
9
|
+
"x-enumNames",
|
|
10
|
+
];
|
|
11
|
+
/** The keywords that hold one schema, a list of them, or a map of them. */
|
|
12
|
+
const SCHEMA_KEYWORDS = ["additionalProperties", "items", "not"];
|
|
13
|
+
const SCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
14
|
+
const SCHEMA_MAP_KEYWORDS = ["patternProperties", "properties"];
|
|
15
|
+
/**
|
|
16
|
+
* Copy a schema for one side of the API: without the properties the other side
|
|
17
|
+
* owns, and with its references pointing at that side's schemas.
|
|
18
|
+
*/
|
|
19
|
+
function project(schema, ctx, variant) {
|
|
20
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
21
|
+
return schema;
|
|
22
|
+
}
|
|
23
|
+
const entries = [];
|
|
24
|
+
const removed = new Set();
|
|
25
|
+
for (const [keyword, value] of Object.entries(schema)) {
|
|
26
|
+
if (variant.mode === "form" && DOCUMENTATION_KEYWORDS.includes(keyword)) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (keyword === "$ref" && typeof value === "string") {
|
|
30
|
+
const key = componentSchemaKey(value);
|
|
31
|
+
const renamed = key === undefined ? undefined : variant.renamed.get(key);
|
|
32
|
+
entries.push([
|
|
33
|
+
keyword,
|
|
34
|
+
renamed === undefined ? value : `#/components/schemas/${renamed}`,
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
else if (SCHEMA_KEYWORDS.includes(keyword)) {
|
|
38
|
+
entries.push([keyword, project(value, ctx, variant)]);
|
|
39
|
+
}
|
|
40
|
+
else if (SCHEMA_LIST_KEYWORDS.includes(keyword) && Array.isArray(value)) {
|
|
41
|
+
entries.push([
|
|
42
|
+
keyword,
|
|
43
|
+
value.map((member) => project(member, ctx, variant)),
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
else if (SCHEMA_MAP_KEYWORDS.includes(keyword) &&
|
|
47
|
+
value &&
|
|
48
|
+
typeof value === "object" &&
|
|
49
|
+
!Array.isArray(value)) {
|
|
50
|
+
const members = [];
|
|
51
|
+
for (const [name, member] of Object.entries(value)) {
|
|
52
|
+
if (keyword === "properties" &&
|
|
53
|
+
hasFlag(ctx.doc, member, variant.hidden)) {
|
|
54
|
+
removed.add(name);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
members.push([name, project(member, ctx, variant)]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
entries.push([keyword, Object.fromEntries(members)]);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
entries.push([keyword, value]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return Object.fromEntries(entries.flatMap(([keyword, value]) => {
|
|
67
|
+
if (keyword !== "required" || removed.size === 0) {
|
|
68
|
+
return [[keyword, value]];
|
|
69
|
+
}
|
|
70
|
+
if (!Array.isArray(value))
|
|
71
|
+
return [[keyword, value]];
|
|
72
|
+
const required = value.filter((name) => !removed.has(name));
|
|
73
|
+
return required.length > 0 ? [[keyword, required]] : [];
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
function literal(value, indent = "") {
|
|
77
|
+
if (value === null || typeof value !== "object") {
|
|
78
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
79
|
+
}
|
|
80
|
+
const inner = `${indent} `;
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
if (value.length === 0)
|
|
83
|
+
return "[]";
|
|
84
|
+
const items = value.map((item) => `${inner}${literal(item, inner)},`);
|
|
85
|
+
return `[\n${items.join("\n")}\n${indent}]`;
|
|
86
|
+
}
|
|
87
|
+
const entries = Object.entries(value).filter(([, item]) => item !== undefined);
|
|
88
|
+
if (entries.length === 0)
|
|
89
|
+
return "{}";
|
|
90
|
+
const lines = entries.map(([name, item]) => `${inner}${propertyKey(name)}: ${literal(item, inner)},`);
|
|
91
|
+
return `{\n${lines.join("\n")}\n${indent}}`;
|
|
92
|
+
}
|
|
93
|
+
/** `my-schema` → `my_schema`, as v3 named the constants. */
|
|
94
|
+
function constantName(schemaKey) {
|
|
95
|
+
const name = schemaKey.replace(/[^A-Za-z0-9_$]/g, "_");
|
|
96
|
+
return /^\d/.test(name) ? `_${name}` : name;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generate `schemas.gen.ts`: every component schema as a constant, the way the
|
|
100
|
+
* specification writes it. A schema with `readOnly` or `writeOnly` properties
|
|
101
|
+
* comes in two: what the API returns, and `…WritableSchema` for what it takes.
|
|
102
|
+
*/
|
|
103
|
+
export function emitSchemas(ctx, mode) {
|
|
104
|
+
const schemas = (ctx.doc.components?.schemas ?? {});
|
|
105
|
+
const keys = Object.keys(schemas);
|
|
106
|
+
if (keys.length === 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
const used = new Set();
|
|
109
|
+
const blocks = [];
|
|
110
|
+
const add = (name, schema, variant) => {
|
|
111
|
+
blocks.push(`export const ${uniqueName(name, used)} = ${literal(project(schema, ctx, variant))} as const;`);
|
|
112
|
+
};
|
|
113
|
+
for (const schemaKey of keys) {
|
|
114
|
+
add(`${constantName(schemaKey)}Schema`, schemas[schemaKey], {
|
|
115
|
+
hidden: "writeOnly",
|
|
116
|
+
renamed: new Map(),
|
|
117
|
+
mode,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const writable = keys.filter((schemaKey) => ctx.writableNames?.has(schemaKey));
|
|
121
|
+
const renamed = new Map(writable.map((schemaKey) => [schemaKey, `${schemaKey}Writable`]));
|
|
122
|
+
for (const schemaKey of writable) {
|
|
123
|
+
add(`${constantName(schemaKey)}WritableSchema`, schemas[schemaKey], {
|
|
124
|
+
hidden: "readOnly",
|
|
125
|
+
renamed,
|
|
126
|
+
mode,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return `${HEADER}\n${blocks.join("\n\n")}\n`;
|
|
130
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type OperationIR } from "./operations.mjs";
|
|
2
|
+
import { type Doc } from "./spec.mjs";
|
|
3
|
+
export type ClientKind = "fetch" | "axios";
|
|
4
|
+
export declare const HEADER = "// This file is auto-generated by @7nohe/openapi-react-query-codegen\n";
|
|
5
|
+
export declare function optionsAreOptional(operation: OperationIR): boolean;
|
|
6
|
+
export declare function emitSdk(doc: Doc, operations: OperationIR[], options: {
|
|
7
|
+
kind: ClientKind;
|
|
8
|
+
ext: string;
|
|
9
|
+
/** SDK function name → response transformer from `transformers.gen.ts`. */
|
|
10
|
+
transformers?: ReadonlyMap<string, string>;
|
|
11
|
+
}): string;
|
|
12
|
+
export declare function emitClientFile(options: {
|
|
13
|
+
kind: ClientKind;
|
|
14
|
+
ext: string;
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
}): string;
|
|
17
|
+
export declare function emitIndexFile(ext: string): string;
|
|
18
|
+
export declare function emitServicesShim(ext: string): string;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { classifyResponses, mediaKind, } from "./operations.mjs";
|
|
2
|
+
import { docComment } from "./schema.mjs";
|
|
3
|
+
import { resolveRef } from "./spec.mjs";
|
|
4
|
+
export const HEADER = "// This file is auto-generated by @7nohe/openapi-react-query-codegen\n";
|
|
5
|
+
const CONTENT_TYPE_BY_KIND = {
|
|
6
|
+
urlencoded: "application/x-www-form-urlencoded",
|
|
7
|
+
};
|
|
8
|
+
function q(value) {
|
|
9
|
+
return JSON.stringify(value);
|
|
10
|
+
}
|
|
11
|
+
function schemaType(doc, schema) {
|
|
12
|
+
const resolved = resolveRef(doc, schema);
|
|
13
|
+
if (!resolved || typeof resolved !== "object")
|
|
14
|
+
return undefined;
|
|
15
|
+
if (Array.isArray(resolved.type)) {
|
|
16
|
+
const types = resolved.type.filter((item) => item !== "null");
|
|
17
|
+
// Alternatives of different shapes have no one type to go by.
|
|
18
|
+
return types.length > 1 && !types.every((item) => PRIMITIVE_TYPES.has(item))
|
|
19
|
+
? undefined
|
|
20
|
+
: types[0];
|
|
21
|
+
}
|
|
22
|
+
if (resolved.type)
|
|
23
|
+
return resolved.type;
|
|
24
|
+
if (resolved.properties)
|
|
25
|
+
return "object";
|
|
26
|
+
if (resolved.items)
|
|
27
|
+
return "array";
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const PRIMITIVE_TYPES = new Set(["boolean", "integer", "number", "string"]);
|
|
31
|
+
/**
|
|
32
|
+
* Query parameters the runtime defaults would serialize differently from the
|
|
33
|
+
* OpenAPI ones. The runtime leaves arrays in `form` style with `explode`, and
|
|
34
|
+
* objects in `deepObject` style, so only the rest is spelled out. OpenAPI
|
|
35
|
+
* defaults a query parameter to `form`, and `explode` to true for `form` only.
|
|
36
|
+
*/
|
|
37
|
+
function querySerializerEntries(doc, parameters) {
|
|
38
|
+
const entries = [];
|
|
39
|
+
for (const parameter of parameters) {
|
|
40
|
+
if (parameter.in !== "query")
|
|
41
|
+
continue;
|
|
42
|
+
if (parameter.json) {
|
|
43
|
+
entries.push(`${q(parameter.name)}: { json: true }`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const type = schemaType(doc, parameter.schema);
|
|
47
|
+
if (type !== undefined && PRIMITIVE_TYPES.has(type))
|
|
48
|
+
continue;
|
|
49
|
+
const style = parameter.style ?? "form";
|
|
50
|
+
const explode = parameter.explode ?? style === "form";
|
|
51
|
+
if (style === "deepObject")
|
|
52
|
+
continue;
|
|
53
|
+
// A schema without a plain type (`anyOf`, say) can hold either shape.
|
|
54
|
+
const settings = [];
|
|
55
|
+
if (type !== "object" && !(style === "form" && explode)) {
|
|
56
|
+
settings.push(`array: { style: ${q(style)}, explode: ${explode} }`);
|
|
57
|
+
}
|
|
58
|
+
if (type !== "array") {
|
|
59
|
+
settings.push(`object: { style: "form", explode: ${explode} }`);
|
|
60
|
+
}
|
|
61
|
+
if (settings.length > 0) {
|
|
62
|
+
entries.push(`${q(parameter.name)}: { ${settings.join(", ")} }`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return entries;
|
|
66
|
+
}
|
|
67
|
+
/** Path parameters that are not in the default `simple` style. */
|
|
68
|
+
function pathSerializerEntries(doc, parameters) {
|
|
69
|
+
const entries = [];
|
|
70
|
+
for (const parameter of parameters) {
|
|
71
|
+
if (parameter.in !== "path")
|
|
72
|
+
continue;
|
|
73
|
+
if (parameter.json) {
|
|
74
|
+
entries.push(`${q(parameter.name)}: { json: true }`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const style = parameter.style === "label" || parameter.style === "matrix"
|
|
78
|
+
? parameter.style
|
|
79
|
+
: "simple";
|
|
80
|
+
const explode = parameter.explode ?? false;
|
|
81
|
+
if (style === "simple") {
|
|
82
|
+
// `explode` only changes how an object is written.
|
|
83
|
+
const type = schemaType(doc, parameter.schema);
|
|
84
|
+
if (!explode || type === "array" || PRIMITIVE_TYPES.has(type ?? "")) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
entries.push(`${q(parameter.name)}: { style: ${q(style)}, explode: ${explode} }`);
|
|
89
|
+
}
|
|
90
|
+
return entries;
|
|
91
|
+
}
|
|
92
|
+
function securityLiteral(security) {
|
|
93
|
+
const key = security.key ? `key: ${q(security.key)}, ` : "";
|
|
94
|
+
if (security.type === "apiKey") {
|
|
95
|
+
return `{ in: ${q(security.in ?? "header")}, ${key}name: ${q(security.name ?? "")}, type: "apiKey" }`;
|
|
96
|
+
}
|
|
97
|
+
const scheme = security.scheme === "basic" ? "basic" : "bearer";
|
|
98
|
+
return `{ ${key}scheme: ${q(scheme)}, type: "http" }`;
|
|
99
|
+
}
|
|
100
|
+
/** One alternative: a scheme, or the array of schemes it needs together. */
|
|
101
|
+
function securityRequirementLiteral(requirement) {
|
|
102
|
+
const literals = requirement.map(securityLiteral);
|
|
103
|
+
return literals.length === 1 ? literals[0] : `[${literals.join(", ")}]`;
|
|
104
|
+
}
|
|
105
|
+
function parseKind(mediaType) {
|
|
106
|
+
const kind = mediaKind(mediaType);
|
|
107
|
+
if (kind === "json" || kind === "text")
|
|
108
|
+
return kind;
|
|
109
|
+
if (kind === "form-data")
|
|
110
|
+
return "formData";
|
|
111
|
+
// Readable documents, the same as the runtime when it detects the type.
|
|
112
|
+
const type = mediaType.toLowerCase().split(";")[0].trim();
|
|
113
|
+
return type === "application/xml" || type.endsWith("+xml") ? "text" : "blob";
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* How to read a successful response, when every body the operation can answer
|
|
117
|
+
* with is read the same way. With a mix (`200` JSON and `202` text, say) the
|
|
118
|
+
* runtime has to look at the response itself.
|
|
119
|
+
*/
|
|
120
|
+
function parseAsFor(operation) {
|
|
121
|
+
const kinds = new Set(operation.responses
|
|
122
|
+
.filter((response) => /^2/.test(response.status))
|
|
123
|
+
.flatMap((response) => response.mediaTypes.map(parseKind)));
|
|
124
|
+
return kinds.size === 1 ? [...kinds][0] : undefined;
|
|
125
|
+
}
|
|
126
|
+
export function optionsAreOptional(operation) {
|
|
127
|
+
if (operation.body?.required)
|
|
128
|
+
return false;
|
|
129
|
+
return !operation.parameters.some((parameter) => parameter.required);
|
|
130
|
+
}
|
|
131
|
+
function emitOperation(operation, doc, kind, transformer) {
|
|
132
|
+
const names = operation.typeNames;
|
|
133
|
+
const { responses, errors } = classifyResponses(operation);
|
|
134
|
+
const dataType = names.data;
|
|
135
|
+
const responsesType = responses.length > 0 ? names.responses : "unknown";
|
|
136
|
+
const errorsType = errors.length > 0 ? names.errors : "unknown";
|
|
137
|
+
const usedTypes = [dataType];
|
|
138
|
+
if (responses.length > 0)
|
|
139
|
+
usedTypes.push(responsesType);
|
|
140
|
+
if (errors.length > 0)
|
|
141
|
+
usedTypes.push(errorsType);
|
|
142
|
+
const optional = optionsAreOptional(operation);
|
|
143
|
+
const access = optional ? "options?" : "options";
|
|
144
|
+
const lines = [];
|
|
145
|
+
if (transformer)
|
|
146
|
+
lines.push(`responseTransformer: ${transformer}`);
|
|
147
|
+
// Axios has to know before the response arrives. The fetch runtime follows
|
|
148
|
+
// the response's own Content-Type, or what the client is configured to do.
|
|
149
|
+
const parseAs = kind === "axios" ? parseAsFor(operation) : undefined;
|
|
150
|
+
if (parseAs) {
|
|
151
|
+
lines.push(`responseType: ${q(parseAs === "formData" ? "json" : parseAs)}`);
|
|
152
|
+
}
|
|
153
|
+
const security = operation.security.map(securityRequirementLiteral);
|
|
154
|
+
if (security.length > 0)
|
|
155
|
+
lines.push(`security: [${security.join(", ")}]`);
|
|
156
|
+
const queryEntries = querySerializerEntries(doc, operation.parameters);
|
|
157
|
+
if (queryEntries.length > 0) {
|
|
158
|
+
lines.push(`querySerializer: { parameters: { ${queryEntries.join(", ")} } }`);
|
|
159
|
+
}
|
|
160
|
+
const pathEntries = pathSerializerEntries(doc, operation.parameters);
|
|
161
|
+
if (pathEntries.length > 0) {
|
|
162
|
+
lines.push(`pathSerializer: { parameters: { ${pathEntries.join(", ")} } }`);
|
|
163
|
+
}
|
|
164
|
+
const jsonHeaders = operation.parameters
|
|
165
|
+
.filter((parameter) => parameter.in === "header" && parameter.json)
|
|
166
|
+
.map((parameter) => q(parameter.name));
|
|
167
|
+
if (jsonHeaders.length > 0) {
|
|
168
|
+
lines.push(`jsonHeaders: [${jsonHeaders.join(", ")}]`);
|
|
169
|
+
}
|
|
170
|
+
lines.push(`url: ${q(operation.path)}`);
|
|
171
|
+
const bodyKind = operation.body?.kind;
|
|
172
|
+
if (bodyKind === "json" ||
|
|
173
|
+
bodyKind === "form-data" ||
|
|
174
|
+
bodyKind === "urlencoded") {
|
|
175
|
+
lines.push(`bodyType: ${q(bodyKind)}`);
|
|
176
|
+
}
|
|
177
|
+
lines.push("...options");
|
|
178
|
+
if (operation.body) {
|
|
179
|
+
// Multipart clears any configured value: the boundary has to come from the
|
|
180
|
+
// platform, which only adds the header when there is none.
|
|
181
|
+
const contentType = bodyKind === "form-data"
|
|
182
|
+
? "null"
|
|
183
|
+
: q(CONTENT_TYPE_BY_KIND[operation.body.kind] ??
|
|
184
|
+
operation.body.mediaType);
|
|
185
|
+
lines.push(`headers: {\n "Content-Type": ${contentType},\n ...${access}.headers\n }`);
|
|
186
|
+
}
|
|
187
|
+
const body = lines
|
|
188
|
+
.map((line, index) => ` ${line}${index < lines.length - 1 ? "," : ""}`)
|
|
189
|
+
.join("\n");
|
|
190
|
+
const doc2 = operation.jsDoc ? `${operation.jsDoc}\n` : "";
|
|
191
|
+
const generics = `<${responsesType}, ${errorsType}, ThrowOnError>`;
|
|
192
|
+
const code = `${doc2}export const ${operation.functionName} = <ThrowOnError extends boolean = false>(${optional ? "options?" : "options"}: Options<${dataType}, ThrowOnError>): RequestResult<${responsesType}, ${errorsType}, ThrowOnError> => (${access}.client ?? client).${operation.method}${generics}({\n${body}\n});`;
|
|
193
|
+
return { code, usedTypes };
|
|
194
|
+
}
|
|
195
|
+
export function emitSdk(doc, operations, options) {
|
|
196
|
+
const used = new Set();
|
|
197
|
+
const blocks = [];
|
|
198
|
+
for (const operation of operations) {
|
|
199
|
+
const { code, usedTypes } = emitOperation(operation, doc, options.kind, options.transformers?.get(operation.functionName));
|
|
200
|
+
for (const type of usedTypes)
|
|
201
|
+
used.add(type);
|
|
202
|
+
blocks.push(code);
|
|
203
|
+
}
|
|
204
|
+
const typeImports = [...used].sort().join(", ");
|
|
205
|
+
const imports = [
|
|
206
|
+
`import { client } from "./client.gen${options.ext}";`,
|
|
207
|
+
`import type { Client, Options as Options2, RequestResult, TDataShape } from "./runtime.gen${options.ext}";`,
|
|
208
|
+
];
|
|
209
|
+
const transformers = [...(options.transformers?.values() ?? [])].sort();
|
|
210
|
+
if (transformers.length > 0) {
|
|
211
|
+
imports.push(`import { ${transformers.join(", ")} } from "./transformers.gen${options.ext}";`);
|
|
212
|
+
}
|
|
213
|
+
if (typeImports) {
|
|
214
|
+
imports.push(`import type { ${typeImports} } from "./types.gen${options.ext}";`);
|
|
215
|
+
}
|
|
216
|
+
const optionsType = `export type Options<
|
|
217
|
+
TData extends TDataShape = TDataShape,
|
|
218
|
+
ThrowOnError extends boolean = boolean,
|
|
219
|
+
> = Options2<TData, ThrowOnError> & {
|
|
220
|
+
${docComment({ description: "You can provide a client instance returned by `createClient()` instead of\nindividual options. This might be also useful if you want to implement a\ncustom client." }, " ")} client?: Client;
|
|
221
|
+
${docComment({ description: "You can pass arbitrary values through the `meta` object. This can be\nused to access values that aren't defined as part of the SDK function." }, " ")} meta?: Record<string, unknown>;
|
|
222
|
+
};`;
|
|
223
|
+
return `${HEADER}\n${imports.join("\n")}\n\n${optionsType}\n\n${blocks.join("\n\n")}\n`;
|
|
224
|
+
}
|
|
225
|
+
export function emitClientFile(options) {
|
|
226
|
+
const baseUrlKey = options.kind === "axios" ? "baseURL" : "baseUrl";
|
|
227
|
+
const config = options.baseUrl
|
|
228
|
+
? `{ ${baseUrlKey}: ${q(options.baseUrl)} }`
|
|
229
|
+
: "";
|
|
230
|
+
return `${HEADER}
|
|
231
|
+
import {
|
|
232
|
+
type ClientOptions,
|
|
233
|
+
type Config,
|
|
234
|
+
createClient,
|
|
235
|
+
createConfig,
|
|
236
|
+
} from "./runtime.gen${options.ext}";
|
|
237
|
+
import type { ClientOptions as ClientOptions2 } from "./types.gen${options.ext}";
|
|
238
|
+
|
|
239
|
+
${docComment({
|
|
240
|
+
description: "The `createClientConfig()` function will be called on client initialization\nand the returned object will become the client's initial configuration.\n\nYou may want to initialize your client this way instead of calling\n`setConfig()`. This is useful for example if you're using Next.js\nto ensure your client always has the correct values.",
|
|
241
|
+
})}export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (
|
|
242
|
+
override?: Config<ClientOptions & T>,
|
|
243
|
+
) => Config<Required<ClientOptions> & T>;
|
|
244
|
+
|
|
245
|
+
export const client = createClient(createConfig<ClientOptions2>(${config}));
|
|
246
|
+
`;
|
|
247
|
+
}
|
|
248
|
+
export function emitIndexFile(ext) {
|
|
249
|
+
return `${HEADER}\nexport * from "./sdk.gen${ext}";\nexport * from "./types.gen${ext}";\n`;
|
|
250
|
+
}
|
|
251
|
+
export function emitServicesShim(ext) {
|
|
252
|
+
return `// This file is auto-generated for backward compatibility\nexport * from "./client.gen${ext}";\nexport * from "./sdk.gen${ext}";\n`;
|
|
253
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type OperationIR } from "./operations.mjs";
|
|
2
|
+
import { type SchemaCtx } from "./schema.mjs";
|
|
3
|
+
export type EmittedTransformers = {
|
|
4
|
+
/** The file, or undefined when no response holds a date. */
|
|
5
|
+
content: string | undefined;
|
|
6
|
+
/** SDK function name → name of its exported response transformer. */
|
|
7
|
+
byOperation: Map<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* Unions whose members cannot be told apart at runtime. Their dates stay
|
|
10
|
+
* strings although the types say `Date`.
|
|
11
|
+
*/
|
|
12
|
+
skipped: string[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Generate `transformers.gen.ts` for `--useDateType`: a function per operation
|
|
16
|
+
* that turns the `date` and `date-time` strings of its successful response
|
|
17
|
+
* into `Date` objects, as the generated types say they are.
|
|
18
|
+
*/
|
|
19
|
+
export declare function emitTransformers(operations: OperationIR[], ctx: SchemaCtx): EmittedTransformers;
|