@kubb/swagger-ts 2.0.0-beta.6 → 2.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/dist/components.cjs +80 -43
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +66 -17
- package/dist/components.d.ts +66 -17
- package/dist/components.js +85 -46
- package/dist/components.js.map +1 -1
- package/dist/index.cjs +82 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +414 -12
- package/dist/index.d.ts +414 -12
- package/dist/index.js +83 -40
- package/dist/index.js.map +1 -1
- package/dist/infer.d.ts +381 -0
- package/dist/infer.js +5 -0
- package/dist/infer.js.map +1 -0
- package/package.json +14 -8
- package/src/OperationGenerator.tsx +15 -3
- package/src/components/Oas.tsx +84 -0
- package/src/components/index.ts +1 -0
- package/src/infer/index.ts +7 -0
- package/src/infer/mappers.ts +93 -0
- package/src/infer/model.ts +38 -0
- package/src/infer/parse.ts +58 -0
- package/src/infer/requestParams.ts +170 -0
- package/src/infer/response.ts +39 -0
- package/src/infer/security.ts +158 -0
- package/src/plugin.ts +10 -25
- package/src/types.ts +26 -11
package/dist/infer.d.ts
ADDED
@@ -0,0 +1,381 @@
|
|
1
|
+
import { OasTypes } from '@kubb/swagger';
|
2
|
+
import { Pipe, Tuples, Fn, Call, Objects, Booleans, Strings } from 'hotscript';
|
3
|
+
import { JSONSchema, FromSchema } from 'json-schema-to-ts';
|
4
|
+
import { Object as Object$1 } from 'ts-toolbelt';
|
5
|
+
import { TupleToUnion, SplitByDelimiter } from '@kubb/types';
|
6
|
+
|
7
|
+
declare namespace Checks$5 {
|
8
|
+
type Required = {
|
9
|
+
required: true;
|
10
|
+
};
|
11
|
+
type Schemas = {
|
12
|
+
schema: JSONSchema;
|
13
|
+
};
|
14
|
+
type Enum = {
|
15
|
+
type: JSONSchemaTypeName;
|
16
|
+
enum?: any[];
|
17
|
+
};
|
18
|
+
type Parameters = {
|
19
|
+
in: string;
|
20
|
+
required?: boolean;
|
21
|
+
}[];
|
22
|
+
type SingleParameter<TParamType> = [{
|
23
|
+
in: TParamType;
|
24
|
+
required?: true;
|
25
|
+
}];
|
26
|
+
type Responses = {
|
27
|
+
responses: any;
|
28
|
+
};
|
29
|
+
}
|
30
|
+
type PathMap<TOAS extends OasTypes.OASDocument> = TOAS['paths'];
|
31
|
+
interface ParamPropMap {
|
32
|
+
query: 'query';
|
33
|
+
path: 'params';
|
34
|
+
header: 'headers';
|
35
|
+
}
|
36
|
+
type JSONSchemaTypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
37
|
+
type ParamObj<TParameter extends {
|
38
|
+
name: string;
|
39
|
+
}> = TParameter extends Checks$5.Required ? {
|
40
|
+
[TName in TParameter['name']]: TParameter extends Checks$5.Schemas ? FromSchema<TParameter['schema']> : TParameter extends Checks$5.Enum ? FromSchema<{
|
41
|
+
type: TParameter['type'];
|
42
|
+
enum: TParameter['enum'];
|
43
|
+
}> : unknown;
|
44
|
+
} : {
|
45
|
+
[TName in TParameter['name']]?: TParameter extends Checks$5.Schemas ? FromSchema<TParameter['schema']> : TParameter extends Checks$5.Enum ? FromSchema<{
|
46
|
+
type: TParameter['type'];
|
47
|
+
enum: TParameter['enum'];
|
48
|
+
}> : unknown;
|
49
|
+
};
|
50
|
+
interface ParamToRequestParam<TParameters extends Checks$5.Parameters> extends Fn {
|
51
|
+
return: this['arg0'] extends {
|
52
|
+
name: string;
|
53
|
+
in: infer TParamType;
|
54
|
+
} ? TParameters extends Checks$5.SingleParameter<TParamType> ? {
|
55
|
+
[TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]: ParamObj<this['arg0']>;
|
56
|
+
} : {
|
57
|
+
[TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]?: ParamObj<this['arg0']>;
|
58
|
+
} : {};
|
59
|
+
}
|
60
|
+
type ParamMap<TParameters extends Checks$5.Parameters> = Pipe<TParameters, [
|
61
|
+
Tuples.Map<ParamToRequestParam<TParameters>>,
|
62
|
+
Tuples.ToIntersection
|
63
|
+
]>;
|
64
|
+
type MethodMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>> = PathMap<TOAS>[TPath];
|
65
|
+
type StatusMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>> = MethodMap<TOAS, TPath>[TMethod] extends Checks$5.Responses ? MethodMap<TOAS, TPath>[TMethod]['responses'] : never;
|
66
|
+
|
67
|
+
declare namespace Checks$4 {
|
68
|
+
type ModelWithSchemas = {
|
69
|
+
components: {
|
70
|
+
schemas: Record<string, JSONSchema>;
|
71
|
+
};
|
72
|
+
};
|
73
|
+
type ModelWithSchemasNamed<TName extends string | number | symbol> = {
|
74
|
+
components: {
|
75
|
+
schemas: {
|
76
|
+
[TModelName in TName]: JSONSchema;
|
77
|
+
};
|
78
|
+
};
|
79
|
+
};
|
80
|
+
type ModelWithDefinitions = {
|
81
|
+
definitions: Record<string, JSONSchema>;
|
82
|
+
};
|
83
|
+
type ModelWithDefinitionsNamed<TName extends string | number | symbol = never> = {
|
84
|
+
definitions: {
|
85
|
+
[TModelName in TName]: JSONSchema;
|
86
|
+
};
|
87
|
+
};
|
88
|
+
}
|
89
|
+
type Model<TOAS extends OasTypes.OASDocument, TName extends TOAS extends Checks$4.ModelWithSchemas ? keyof TOAS['components']['schemas'] : TOAS extends Checks$4.ModelWithDefinitions ? keyof TOAS['definitions'] : never> = TOAS extends Checks$4.ModelWithSchemasNamed<TName> ? FromSchema<TOAS['components']['schemas'][TName]> : TOAS extends Checks$4.ModelWithDefinitionsNamed<TName> ? FromSchema<TOAS['definitions'][TName]> : never;
|
90
|
+
|
91
|
+
declare namespace Checks$3 {
|
92
|
+
type AllOFf = {
|
93
|
+
allOf: any[];
|
94
|
+
};
|
95
|
+
type Object = {
|
96
|
+
type: 'object';
|
97
|
+
properties: any;
|
98
|
+
};
|
99
|
+
type Properties = {
|
100
|
+
properties: any;
|
101
|
+
};
|
102
|
+
type PropertiesRequired = {
|
103
|
+
properties: Record<string, any>;
|
104
|
+
required: string[];
|
105
|
+
};
|
106
|
+
}
|
107
|
+
type FixAdditionalPropertiesForAllOf<T> = T extends Checks$3.AllOFf ? Omit<T, 'allOf'> & {
|
108
|
+
allOf: Call<Tuples.Map<Objects.Omit<'additionalProperties'>>, T['allOf']>;
|
109
|
+
} : T;
|
110
|
+
type FixMissingAdditionalProperties<T> = T extends Checks$3.Object ? Omit<T, 'additionalProperties'> & {
|
111
|
+
additionalProperties: false;
|
112
|
+
} : T;
|
113
|
+
type FixMissingTypeObject<T> = T extends Checks$3.Properties ? T & {
|
114
|
+
type: 'object';
|
115
|
+
} : T;
|
116
|
+
type FixExtraRequiredFields<T> = T extends Checks$3.PropertiesRequired ? Omit<T, 'required'> & {
|
117
|
+
required: Call<Tuples.Filter<Booleans.Extends<keyof T['properties']>>, T['required']>;
|
118
|
+
} : T;
|
119
|
+
type FixJSONSchema<T> = FixAdditionalPropertiesForAllOf<FixMissingAdditionalProperties<FixMissingTypeObject<FixExtraRequiredFields<T>>>>;
|
120
|
+
type Mutable<Type> = FixJSONSchema<{
|
121
|
+
-readonly [Key in keyof Type]: Mutable<Type[Key]>;
|
122
|
+
}>;
|
123
|
+
type RefToPath<T extends string> = T extends `#/${infer Ref}` ? Call<Strings.Split<'/'>, Ref> : never;
|
124
|
+
type ResolveRef<TObj, TRef extends string> = {
|
125
|
+
$id: TRef;
|
126
|
+
} & Object$1.Path<TObj, RefToPath<TRef>>;
|
127
|
+
type ResolveRefInObj<T, TBase> = T extends {
|
128
|
+
$ref: infer Ref;
|
129
|
+
} ? Ref extends string ? ResolveRef<TBase, Ref> : T : T;
|
130
|
+
type ResolveRefsInObj<T, TBase = T> = {
|
131
|
+
[K in keyof T]: ResolveRefsInObj<ResolveRefInObj<T[K], TBase>, TBase>;
|
132
|
+
};
|
133
|
+
type Parse<TOAS> = Mutable<ResolveRefsInObj<TOAS>>;
|
134
|
+
|
135
|
+
declare namespace Checks$2 {
|
136
|
+
type Security = {
|
137
|
+
security: {
|
138
|
+
[key: string]: any;
|
139
|
+
}[];
|
140
|
+
};
|
141
|
+
namespace AuthParams {
|
142
|
+
type Basic = {
|
143
|
+
type: 'http';
|
144
|
+
scheme: 'basic';
|
145
|
+
} | {
|
146
|
+
type: 'basic';
|
147
|
+
};
|
148
|
+
type Bearer = {
|
149
|
+
type: 'http';
|
150
|
+
scheme: 'bearer';
|
151
|
+
} | {
|
152
|
+
type: 'bearer';
|
153
|
+
};
|
154
|
+
type OAuth2 = {
|
155
|
+
type: 'oauth2';
|
156
|
+
};
|
157
|
+
type ApiKey = {
|
158
|
+
type: 'apiKey';
|
159
|
+
in: 'header';
|
160
|
+
};
|
161
|
+
}
|
162
|
+
namespace AuthName {
|
163
|
+
type Basic = `basic${string}`;
|
164
|
+
type Bearer = `bearer${string}`;
|
165
|
+
type OAuth2 = `oauth${string}`;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
type SecuritySchemeName<T extends Checks$2.Security> = Call<Tuples.Map<Objects.Keys>, T['security']>[number];
|
169
|
+
declare namespace AuthParams {
|
170
|
+
type Basic<TSecurityScheme> = TSecurityScheme extends Checks$2.AuthParams.Basic ? {
|
171
|
+
headers: {
|
172
|
+
/**
|
173
|
+
* `Authorization` header is required for basic authentication
|
174
|
+
* @see https://en.wikipedia.org/wiki/Basic_access_authentication
|
175
|
+
*
|
176
|
+
* It contains the word `Basic` followed by a space and a base64-encoded string `username:password`
|
177
|
+
*
|
178
|
+
* @example
|
179
|
+
* ```
|
180
|
+
* Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
|
181
|
+
* ```
|
182
|
+
*/
|
183
|
+
Authorization: `Basic ${string}`;
|
184
|
+
};
|
185
|
+
} : {};
|
186
|
+
type Bearer<TSecurityScheme> = TSecurityScheme extends Checks$2.AuthParams.Bearer ? {
|
187
|
+
/**
|
188
|
+
* `Authorization` header is required for bearer authentication
|
189
|
+
* @see https://swagger.io/docs/specification/authentication/bearer-authentication/
|
190
|
+
*/
|
191
|
+
headers: {
|
192
|
+
/**
|
193
|
+
* It contains the word `Bearer` followed by a space and the token
|
194
|
+
*
|
195
|
+
* @example
|
196
|
+
* ```
|
197
|
+
* Authorization: Bearer {token}
|
198
|
+
* ```
|
199
|
+
*/
|
200
|
+
Authorization: `Bearer ${string}`;
|
201
|
+
};
|
202
|
+
} : {};
|
203
|
+
type ApiKey<TSecurityScheme> = TSecurityScheme extends Checks$2.AuthParams.ApiKey & {
|
204
|
+
name: infer TApiKeyHeaderName;
|
205
|
+
} ? {
|
206
|
+
headers: {
|
207
|
+
[THeaderName in TApiKeyHeaderName extends string ? TApiKeyHeaderName : never]: string;
|
208
|
+
};
|
209
|
+
} : TSecurityScheme extends {
|
210
|
+
type: 'apiKey';
|
211
|
+
in: 'query';
|
212
|
+
name: infer TApiKeyQueryName;
|
213
|
+
} ? {
|
214
|
+
query: {
|
215
|
+
[TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string;
|
216
|
+
};
|
217
|
+
} : {};
|
218
|
+
type OAuth2<TSecurityScheme> = TSecurityScheme extends Checks$2.AuthParams.OAuth2 ? {
|
219
|
+
/**
|
220
|
+
* `Authorization` header is required for OAuth2.
|
221
|
+
*/
|
222
|
+
headers: {
|
223
|
+
/**
|
224
|
+
* The access token string as issued by the authorization server.
|
225
|
+
* @example `Authorization: Bearer <access_token>`
|
226
|
+
*/
|
227
|
+
Authorization: `Bearer ${string}`;
|
228
|
+
};
|
229
|
+
} : {};
|
230
|
+
}
|
231
|
+
type OASSecurityParams<TSecurityScheme> = AuthParams.Basic<TSecurityScheme> & AuthParams.Bearer<TSecurityScheme> & AuthParams.ApiKey<TSecurityScheme> & AuthParams.OAuth2<TSecurityScheme>;
|
232
|
+
type SecurityParamsBySecurityRef<TOAS, TSecurityObj> = TSecurityObj extends Checks$2.Security ? TOAS extends {
|
233
|
+
components: {
|
234
|
+
securitySchemes: {
|
235
|
+
[TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme;
|
236
|
+
};
|
237
|
+
};
|
238
|
+
} | {
|
239
|
+
securityDefinitions: {
|
240
|
+
[TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme;
|
241
|
+
};
|
242
|
+
} ? OASSecurityParams<TSecurityScheme> : SecuritySchemeName<TSecurityObj> extends Checks$2.AuthName.Basic ? AuthParams.Basic<{
|
243
|
+
type: 'http';
|
244
|
+
scheme: 'basic';
|
245
|
+
}> : SecuritySchemeName<TSecurityObj> extends Checks$2.AuthName.Bearer ? AuthParams.Bearer<{
|
246
|
+
type: 'http';
|
247
|
+
scheme: 'bearer';
|
248
|
+
}> : SecuritySchemeName<TSecurityObj> extends Checks$2.AuthName.OAuth2 ? AuthParams.OAuth2<{
|
249
|
+
type: 'oauth2';
|
250
|
+
}> : {} : {};
|
251
|
+
|
252
|
+
declare namespace Checks$1 {
|
253
|
+
type RequestBodyJson = {
|
254
|
+
requestBody: {
|
255
|
+
content: {
|
256
|
+
'application/json': {
|
257
|
+
schema: JSONSchema;
|
258
|
+
};
|
259
|
+
};
|
260
|
+
};
|
261
|
+
};
|
262
|
+
type RequestBodyFormData = {
|
263
|
+
requestBody: {
|
264
|
+
content: {
|
265
|
+
'multipart/form-data': {
|
266
|
+
schema: JSONSchema;
|
267
|
+
};
|
268
|
+
};
|
269
|
+
};
|
270
|
+
};
|
271
|
+
type RequestBodyFormEncoded = {
|
272
|
+
requestBody: {
|
273
|
+
content: {
|
274
|
+
'application/x-www-form-urlencoded': {
|
275
|
+
schema: JSONSchema;
|
276
|
+
};
|
277
|
+
};
|
278
|
+
};
|
279
|
+
};
|
280
|
+
type Parameters = {
|
281
|
+
parameters: {
|
282
|
+
name: string;
|
283
|
+
in: string;
|
284
|
+
}[];
|
285
|
+
};
|
286
|
+
type PathBrackets = `${string}{${string}}${string}`;
|
287
|
+
type PathPattern = `${string}:${string}${string}`;
|
288
|
+
type Required = {
|
289
|
+
required: true;
|
290
|
+
};
|
291
|
+
}
|
292
|
+
type ExtractPathParamsWithPattern<TPath extends string> = Pipe<TPath, [
|
293
|
+
Strings.Split<'/'>,
|
294
|
+
Tuples.Filter<Strings.StartsWith<':'>>,
|
295
|
+
Tuples.Map<Strings.Trim<':'>>,
|
296
|
+
Tuples.ToUnion
|
297
|
+
]>;
|
298
|
+
type IsPathParameter<T extends string> = T extends `{${infer U}}` ? U : never;
|
299
|
+
type ExtractPathParameters<T extends any[]> = {
|
300
|
+
[K in keyof T]: IsPathParameter<T[K]>;
|
301
|
+
};
|
302
|
+
type ExtractSegments<TPath extends string> = SplitByDelimiter<TPath, '/'>;
|
303
|
+
type ExtractSubSegments<T extends any[]> = {
|
304
|
+
[K in keyof T]: SplitByDelimiter<T[K], ';'>;
|
305
|
+
};
|
306
|
+
type ExtractPathParamsWithBrackets<TPath extends string> = TupleToUnion<ExtractPathParameters<ExtractSubSegments<ExtractSegments<TPath>>[number]>>;
|
307
|
+
type RequestParams<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>> = (MethodMap<TOAS, TPath>[TMethod] extends Checks$1.RequestBodyJson ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks$1.Required ? {
|
308
|
+
/**
|
309
|
+
* The request body in JSON is required for this request.
|
310
|
+
*
|
311
|
+
* The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
|
312
|
+
*/
|
313
|
+
json: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>;
|
314
|
+
} : {
|
315
|
+
/**
|
316
|
+
* The request body in JSON is optional for this request.
|
317
|
+
*
|
318
|
+
* The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
|
319
|
+
*/
|
320
|
+
json?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>;
|
321
|
+
} : MethodMap<TOAS, TPath>[TMethod] extends Checks$1.RequestBodyFormData ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks$1.Required ? {
|
322
|
+
/**
|
323
|
+
* The request body in multipart/form-data is required for this request.
|
324
|
+
*
|
325
|
+
* The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
|
326
|
+
*/
|
327
|
+
formData: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>;
|
328
|
+
} : {
|
329
|
+
/**
|
330
|
+
* The request body in multipart/form-data is optional for this request.
|
331
|
+
*
|
332
|
+
* The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
|
333
|
+
*/
|
334
|
+
formData?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>;
|
335
|
+
} : MethodMap<TOAS, TPath>[TMethod] extends Checks$1.RequestBodyFormEncoded ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks$1.Required ? {
|
336
|
+
/**
|
337
|
+
* The request body in application/x-www-form-urlencoded is required for this request.
|
338
|
+
*
|
339
|
+
* The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
|
340
|
+
*/
|
341
|
+
formUrlEncoded: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>;
|
342
|
+
} : {
|
343
|
+
/**
|
344
|
+
* The request body in application/x-www-form-urlencoded is optional for this request.
|
345
|
+
*
|
346
|
+
* The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
|
347
|
+
*/
|
348
|
+
formUrlEncoded?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>;
|
349
|
+
} : {}) & (MethodMap<TOAS, TPath>[TMethod] extends Checks$1.Parameters ? ParamMap<MethodMap<TOAS, TPath>[TMethod]['parameters']> : {}) & // If there is any parameters defined in path but not in the parameters array, we should add them to the params
|
350
|
+
(TPath extends Checks$1.PathBrackets ? {
|
351
|
+
/**
|
352
|
+
* Parameters defined in the path are required for this request.
|
353
|
+
*
|
354
|
+
* The value of `params` will be used to replace the path parameters.
|
355
|
+
*
|
356
|
+
* For example if path is `/todos/{id}` and `params` is `{ id: '1' }`, the path will be `/todos/1`
|
357
|
+
*/
|
358
|
+
params: Record<ExtractPathParamsWithBrackets<TPath>, string | number | bigint | boolean>;
|
359
|
+
} : {}) & (TPath extends Checks$1.PathPattern ? {
|
360
|
+
/**
|
361
|
+
* Parameters defined in the path are required for this request.
|
362
|
+
*
|
363
|
+
* The value of `params` will be used to replace the path parameters.
|
364
|
+
*
|
365
|
+
* For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
|
366
|
+
*/
|
367
|
+
params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>;
|
368
|
+
} : {}) & // Respect security definitions in path object
|
369
|
+
SecurityParamsBySecurityRef<TOAS, MethodMap<TOAS, TPath>[TMethod]> & // Respect global security definitions
|
370
|
+
SecurityParamsBySecurityRef<TOAS, TOAS>;
|
371
|
+
|
372
|
+
declare namespace Checks {
|
373
|
+
type Content = {
|
374
|
+
content: any;
|
375
|
+
};
|
376
|
+
}
|
377
|
+
type ResponseSchemas<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>, TStatus extends keyof StatusMap<TOAS, TPath, TMethod>> = StatusMap<TOAS, TPath, TMethod>[TStatus]['content'];
|
378
|
+
type JSONResponseSchema<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>, TStatus extends keyof StatusMap<TOAS, TPath, TMethod>> = StatusMap<TOAS, TPath, TMethod>[TStatus] extends Checks.Content ? ResponseSchemas<TOAS, TPath, TMethod, TStatus>[keyof ResponseSchemas<TOAS, TPath, TMethod, TStatus>]['schema'] : StatusMap<TOAS, TPath, TMethod>[TStatus]['schema'];
|
379
|
+
type Response<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>, TStatusCode extends keyof StatusMap<TOAS, TPath, TMethod> = 200> = FromSchema<JSONResponseSchema<TOAS, TPath, TMethod, TStatusCode>>;
|
380
|
+
|
381
|
+
export type { MethodMap, Model, Parse, PathMap, RequestParams, Response, StatusMap };
|
package/dist/infer.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@kubb/swagger-ts",
|
3
|
-
"version": "2.0.0-beta.
|
3
|
+
"version": "2.0.0-beta.8",
|
4
4
|
"description": "Generator swagger-ts",
|
5
5
|
"keywords": [
|
6
6
|
"typescript",
|
@@ -30,6 +30,9 @@
|
|
30
30
|
"require": "./dist/components.cjs",
|
31
31
|
"default": "./dist/components.cjs"
|
32
32
|
},
|
33
|
+
"./infer": {
|
34
|
+
"types": "./dist/infer.d.ts"
|
35
|
+
},
|
33
36
|
"./package.json": "./package.json",
|
34
37
|
"./*": "./*"
|
35
38
|
},
|
@@ -43,15 +46,19 @@
|
|
43
46
|
"!/**/__tests__/**"
|
44
47
|
],
|
45
48
|
"dependencies": {
|
46
|
-
"
|
47
|
-
"
|
48
|
-
"
|
49
|
-
"@kubb/
|
50
|
-
"@kubb/
|
49
|
+
"json-schema-to-ts": "^2.9.2",
|
50
|
+
"hotscript": "^1.0.13",
|
51
|
+
"ts-toolbelt": "^9.6.0",
|
52
|
+
"@kubb/core": "2.0.0-beta.8",
|
53
|
+
"@kubb/parser": "2.0.0-beta.8",
|
54
|
+
"@kubb/react": "2.0.0-beta.8",
|
55
|
+
"@kubb/types": "2.0.0-beta.8",
|
56
|
+
"@kubb/swagger": "2.0.0-beta.8"
|
51
57
|
},
|
52
58
|
"devDependencies": {
|
53
59
|
"@types/react": "^18.2.38",
|
54
60
|
"eslint": "^8.54.0",
|
61
|
+
"expect-type": "^0.17.3",
|
55
62
|
"react": "^18.2.0",
|
56
63
|
"tsup": "^8.0.1",
|
57
64
|
"@kubb/eslint-config": "1.1.8",
|
@@ -59,8 +66,7 @@
|
|
59
66
|
"@kubb/tsup-config": "1.1.8"
|
60
67
|
},
|
61
68
|
"peerDependencies": {
|
62
|
-
"
|
63
|
-
"@kubb/react": "2.0.0-beta.6"
|
69
|
+
"@kubb/react": "2.0.0-beta.8"
|
64
70
|
},
|
65
71
|
"packageManager": "pnpm@8.3.0",
|
66
72
|
"engines": {
|
@@ -2,16 +2,28 @@ import { createRoot } from '@kubb/react'
|
|
2
2
|
import { OperationGenerator as Generator } from '@kubb/swagger'
|
3
3
|
|
4
4
|
import { Mutation } from './components/Mutation.tsx'
|
5
|
+
import { Oas } from './components/Oas.tsx'
|
5
6
|
import { Query } from './components/Query.tsx'
|
6
7
|
|
7
|
-
import type { KubbFile } from '@kubb/core'
|
8
8
|
import type { AppContextProps } from '@kubb/react'
|
9
9
|
import type { Operation, OperationMethodResult, OperationSchemas } from '@kubb/swagger'
|
10
10
|
import type { FileMeta, PluginOptions } from './types.ts'
|
11
11
|
|
12
12
|
export class OperationGenerator extends Generator<PluginOptions['resolvedOptions'], PluginOptions> {
|
13
|
-
async all():
|
14
|
-
|
13
|
+
async all(): OperationMethodResult<FileMeta> {
|
14
|
+
const { oas, pluginManager, plugin } = this.context
|
15
|
+
|
16
|
+
if (!plugin.options.oasType) {
|
17
|
+
return null
|
18
|
+
}
|
19
|
+
|
20
|
+
const root = createRoot<AppContextProps>({ logger: pluginManager.logger })
|
21
|
+
root.render(
|
22
|
+
<Oas.File name="oas" typeName="Oas" />,
|
23
|
+
{ meta: { oas, pluginManager, plugin } },
|
24
|
+
)
|
25
|
+
|
26
|
+
return root.files
|
15
27
|
}
|
16
28
|
|
17
29
|
async get(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
|
@@ -0,0 +1,84 @@
|
|
1
|
+
import { File, Type, usePlugin } from '@kubb/react'
|
2
|
+
import { useFile } from '@kubb/react'
|
3
|
+
import { useOas } from '@kubb/swagger/hooks'
|
4
|
+
|
5
|
+
import type { OasTypes } from '@kubb/swagger'
|
6
|
+
import type { ReactNode } from 'react'
|
7
|
+
import type { FileMeta, PluginOptions } from '../types.ts'
|
8
|
+
|
9
|
+
type TemplateProps = {
|
10
|
+
/**
|
11
|
+
* Name of the function
|
12
|
+
*/
|
13
|
+
name: string
|
14
|
+
typeName: string
|
15
|
+
api: OasTypes.OASDocument
|
16
|
+
}
|
17
|
+
|
18
|
+
function Template({
|
19
|
+
name,
|
20
|
+
typeName,
|
21
|
+
api,
|
22
|
+
}: TemplateProps): ReactNode {
|
23
|
+
return (
|
24
|
+
<>
|
25
|
+
{`export const ${name} = ${JSON.stringify(api, undefined, 2)} as const`}
|
26
|
+
<br />
|
27
|
+
<Type name={typeName} export>
|
28
|
+
{`Parse<typeof ${name}>`}
|
29
|
+
</Type>
|
30
|
+
</>
|
31
|
+
)
|
32
|
+
}
|
33
|
+
|
34
|
+
const defaultTemplates = { default: Template } as const
|
35
|
+
|
36
|
+
type Props = {
|
37
|
+
name: string
|
38
|
+
typeName: string
|
39
|
+
/**
|
40
|
+
* This will make it possible to override the default behaviour.
|
41
|
+
*/
|
42
|
+
Template?: React.ComponentType<React.ComponentProps<typeof Template>>
|
43
|
+
}
|
44
|
+
|
45
|
+
export function Oas({
|
46
|
+
name,
|
47
|
+
typeName,
|
48
|
+
Template = defaultTemplates.default,
|
49
|
+
}: Props): ReactNode {
|
50
|
+
const oas = useOas()
|
51
|
+
|
52
|
+
return <Template name={name} typeName={typeName} api={oas.api} />
|
53
|
+
}
|
54
|
+
|
55
|
+
type FileProps = {
|
56
|
+
name: string
|
57
|
+
typeName: string
|
58
|
+
/**
|
59
|
+
* This will make it possible to override the default behaviour.
|
60
|
+
*/
|
61
|
+
templates?: typeof defaultTemplates
|
62
|
+
}
|
63
|
+
|
64
|
+
Oas.File = function({ name, typeName, templates = defaultTemplates }: FileProps): ReactNode {
|
65
|
+
const { key: pluginKey } = usePlugin<PluginOptions>()
|
66
|
+
const file = useFile({ name, pluginKey })
|
67
|
+
|
68
|
+
const Template = templates.default
|
69
|
+
|
70
|
+
return (
|
71
|
+
<File<FileMeta>
|
72
|
+
baseName={file.baseName}
|
73
|
+
path={file.path}
|
74
|
+
meta={file.meta}
|
75
|
+
>
|
76
|
+
<File.Import name={['Parse']} path="@kubb/swagger-ts/infer" isTypeOnly />
|
77
|
+
<File.Source>
|
78
|
+
<Oas Template={Template} name={name} typeName={typeName} />
|
79
|
+
</File.Source>
|
80
|
+
</File>
|
81
|
+
)
|
82
|
+
}
|
83
|
+
|
84
|
+
Oas.templates = defaultTemplates
|
package/src/components/index.ts
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
// based on https://github.com/ardatan/feTS/tree/master
|
2
|
+
|
3
|
+
export type { MethodMap, PathMap, StatusMap } from './mappers.ts'
|
4
|
+
export type { Model } from './model.ts'
|
5
|
+
export type { Parse } from './parse.ts'
|
6
|
+
export type { RequestParams } from './requestParams.ts'
|
7
|
+
export type { Response } from './response.ts'
|
@@ -0,0 +1,93 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
3
|
+
import type { OasTypes } from '@kubb/swagger'
|
4
|
+
import type { Fn, Pipe, Tuples } from 'hotscript'
|
5
|
+
import type {
|
6
|
+
FromSchema,
|
7
|
+
JSONSchema,
|
8
|
+
} from 'json-schema-to-ts'
|
9
|
+
|
10
|
+
namespace Checks {
|
11
|
+
export type Required = { required: true }
|
12
|
+
|
13
|
+
export type Schemas = {
|
14
|
+
schema: JSONSchema
|
15
|
+
}
|
16
|
+
export type Enum = { type: JSONSchemaTypeName; enum?: any[] }
|
17
|
+
export type Parameters = { in: string; required?: boolean }[]
|
18
|
+
export type SingleParameter<TParamType> = [{ in: TParamType; required?: true }]
|
19
|
+
export type Responses = { responses: any }
|
20
|
+
}
|
21
|
+
|
22
|
+
export type PathMap<TOAS extends OasTypes.OASDocument> = TOAS['paths']
|
23
|
+
|
24
|
+
interface ParamPropMap {
|
25
|
+
query: 'query'
|
26
|
+
path: 'params'
|
27
|
+
header: 'headers'
|
28
|
+
}
|
29
|
+
|
30
|
+
type JSONSchemaTypeName =
|
31
|
+
| 'string'
|
32
|
+
| 'number'
|
33
|
+
| 'integer'
|
34
|
+
| 'boolean'
|
35
|
+
| 'object'
|
36
|
+
| 'array'
|
37
|
+
| 'null'
|
38
|
+
|
39
|
+
type ParamObj<
|
40
|
+
TParameter extends {
|
41
|
+
name: string
|
42
|
+
},
|
43
|
+
> = TParameter extends Checks.Required ? {
|
44
|
+
[TName in TParameter['name']]: TParameter extends Checks.Schemas ? FromSchema<TParameter['schema']>
|
45
|
+
: TParameter extends Checks.Enum ? FromSchema<{
|
46
|
+
type: TParameter['type']
|
47
|
+
enum: TParameter['enum']
|
48
|
+
}>
|
49
|
+
: unknown
|
50
|
+
}
|
51
|
+
: {
|
52
|
+
[TName in TParameter['name']]?: TParameter extends Checks.Schemas ? FromSchema<TParameter['schema']>
|
53
|
+
: TParameter extends Checks.Enum ? FromSchema<{
|
54
|
+
type: TParameter['type']
|
55
|
+
enum: TParameter['enum']
|
56
|
+
}>
|
57
|
+
: unknown
|
58
|
+
}
|
59
|
+
|
60
|
+
interface ParamToRequestParam<TParameters extends Checks.Parameters> extends Fn {
|
61
|
+
return: this['arg0'] extends { name: string; in: infer TParamType }
|
62
|
+
// If there is any required parameter for this parameter type, make that parameter type required
|
63
|
+
? TParameters extends Checks.SingleParameter<TParamType> ? {
|
64
|
+
[
|
65
|
+
TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType]
|
66
|
+
: never
|
67
|
+
]: ParamObj<this['arg0']>
|
68
|
+
}
|
69
|
+
: {
|
70
|
+
[
|
71
|
+
TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType]
|
72
|
+
: never
|
73
|
+
]?: ParamObj<this['arg0']>
|
74
|
+
}
|
75
|
+
: {}
|
76
|
+
}
|
77
|
+
|
78
|
+
export type ParamMap<TParameters extends Checks.Parameters> = Pipe<
|
79
|
+
TParameters,
|
80
|
+
[Tuples.Map<ParamToRequestParam<TParameters>>, Tuples.ToIntersection]
|
81
|
+
>
|
82
|
+
|
83
|
+
export type MethodMap<
|
84
|
+
TOAS extends OasTypes.OASDocument,
|
85
|
+
TPath extends keyof PathMap<TOAS>,
|
86
|
+
> = PathMap<TOAS>[TPath]
|
87
|
+
|
88
|
+
export type StatusMap<
|
89
|
+
TOAS extends OasTypes.OASDocument,
|
90
|
+
TPath extends keyof PathMap<TOAS>,
|
91
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
92
|
+
> = MethodMap<TOAS, TPath>[TMethod] extends Checks.Responses ? MethodMap<TOAS, TPath>[TMethod]['responses']
|
93
|
+
: never
|