typed-openapi 1.5.0 → 2.0.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.
@@ -0,0 +1,244 @@
1
+ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
2
+ import { OpenAPIObject, ReferenceObject, OperationObject, SchemaObject } from 'openapi3-ts/oas31';
3
+ import { SchemaObject as SchemaObject$1 } from 'openapi3-ts/oas30';
4
+
5
+ declare class Box<T extends AnyBoxDef = AnyBoxDef> {
6
+ definition: T;
7
+ type: T["type"];
8
+ value: T["value"];
9
+ params: T["params"];
10
+ schema: T["schema"];
11
+ ctx: T["ctx"];
12
+ constructor(definition: T);
13
+ toJSON(): {
14
+ type: T["type"];
15
+ value: T["value"];
16
+ };
17
+ toString(): string;
18
+ recompute(callback: OpenapiSchemaConvertContext["onBox"]): Box<AnyBoxDef>;
19
+ static fromJSON(json: string): Box<any>;
20
+ static isBox(box: unknown): box is Box<AnyBoxDef>;
21
+ static isUnion(box: Box<AnyBoxDef>): box is Box<BoxUnion>;
22
+ static isIntersection(box: Box<AnyBoxDef>): box is Box<BoxIntersection>;
23
+ static isArray(box: Box<AnyBoxDef>): box is Box<BoxArray>;
24
+ static isOptional(box: Box<AnyBoxDef>): box is Box<BoxOptional>;
25
+ static isReference(box: Box<AnyBoxDef>): box is Box<BoxRef>;
26
+ static isKeyword(box: Box<AnyBoxDef>): box is Box<BoxKeyword>;
27
+ static isObject(box: Box<AnyBoxDef>): box is Box<BoxObject>;
28
+ static isLiteral(box: Box<AnyBoxDef>): box is Box<BoxLiteral>;
29
+ }
30
+
31
+ type RefInfo = {
32
+ /**
33
+ * The (potentially autocorrected) ref
34
+ * @example "#/components/schemas/MySchema"
35
+ */
36
+ ref: string;
37
+ /**
38
+ * The name of the ref
39
+ * @example "MySchema"
40
+ * */
41
+ name: string;
42
+ normalized: string;
43
+ kind: "schemas" | "responses" | "parameters" | "requestBodies" | "headers";
44
+ };
45
+ declare const createRefResolver: (doc: OpenAPIObject, factory: GenericFactory, nameTransform?: NameTransformOptions) => {
46
+ get: <T = LibSchemaObject>(ref: string) => NonNullable<T>;
47
+ unwrap: <T extends ReferenceObject | {}>(component: T) => Exclude<T, ReferenceObject>;
48
+ getInfosByRef: (ref: string) => RefInfo;
49
+ infos: Map<string, RefInfo>;
50
+ /**
51
+ * Get the schemas in the order they should be generated, depending on their dependencies
52
+ * so that a schema is generated before the ones that depend on it
53
+ */
54
+ getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
55
+ directDependencies: Map<string, Set<string>>;
56
+ transitiveDependencies: Map<string, Set<string>>;
57
+ };
58
+ interface RefResolver extends ReturnType<typeof createRefResolver> {
59
+ }
60
+
61
+ declare const mapOpenApiEndpoints: (doc: OpenAPIObject, options?: {
62
+ nameTransform?: NameTransformOptions;
63
+ }) => {
64
+ doc: OpenAPIObject;
65
+ refs: {
66
+ get: <T = LibSchemaObject>(ref: string) => NonNullable<T>;
67
+ unwrap: <T extends openapi3_ts_oas31.ReferenceObject | {}>(component: T) => Exclude<T, openapi3_ts_oas31.ReferenceObject>;
68
+ getInfosByRef: (ref: string) => RefInfo;
69
+ infos: Map<string, RefInfo>;
70
+ getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
71
+ directDependencies: Map<string, Set<string>>;
72
+ transitiveDependencies: Map<string, Set<string>>;
73
+ };
74
+ endpointList: Endpoint<DefaultEndpoint>[];
75
+ factory: {
76
+ union: (types: StringOrBox[]) => string;
77
+ intersection: (types: StringOrBox[]) => string;
78
+ array: (type: StringOrBox) => string;
79
+ optional: (type: StringOrBox) => string;
80
+ reference: (name: string, typeArgs: StringOrBox[] | undefined) => string;
81
+ literal: (value: StringOrBox) => string;
82
+ string: () => "string";
83
+ number: () => "number";
84
+ boolean: () => "boolean";
85
+ unknown: () => "unknown";
86
+ any: () => "any";
87
+ never: () => "never";
88
+ object: (props: Record<string, StringOrBox>) => string;
89
+ };
90
+ };
91
+ type MutationMethod = "post" | "put" | "patch" | "delete";
92
+ type Method = "get" | "head" | "options" | MutationMethod;
93
+ type EndpointParameters = {
94
+ body?: Box<BoxRef>;
95
+ query?: Box<BoxRef> | Record<string, AnyBox>;
96
+ header?: Box<BoxRef> | Record<string, AnyBox>;
97
+ path?: Box<BoxRef> | Record<string, AnyBox>;
98
+ };
99
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
100
+ type DefaultEndpoint = {
101
+ parameters?: EndpointParameters | undefined;
102
+ response: AnyBox;
103
+ responses?: Record<string, AnyBox>;
104
+ responseHeaders?: Record<string, AnyBox>;
105
+ };
106
+ type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
107
+ operation: OperationObject;
108
+ method: Method;
109
+ path: string;
110
+ parameters?: TConfig["parameters"];
111
+ requestFormat: RequestFormat;
112
+ meta: {
113
+ alias: string;
114
+ hasParameters: boolean;
115
+ areParametersRequired: boolean;
116
+ };
117
+ response: TConfig["response"];
118
+ responses?: TConfig["responses"];
119
+ responseHeaders?: TConfig["responseHeaders"];
120
+ };
121
+
122
+ type LibSchemaObject = SchemaObject & SchemaObject$1;
123
+ type BoxDefinition = {
124
+ type: string;
125
+ params: unknown;
126
+ value: string;
127
+ };
128
+ type BoxParams = string | BoxDefinition;
129
+ type WithSchema = {
130
+ schema: LibSchemaObject | ReferenceObject | undefined;
131
+ ctx: OpenapiSchemaConvertContext;
132
+ };
133
+ type BoxUnion = WithSchema & {
134
+ type: "union";
135
+ params: {
136
+ types: Array<BoxParams>;
137
+ };
138
+ value: string;
139
+ };
140
+ type BoxIntersection = WithSchema & {
141
+ type: "intersection";
142
+ params: {
143
+ types: Array<BoxParams>;
144
+ };
145
+ value: string;
146
+ };
147
+ type BoxArray = WithSchema & {
148
+ type: "array";
149
+ params: {
150
+ type: BoxParams;
151
+ };
152
+ value: string;
153
+ };
154
+ type BoxOptional = WithSchema & {
155
+ type: "optional";
156
+ params: {
157
+ type: BoxParams;
158
+ };
159
+ value: string;
160
+ };
161
+ type BoxRef = WithSchema & {
162
+ type: "ref";
163
+ params: {
164
+ name: string;
165
+ generics?: BoxParams[] | undefined;
166
+ };
167
+ value: string;
168
+ };
169
+ type BoxLiteral = WithSchema & {
170
+ type: "literal";
171
+ params: {};
172
+ value: string;
173
+ };
174
+ type BoxKeyword = WithSchema & {
175
+ type: "keyword";
176
+ params: {
177
+ name: string;
178
+ };
179
+ value: string;
180
+ };
181
+ type BoxObject = WithSchema & {
182
+ type: "object";
183
+ params: {
184
+ props: Record<string, BoxParams>;
185
+ };
186
+ value: string;
187
+ };
188
+ type AnyBoxDef = BoxUnion | BoxIntersection | BoxArray | BoxOptional | BoxRef | BoxLiteral | BoxKeyword | BoxObject;
189
+ type AnyBox = Box<AnyBoxDef>;
190
+ type OpenapiSchemaConvertArgs = {
191
+ schema: SchemaObject | ReferenceObject;
192
+ ctx: OpenapiSchemaConvertContext;
193
+ meta?: {} | undefined;
194
+ };
195
+ type FactoryCreator = (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => GenericFactory;
196
+ type NameTransformOptions = {
197
+ transformSchemaName?: (name: string) => string;
198
+ transformEndpointName?: (endpoint: {
199
+ alias: string;
200
+ operation: OperationObject;
201
+ method: Method;
202
+ path: string;
203
+ }) => string;
204
+ };
205
+ type OpenapiSchemaConvertContext = {
206
+ factory: FactoryCreator | GenericFactory;
207
+ refs: RefResolver;
208
+ onBox?: (box: Box<AnyBoxDef>) => Box<AnyBoxDef>;
209
+ nameTransform?: NameTransformOptions;
210
+ };
211
+ type StringOrBox = string | Box<AnyBoxDef>;
212
+ type BoxFactory = {
213
+ union: (types: Array<StringOrBox>) => Box<BoxUnion>;
214
+ intersection: (types: Array<StringOrBox>) => Box<BoxIntersection>;
215
+ array: (type: StringOrBox) => Box<BoxArray>;
216
+ object: (props: Record<string, StringOrBox>) => Box<BoxObject>;
217
+ optional: (type: StringOrBox) => Box<BoxOptional>;
218
+ reference: (name: string, generics?: Array<StringOrBox> | undefined) => Box<BoxRef>;
219
+ literal: (value: StringOrBox) => Box<BoxLiteral>;
220
+ string: () => Box<BoxKeyword>;
221
+ number: () => Box<BoxKeyword>;
222
+ boolean: () => Box<BoxKeyword>;
223
+ unknown: () => Box<BoxKeyword>;
224
+ any: () => Box<BoxKeyword>;
225
+ never: () => Box<BoxKeyword>;
226
+ };
227
+ type GenericFactory = {
228
+ callback?: OpenapiSchemaConvertContext["onBox"];
229
+ union: (types: Array<StringOrBox>) => string;
230
+ intersection: (types: Array<StringOrBox>) => string;
231
+ array: (type: StringOrBox) => string;
232
+ object: (props: Record<string, StringOrBox>) => string;
233
+ optional: (type: StringOrBox) => string;
234
+ reference: (name: string, generics?: Array<StringOrBox> | undefined) => string;
235
+ literal: (value: StringOrBox) => string;
236
+ string: () => string;
237
+ number: () => string;
238
+ boolean: () => string;
239
+ unknown: () => string;
240
+ any: () => string;
241
+ never: () => string;
242
+ };
243
+
244
+ export { type AnyBoxDef as A, type BoxFactory as B, type EndpointParameters as E, type FactoryCreator as F, type GenericFactory as G, type LibSchemaObject as L, type Method as M, type NameTransformOptions as N, type OpenapiSchemaConvertContext as O, type RefInfo as R, type StringOrBox as S, type WithSchema as W, type OpenapiSchemaConvertArgs as a, Box as b, type Endpoint as c, createRefResolver as d, type RefResolver as e, type BoxDefinition as f, type BoxParams as g, type BoxUnion as h, type BoxIntersection as i, type BoxArray as j, type BoxOptional as k, type BoxRef as l, mapOpenApiEndpoints as m, type BoxLiteral as n, type BoxKeyword as o, type BoxObject as p, type AnyBox as q };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "1.5.0",
4
+ "version": "2.0.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "exports": {
8
8
  ".": "./dist/index.js",
9
- "./node": "./dist/node.export.js"
9
+ "./node": "./dist/node.export.js",
10
+ "./pretty": "./dist/pretty.export.js"
10
11
  },
