@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.
@@ -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
- const isMember = (value) => typeof value === "string" || typeof value === "number";
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"];
@@ -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[];
@@ -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;