Skip to content

Commit 92c2344

Browse files
author
svolkov
committed
fix: complex types (oneOf, anyOf)
1 parent f3d5ac3 commit 92c2344

File tree

5 files changed

+157
-2
lines changed

5 files changed

+157
-2
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = {
3737
fs.writeFileSync(path.resolve(__dirname, output, `./${name}`), sourceFile, _.noop)
3838
console.log(`✔️ your typescript api file created in "${output}"`)
3939
}
40+
4041
resolve(sourceFile);
4142
}).catch(e =>{
4243
reject(e);

src/routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const parseRoutes = (routes, parsedSchemas) =>
4646

4747
return acc;
4848
}, {})
49+
4950
return [
5051
...routes,
5152
..._.map(requestInfoByMethodsMap, (requestInfo, method) => {

src/schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const complexTypeGetter = ({description, ...schema}) => parseSchema(schema, null
4040
const complexSchemaParsers = {
4141
'oneOf': (schema) => {
4242
// T1 | T2
43-
const combined = _.map(schema.allOf, complexTypeGetter);
43+
const combined = _.map(schema.oneOf, complexTypeGetter);
4444
return combined.join(' | ');
4545
},
4646
'allOf': (schema) => {
@@ -50,7 +50,7 @@ const complexSchemaParsers = {
5050
},
5151
'anyOf': (schema) => {
5252
// T1 | T2 | (T1 & T2)
53-
const combined = _.map(schema.allOf, complexTypeGetter);
53+
const combined = _.map(schema.anyOf, complexTypeGetter);
5454
return `${combined.join(' | ')}` + (combined.length > 1 ? ` | (${combined.join(' & ')})` : '');
5555
},
5656
// TODO
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
/*
5+
* ---------------------------------------------------------------
6+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7+
* ## ##
8+
* ## AUTHOR: acacode ##
9+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10+
* ---------------------------------------------------------------
11+
*/
12+
13+
14+
export interface PetByAge {
15+
age?: number;
16+
nickname?: string;
17+
}
18+
19+
export interface PetByType {
20+
pet_type?: "Cat" | "Dog";
21+
hunts?: boolean;
22+
}
23+
24+
export type RequestParams = Omit<RequestInit, "body" | "method"> & {
25+
secure?: boolean;
26+
}
27+
28+
type ApiConfig<SecurityDataType> = {
29+
baseUrl?: string,
30+
baseApiParams?: RequestParams,
31+
securityWorker?: (securityData: SecurityDataType) => RequestParams,
32+
}
33+
34+
35+
36+
export class Api<SecurityDataType> {
37+
38+
public baseUrl = "";
39+
public title = "Anyof Example";
40+
public version = "1.0.0";
41+
42+
private securityData: SecurityDataType = (null as any);
43+
private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any
44+
45+
private baseApiParams: RequestParams = {
46+
credentials: 'same-origin',
47+
headers: {
48+
'Content-Type': 'application/json'
49+
},
50+
redirect: 'follow',
51+
referrerPolicy: 'no-referrer',
52+
}
53+
54+
constructor({ baseUrl,baseApiParams,securityWorker, }: ApiConfig<SecurityDataType> = {}) {
55+
this.baseUrl = baseUrl || this.baseUrl;
56+
this.baseApiParams = baseApiParams || this.baseApiParams;
57+
this.securityWorker = securityWorker || this.securityWorker;
58+
}
59+
60+
public setSecurityData = (data: SecurityDataType) => {
61+
this.securityData = data
62+
}
63+
64+
65+
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
66+
return {
67+
...this.baseApiParams,
68+
...params,
69+
...(securityParams || {}),
70+
headers: {
71+
...(this.baseApiParams.headers || {}),
72+
...(params.headers || {}),
73+
...((securityParams && securityParams.headers) || {})
74+
}
75+
}
76+
}
77+
78+
private safeParseResponse = <T = any>(response: Response): Promise<T> =>
79+
response.json()
80+
.then(data => data)
81+
.catch(e => response.text);
82+
83+
public request = <T = any>(
84+
path: string,
85+
method: string,
86+
{ secure, ...params }: RequestParams = {},
87+
body?: any,
88+
secureByDefault?: boolean,
89+
): Promise<T> =>
90+
fetch(`${this.baseUrl}${path}`, {
91+
// @ts-ignore
92+
...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)),
93+
method,
94+
body: body ? JSON.stringify(body) : null,
95+
}).then(async response => {
96+
const data = await this.safeParseResponse<T>(response);
97+
if (!response.ok) throw data
98+
return data
99+
})
100+
101+
102+
103+
pets = {
104+
105+
106+
/**
107+
* @name patch
108+
* @request PATCH:/pets
109+
*/
110+
patch: (data: PetByAge | PetByType | (PetByAge & PetByType), params?: RequestParams) =>
111+
this.request<any>(`/pets`, "PATCH", params, data),
112+
}
113+
114+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Anyof Example
4+
version: 1.0.0
5+
paths:
6+
/pets:
7+
patch:
8+
requestBody:
9+
content:
10+
application/json:
11+
schema:
12+
anyOf:
13+
- $ref: '#/components/schemas/PetByAge'
14+
- $ref: '#/components/schemas/PetByType'
15+
responses:
16+
'200':
17+
description: Updated
18+
components:
19+
schemas:
20+
PetByAge:
21+
type: object
22+
properties:
23+
age:
24+
type: integer
25+
nickname:
26+
type: string
27+
required:
28+
- age
29+
30+
PetByType:
31+
type: object
32+
properties:
33+
pet_type:
34+
type: string
35+
enum: [Cat, Dog]
36+
hunts:
37+
type: boolean
38+
required:
39+
- pet_type

0 commit comments

Comments
 (0)