11
12
  "bin": {
12
13
  "typed-openapi": "bin.js"
@@ -29,8 +30,10 @@
29
30
  },
30
31
  "devDependencies": {
31
32
  "@changesets/cli": "^2.29.4",
33
+ "@tanstack/react-query": "5.85.0",
32
34
  "@types/node": "^22.15.17",
33
35
  "@types/prettier": "3.0.0",
36
+ "msw": "2.10.5",
34
37
  "tsup": "^8.4.0",
35
38
  "typescript": "^5.8.3",
36
39
  "vitest": "^3.1.3"
@@ -63,6 +66,9 @@
63
66
  "dev": "tsup --watch",
64
67
  "build": "tsup",
65
68
  "test": "vitest",
69
+ "generate:runtime": "node bin.js ./tests/samples/petstore.yaml --output ./tmp/generated-client.ts --tanstack generated-tanstack.ts",
70
+ "test:runtime:run": "vitest run tests/integration-runtime-msw.test.ts",
71
+ "test:runtime": "pnpm run generate:runtime && pnpm run test:runtime:run",
66
72
  "fmt": "prettier --write src",
67
73
  "typecheck": "tsc -b ./tsconfig.build.json"
68
74
  }
package/src/cli.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { cac } from "cac";
2
-
3
2
  import { readFileSync } from "fs";
