typed-openapi 0.1.1 → 0.1.3
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/dist/chunk-E7HJBSR2.js +815 -0
- package/dist/cli.cjs +28 -18
- package/dist/cli.js +12 -802
- package/dist/index.cjs +859 -0
- package/dist/index.d.cts +266 -0
- package/dist/index.d.ts +266 -0
- package/dist/index.js +20 -0
- package/package.json +18 -5
- package/src/cli.ts +0 -1
- package/src/format.ts +18 -10
- package/src/generator.ts +18 -13
- package/src/index.ts +7 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
|
+
import { OpenAPIObject, SchemaObject, ReferenceObject, OperationObject } from 'openapi3-ts/oas31';
|
|
3
|
+
import * as arktype from 'arktype';
|
|
4
|
+
import * as Codegen from '@sinclair/typebox-codegen';
|
|
5
|
+
|
|
6
|
+
declare class Box<T extends AnyBoxDef = AnyBoxDef> {
|
|
7
|
+
definition: T;
|
|
8
|
+
type: T["type"];
|
|
9
|
+
value: T["value"];
|
|
10
|
+
params: T["params"];
|
|
11
|
+
schema: T["schema"];
|
|
12
|
+
ctx: T["ctx"];
|
|
13
|
+
constructor(definition: T);
|
|
14
|
+
toJSON(): {
|
|
15
|
+
type: T["type"];
|
|
16
|
+
value: T["value"];
|
|
17
|
+
};
|
|
18
|
+
toString(): string;
|
|
19
|
+
recompute(callback: OpenapiSchemaConvertContext["onBox"]): Box<AnyBoxDef>;
|
|
20
|
+
static fromJSON(json: string): Box<any>;
|
|
21
|
+
static isBox(box: unknown): box is Box<AnyBoxDef>;
|
|
22
|
+
static isUnion(box: Box<AnyBoxDef>): box is Box<BoxUnion>;
|
|
23
|
+
static isIntersection(box: Box<AnyBoxDef>): box is Box<BoxIntersection>;
|
|
24
|
+
static isArray(box: Box<AnyBoxDef>): box is Box<BoxArray>;
|
|
25
|
+
static isOptional(box: Box<AnyBoxDef>): box is Box<BoxOptional>;
|
|
26
|
+
static isReference(box: Box<AnyBoxDef>): box is Box<BoxRef>;
|
|
27
|
+
static isKeyword(box: Box<AnyBoxDef>): box is Box<BoxKeyword>;
|
|
28
|
+
static isObject(box: Box<AnyBoxDef>): box is Box<BoxObject>;
|
|
29
|
+
static isLiteral(box: Box<AnyBoxDef>): box is Box<BoxLiteral>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type RefInfo = {
|
|
33
|
+
/**
|
|
34
|
+
* The (potentially autocorrected) ref
|
|
35
|
+
* @example "#/components/schemas/MySchema"
|
|
36
|
+
*/
|
|
37
|
+
ref: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the ref
|
|
40
|
+
* @example "MySchema"
|
|
41
|
+
* */
|
|
42
|
+
name: string;
|
|
43
|
+
normalized: string;
|
|
44
|
+
kind: "schemas" | "responses" | "parameters" | "requestBodies" | "headers";
|
|
45
|
+
};
|
|
46
|
+
declare const createRefResolver: (doc: OpenAPIObject, factory: GenericFactory) => {
|
|
47
|
+
get: <T = SchemaObject>(ref: string) => NonNullable<T>;
|
|
48
|
+
unwrap: <T_1 extends {} | ReferenceObject>(component: T_1) => Exclude<T_1, ReferenceObject>;
|
|
49
|
+
getInfosByRef: (ref: string) => RefInfo;
|
|
50
|
+
infos: Map<string, RefInfo>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the schemas in the order they should be generated, depending on their dependencies
|
|
53
|
+
* so that a schema is generated before the ones that depend on it
|
|
54
|
+
*/
|
|
55
|
+
getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
|
|
56
|
+
directDependencies: Map<string, Set<string>>;
|
|
57
|
+
transitiveDependencies: Map<string, Set<string>>;
|
|
58
|
+
};
|
|
59
|
+
interface RefResolver extends ReturnType<typeof createRefResolver> {
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type BoxDefinition = {
|
|
63
|
+
type: string;
|
|
64
|
+
params: unknown;
|
|
65
|
+
value: string;
|
|
66
|
+
};
|
|
67
|
+
type BoxParams = string | BoxDefinition;
|
|
68
|
+
type WithSchema = {
|
|
69
|
+
schema: SchemaObject | ReferenceObject | undefined;
|
|
70
|
+
ctx: OpenapiSchemaConvertContext;
|
|
71
|
+
};
|
|
72
|
+
type BoxUnion = WithSchema & {
|
|
73
|
+
type: "union";
|
|
74
|
+
params: {
|
|
75
|
+
types: Array<BoxParams>;
|
|
76
|
+
};
|
|
77
|
+
value: string;
|
|
78
|
+
};
|
|
79
|
+
type BoxIntersection = WithSchema & {
|
|
80
|
+
type: "intersection";
|
|
81
|
+
params: {
|
|
82
|
+
types: Array<BoxParams>;
|
|
83
|
+
};
|
|
84
|
+
value: string;
|
|
85
|
+
};
|
|
86
|
+
type BoxArray = WithSchema & {
|
|
87
|
+
type: "array";
|
|
88
|
+
params: {
|
|
89
|
+
type: BoxParams;
|
|
90
|
+
};
|
|
91
|
+
value: string;
|
|
92
|
+
};
|
|
93
|
+
type BoxOptional = WithSchema & {
|
|
94
|
+
type: "optional";
|
|
95
|
+
params: {
|
|
96
|
+
type: BoxParams;
|
|
97
|
+
};
|
|
98
|
+
value: string;
|
|
99
|
+
};
|
|
100
|
+
type BoxRef = WithSchema & {
|
|
101
|
+
type: "ref";
|
|
102
|
+
params: {
|
|
103
|
+
name: string;
|
|
104
|
+
generics?: BoxParams[] | undefined;
|
|
105
|
+
};
|
|
106
|
+
value: string;
|
|
107
|
+
};
|
|
108
|
+
type BoxLiteral = WithSchema & {
|
|
109
|
+
type: "literal";
|
|
110
|
+
params: {};
|
|
111
|
+
value: string;
|
|
112
|
+
};
|
|
113
|
+
type BoxKeyword = WithSchema & {
|
|
114
|
+
type: "keyword";
|
|
115
|
+
params: {
|
|
116
|
+
name: string;
|
|
117
|
+
};
|
|
118
|
+
value: string;
|
|
119
|
+
};
|
|
120
|
+
type BoxObject = WithSchema & {
|
|
121
|
+
type: "object";
|
|
122
|
+
params: {
|
|
123
|
+
props: Record<string, BoxParams>;
|
|
124
|
+
};
|
|
125
|
+
value: string;
|
|
126
|
+
};
|
|
127
|
+
type AnyBoxDef = BoxUnion | BoxIntersection | BoxArray | BoxOptional | BoxRef | BoxLiteral | BoxKeyword | BoxObject;
|
|
128
|
+
type AnyBox = Box<AnyBoxDef>;
|
|
129
|
+
type OpenapiSchemaConvertArgs = {
|
|
130
|
+
schema: SchemaObject | ReferenceObject;
|
|
131
|
+
ctx: OpenapiSchemaConvertContext;
|
|
132
|
+
meta?: {} | undefined;
|
|
133
|
+
};
|
|
134
|
+
type FactoryCreator = (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => GenericFactory;
|
|
135
|
+
type OpenapiSchemaConvertContext = {
|
|
136
|
+
factory: FactoryCreator | GenericFactory;
|
|
137
|
+
refs: RefResolver;
|
|
138
|
+
onBox?: (box: Box<AnyBoxDef>) => Box<AnyBoxDef>;
|
|
139
|
+
};
|
|
140
|
+
type StringOrBox = string | Box<AnyBoxDef>;
|
|
141
|
+
type BoxFactory = {
|
|
142
|
+
union: (types: Array<StringOrBox>) => Box<BoxUnion>;
|
|
143
|
+
intersection: (types: Array<StringOrBox>) => Box<BoxIntersection>;
|
|
144
|
+
array: (type: StringOrBox) => Box<BoxArray>;
|
|
145
|
+
object: (props: Record<string, StringOrBox>) => Box<BoxObject>;
|
|
146
|
+
optional: (type: StringOrBox) => Box<BoxOptional>;
|
|
147
|
+
reference: (name: string, generics?: Array<StringOrBox> | undefined) => Box<BoxRef>;
|
|
148
|
+
literal: (value: StringOrBox) => Box<BoxLiteral>;
|
|
149
|
+
string: () => Box<BoxKeyword>;
|
|
150
|
+
number: () => Box<BoxKeyword>;
|
|
151
|
+
boolean: () => Box<BoxKeyword>;
|
|
152
|
+
unknown: () => Box<BoxKeyword>;
|
|
153
|
+
any: () => Box<BoxKeyword>;
|
|
154
|
+
never: () => Box<BoxKeyword>;
|
|
155
|
+
};
|
|
156
|
+
type GenericFactory = {
|
|
157
|
+
callback?: OpenapiSchemaConvertContext["onBox"];
|
|
158
|
+
union: (types: Array<StringOrBox>) => string;
|
|
159
|
+
intersection: (types: Array<StringOrBox>) => string;
|
|
160
|
+
array: (type: StringOrBox) => string;
|
|
161
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
162
|
+
optional: (type: StringOrBox) => string;
|
|
163
|
+
reference: (name: string, generics?: Array<StringOrBox> | undefined) => string;
|
|
164
|
+
literal: (value: StringOrBox) => string;
|
|
165
|
+
string: () => string;
|
|
166
|
+
number: () => string;
|
|
167
|
+
boolean: () => string;
|
|
168
|
+
unknown: () => string;
|
|
169
|
+
any: () => string;
|
|
170
|
+
never: () => string;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
declare const unwrap: (param: StringOrBox) => string;
|
|
174
|
+
declare const createFactory: <T extends GenericFactory | FactoryCreator>(f: T) => T;
|
|
175
|
+
/**
|
|
176
|
+
* Create a box-factory using your schema provider and automatically add the input schema to each box.
|
|
177
|
+
*/
|
|
178
|
+
declare const createBoxFactory: (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => BoxFactory;
|
|
179
|
+
|
|
180
|
+
declare const mapOpenApiEndpoints: (doc: OpenAPIObject) => {
|
|
181
|
+
doc: OpenAPIObject;
|
|
182
|
+
refs: {
|
|
183
|
+
get: <T = openapi3_ts_oas31.SchemaObject>(ref: string) => NonNullable<T>;
|
|
184
|
+
unwrap: <T_1 extends {} | openapi3_ts_oas31.ReferenceObject>(component: T_1) => Exclude<T_1, openapi3_ts_oas31.ReferenceObject>;
|
|
185
|
+
getInfosByRef: (ref: string) => RefInfo;
|
|
186
|
+
infos: Map<string, RefInfo>;
|
|
187
|
+
getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
|
|
188
|
+
directDependencies: Map<string, Set<string>>;
|
|
189
|
+
transitiveDependencies: Map<string, Set<string>>;
|
|
190
|
+
};
|
|
191
|
+
endpointList: Endpoint<DefaultEndpoint>[];
|
|
192
|
+
factory: {
|
|
193
|
+
union: (types: StringOrBox[]) => string;
|
|
194
|
+
intersection: (types: StringOrBox[]) => string;
|
|
195
|
+
array: (type: StringOrBox) => string;
|
|
196
|
+
optional: (type: StringOrBox) => string;
|
|
197
|
+
reference: (name: string, typeArgs: StringOrBox[] | undefined) => string;
|
|
198
|
+
literal: (value: StringOrBox) => string;
|
|
199
|
+
string: () => "string";
|
|
200
|
+
number: () => "number";
|
|
201
|
+
boolean: () => "boolean";
|
|
202
|
+
unknown: () => "unknown";
|
|
203
|
+
any: () => "any";
|
|
204
|
+
never: () => "never";
|
|
205
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
type MutationMethod = "post" | "put" | "patch" | "delete";
|
|
209
|
+
type Method = "get" | "head" | "options" | MutationMethod;
|
|
210
|
+
type EndpointParameters = {
|
|
211
|
+
body?: Box;
|
|
212
|
+
query?: Box<BoxRef> | Record<string, AnyBox>;
|
|
213
|
+
header?: Box<BoxRef> | Record<string, AnyBox>;
|
|
214
|
+
path?: Box<BoxRef> | Record<string, AnyBox>;
|
|
215
|
+
};
|
|
216
|
+
type DefaultEndpoint = {
|
|
217
|
+
parameters?: EndpointParameters | undefined;
|
|
218
|
+
response: AnyBox;
|
|
219
|
+
};
|
|
220
|
+
type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
|
|
221
|
+
operation: OperationObject;
|
|
222
|
+
method: Method;
|
|
223
|
+
path: string;
|
|
224
|
+
parameters?: TConfig["parameters"];
|
|
225
|
+
meta: {
|
|
226
|
+
alias: string;
|
|
227
|
+
hasParameters: boolean;
|
|
228
|
+
areParametersRequired: boolean;
|
|
229
|
+
};
|
|
230
|
+
response: TConfig["response"];
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
type GeneratorOptions = ReturnType<typeof mapOpenApiEndpoints> & {
|
|
234
|
+
runtime?: "none" | keyof typeof runtimeValidationGenerator;
|
|
235
|
+
};
|
|
236
|
+
declare const allowedRuntimes: arktype.Type<"zod" | "arktype" | "typebox" | "valibot" | "yup" | "io-ts" | "none">;
|
|
237
|
+
type OutputRuntime = typeof allowedRuntimes.infer;
|
|
238
|
+
declare const runtimeValidationGenerator: {
|
|
239
|
+
arktype: typeof Codegen.ModelToArkType.Generate;
|
|
240
|
+
"io-ts": typeof Codegen.ModelToIoTs.Generate;
|
|
241
|
+
typebox: typeof Codegen.ModelToTypeBox.Generate;
|
|
242
|
+
valibot: typeof Codegen.ModelToValibot.Generate;
|
|
243
|
+
yup: typeof Codegen.ModelToYup.Generate;
|
|
244
|
+
zod: typeof Codegen.ModelToZod.Generate;
|
|
245
|
+
};
|
|
246
|
+
declare const generateFile: (options: GeneratorOptions) => string;
|
|
247
|
+
|
|
248
|
+
declare const openApiSchemaToTs: ({ schema, meta: _inheritedMeta, ctx }: OpenapiSchemaConvertArgs) => Box<AnyBoxDef>;
|
|
249
|
+
|
|
250
|
+
declare const tsFactory: {
|
|
251
|
+
union: (types: StringOrBox[]) => string;
|
|
252
|
+
intersection: (types: StringOrBox[]) => string;
|
|
253
|
+
array: (type: StringOrBox) => string;
|
|
254
|
+
optional: (type: StringOrBox) => string;
|
|
255
|
+
reference: (name: string, typeArgs: StringOrBox[] | undefined) => string;
|
|
256
|
+
literal: (value: StringOrBox) => string;
|
|
257
|
+
string: () => "string";
|
|
258
|
+
number: () => "number";
|
|
259
|
+
boolean: () => "boolean";
|
|
260
|
+
unknown: () => "unknown";
|
|
261
|
+
any: () => "any";
|
|
262
|
+
never: () => "never";
|
|
263
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export { AnyBox, AnyBoxDef, BoxArray, BoxDefinition, BoxFactory, BoxIntersection, BoxKeyword, BoxLiteral, BoxObject, BoxOptional, BoxParams, BoxRef, BoxUnion, Endpoint, EndpointParameters, FactoryCreator, GenericFactory, OpenapiSchemaConvertArgs, OpenapiSchemaConvertContext, OutputRuntime, RefInfo, RefResolver, StringOrBox, WithSchema, createBoxFactory, createFactory, createRefResolver, generateFile, mapOpenApiEndpoints, openApiSchemaToTs, tsFactory, unwrap };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
|
+
import { OpenAPIObject, SchemaObject, ReferenceObject, OperationObject } from 'openapi3-ts/oas31';
|
|
3
|
+
import * as arktype from 'arktype';
|
|
4
|
+
import * as Codegen from '@sinclair/typebox-codegen';
|
|
5
|
+
|
|
6
|
+
declare class Box<T extends AnyBoxDef = AnyBoxDef> {
|
|
7
|
+
definition: T;
|
|
8
|
+
type: T["type"];
|
|
9
|
+
value: T["value"];
|
|
10
|
+
params: T["params"];
|
|
11
|
+
schema: T["schema"];
|
|
12
|
+
ctx: T["ctx"];
|
|
13
|
+
constructor(definition: T);
|
|
14
|
+
toJSON(): {
|
|
15
|
+
type: T["type"];
|
|
16
|
+
value: T["value"];
|
|
17
|
+
};
|
|
18
|
+
toString(): string;
|
|
19
|
+
recompute(callback: OpenapiSchemaConvertContext["onBox"]): Box<AnyBoxDef>;
|
|
20
|
+
static fromJSON(json: string): Box<any>;
|
|
21
|
+
static isBox(box: unknown): box is Box<AnyBoxDef>;
|
|
22
|
+
static isUnion(box: Box<AnyBoxDef>): box is Box<BoxUnion>;
|
|
23
|
+
static isIntersection(box: Box<AnyBoxDef>): box is Box<BoxIntersection>;
|
|
24
|
+
static isArray(box: Box<AnyBoxDef>): box is Box<BoxArray>;
|
|
25
|
+
static isOptional(box: Box<AnyBoxDef>): box is Box<BoxOptional>;
|
|
26
|
+
static isReference(box: Box<AnyBoxDef>): box is Box<BoxRef>;
|
|
27
|
+
static isKeyword(box: Box<AnyBoxDef>): box is Box<BoxKeyword>;
|
|
28
|
+
static isObject(box: Box<AnyBoxDef>): box is Box<BoxObject>;
|
|
29
|
+
static isLiteral(box: Box<AnyBoxDef>): box is Box<BoxLiteral>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type RefInfo = {
|
|
33
|
+
/**
|
|
34
|
+
* The (potentially autocorrected) ref
|
|
35
|
+
* @example "#/components/schemas/MySchema"
|
|
36
|
+
*/
|
|
37
|
+
ref: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the ref
|
|
40
|
+
* @example "MySchema"
|
|
41
|
+
* */
|
|
42
|
+
name: string;
|
|
43
|
+
normalized: string;
|
|
44
|
+
kind: "schemas" | "responses" | "parameters" | "requestBodies" | "headers";
|
|
45
|
+
};
|
|
46
|
+
declare const createRefResolver: (doc: OpenAPIObject, factory: GenericFactory) => {
|
|
47
|
+
get: <T = SchemaObject>(ref: string) => NonNullable<T>;
|
|
48
|
+
unwrap: <T_1 extends {} | ReferenceObject>(component: T_1) => Exclude<T_1, ReferenceObject>;
|
|
49
|
+
getInfosByRef: (ref: string) => RefInfo;
|
|
50
|
+
infos: Map<string, RefInfo>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the schemas in the order they should be generated, depending on their dependencies
|
|
53
|
+
* so that a schema is generated before the ones that depend on it
|
|
54
|
+
*/
|
|
55
|
+
getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
|
|
56
|
+
directDependencies: Map<string, Set<string>>;
|
|
57
|
+
transitiveDependencies: Map<string, Set<string>>;
|
|
58
|
+
};
|
|
59
|
+
interface RefResolver extends ReturnType<typeof createRefResolver> {
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type BoxDefinition = {
|
|
63
|
+
type: string;
|
|
64
|
+
params: unknown;
|
|
65
|
+
value: string;
|
|
66
|
+
};
|
|
67
|
+
type BoxParams = string | BoxDefinition;
|
|
68
|
+
type WithSchema = {
|
|
69
|
+
schema: SchemaObject | ReferenceObject | undefined;
|
|
70
|
+
ctx: OpenapiSchemaConvertContext;
|
|
71
|
+
};
|
|
72
|
+
type BoxUnion = WithSchema & {
|
|
73
|
+
type: "union";
|
|
74
|
+
params: {
|
|
75
|
+
types: Array<BoxParams>;
|
|
76
|
+
};
|
|
77
|
+
value: string;
|
|
78
|
+
};
|
|
79
|
+
type BoxIntersection = WithSchema & {
|
|
80
|
+
type: "intersection";
|
|
81
|
+
params: {
|
|
82
|
+
types: Array<BoxParams>;
|
|
83
|
+
};
|
|
84
|
+
value: string;
|
|
85
|
+
};
|
|
86
|
+
type BoxArray = WithSchema & {
|
|
87
|
+
type: "array";
|
|
88
|
+
params: {
|
|
89
|
+
type: BoxParams;
|
|
90
|
+
};
|
|
91
|
+
value: string;
|
|
92
|
+
};
|
|
93
|
+
type BoxOptional = WithSchema & {
|
|
94
|
+
type: "optional";
|
|
95
|
+
params: {
|
|
96
|
+
type: BoxParams;
|
|
97
|
+
};
|
|
98
|
+
value: string;
|
|
99
|
+
};
|
|
100
|
+
type BoxRef = WithSchema & {
|
|
101
|
+
type: "ref";
|
|
102
|
+
params: {
|
|
103
|
+
name: string;
|
|
104
|
+
generics?: BoxParams[] | undefined;
|
|
105
|
+
};
|
|
106
|
+
value: string;
|
|
107
|
+
};
|
|
108
|
+
type BoxLiteral = WithSchema & {
|
|
109
|
+
type: "literal";
|
|
110
|
+
params: {};
|
|
111
|
+
value: string;
|
|
112
|
+
};
|
|
113
|
+
type BoxKeyword = WithSchema & {
|
|
114
|
+
type: "keyword";
|
|
115
|
+
params: {
|
|
116
|
+
name: string;
|
|
117
|
+
};
|
|
118
|
+
value: string;
|
|
119
|
+
};
|
|
120
|
+
type BoxObject = WithSchema & {
|
|
121
|
+
type: "object";
|
|
122
|
+
params: {
|
|
123
|
+
props: Record<string, BoxParams>;
|
|
124
|
+
};
|
|
125
|
+
value: string;
|
|
126
|
+
};
|
|
127
|
+
type AnyBoxDef = BoxUnion | BoxIntersection | BoxArray | BoxOptional | BoxRef | BoxLiteral | BoxKeyword | BoxObject;
|
|
128
|
+
type AnyBox = Box<AnyBoxDef>;
|
|
129
|
+
type OpenapiSchemaConvertArgs = {
|
|
130
|
+
schema: SchemaObject | ReferenceObject;
|
|
131
|
+
ctx: OpenapiSchemaConvertContext;
|
|
132
|
+
meta?: {} | undefined;
|
|
133
|
+
};
|
|
134
|
+
type FactoryCreator = (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => GenericFactory;
|
|
135
|
+
type OpenapiSchemaConvertContext = {
|
|
136
|
+
factory: FactoryCreator | GenericFactory;
|
|
137
|
+
refs: RefResolver;
|
|
138
|
+
onBox?: (box: Box<AnyBoxDef>) => Box<AnyBoxDef>;
|
|
139
|
+
};
|
|
140
|
+
type StringOrBox = string | Box<AnyBoxDef>;
|
|
141
|
+
type BoxFactory = {
|
|
142
|
+
union: (types: Array<StringOrBox>) => Box<BoxUnion>;
|
|
143
|
+
intersection: (types: Array<StringOrBox>) => Box<BoxIntersection>;
|
|
144
|
+
array: (type: StringOrBox) => Box<BoxArray>;
|
|
145
|
+
object: (props: Record<string, StringOrBox>) => Box<BoxObject>;
|
|
146
|
+
optional: (type: StringOrBox) => Box<BoxOptional>;
|
|
147
|
+
reference: (name: string, generics?: Array<StringOrBox> | undefined) => Box<BoxRef>;
|
|
148
|
+
literal: (value: StringOrBox) => Box<BoxLiteral>;
|
|
149
|
+
string: () => Box<BoxKeyword>;
|
|
150
|
+
number: () => Box<BoxKeyword>;
|
|
151
|
+
boolean: () => Box<BoxKeyword>;
|
|
152
|
+
unknown: () => Box<BoxKeyword>;
|
|
153
|
+
any: () => Box<BoxKeyword>;
|
|
154
|
+
never: () => Box<BoxKeyword>;
|
|
155
|
+
};
|
|
156
|
+
type GenericFactory = {
|
|
157
|
+
callback?: OpenapiSchemaConvertContext["onBox"];
|
|
158
|
+
union: (types: Array<StringOrBox>) => string;
|
|
159
|
+
intersection: (types: Array<StringOrBox>) => string;
|
|
160
|
+
array: (type: StringOrBox) => string;
|
|
161
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
162
|
+
optional: (type: StringOrBox) => string;
|
|
163
|
+
reference: (name: string, generics?: Array<StringOrBox> | undefined) => string;
|
|
164
|
+
literal: (value: StringOrBox) => string;
|
|
165
|
+
string: () => string;
|
|
166
|
+
number: () => string;
|
|
167
|
+
boolean: () => string;
|
|
168
|
+
unknown: () => string;
|
|
169
|
+
any: () => string;
|
|
170
|
+
never: () => string;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
declare const unwrap: (param: StringOrBox) => string;
|
|
174
|
+
declare const createFactory: <T extends GenericFactory | FactoryCreator>(f: T) => T;
|
|
175
|
+
/**
|
|
176
|
+
* Create a box-factory using your schema provider and automatically add the input schema to each box.
|
|
177
|
+
*/
|
|
178
|
+
declare const createBoxFactory: (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => BoxFactory;
|
|
179
|
+
|
|
180
|
+
declare const mapOpenApiEndpoints: (doc: OpenAPIObject) => {
|
|
181
|
+
doc: OpenAPIObject;
|
|
182
|
+
refs: {
|
|
183
|
+
get: <T = openapi3_ts_oas31.SchemaObject>(ref: string) => NonNullable<T>;
|
|
184
|
+
unwrap: <T_1 extends {} | openapi3_ts_oas31.ReferenceObject>(component: T_1) => Exclude<T_1, openapi3_ts_oas31.ReferenceObject>;
|
|
185
|
+
getInfosByRef: (ref: string) => RefInfo;
|
|
186
|
+
infos: Map<string, RefInfo>;
|
|
187
|
+
getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][];
|
|
188
|
+
directDependencies: Map<string, Set<string>>;
|
|
189
|
+
transitiveDependencies: Map<string, Set<string>>;
|
|
190
|
+
};
|
|
191
|
+
endpointList: Endpoint<DefaultEndpoint>[];
|
|
192
|
+
factory: {
|
|
193
|
+
union: (types: StringOrBox[]) => string;
|
|
194
|
+
intersection: (types: StringOrBox[]) => string;
|
|
195
|
+
array: (type: StringOrBox) => string;
|
|
196
|
+
optional: (type: StringOrBox) => string;
|
|
197
|
+
reference: (name: string, typeArgs: StringOrBox[] | undefined) => string;
|
|
198
|
+
literal: (value: StringOrBox) => string;
|
|
199
|
+
string: () => "string";
|
|
200
|
+
number: () => "number";
|
|
201
|
+
boolean: () => "boolean";
|
|
202
|
+
unknown: () => "unknown";
|
|
203
|
+
any: () => "any";
|
|
204
|
+
never: () => "never";
|
|
205
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
type MutationMethod = "post" | "put" | "patch" | "delete";
|
|
209
|
+
type Method = "get" | "head" | "options" | MutationMethod;
|
|
210
|
+
type EndpointParameters = {
|
|
211
|
+
body?: Box;
|
|
212
|
+
query?: Box<BoxRef> | Record<string, AnyBox>;
|
|
213
|
+
header?: Box<BoxRef> | Record<string, AnyBox>;
|
|
214
|
+
path?: Box<BoxRef> | Record<string, AnyBox>;
|
|
215
|
+
};
|
|
216
|
+
type DefaultEndpoint = {
|
|
217
|
+
parameters?: EndpointParameters | undefined;
|
|
218
|
+
response: AnyBox;
|
|
219
|
+
};
|
|
220
|
+
type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
|
|
221
|
+
operation: OperationObject;
|
|
222
|
+
method: Method;
|
|
223
|
+
path: string;
|
|
224
|
+
parameters?: TConfig["parameters"];
|
|
225
|
+
meta: {
|
|
226
|
+
alias: string;
|
|
227
|
+
hasParameters: boolean;
|
|
228
|
+
areParametersRequired: boolean;
|
|
229
|
+
};
|
|
230
|
+
response: TConfig["response"];
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
type GeneratorOptions = ReturnType<typeof mapOpenApiEndpoints> & {
|
|
234
|
+
runtime?: "none" | keyof typeof runtimeValidationGenerator;
|
|
235
|
+
};
|
|
236
|
+
declare const allowedRuntimes: arktype.Type<"zod" | "arktype" | "typebox" | "valibot" | "yup" | "io-ts" | "none">;
|
|
237
|
+
type OutputRuntime = typeof allowedRuntimes.infer;
|
|
238
|
+
declare const runtimeValidationGenerator: {
|
|
239
|
+
arktype: typeof Codegen.ModelToArkType.Generate;
|
|
240
|
+
"io-ts": typeof Codegen.ModelToIoTs.Generate;
|
|
241
|
+
typebox: typeof Codegen.ModelToTypeBox.Generate;
|
|
242
|
+
valibot: typeof Codegen.ModelToValibot.Generate;
|
|
243
|
+
yup: typeof Codegen.ModelToYup.Generate;
|
|
244
|
+
zod: typeof Codegen.ModelToZod.Generate;
|
|
245
|
+
};
|
|
246
|
+
declare const generateFile: (options: GeneratorOptions) => string;
|
|
247
|
+
|
|
248
|
+
declare const openApiSchemaToTs: ({ schema, meta: _inheritedMeta, ctx }: OpenapiSchemaConvertArgs) => Box<AnyBoxDef>;
|
|
249
|
+
|
|
250
|
+
declare const tsFactory: {
|
|
251
|
+
union: (types: StringOrBox[]) => string;
|
|
252
|
+
intersection: (types: StringOrBox[]) => string;
|
|
253
|
+
array: (type: StringOrBox) => string;
|
|
254
|
+
optional: (type: StringOrBox) => string;
|
|
255
|
+
reference: (name: string, typeArgs: StringOrBox[] | undefined) => string;
|
|
256
|
+
literal: (value: StringOrBox) => string;
|
|
257
|
+
string: () => "string";
|
|
258
|
+
number: () => "number";
|
|
259
|
+
boolean: () => "boolean";
|
|
260
|
+
unknown: () => "unknown";
|
|
261
|
+
any: () => "any";
|
|
262
|
+
never: () => "never";
|
|
263
|
+
object: (props: Record<string, StringOrBox>) => string;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export { AnyBox, AnyBoxDef, BoxArray, BoxDefinition, BoxFactory, BoxIntersection, BoxKeyword, BoxLiteral, BoxObject, BoxOptional, BoxParams, BoxRef, BoxUnion, Endpoint, EndpointParameters, FactoryCreator, GenericFactory, OpenapiSchemaConvertArgs, OpenapiSchemaConvertContext, OutputRuntime, RefInfo, RefResolver, StringOrBox, WithSchema, createBoxFactory, createFactory, createRefResolver, generateFile, mapOpenApiEndpoints, openApiSchemaToTs, tsFactory, unwrap };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createBoxFactory,
|
|
3
|
+
createFactory,
|
|
4
|
+
createRefResolver,
|
|
5
|
+
generateFile,
|
|
6
|
+
mapOpenApiEndpoints,
|
|
7
|
+
openApiSchemaToTs,
|
|
8
|
+
tsFactory,
|
|
9
|
+
unwrap
|
|
10
|
+
} from "./chunk-E7HJBSR2.js";
|
|
11
|
+
export {
|
|
12
|
+
createBoxFactory,
|
|
13
|
+
createFactory,
|
|
14
|
+
createRefResolver,
|
|
15
|
+
generateFile,
|
|
16
|
+
mapOpenApiEndpoints,
|
|
17
|
+
openApiSchemaToTs,
|
|
18
|
+
tsFactory,
|
|
19
|
+
unwrap
|
|
20
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-openapi",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"bin": {
|
|
@@ -9,24 +9,24 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/astahmer/typed-openapi.git"
|
|
12
|
+
"url": "https://github.com/astahmer/typed-openapi.git",
|
|
13
|
+
"directory": "packages/typed-openapi"
|
|
13
14
|
},
|
|
14
15
|
"dependencies": {
|
|
15
16
|
"@apidevtools/swagger-parser": "^10.1.0",
|
|
16
17
|
"@changesets/cli": "^2.26.2",
|
|
17
|
-
"@dprint/formatter": "^0.2.0",
|
|
18
|
-
"@dprint/typescript": "^0.85.1",
|
|
19
18
|
"@sinclair/typebox-codegen": "^0.8.5",
|
|
20
19
|
"arktype": "1.0.18-alpha",
|
|
21
20
|
"cac": "^6.7.14",
|
|
22
|
-
"dprint": "^0.40.2",
|
|
23
21
|
"openapi3-ts": "^4.1.2",
|
|
24
22
|
"pastable": "^2.2.1",
|
|
25
23
|
"pathe": "^1.1.1",
|
|
24
|
+
"prettier": "2.8.4",
|
|
26
25
|
"ts-pattern": "^5.0.4"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
28
|
"@types/node": "^20.4.5",
|
|
29
|
+
"@types/prettier": "2.7.3",
|
|
30
30
|
"tsup": "^7.1.0",
|
|
31
31
|
"typescript": "^5.1.6",
|
|
32
32
|
"vite-node": "^0.33.0",
|
|
@@ -38,6 +38,19 @@
|
|
|
38
38
|
"cli",
|
|
39
39
|
"README.md"
|
|
40
40
|
],
|
|
41
|
+
"keywords": [
|
|
42
|
+
"typescript",
|
|
43
|
+
"openapi",
|
|
44
|
+
"generator",
|
|
45
|
+
"runtime",
|
|
46
|
+
"typesafe",
|
|
47
|
+
"zod",
|
|
48
|
+
"arktype",
|
|
49
|
+
"typebox",
|
|
50
|
+
"valibot",
|
|
51
|
+
"yup",
|
|
52
|
+
"io-ts"
|
|
53
|
+
],
|
|
41
54
|
"sideEffects": false,
|
|
42
55
|
"publishConfig": {
|
|
43
56
|
"access": "public"
|
package/src/cli.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { join } from "pathe";
|
|
|
5
5
|
import { type } from "arktype";
|
|
6
6
|
|
|
7
7
|
import { writeFile } from "fs/promises";
|
|
8
|
-
// @ts-expect-error
|
|
9
8
|
import { name, version } from "../package.json";
|
|
10
9
|
import { allowedRuntimes, generateFile } from "./generator";
|
|
11
10
|
import { mapOpenApiEndpoints } from "./map-openapi-endpoints";
|
package/src/format.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { join } from "path";
|
|
1
|
+
import prettier, { type Options } from "prettier";
|
|
2
|
+
import parserTypescript from "prettier/parser-typescript";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
/** @see https://github.dev/stephenh/ts-poet/blob/5ea0dbb3c9f1f4b0ee51a54abb2d758102eda4a2/src/Code.ts#L231 */
|
|
5
|
+
function maybePretty(input: string, options?: Options | null): string {
|
|
6
|
+
try {
|
|
7
|
+
return prettier.format(input, {
|
|
8
|
+
parser: "typescript",
|
|
9
|
+
plugins: [parserTypescript],
|
|
10
|
+
...options,
|
|
11
|
+
});
|
|
12
|
+
} catch (err) {
|
|
13
|
+
console.warn("Failed to format code");
|
|
14
|
+
console.warn(err);
|
|
15
|
+
return input; // assume it's invalid syntax and ignore
|
|
16
|
+
}
|
|
17
|
+
}
|
|
8
18
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export const prettify = (code: string, options?: GlobalConfiguration) =>
|
|
12
|
-
formatter.formatText("code.ts", code, options as any);
|
|
19
|
+
export const prettify = (str: string, options?: Options | null) =>
|
|
20
|
+
maybePretty(str, { printWidth: 120, trailingComma: "all", ...options });
|