@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ClientKind } from "./emitSdk.mjs";
|
|
2
2
|
import { type OperationIR } from "./operations.mjs";
|
|
3
3
|
import { type SchemaCtx } from "./schema.mjs";
|
|
4
|
-
import type { Doc } from "./spec.mjs";
|
|
4
|
+
import type { Doc, Json } from "./spec.mjs";
|
|
5
5
|
export type EnumsMode = "javascript" | "typescript" | false | undefined;
|
|
6
6
|
export type EmittedTypes = {
|
|
7
7
|
content: string;
|
|
@@ -10,6 +10,11 @@ export type EmittedTypes = {
|
|
|
10
10
|
};
|
|
11
11
|
/** First server URL with its variables replaced by their defaults. */
|
|
12
12
|
export declare function getBaseUrl(doc: Doc): string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a component schema is declared as an enum of its own (`--enums`): a
|
|
15
|
+
* TypeScript enum, or an object of its values with the type of them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isEnumDeclaration(schema: Json, enumsMode: EnumsMode): boolean;
|
|
13
18
|
export declare function emitTypes(operations: OperationIR[], ctx: SchemaCtx, options: {
|
|
14
19
|
enums?: EnumsMode;
|
|
15
20
|
clientKind: ClientKind;
|
|
@@ -103,19 +103,28 @@ function enumKey(name) {
|
|
|
103
103
|
// `01` would be an octal literal, which TypeScript rejects.
|
|
104
104
|
return /^(0|[1-9]\d*)$/.test(name) ? name : propertyKey(name);
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether a component schema is declared as an enum of its own (`--enums`): a
|
|
108
|
+
* TypeScript enum, or an object of its values with the type of them.
|
|
109
|
+
*/
|
|
110
|
+
export function isEnumDeclaration(schema, enumsMode) {
|
|
111
|
+
if (!enumsMode)
|
|
112
|
+
return false;
|
|
113
|
+
const values = Array.isArray(schema?.enum) ? schema.enum : [];
|
|
114
|
+
const isMember = (value) => typeof value === "string" || typeof value === "number";
|
|
115
|
+
// A TypeScript enum can only hold strings and numbers, an object anything.
|
|
116
|
+
return (values.some((value) => value !== null) &&
|
|
117
|
+
values.every((value) => isMember(value) ||
|
|
118
|
+
(enumsMode === "javascript" &&
|
|
119
|
+
(value === null || typeof value === "boolean"))));
|
|
120
|
+
}
|
|
106
121
|
function emitComponentSchema(schema, key, name, ctx, enumsMode) {
|
|
107
122
|
const comment = docComment({
|
|
108
123
|
description: schemaDescription(schema),
|
|
109
124
|
deprecated: schema?.deprecated,
|
|
110
125
|
});
|
|
111
126
|
const values = Array.isArray(schema?.enum) ? schema.enum : [];
|
|
112
|
-
|
|
113
|
-
// A TypeScript enum can only hold strings and numbers, an object anything.
|
|
114
|
-
const isLiteralEnum = values.some((value) => value !== null) &&
|
|
115
|
-
values.every((value) => isMember(value) ||
|
|
116
|
-
(enumsMode === "javascript" &&
|
|
117
|
-
(value === null || typeof value === "boolean")));
|
|
118
|
-
if (enumsMode && isLiteralEnum) {
|
|
127
|
+
if (isEnumDeclaration(schema, enumsMode)) {
|
|
119
128
|
const used = new Set();
|
|
120
129
|
const memberNames = schema["x-enum-varnames"] ?? schema["x-enumNames"];
|
|
121
130
|
const descriptions = schema["x-enum-descriptions"];
|
package/dist/client/index.d.mts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { GeneratedFile, OperationInfo } from "../types.mjs";
|
|
2
|
+
import { type MswMajor } from "./emitMocks.mjs";
|
|
2
3
|
import { type SchemasMode } from "./emitSchemas.mjs";
|
|
3
4
|
import { type ClientKind } from "./emitSdk.mjs";
|
|
4
5
|
import { type EnumsMode } from "./emitTypes.mjs";
|
|
6
|
+
export type { MswMajor } from "./emitMocks.mjs";
|
|
5
7
|
export type { SchemasMode } from "./emitSchemas.mjs";
|
|
6
8
|
export type { ClientKind } from "./emitSdk.mjs";
|
|
7
9
|
export type { EnumsMode } from "./emitTypes.mjs";
|
|
10
|
+
export { detectMswMajor } from "./msw.mjs";
|
|
8
11
|
export { SpecError } from "./spec.mjs";
|
|
9
12
|
export { detectImportExtension, findNearestTsConfigPath } from "./tsconfig.mjs";
|
|
10
13
|
export interface ClientGenOptions {
|
|
@@ -14,6 +17,13 @@ export interface ClientGenOptions {
|
|
|
14
17
|
enums?: EnumsMode;
|
|
15
18
|
/** `.js` for Node16/NodeNext module resolution, otherwise empty. */
|
|
16
19
|
importExtension: "" | ".js";
|
|
20
|
+
/**
|
|
21
|
+
* Also generate mock data and MSW request handlers, written for this major
|
|
22
|
+
* version of MSW. Off when not set.
|
|
23
|
+
*/
|
|
24
|
+
mocks?: {
|
|
25
|
+
mswMajor: MswMajor;
|
|
26
|
+
};
|
|
17
27
|
/** Name operations after the HTTP method and path instead of operationId. */
|
|
18
28
|
noOperationId?: boolean;
|
|
19
29
|
/** Query parameter used for pagination. */
|
|
@@ -31,6 +41,8 @@ export interface ClientGenOptions {
|
|
|
31
41
|
}
|
|
32
42
|
export interface GeneratedClient {
|
|
33
43
|
files: GeneratedFile[];
|
|
44
|
+
/** The files of the mocks directory, when `mocks` is set. */
|
|
45
|
+
mockFiles?: GeneratedFile[];
|
|
34
46
|
operations: OperationInfo[];
|
|
35
47
|
/** Type names exported from `types.gen.ts`. */
|
|
36
48
|
modelNames: string[];
|
package/dist/client/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { emitMocks } from "./emitMocks.mjs";
|
|
3
4
|
import { emitSchemas } from "./emitSchemas.mjs";
|
|
4
5
|
import { emitClientFile, emitIndexFile, emitSdk, emitServicesShim, optionsAreOptional, } from "./emitSdk.mjs";
|
|
5
6
|
import { emitTransformers } from "./emitTransformers.mjs";
|
|
@@ -8,6 +9,7 @@ import { capitalizeFirst } from "./naming.mjs";
|
|
|
8
9
|
import { buildSchemaNames, buildWritableNames, collectOperations, schemaPrimitiveKind, } from "./operations.mjs";
|
|
9
10
|
import { renderSchema } from "./schema.mjs";
|
|
10
11
|
import { bundleExternalRefs, loadSpec } from "./spec.mjs";
|
|
12
|
+
export { detectMswMajor } from "./msw.mjs";
|
|
11
13
|
export { SpecError } from "./spec.mjs";
|
|
12
14
|
export { detectImportExtension, findNearestTsConfigPath } from "./tsconfig.mjs";
|
|
13
15
|
const RUNTIME_FILES = {
|
|
@@ -119,9 +121,19 @@ export async function generateClient(options) {
|
|
|
119
121
|
if (schemas !== undefined) {
|
|
120
122
|
files.push({ name: "schemas.gen.ts", content: schemas });
|
|
121
123
|
}
|
|
124
|
+
const mocks = options.mocks
|
|
125
|
+
? emitMocks(operations, ctx, {
|
|
126
|
+
mswMajor: options.mocks.mswMajor,
|
|
127
|
+
enums: options.enums,
|
|
128
|
+
ext,
|
|
129
|
+
})
|
|
130
|
+
: undefined;
|
|
131
|
+
if (mocks)
|
|
132
|
+
warnings.push(...mocks.warnings);
|
|
122
133
|
const emittedTypes = new Set(types.exportedNames);
|
|
123
134
|
return {
|
|
124
135
|
files,
|
|
136
|
+
mockFiles: mocks?.files,
|
|
125
137
|
operations: operations.map((operation) => toOperationInfo(operation, ctx, options.pageParam, emittedTypes)),
|
|
126
138
|
modelNames: types.exportedNames,
|
|
127
139
|
serviceNames: operations.map((operation) => operation.functionName),
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type SchemaCtx } from "./schema.mjs";
|
|
2
|
+
import { type Json } from "./spec.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* What the values of a mock are built with. A value is a TypeScript expression
|
|
5
|
+
* of the type `renderSchema` gives the same schema, so the two read a schema
|
|
6
|
+
* the same way.
|
|
7
|
+
*/
|
|
8
|
+
export interface MockValueCtx {
|
|
9
|
+
/** The context the response types are rendered with (`read` mode). */
|
|
10
|
+
schema: SchemaCtx;
|
|
11
|
+
/**
|
|
12
|
+
* The call of a component schema's mock function. Undefined while that mock
|
|
13
|
+
* is being built: a value that held itself would never end.
|
|
14
|
+
*/
|
|
15
|
+
callMock: (key: string) => string | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* The type to assert a literal of a component schema to, when the schema is
|
|
18
|
+
* a TypeScript enum (which no literal is assignable to).
|
|
19
|
+
*/
|
|
20
|
+
enumCast: (key: string) => string | undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* An expression for a value of the schema, written for a place at `indent`.
|
|
24
|
+
* Undefined when no value ends: the schema allows none, or holds itself
|
|
25
|
+
* through a mock that is still being built.
|
|
26
|
+
*/
|
|
27
|
+
export declare function mockValue(schema: Json, ctx: MockValueCtx, indent?: string, seen?: ReadonlySet<string>): string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* An example from the specification as an expression of the schema's type, or
|
|
30
|
+
* undefined when it is not a value of that type. Examples drift from their
|
|
31
|
+
* schemas, and one that did would not compile. A composed schema is not
|
|
32
|
+
* checked: its example is passed over, and the value is made up instead.
|
|
33
|
+
*/
|
|
34
|
+
export declare function exampleValue(schema: Json, value: Json, ctx: MockValueCtx, indent?: string, depth?: number): string | undefined;
|