4
3
  import { generateClientFiles } from "./generate-client-files.ts";
5
4
  import { allowedRuntimes } from "./generator.ts";
@@ -11,16 +10,22 @@ cli
11
10
  .command("<input>", "Generate")
12
11
  .option("-o, --output <path>", "Output path for the api client ts file (defaults to `<input>.<runtime>.ts`)")
13
12
  .option(
14
- "-r, --runtime <name>",
13
+ "-r, --runtime <n>",
15
14
  `Runtime to use for validation; defaults to \`none\`; available: ${allowedRuntimes.toString()}`,
16
15
  { default: "none" },
17
16
  )
18
17
  .option("--schemas-only", "Only generate schemas, skipping client generation (defaults to false)", { default: false })
18
+ .option("--include-client", "Include API client types and implementation (defaults to true)", { default: true })
19
+ .option(
20
+ "--success-status-codes <codes>",
21
+ "Comma-separated list of success status codes (defaults to 2xx and 3xx ranges)",
22
+ )
23
+ .option("--error-status-codes <codes>", "Comma-separated list of error status codes (defaults to 4xx and 5xx ranges)")
19
24
  .option(
20
25
  "--tanstack [name]",
21
26
  "Generate tanstack client, defaults to false, can optionally specify a name for the generated file",
22
27
  )
