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