@kubb/oas 4.12.10 → 4.12.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,376 +0,0 @@
1
- import * as OasTypes from "oas/types";
2
- import { HttpMethods as HttpMethod } from "oas/types";
3
- import { Booleans, Call, Fn, Objects, Pipe, Strings, Tuples } from "hotscript";
4
- import { Object as Object$1 } from "ts-toolbelt";
5
- import { FromSchema, JSONSchema } from "json-schema-to-ts";
6
- import { Operation as Operation$1 } from "oas/operation";
7
- import { OpenAPIV3 as OpenAPIV3$1, OpenAPIV3_1 as OpenAPIV3_1$1 } from "openapi-types";
8
-
9
- //#region src/infer/infer.d.ts
10
- type Checks$5 = {
11
- AllOFf: {
12
- allOf: any[];
13
- };
14
- Object: {
15
- type: 'object';
16
- properties: any;
17
- };
18
- Properties: {
19
- properties: any;
20
- };
21
- PropertiesRequired: {
22
- properties: Record<string, any>;
23
- required: string[];
24
- };
25
- };
26
- type FixAdditionalPropertiesForAllOf<T> = T extends Checks$5['AllOFf'] ? Omit<T, 'allOf'> & {
27
- allOf: Call<Tuples.Map<Objects.Omit<'additionalProperties'>>, T['allOf']>;
28
- } : T;
29
- type FixMissingAdditionalProperties<T> = T extends Checks$5['Object'] ? Omit<T, 'additionalProperties'> & {
30
- additionalProperties: false;
31
- } : T;
32
- type FixMissingTypeObject<T> = T extends Checks$5['Properties'] ? T & {
33
- type: 'object';
34
- } : T;
35
- type FixExtraRequiredFields<T> = T extends Checks$5['PropertiesRequired'] ? Omit<T, 'required'> & {
36
- required: Call<Tuples.Filter<Booleans.Extends<keyof T['properties']>>, T['required']>;
37
- } : T;
38
- type FixJSONSchema<T> = FixAdditionalPropertiesForAllOf<FixMissingAdditionalProperties<FixMissingTypeObject<FixExtraRequiredFields<T>>>>;
39
- type Mutable<Type> = FixJSONSchema<{ -readonly [Key in keyof Type]: Mutable<Type[Key]> }>;
40
- type RefToPath<T extends string> = T extends `#/${infer Ref}` ? Call<Strings.Split<'/'>, Ref> : never;
41
- type ResolveRef<TObj, TRef extends string> = {
42
- $id: TRef;
43
- } & Object$1.Path<TObj, RefToPath<TRef>>;
44
- type ResolveRefInObj<T, TBase> = T extends {
45
- $ref: infer Ref;
46
- } ? (Ref extends string ? ResolveRef<TBase, Ref> : T) : T;
47
- type ResolveRefsInObj<T, TBase = T> = { [K in keyof T]: ResolveRefsInObj<ResolveRefInObj<T[K], TBase>, TBase> };
48
- type Infer<TOas> = Mutable<ResolveRefsInObj<TOas>>;
49
- //#endregion
50
- //#region src/types.d.ts
51
- type contentType = 'application/json' | (string & {});
52
- type SchemaObject$1 = OasTypes.SchemaObject & {
53
- 'x-nullable'?: boolean;
54
- $ref?: string;
55
- };
56
- declare const HttpMethods: {
57
- GET: "get";
58
- POST: "post";
59
- PUT: "put";
60
- PATCH: "patch";
61
- DELETE: "delete";
62
- HEAD: "head";
63
- OPTIONS: "options";
64
- TRACE: "trace";
65
- };
66
- //#endregion
67
- //#region src/infer/mappers.d.ts
68
- type Checks$4<TParamType$1 = never> = {
69
- Required: {
70
- required: true;
71
- };
72
- Schemas: {
73
- schema: JSONSchema;
74
- };
75
- Enum: {
76
- type: JSONSchemaTypeName;
77
- enum?: any[];
78
- };
79
- Parameters: {
80
- in: string;
81
- required?: boolean;
82
- }[];
83
- SingleParameter: [{
84
- in: TParamType$1;
85
- required?: true;
86
- }];
87
- Responses: {
88
- responses: any;
89
- };
90
- };
91
- type PathMap<TOAS extends OasTypes.OASDocument> = TOAS['paths'];
92
- interface ParamPropMap {
93
- query: 'query';
94
- path: 'params';
95
- header: 'headers';
96
- }
97
- type JSONSchemaTypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
98
- type ParamObj<TParameter extends {
99
- name: string;
100
- }> = TParameter extends Checks$4['Required'] ? { [TName in TParameter['name']]: TParameter extends Checks$4['Schemas'] ? FromSchema<TParameter['schema']> : TParameter extends Checks$4['Enum'] ? FromSchema<{
101
- type: TParameter['type'];
102
- enum: TParameter['enum'];
103
- }> : unknown } : { [TName in TParameter['name']]?: TParameter extends Checks$4['Schemas'] ? FromSchema<TParameter['schema']> : TParameter extends Checks$4['Enum'] ? FromSchema<{
104
- type: TParameter['type'];
105
- enum: TParameter['enum'];
106
- }> : unknown };
107
- interface ParamToRequestParam<TParameters extends Checks$4['Parameters']> extends Fn {
108
- return: this['arg0'] extends {
109
- name: string;
110
- in: infer TParamType;
111
- } ? TParameters extends Checks$4<TParamType>['SingleParameter'] ? { [TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]: ParamObj<this['arg0']> } : { [TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]?: ParamObj<this['arg0']> } : {};
112
- }
113
- type ParamMap<TParameters extends Checks$4['Parameters']> = Pipe<TParameters, [Tuples.Map<ParamToRequestParam<TParameters>>, Tuples.ToIntersection]>;
114
- type MethodMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>> = PathMap<TOAS>[TPath];
115
- type StatusMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>> = MethodMap<TOAS, TPath>[TMethod] extends Checks$4['Responses'] ? MethodMap<TOAS, TPath>[TMethod]['responses'] : never;
116
- //#endregion
117
- //#region src/infer/model.d.ts
118
- type Checks$3<TName extends string | number | symbol = never> = {
119
- ModelWithSchemas: {
120
- components: {
121
- schemas: Record<string, JSONSchema>;
122
- };
123
- };
124
- ModelWithSchemasNamed: {
125
- components: {
126
- schemas: { [TModelName in TName]: JSONSchema };
127
- };
128
- };
129
- ModelWithDefinitions: {
130
- definitions: Record<string, JSONSchema>;
131
- };
132
- ModelWithDefinitionsNamed: {
133
- definitions: { [TModelName in TName]: JSONSchema };
134
- };
135
- };
136
- type Model<TOAS extends OasTypes.OASDocument, TName extends (TOAS extends Checks$3['ModelWithSchemas'] ? keyof TOAS['components']['schemas'] : TOAS extends Checks$3['ModelWithDefinitions'] ? keyof TOAS['definitions'] : never)> = TOAS extends Checks$3<TName>['ModelWithSchemasNamed'] ? FromSchema<TOAS['components']['schemas'][TName]> : TOAS extends Checks$3<TName>['ModelWithDefinitionsNamed'] ? FromSchema<TOAS['definitions'][TName]> : never;
137
- //#endregion
138
- //#region src/infer/security.d.ts
139
- type Checks$2 = {
140
- Security: {
141
- security: {
142
- [key: string]: any;
143
- }[];
144
- };
145
- AuthParams: {
146
- Basic: {
147
- type: 'http';
148
- scheme: 'basic';
149
- } | {
150
- type: 'basic';
151
- };
152
- Bearer: {
153
- type: 'http';
154
- scheme: 'bearer';
155
- } | {
156
- type: 'bearer';
157
- };
158
- OAuth2: {
159
- type: 'oauth2';
160
- };
161
- ApiKey: {
162
- type: 'apiKey';
163
- in: 'header';
164
- };
165
- };
166
- AuthName: {
167
- Basic: `basic${string}`;
168
- Bearer: `bearer${string}`;
169
- OAuth2: `oauth${string}`;
170
- };
171
- };
172
- type SecuritySchemeName<T extends Checks$2['Security']> = Call<Tuples.Map<Objects.Keys>, T['security']>[number];
173
- declare namespace AuthParams {
174
- type Basic<TSecurityScheme$1> = TSecurityScheme$1 extends Checks$2['AuthParams']['Basic'] ? {
175
- headers: {
176
- /**
177
- * `Authorization` header is required for basic authentication
178
- * @see https://en.wikipedia.org/wiki/Basic_access_authentication
179
- *
180
- * It contains the word `Basic` followed by a space and a base64-encoded string `username:password`
181
- *
182
- * @example
183
- * ```
184
- * Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
185
- * ```
186
- */
187
- Authorization: `Basic ${string}`;
188
- };
189
- } : {};
190
- type Bearer<TSecurityScheme$1> = TSecurityScheme$1 extends Checks$2['AuthParams']['Bearer'] ? {
191
- /**
192
- * `Authorization` header is required for bearer authentication
193
- * @see https://swagger.io/docs/specification/authentication/bearer-authentication/
194
- */
195
- headers: {
196
- /**
197
- * It contains the word `Bearer` followed by a space and the token
198
- *
199
- * @example
200
- * ```
201
- * Authorization: Bearer {token}
202
- * ```
203
- */
204
- Authorization: `Bearer ${string}`;
205
- };
206
- } : {};
207
- type ApiKey<TSecurityScheme$1> = TSecurityScheme$1 extends Checks$2['AuthParams']['ApiKey'] & {
208
- name: infer TApiKeyHeaderName;
209
- } ? {
210
- headers: { [THeaderName in TApiKeyHeaderName extends string ? TApiKeyHeaderName : never]: string };
211
- } : TSecurityScheme$1 extends {
212
- type: 'apiKey';
213
- in: 'query';
214
- name: infer TApiKeyQueryName;
215
- } ? {
216
- query: { [TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string };
217
- } : {};
218
- type OAuth2<TSecurityScheme$1> = TSecurityScheme$1 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$1> = AuthParams.Basic<TSecurityScheme$1> & AuthParams.Bearer<TSecurityScheme$1> & AuthParams.ApiKey<TSecurityScheme$1> & AuthParams.OAuth2<TSecurityScheme$1>;
232
- type SecurityParamsBySecurityRef<TOAS, TSecurityObj> = TSecurityObj extends Checks$2['Security'] ? TOAS extends {
233
- components: {
234
- securitySchemes: { [TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme };
235
- };
236
- } | {
237
- securityDefinitions: { [TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme };
238
- } ? OASSecurityParams<TSecurityScheme> : SecuritySchemeName<TSecurityObj> extends Checks$2['AuthName']['Basic'] ? AuthParams.Basic<{
239
- type: 'http';
240
- scheme: 'basic';
241
- }> : SecuritySchemeName<TSecurityObj> extends Checks$2['AuthName']['Bearer'] ? AuthParams.Bearer<{
242
- type: 'http';
243
- scheme: 'bearer';
244
- }> : SecuritySchemeName<TSecurityObj> extends Checks$2['AuthName']['OAuth2'] ? AuthParams.OAuth2<{
245
- type: 'oauth2';
246
- }> : {} : {};
247
- //#endregion
248
- //#region src/infer/requestParams.d.ts
249
- type TupleToUnion<T> = T extends any[] ? T[number] : never;
250
- type SplitByDelimiter<T extends string, D extends string> = T extends `${infer P}${D}${infer Q}` ? [P, ...SplitByDelimiter<Q, D>] : [T];
251
- type Checks$1 = {
252
- RequestBodyJson: {
253
- requestBody: {
254
- content: {
255
- 'application/json': {
256
- schema: JSONSchema;
257
- };
258
- };
259
- };
260
- };
261
- RequestBodyFormData: {
262
- requestBody: {
263
- content: {
264
- 'multipart/form-data': {
265
- schema: JSONSchema;
266
- };
267
- };
268
- };
269
- };
270
- RequestBodyFormEncoded: {
271
- requestBody: {
272
- content: {
273
- 'application/x-www-form-urlencoded': {
274
- schema: JSONSchema;
275
- };
276
- };
277
- };
278
- };
279
- Parameters: {
280
- parameters: {
281
- name: string;
282
- in: string;
283
- }[];
284
- };
285
- PathBrackets: `${string}{${string}}${string}`;
286
- PathPattern: `${string}:${string}${string}`;
287
- Required: {
288
- required: true;
289
- };
290
- };
291
- type ExtractPathParamsWithPattern<TPath extends string> = Pipe<TPath, [Strings.Split<'/'>, Tuples.Filter<Strings.StartsWith<':'>>, Tuples.Map<Strings.Trim<':'>>, Tuples.ToUnion]>;
292
- type IsPathParameter<T extends string> = T extends `{${infer U}}` ? U : never;
293
- type ExtractPathParameters<T extends any[]> = { [K in keyof T]: IsPathParameter<T[K]> };
294
- type ExtractSegments<TPath extends string> = SplitByDelimiter<TPath, '/'>;
295
- type ExtractSubSegments<T extends any[]> = { [K in keyof T]: SplitByDelimiter<T[K], ';'> };
296
- type ExtractPathParamsWithBrackets<TPath extends string> = TupleToUnion<ExtractPathParameters<ExtractSubSegments<ExtractSegments<TPath>>[number]>>;
297
- 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'] ? {
298
- /**
299
- * The request body in JSON is required for this request.
300
- *
301
- * The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
302
- */
303
- json: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>;
304
- } : {
305
- /**
306
- * The request body in JSON is optional for this request.
307
- *
308
- * The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
309
- */
310
- json?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>;
311
- } : MethodMap<TOAS, TPath>[TMethod] extends Checks$1['RequestBodyFormData'] ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks$1['Required'] ? {
312
- /**
313
- * The request body in multipart/form-data is required for this request.
314
- *
315
- * The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
316
- */
317
- formData: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>;
318
- } : {
319
- /**
320
- * The request body in multipart/form-data is optional for this request.
321
- *
322
- * The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
323
- */
324
- formData?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>;
325
- } : MethodMap<TOAS, TPath>[TMethod] extends Checks$1['RequestBodyFormEncoded'] ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks$1['Required'] ? {
326
- /**
327
- * The request body in application/x-www-form-urlencoded is required for this request.
328
- *
329
- * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
330
- */
331
- formUrlEncoded: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>;
332
- } : {
333
- /**
334
- * The request body in application/x-www-form-urlencoded is optional for this request.
335
- *
336
- * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
337
- */
338
- formUrlEncoded?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>;
339
- } : {}) & (MethodMap<TOAS, TPath>[TMethod] extends Checks$1['Parameters'] ? ParamMap<MethodMap<TOAS, TPath>[TMethod]['parameters']> : {}) &
340
- // If there is any parameters defined in path but not in the parameters array, we should add them to the params
341
- (TPath extends Checks$1['PathBrackets'] ? {
342
- /**
343
- * Parameters defined in the path are required for this request.
344
- *
345
- * The value of `params` will be used to replace the path parameters.
346
- *
347
- * For example if path is `/todos/{id}` and `params` is `{ id: '1' }`, the path will be `/todos/1`
348
- */
349
- params: Record<ExtractPathParamsWithBrackets<TPath>, string | number | bigint | boolean>;
350
- } : {}) & (TPath extends Checks$1['PathPattern'] ? {
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<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>;
359
- } : {}) &
360
- // Respect security definitions in path object
361
- SecurityParamsBySecurityRef<TOAS, MethodMap<TOAS, TPath>[TMethod]> &
362
- // Respect global security definitions
363
- SecurityParamsBySecurityRef<TOAS, TOAS>;
364
- //#endregion
365
- //#region src/infer/response.d.ts
366
- type Checks = {
367
- Content: {
368
- content: any;
369
- };
370
- };
371
- 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'];
372
- 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'];
373
- 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>>;
374
- //#endregion
375
- export { PathMap as a, HttpMethods as c, OpenAPIV3_1$1 as d, Operation$1 as f, Infer as h, MethodMap as i, OasTypes as l, contentType as m, RequestParams as n, StatusMap as o, SchemaObject$1 as p, Model as r, HttpMethod as s, Response as t, OpenAPIV3$1 as u };
376
- //# sourceMappingURL=index-CT3Sz4Y8.d.ts.map
package/dist/infer.cjs DELETED
@@ -1 +0,0 @@
1
- const require_index = require('./index.cjs');
package/dist/infer.d.cts DELETED
@@ -1,2 +0,0 @@
1
- import { a as PathMap, h as Infer, i as MethodMap, n as RequestParams, o as StatusMap, r as Model, t as Response } from "./index-BuoKUWab.cjs";
2
- export { Infer, MethodMap, Model, PathMap, RequestParams, Response, StatusMap };
package/dist/infer.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { a as PathMap, h as Infer, i as MethodMap, n as RequestParams, o as StatusMap, r as Model, t as Response } from "./index-CT3Sz4Y8.js";
2
- export { Infer, MethodMap, Model, PathMap, RequestParams, Response, StatusMap };
package/dist/infer.js DELETED
@@ -1 +0,0 @@
1
- export { };
@@ -1,7 +0,0 @@
1
- // based on https://github.com/ardatan/feTS/tree/master
2
-
3
- export type { Infer } from './infer.ts'
4
- export type { MethodMap, PathMap, StatusMap } from './mappers.ts'
5
- export type { Model } from './model.ts'
6
- export type { RequestParams } from './requestParams.ts'
7
- export type { Response } from './response.ts'
@@ -1,51 +0,0 @@
1
- import type { Booleans, Call, Objects, Strings, Tuples } from 'hotscript'
2
- import type { Object } from 'ts-toolbelt'
3
-
4
- type Checks = {
5
- AllOFf: { allOf: any[] }
6
- Object: {
7
- type: 'object'
8
- properties: any
9
- }
10
- Properties: { properties: any }
11
- PropertiesRequired: {
12
- properties: Record<string, any>
13
- required: string[]
14
- }
15
- }
16
-
17
- type FixAdditionalPropertiesForAllOf<T> = T extends Checks['AllOFf']
18
- ? Omit<T, 'allOf'> & {
19
- allOf: Call<Tuples.Map<Objects.Omit<'additionalProperties'>>, T['allOf']>
20
- }
21
- : T
22
-
23
- type FixMissingAdditionalProperties<T> = T extends Checks['Object'] ? Omit<T, 'additionalProperties'> & { additionalProperties: false } : T
24
- type FixMissingTypeObject<T> = T extends Checks['Properties'] ? T & { type: 'object' } : T
25
-
26
- type FixExtraRequiredFields<T> = T extends Checks['PropertiesRequired']
27
- ? Omit<T, 'required'> & {
28
- required: Call<Tuples.Filter<Booleans.Extends<keyof T['properties']>>, T['required']>
29
- }
30
- : T
31
-
32
- // Later suggest using json-machete
33
- type FixJSONSchema<T> = FixAdditionalPropertiesForAllOf<FixMissingAdditionalProperties<FixMissingTypeObject<FixExtraRequiredFields<T>>>>
34
-
35
- type Mutable<Type> = FixJSONSchema<{
36
- -readonly [Key in keyof Type]: Mutable<Type[Key]>
37
- }>
38
-
39
- type RefToPath<T extends string> = T extends `#/${infer Ref}` ? Call<Strings.Split<'/'>, Ref> : never
40
-
41
- type ResolveRef<TObj, TRef extends string> = {
42
- $id: TRef
43
- } & Object.Path<TObj, RefToPath<TRef>>
44
-
45
- type ResolveRefInObj<T, TBase> = T extends { $ref: infer Ref } ? (Ref extends string ? ResolveRef<TBase, Ref> : T) : T
46
-
47
- type ResolveRefsInObj<T, TBase = T> = {
48
- [K in keyof T]: ResolveRefsInObj<ResolveRefInObj<T[K], TBase>, TBase>
49
- }
50
-
51
- export type Infer<TOas> = Mutable<ResolveRefsInObj<TOas>>
@@ -1,75 +0,0 @@
1
- import type { Fn, Pipe, Tuples } from 'hotscript'
2
- import type { FromSchema, JSONSchema } from 'json-schema-to-ts'
3
- import type { OasTypes } from '../types.ts'
4
-
5
- type Checks<TParamType = never> = {
6
- Required: { required: true }
7
-
8
- Schemas: {
9
- schema: JSONSchema
10
- }
11
- Enum: { type: JSONSchemaTypeName; enum?: any[] }
12
- Parameters: { in: string; required?: boolean }[]
13
- SingleParameter: [{ in: TParamType; required?: true }]
14
- Responses: { responses: any }
15
- }
16
-
17
- export type PathMap<TOAS extends OasTypes.OASDocument> = TOAS['paths']
18
-
19
- interface ParamPropMap {
20
- query: 'query'
21
- path: 'params'
22
- header: 'headers'
23
- }
24
-
25
- type JSONSchemaTypeName = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null'
26
-
27
- type ParamObj<
28
- TParameter extends {
29
- name: string
30
- },
31
- > = TParameter extends Checks['Required']
32
- ? {
33
- [TName in TParameter['name']]: TParameter extends Checks['Schemas']
34
- ? FromSchema<TParameter['schema']>
35
- : TParameter extends Checks['Enum']
36
- ? FromSchema<{
37
- type: TParameter['type']
38
- enum: TParameter['enum']
39
- }>
40
- : unknown
41
- }
42
- : {
43
- [TName in TParameter['name']]?: TParameter extends Checks['Schemas']
44
- ? FromSchema<TParameter['schema']>
45
- : TParameter extends Checks['Enum']
46
- ? FromSchema<{
47
- type: TParameter['type']
48
- enum: TParameter['enum']
49
- }>
50
- : unknown
51
- }
52
-
53
- interface ParamToRequestParam<TParameters extends Checks['Parameters']> extends Fn {
54
- return: this['arg0'] extends { name: string; in: infer TParamType }
55
- ? // If there is any required parameter for this parameter type, make that parameter type required
56
- TParameters extends Checks<TParamType>['SingleParameter']
57
- ? {
58
- [TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]: ParamObj<this['arg0']>
59
- }
60
- : {
61
- [TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType] : never]?: ParamObj<this['arg0']>
62
- }
63
- : {}
64
- }
65
-
66
- export type ParamMap<TParameters extends Checks['Parameters']> = Pipe<TParameters, [Tuples.Map<ParamToRequestParam<TParameters>>, Tuples.ToIntersection]>
67
-
68
- export type MethodMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>> = PathMap<TOAS>[TPath]
69
-
70
- export type StatusMap<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>> = MethodMap<
71
- TOAS,
72
- TPath
73
- >[TMethod] extends Checks['Responses']
74
- ? MethodMap<TOAS, TPath>[TMethod]['responses']
75
- : never
@@ -1,38 +0,0 @@
1
- import type { FromSchema, JSONSchema } from 'json-schema-to-ts'
2
- import type { OasTypes } from '../types.ts'
3
-
4
- type Checks<TName extends string | number | symbol = never> = {
5
- ModelWithSchemas: {
6
- components: {
7
- schemas: Record<string, JSONSchema>
8
- }
9
- }
10
- ModelWithSchemasNamed: {
11
- components: {
12
- schemas: {
13
- [TModelName in TName]: JSONSchema
14
- }
15
- }
16
- }
17
- ModelWithDefinitions: {
18
- definitions: Record<string, JSONSchema>
19
- }
20
- ModelWithDefinitionsNamed: {
21
- definitions: {
22
- [TModelName in TName]: JSONSchema
23
- }
24
- }
25
- }
26
-
27
- export type Model<
28
- TOAS extends OasTypes.OASDocument,
29
- TName extends TOAS extends Checks['ModelWithSchemas']
30
- ? keyof TOAS['components']['schemas']
31
- : TOAS extends Checks['ModelWithDefinitions']
32
- ? keyof TOAS['definitions']
33
- : never,
34
- > = TOAS extends Checks<TName>['ModelWithSchemasNamed']
35
- ? FromSchema<TOAS['components']['schemas'][TName]>
36
- : TOAS extends Checks<TName>['ModelWithDefinitionsNamed']
37
- ? FromSchema<TOAS['definitions'][TName]>
38
- : never