@7nohe/openapi-react-query-codegen 4.0.0-beta.7 → 4.0.0-beta.8
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 +31 -3
- package/README.md +56 -9
- package/dist/check.mjs +14 -4
- package/dist/cli.d.mts +2 -0
- package/dist/client/emitMocks.d.mts +23 -0
- package/dist/client/emitMocks.mjs +353 -0
- package/dist/client/emitSdk.mjs +8 -3
- package/dist/client/emitTypes.d.mts +6 -1
- package/dist/client/emitTypes.mjs +16 -7
- package/dist/client/index.d.mts +12 -0
- package/dist/client/index.mjs +12 -0
- package/dist/client/mockValue.d.mts +34 -0
- package/dist/client/mockValue.mjs +796 -0
- package/dist/client/msw.d.mts +11 -0
- package/dist/client/msw.mjs +27 -0
- package/dist/client/operations.d.mts +4 -0
- package/dist/client/operations.mjs +25 -0
- package/dist/client/schema.d.mts +25 -3
- package/dist/client/schema.mjs +36 -24
- package/dist/common.d.mts +1 -0
- package/dist/common.mjs +4 -1
- package/dist/constants.d.mts +1 -0
- package/dist/constants.mjs +1 -0
- package/dist/format.d.mts +3 -1
- package/dist/format.mjs +3 -3
- package/dist/generate.mjs +19 -2
- package/dist/hooks/buildCommon.d.mts +6 -1
- package/dist/hooks/buildCommon.mjs +7 -2
- package/dist/runCli.mjs +1 -0
- package/package.json +16 -9
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MswMajor } from "./emitMocks.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* The major version of MSW the project in `cwd` has installed, which decides
|
|
4
|
+
* where the generated handlers import `http` from. Version 3 when MSW is not
|
|
5
|
+
* installed yet: that is what installing it now gives.
|
|
6
|
+
*/
|
|
7
|
+
export declare function detectMswMajor(cwd: string): {
|
|
8
|
+
major: MswMajor;
|
|
9
|
+
/** Set when the installed version cannot run the handlers. */
|
|
10
|
+
warning?: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
/**
|
|
5
|
+
* The major version of MSW the project in `cwd` has installed, which decides
|
|
6
|
+
* where the generated handlers import `http` from. Version 3 when MSW is not
|
|
7
|
+
* installed yet: that is what installing it now gives.
|
|
8
|
+
*/
|
|
9
|
+
export function detectMswMajor(cwd) {
|
|
10
|
+
let version;
|
|
11
|
+
try {
|
|
12
|
+
const require = createRequire(path.join(cwd, "package.json"));
|
|
13
|
+
version = JSON.parse(readFileSync(require.resolve("msw/package.json"), "utf-8")).version;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return { major: 3 };
|
|
17
|
+
}
|
|
18
|
+
const major = Number.parseInt(String(version), 10);
|
|
19
|
+
if (major >= 3)
|
|
20
|
+
return { major: 3 };
|
|
21
|
+
if (major === 2)
|
|
22
|
+
return { major: 2 };
|
|
23
|
+
return {
|
|
24
|
+
major: 2,
|
|
25
|
+
warning: `--mock: the handlers need MSW 2 or later, and version ${String(version)} is installed.`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -30,6 +30,10 @@ export interface ResponseIR {
|
|
|
30
30
|
schema?: Json;
|
|
31
31
|
/** Every media type the response declares. */
|
|
32
32
|
mediaTypes: string[];
|
|
33
|
+
/** The media type `schema` and `example` are taken from. */
|
|
34
|
+
mediaType?: string;
|
|
35
|
+
/** The body the media type gives as its `example`, or as the first of its `examples`. */
|
|
36
|
+
example?: Json;
|
|
33
37
|
}
|
|
34
38
|
export interface SecurityIR {
|
|
35
39
|
type: "http" | "apiKey";
|
|
@@ -93,6 +93,29 @@ export function pickContent(content) {
|
|
|
93
93
|
const [mediaType, media] = json ?? entries[0];
|
|
94
94
|
return { mediaType, media };
|
|
95
95
|
}
|
|
96
|
+
/** The example of a media type: its `example`, or the first of its `examples`. */
|
|
97
|
+
function mediaExample(doc, media) {
|
|
98
|
+
if (!media || typeof media !== "object")
|
|
99
|
+
return undefined;
|
|
100
|
+
if (media.example !== undefined)
|
|
101
|
+
return media.example;
|
|
102
|
+
const examples = media.examples && typeof media.examples === "object"
|
|
103
|
+
? Object.values(media.examples)
|
|
104
|
+
: [];
|
|
105
|
+
for (const raw of examples) {
|
|
106
|
+
let example;
|
|
107
|
+
try {
|
|
108
|
+
example = deref(doc, raw);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// An example that is not there is no example.
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (example?.value !== undefined)
|
|
115
|
+
return example.value;
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
96
119
|
function buildJsDoc(summary, description, deprecated) {
|
|
97
120
|
const text = description ?? summary;
|
|
98
121
|
const block = docComment({ description: text, deprecated });
|
|
@@ -239,6 +262,8 @@ export function collectOperations(doc, options = {}) {
|
|
|
239
262
|
description: response.description,
|
|
240
263
|
schema: content?.media?.schema,
|
|
241
264
|
mediaTypes: Object.keys(response.content ?? {}),
|
|
265
|
+
mediaType: content?.mediaType,
|
|
266
|
+
example: mediaExample(doc, content?.media),
|
|
242
267
|
});
|
|
243
268
|
}
|
|
244
269
|
const deprecated = operation.deprecated === true;
|
package/dist/client/schema.d.mts
CHANGED
|
@@ -25,18 +25,28 @@ export declare function docComment(parts: {
|
|
|
25
25
|
}, indent?: string): string;
|
|
26
26
|
/** `title` and `description` of a schema, as one documentation text. */
|
|
27
27
|
export declare function schemaDescription(schema: Json): string | undefined;
|
|
28
|
+
export declare function isNullableSchema(schema: Json): boolean;
|
|
28
29
|
type Seen = ReadonlySet<string>;
|
|
30
|
+
/** Marks a `$ref` member of an `allOf` whose discriminator literal is replaced. */
|
|
31
|
+
export declare const OMITTED_PROPERTY = "x-orqc-omit";
|
|
32
|
+
/** True when a schema or what it is composed of requires `name`. */
|
|
33
|
+
export declare function requiresProperty(doc: Doc, schema: Json, name: string, visited?: Set<any>): boolean;
|
|
29
34
|
/**
|
|
30
|
-
*
|
|
31
|
-
* discriminator gets its own value of the discriminator property, from the
|
|
32
|
-
* mapping or else its name, so that a union of subtypes narrows on it.
|
|
35
|
+
* A component schema as it is rendered. A subtype that builds on a schema with
|
|
36
|
+
* a discriminator gets its own value of the discriminator property, from the
|
|
37
|
+
* mapping or else its name, so that a union of subtypes narrows on it. Any
|
|
38
|
+
* other schema is returned as it is.
|
|
33
39
|
*/
|
|
40
|
+
export declare function componentSchemaView(schema: Json, key: string, ctx: SchemaCtx): Json;
|
|
41
|
+
/** Render a component schema, as {@link componentSchemaView} shows it. */
|
|
34
42
|
export declare function renderComponentSchema(schema: Json, key: string, ctx: SchemaCtx): string;
|
|
35
43
|
/**
|
|
36
44
|
* Whether a schema that is composed of others (`allOf`, `oneOf`, `anyOf`) also
|
|
37
45
|
* describes an object itself, which its members are then intersected with.
|
|
38
46
|
*/
|
|
39
47
|
export declare function hasObjectKeywords(schema: Json): boolean;
|
|
48
|
+
/** What a schema without a `type` is taken for, going by its keywords. */
|
|
49
|
+
export declare function impliedType(schema: Json): "object" | "array" | undefined;
|
|
40
50
|
/**
|
|
41
51
|
* A kind of value, as far as generated code tells values apart. `other` is a
|
|
42
52
|
* `type` this generator does not know, which is none of the rest.
|
|
@@ -56,6 +66,18 @@ export declare function schemaKinds(doc: Doc, schema: Json, depth?: number): Set
|
|
|
56
66
|
export declare function renderSchema(schema: Json, ctx: SchemaCtx, indent?: string, seen?: Seen): string;
|
|
57
67
|
/** Whether a string schema holds a date, which `--useDateType` makes a `Date`. */
|
|
58
68
|
export declare function isDateString(schema: Json): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Whether the schema is a file the way OpenAPI 3.1 writes one: a media type
|
|
71
|
+
* that is not text, and no `contentEncoding` making a string of it.
|
|
72
|
+
*/
|
|
73
|
+
export declare function hasBinaryContent(schema: Json): boolean;
|
|
74
|
+
/** An `allOf` member that declares nothing and only makes properties required. */
|
|
75
|
+
export declare function isRequiredOnly(member: Json): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* The properties an `allOf` requires while another of its members declares
|
|
78
|
+
* them, each with its declaration.
|
|
79
|
+
*/
|
|
80
|
+
export declare function inheritedRequired(doc: Doc, schema: Json): Record<string, Json>;
|
|
59
81
|
/**
|
|
60
82
|
* The values of the discriminator property that a value of `member` has, or
|
|
61
83
|
* undefined when nothing tells the member apart.
|
package/dist/client/schema.mjs
CHANGED
|
@@ -121,7 +121,7 @@ function intersection(types) {
|
|
|
121
121
|
.map((type) => unique.length > 1 && hasTopLevelUnion(type) ? `(${type})` : type)
|
|
122
122
|
.join(" & ");
|
|
123
123
|
}
|
|
124
|
-
function isNullableSchema(schema) {
|
|
124
|
+
export function isNullableSchema(schema) {
|
|
125
125
|
return (schema.nullable === true ||
|
|
126
126
|
(Array.isArray(schema.type) && schema.type.includes("null")));
|
|
127
127
|
}
|
|
@@ -138,7 +138,7 @@ function globalTypeRef(name, ctx) {
|
|
|
138
138
|
return [...ctx.names.values()].includes(name) ? `globalThis.${name}` : name;
|
|
139
139
|
}
|
|
140
140
|
/** Marks a `$ref` member of an `allOf` whose discriminator literal is replaced. */
|
|
141
|
-
const OMITTED_PROPERTY = "x-orqc-omit";
|
|
141
|
+
export const OMITTED_PROPERTY = "x-orqc-omit";
|
|
142
142
|
/** The discriminator of what an `allOf` builds on, if any. */
|
|
143
143
|
function findInheritedDiscriminator(doc, schema, visited = new Set()) {
|
|
144
144
|
const members = Array.isArray(schema?.allOf) ? schema.allOf : [];
|
|
@@ -158,7 +158,7 @@ function findInheritedDiscriminator(doc, schema, visited = new Set()) {
|
|
|
158
158
|
return undefined;
|
|
159
159
|
}
|
|
160
160
|
/** True when a schema or what it is composed of requires `name`. */
|
|
161
|
-
function requiresProperty(doc, schema, name, visited = new Set()) {
|
|
161
|
+
export function requiresProperty(doc, schema, name, visited = new Set()) {
|
|
162
162
|
const resolved = resolveRef(doc, schema);
|
|
163
163
|
if (!resolved || typeof resolved !== "object" || visited.has(resolved)) {
|
|
164
164
|
return false;
|
|
@@ -171,21 +171,22 @@ function requiresProperty(doc, schema, name, visited = new Set()) {
|
|
|
171
171
|
return members.some((member) => requiresProperty(doc, member, name, visited));
|
|
172
172
|
}
|
|
173
173
|
/**
|
|
174
|
-
*
|
|
175
|
-
* discriminator gets its own value of the discriminator property, from the
|
|
176
|
-
* mapping or else its name, so that a union of subtypes narrows on it.
|
|
174
|
+
* A component schema as it is rendered. A subtype that builds on a schema with
|
|
175
|
+
* a discriminator gets its own value of the discriminator property, from the
|
|
176
|
+
* mapping or else its name, so that a union of subtypes narrows on it. Any
|
|
177
|
+
* other schema is returned as it is.
|
|
177
178
|
*/
|
|
178
|
-
export function
|
|
179
|
+
export function componentSchemaView(schema, key, ctx) {
|
|
179
180
|
const discriminator = schema?.discriminator
|
|
180
181
|
? undefined
|
|
181
182
|
: findInheritedDiscriminator(ctx.doc, schema);
|
|
182
183
|
if (!discriminator)
|
|
183
|
-
return
|
|
184
|
+
return schema;
|
|
184
185
|
const property = discriminator.propertyName;
|
|
185
186
|
const pinned = pinnedValues(schema, property, ctx);
|
|
186
187
|
const allowed = valuesNaming(discriminator, key, pinned, (target) => isMappingTarget(target, key));
|
|
187
188
|
if (allowed.length === 0 || pinned?.length === 1)
|
|
188
|
-
return
|
|
189
|
+
return schema;
|
|
189
190
|
const members = schema.allOf.map((member) =>
|
|
190
191
|
// A parent that is a subtype itself has a value of its own to replace.
|
|
191
192
|
typeof member?.$ref === "string" &&
|
|
@@ -197,7 +198,11 @@ export function renderComponentSchema(schema, key, ctx) {
|
|
|
197
198
|
required: requiresProperty(ctx.doc, schema, property) ? [property] : [],
|
|
198
199
|
properties: { [property]: { enum: allowed } },
|
|
199
200
|
};
|
|
200
|
-
return
|
|
201
|
+
return { ...schema, allOf: [...members, tag] };
|
|
202
|
+
}
|
|
203
|
+
/** Render a component schema, as {@link componentSchemaView} shows it. */
|
|
204
|
+
export function renderComponentSchema(schema, key, ctx) {
|
|
205
|
+
return renderSchema(componentSchemaView(schema, key, ctx), ctx);
|
|
201
206
|
}
|
|
202
207
|
/**
|
|
203
208
|
* Whether a schema that is composed of others (`allOf`, `oneOf`, `anyOf`) also
|
|
@@ -209,7 +214,7 @@ export function hasObjectKeywords(schema) {
|
|
|
209
214
|
schema.additionalProperties);
|
|
210
215
|
}
|
|
211
216
|
/** What a schema without a `type` is taken for, going by its keywords. */
|
|
212
|
-
function impliedType(schema) {
|
|
217
|
+
export function impliedType(schema) {
|
|
213
218
|
if (schema.properties ||
|
|
214
219
|
schema.patternProperties ||
|
|
215
220
|
schema.additionalProperties !== undefined) {
|
|
@@ -407,7 +412,7 @@ const TEXT_MEDIA_TYPE = /^(text|multipart)\/|(json|xml|yaml|javascript|ecmascrip
|
|
|
407
412
|
* Whether the schema is a file the way OpenAPI 3.1 writes one: a media type
|
|
408
413
|
* that is not text, and no `contentEncoding` making a string of it.
|
|
409
414
|
*/
|
|
410
|
-
function hasBinaryContent(schema) {
|
|
415
|
+
export function hasBinaryContent(schema) {
|
|
411
416
|
const mediaType = schema.contentMediaType;
|
|
412
417
|
return (typeof mediaType === "string" &&
|
|
413
418
|
schema.contentEncoding === undefined &&
|
|
@@ -426,7 +431,7 @@ const TYPE_KEYWORDS = [
|
|
|
426
431
|
"properties",
|
|
427
432
|
];
|
|
428
433
|
/** An `allOf` member that declares nothing and only makes properties required. */
|
|
429
|
-
function isRequiredOnly(member) {
|
|
434
|
+
export function isRequiredOnly(member) {
|
|
430
435
|
if (!member ||
|
|
431
436
|
typeof member !== "object" ||
|
|
432
437
|
(member.type !== undefined && member.type !== "object") ||
|
|
@@ -474,31 +479,38 @@ function undeclaredRequired(doc, schema, visited = new Set()) {
|
|
|
474
479
|
];
|
|
475
480
|
}
|
|
476
481
|
/**
|
|
477
|
-
* The properties an `allOf` requires while another of its members declares
|
|
478
|
-
*
|
|
479
|
-
* intersected with `unknown` would stay `undefined`-able. The result is `{}`
|
|
480
|
-
* when there are none.
|
|
482
|
+
* The properties an `allOf` requires while another of its members declares
|
|
483
|
+
* them, each with its declaration.
|
|
481
484
|
*/
|
|
482
|
-
function
|
|
485
|
+
export function inheritedRequired(doc, schema) {
|
|
483
486
|
const own = Array.isArray(schema.required) ? schema.required : [];
|
|
484
487
|
const names = new Set([
|
|
485
488
|
...own.filter((name) => !Object.hasOwn(schema.properties ?? {}, name)),
|
|
486
489
|
...schema.allOf.flatMap((member) => {
|
|
487
|
-
const required = undeclaredRequired(
|
|
490
|
+
const required = undeclaredRequired(doc, member);
|
|
488
491
|
// A named schema drops what it requires and nobody declares, so an
|
|
489
492
|
// `allOf` around it does too.
|
|
490
493
|
return member?.$ref
|
|
491
|
-
? required.filter((name) => findDeclaredProperty(
|
|
494
|
+
? required.filter((name) => findDeclaredProperty(doc, schema, name) !== undefined)
|
|
492
495
|
: required;
|
|
493
496
|
}),
|
|
494
497
|
]);
|
|
495
|
-
|
|
498
|
+
return Object.fromEntries([...names].map((name) => [
|
|
496
499
|
name,
|
|
497
|
-
findDeclaredProperty(
|
|
500
|
+
findDeclaredProperty(doc, schema, name) ?? {},
|
|
498
501
|
]));
|
|
499
|
-
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* {@link inheritedRequired} as an object type. The properties are repeated
|
|
505
|
+
* with their own type, because an optional property intersected with `unknown`
|
|
506
|
+
* would stay `undefined`-able. The result is `{}` when there are none.
|
|
507
|
+
*/
|
|
508
|
+
function renderInheritedRequired(schema, ctx, indent, seen) {
|
|
509
|
+
const properties = inheritedRequired(ctx.doc, schema);
|
|
510
|
+
const names = Object.keys(properties);
|
|
511
|
+
return names.length === 0
|
|
500
512
|
? "{}"
|
|
501
|
-
: renderObject({ properties, required:
|
|
513
|
+
: renderObject({ properties, required: names }, ctx, indent, seen);
|
|
502
514
|
}
|
|
503
515
|
function renderRef(ref, ctx, indent, seen) {
|
|
504
516
|
const key = componentSchemaKey(ref);
|
package/dist/common.d.mts
CHANGED
|
@@ -9,3 +9,4 @@ export declare function safeParseNumber(value: unknown): number;
|
|
|
9
9
|
export declare function formatOptions(options: LimitedUserConfig): LimitedUserConfig;
|
|
10
10
|
export declare function buildRequestsOutputPath(outputPath: string): string;
|
|
11
11
|
export declare function buildQueriesOutputPath(outputPath: string): string;
|
|
12
|
+
export declare function buildMocksOutputPath(outputPath: string): string;
|
package/dist/common.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import { queriesOutputPath, requestsOutputPath } from "./constants.mjs";
|
|
2
|
+
import { mocksOutputPath, queriesOutputPath, requestsOutputPath, } from "./constants.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Safely parse a value into a number. Checks for NaN and Infinity.
|
|
5
5
|
* Returns NaN if the string is not a valid number.
|
|
@@ -49,3 +49,6 @@ export function buildRequestsOutputPath(outputPath) {
|
|
|
49
49
|
export function buildQueriesOutputPath(outputPath) {
|
|
50
50
|
return path.join(outputPath, queriesOutputPath);
|
|
51
51
|
}
|
|
52
|
+
export function buildMocksOutputPath(outputPath) {
|
|
53
|
+
return path.join(outputPath, mocksOutputPath);
|
|
54
|
+
}
|
package/dist/constants.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare const defaultOutputPath = "openapi";
|
|
2
2
|
export declare const queriesOutputPath = "queries";
|
|
3
3
|
export declare const requestsOutputPath = "requests";
|
|
4
|
+
export declare const mocksOutputPath = "mocks";
|
|
4
5
|
export declare const OpenApiRqFiles: {
|
|
5
6
|
readonly queries: "queries";
|
|
6
7
|
readonly queryOptions: "queryOptions";
|
package/dist/constants.mjs
CHANGED
package/dist/format.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export declare const processOutput: ({ output, format, lint, }: {
|
|
1
|
+
export declare const processOutput: ({ output, format, lint, label, }: {
|
|
2
2
|
output: string;
|
|
3
|
+
/** What `output` holds, for the progress message. */
|
|
4
|
+
label?: string;
|
|
3
5
|
format?: "prettier" | "biome";
|
|
4
6
|
lint?: "biome" | "eslint";
|
|
5
7
|
}) => Promise<void>;
|
package/dist/format.mjs
CHANGED
|
@@ -32,15 +32,15 @@ const linters = {
|
|
|
32
32
|
name: "ESLint",
|
|
33
33
|
},
|
|
34
34
|
};
|
|
35
|
-
export const processOutput = async ({ output, format, lint, }) => {
|
|
35
|
+
export const processOutput = async ({ output, format, lint, label = "queries", }) => {
|
|
36
36
|
if (format) {
|
|
37
37
|
const module = formatters[format];
|
|
38
|
-
console.log(`✨ Running ${module.name} on
|
|
38
|
+
console.log(`✨ Running ${module.name} on ${label}`);
|
|
39
39
|
sync(module.command, module.args(output));
|
|
40
40
|
}
|
|
41
41
|
if (lint) {
|
|
42
42
|
const module = linters[lint];
|
|
43
|
-
console.log(`✨ Running ${module.name} on
|
|
43
|
+
console.log(`✨ Running ${module.name} on ${label}`);
|
|
44
44
|
sync(module.command, module.args(output));
|
|
45
45
|
}
|
|
46
46
|
};
|
package/dist/generate.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { agentsFilePath, claudeImportHint, writeAgentsBlock, } from "./agentsMd.mjs";
|
|
2
|
-
import { detectImportExtension, findNearestTsConfigPath, generateClient, SpecError, writeGeneratedFiles, } from "./client/index.mjs";
|
|
3
|
-
import { buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from "./common.mjs";
|
|
2
|
+
import { detectImportExtension, detectMswMajor, findNearestTsConfigPath, generateClient, SpecError, writeGeneratedFiles, } from "./client/index.mjs";
|
|
3
|
+
import { buildMocksOutputPath, buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from "./common.mjs";
|
|
4
4
|
import { createSource } from "./createSource.mjs";
|
|
5
5
|
import { CodegenError, ErrorCodes, toCodegenError } from "./errors.mjs";
|
|
6
6
|
import { processOutput } from "./format.mjs";
|
|
@@ -49,6 +49,8 @@ async function runGenerate(options, version, runtime) {
|
|
|
49
49
|
const client = resolveClientKind(formattedOptions.client);
|
|
50
50
|
// One answer for both directories: the queries import the requests.
|
|
51
51
|
const importExtension = detectImportExtension(process.cwd());
|
|
52
|
+
// The handlers import MSW the way the installed version exports it.
|
|
53
|
+
const msw = formattedOptions.mock ? detectMswMajor(process.cwd()) : undefined;
|
|
52
54
|
let generated;
|
|
53
55
|
try {
|
|
54
56
|
generated = await generateClient({
|
|
@@ -56,6 +58,7 @@ async function runGenerate(options, version, runtime) {
|
|
|
56
58
|
client,
|
|
57
59
|
enums: formattedOptions.enums,
|
|
58
60
|
importExtension,
|
|
61
|
+
mocks: msw ? { mswMajor: msw.major } : undefined,
|
|
59
62
|
noOperationId: formattedOptions.noOperationId,
|
|
60
63
|
pageParam: formattedOptions.pageParam,
|
|
61
64
|
schemas: formattedOptions.noSchemas
|
|
@@ -68,10 +71,16 @@ async function runGenerate(options, version, runtime) {
|
|
|
68
71
|
throw mapClientError(error, formattedOptions.input);
|
|
69
72
|
}
|
|
70
73
|
if (!runtime.silent) {
|
|
74
|
+
if (msw?.warning)
|
|
75
|
+
console.warn(msw.warning);
|
|
71
76
|
for (const warning of generated.warnings)
|
|
72
77
|
console.warn(warning);
|
|
73
78
|
}
|
|
74
79
|
await writeGeneratedFiles(openApiOutputPath, generated.files);
|
|
80
|
+
const mocksOutputPath = buildMocksOutputPath(options.output);
|
|
81
|
+
if (generated.mockFiles) {
|
|
82
|
+
await writeGeneratedFiles(mocksOutputPath, generated.mockFiles);
|
|
83
|
+
}
|
|
75
84
|
const source = createSource({
|
|
76
85
|
generated,
|
|
77
86
|
client,
|
|
@@ -89,6 +98,14 @@ async function runGenerate(options, version, runtime) {
|
|
|
89
98
|
format: formattedOptions.format,
|
|
90
99
|
lint: formattedOptions.lint,
|
|
91
100
|
});
|
|
101
|
+
if (generated.mockFiles) {
|
|
102
|
+
await processOutput({
|
|
103
|
+
output: mocksOutputPath,
|
|
104
|
+
format: formattedOptions.format,
|
|
105
|
+
lint: formattedOptions.lint,
|
|
106
|
+
label: "mocks",
|
|
107
|
+
});
|
|
108
|
+
}
|
|
92
109
|
if (options.agentsMd) {
|
|
93
110
|
const file = agentsFilePath(options.agentsMd);
|
|
94
111
|
await writeAgentsBlock(file, options.output);
|
|
@@ -13,7 +13,12 @@ export declare function buildDefaultResponseType(op: HookOperation): TypeStateme
|
|
|
13
13
|
export declare function buildQueryResultType(op: HookOperation): TypeStatement;
|
|
14
14
|
/**
|
|
15
15
|
* Build the mutation result type alias.
|
|
16
|
-
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet
|
|
16
|
+
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet<true>>>;
|
|
17
|
+
*
|
|
18
|
+
* `<true>` is ThrowOnError: the hook always calls the SDK with
|
|
19
|
+
* `throwOnError: true`, so a resolved mutation is the success response and
|
|
20
|
+
* never the error one. Without it the alias is the union of both, which hides
|
|
21
|
+
* what only the success side has (the axios response headers, #168).
|
|
17
22
|
*/
|
|
18
23
|
export declare function buildMutationResultType(op: HookOperation): TypeStatement;
|
|
19
24
|
/**
|
|
@@ -30,13 +30,18 @@ export function buildQueryResultType(op) {
|
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Build the mutation result type alias.
|
|
33
|
-
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet
|
|
33
|
+
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet<true>>>;
|
|
34
|
+
*
|
|
35
|
+
* `<true>` is ThrowOnError: the hook always calls the SDK with
|
|
36
|
+
* `throwOnError: true`, so a resolved mutation is the success response and
|
|
37
|
+
* never the error one. Without it the alias is the union of both, which hides
|
|
38
|
+
* what only the success side has (the axios response headers, #168).
|
|
34
39
|
*/
|
|
35
40
|
export function buildMutationResultType(op) {
|
|
36
41
|
return {
|
|
37
42
|
kind: "type",
|
|
38
43
|
name: hookNames(op).mutationResult,
|
|
39
|
-
type: `Awaited<ReturnType<typeof ${op.methodName}
|
|
44
|
+
type: `Awaited<ReturnType<typeof ${op.methodName}<true>>>`,
|
|
40
45
|
service: op.methodName,
|
|
41
46
|
};
|
|
42
47
|
}
|
package/dist/runCli.mjs
CHANGED
|
@@ -45,6 +45,7 @@ export function buildProgram(version, io, { json }) {
|
|
|
45
45
|
.option("--nextPageParam <value>", "Name of the response parameter used for next page", "nextPage")
|
|
46
46
|
.option("--initialPageParam <value>", "Initial page value to query", "1")
|
|
47
47
|
.option("--omitInitialPageParam", "Send no initial page parameter at all (overrides --initialPageParam)")
|
|
48
|
+
.addOption(new Option("--mock <value>", "Also generate mock data and request handlers from the specification, into <output>/mocks").choices(["msw"]))
|
|
48
49
|
.option("--agentsMd [file]", "Also keep a short guide for coding agents (how to look operations up in the output) in AGENTS.md, or in the file named")
|
|
49
50
|
.option("--check", "Do not update the output; exit 1 and list the files that differ if the existing output is out of date")
|
|
50
51
|
.option("--json", "Print errors to stderr as a single line of JSON ({ code, message, details })");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7nohe/openapi-react-query-codegen",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "4.0.0-beta.8",
|
|
4
|
+
"description": "Generate TanStack Query (React Query) hooks, queryOptions factories and a typed fetch/axios client from your OpenAPI spec",
|
|
5
5
|
"bin": {
|
|
6
6
|
"orqc": "dist/cli.mjs",
|
|
7
7
|
"openapi-rq": "dist/cli.mjs",
|
|
@@ -23,20 +23,26 @@
|
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "git+https://github.com/7nohe/openapi-react-query-codegen.git"
|
|
25
25
|
},
|
|
26
|
-
"homepage": "https://
|
|
26
|
+
"homepage": "https://orqc.7nohe.dev",
|
|
27
27
|
"bugs": "https://github.com/7nohe/openapi-react-query-codegen/issues",
|
|
28
28
|
"files": [
|
|
29
29
|
"dist",
|
|
30
30
|
"AGENTS.md"
|
|
31
31
|
],
|
|
32
32
|
"keywords": [
|
|
33
|
+
"openapi",
|
|
34
|
+
"openapi3",
|
|
35
|
+
"swagger",
|
|
33
36
|
"codegen",
|
|
37
|
+
"code-generator",
|
|
38
|
+
"tanstack-query",
|
|
34
39
|
"react-query",
|
|
35
40
|
"react",
|
|
36
|
-
"
|
|
37
|
-
"swagger",
|
|
41
|
+
"hooks",
|
|
38
42
|
"typescript",
|
|
39
|
-
"
|
|
43
|
+
"fetch",
|
|
44
|
+
"axios",
|
|
45
|
+
"orqc"
|
|
40
46
|
],
|
|
41
47
|
"license": "MIT",
|
|
42
48
|
"author": "Daiki Urata (@7nohe)",
|
|
@@ -45,14 +51,15 @@
|
|
|
45
51
|
"yaml": "2.9.1"
|
|
46
52
|
},
|
|
47
53
|
"devDependencies": {
|
|
48
|
-
"@biomejs/biome": "^2.
|
|
54
|
+
"@biomejs/biome": "^2.6.6",
|
|
49
55
|
"@types/cross-spawn": "^6.0.6",
|
|
50
|
-
"@types/node": "^22.20.
|
|
56
|
+
"@types/node": "^22.20.5",
|
|
51
57
|
"@types/semver": "^7.7.1",
|
|
52
58
|
"@vitest/coverage-v8": "^5.1.0",
|
|
53
59
|
"axios": "1.20.0",
|
|
54
|
-
"commander": "^15.
|
|
60
|
+
"commander": "^15.1.0",
|
|
55
61
|
"lefthook": "^2.1.10",
|
|
62
|
+
"msw": "^3.0.4",
|
|
56
63
|
"typescript": "^6.0.3",
|
|
57
64
|
"vite": "^8.3.6",
|
|
58
65
|
"vitest": "^5.1.0"
|