23
- .action(async (input, _options) => {
28
+ .action(async (input: string, _options: any) => {
24
29
  return generateClientFiles(input, _options);
25
30
  });
26
31
 
@@ -3,10 +3,17 @@ import type { OpenAPIObject } from "openapi3-ts/oas31";
3
3
  import { basename, join, dirname } from "pathe";
4
4
  import { type } from "arktype";
5
5
  import { mkdir, writeFile } from "fs/promises";
6
- import { allowedRuntimes, generateFile } from "./generator.ts";
6
+ import {
7
+ allowedRuntimes,
8
+ generateFile,
9
+ DEFAULT_SUCCESS_STATUS_CODES,
10
+ DEFAULT_ERROR_STATUS_CODES,
11
+ type GeneratorOptions,
12
+ } from "./generator.ts";
7
13
  import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
8
14
  import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
9
15
  import { prettify } from "./format.ts";
16
+ import type { NameTransformOptions } from "./types.ts";
10
17
 
11
18
  const cwd = process.cwd();
12
19
  const now = new Date();
@@ -24,21 +31,46 @@ export const optionsSchema = type({
24
31
  runtime: allowedRuntimes,
25
32
  tanstack: "boolean | string",
26
33
  schemasOnly: "boolean",
34
+ "includeClient?": "boolean | 'true' | 'false'",
35
+ "successStatusCodes?": "string",
36
+ "errorStatusCodes?": "string",
27
37
  });
28
38
 
29
- export async function generateClientFiles(input: string, options: typeof optionsSchema.infer) {
39
+ type GenerateClientFilesOptions = typeof optionsSchema.infer & {
40
+ nameTransform?: NameTransformOptions;
41
+ };
42
+
43
+ export async function generateClientFiles(input: string, options: GenerateClientFilesOptions) {
30
44
  const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject;
31
45
 
32
- const ctx = mapOpenApiEndpoints(openApiDoc);
46
+ const ctx = mapOpenApiEndpoints(openApiDoc, options);
33
47
  console.log(`Found ${ctx.endpointList.length} endpoints`);
34
48
 
35
- const content = await prettify(
36
- generateFile({
37
- ...ctx,
38
- runtime: options.runtime,
39
- schemasOnly: options.schemasOnly,
40
- }),
41
- );
49
+ // Parse success status codes if provided
50
+ const successStatusCodes = options.successStatusCodes
51
+ ? (options.successStatusCodes.split(",").map((code) => parseInt(code.trim(), 10)) as readonly number[])
52
+ : undefined;
53
+
54
+ // Parse error status codes if provided
55
+ const errorStatusCodes = options.errorStatusCodes
56
+ ? (options.errorStatusCodes.split(",").map((code) => parseInt(code.trim(), 10)) as readonly number[])
57
+ : undefined;
58
+
59
+ // Convert string boolean to actual boolean
60
+ const includeClient =
61
+ options.includeClient === "false" ? false : options.includeClient === "true" ? true : options.includeClient;
62
+
63
+ const generatorOptions: GeneratorOptions = {
64
+ ...ctx,
65
+ runtime: options.runtime,
66
+ schemasOnly: options.schemasOnly,
67
+ nameTransform: options.nameTransform,
68
+ includeClient: includeClient ?? true,
69
+ successStatusCodes: successStatusCodes ?? DEFAULT_SUCCESS_STATUS_CODES,
70
+ errorStatusCodes: errorStatusCodes ?? DEFAULT_ERROR_STATUS_CODES,
71
+ };
72
+
73
+ const content = await prettify(generateFile(generatorOptions));
42
74
  const outputPath = join(
43
75
  cwd,
44
76
  options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`,
@@ -50,7 +82,7 @@ export async function generateClientFiles(input: string, options: typeof options
50
82
 
51
83
  if (options.tanstack) {
52
84
  const tanstackContent = await generateTanstackQueryFile({
53
- ...ctx,
85
+ ...generatorOptions,
54
86
  relativeApiClientPath: "./" + basename(outputPath),
55
87
  });
56
88
  const tanstackOutputPath = join(