Skip to content

Commit 8ee08e2

Browse files
committed
fix(methods): recognizing return type if TS primitive or complex type
1 parent c4e8b97 commit 8ee08e2

File tree

13 files changed

+87
-78
lines changed

13 files changed

+87
-78
lines changed

src/helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Reference } from 'swagger-schema-official';
22
import { FileInfix } from './types';
33

44
export const BASIC_TS_TYPE_REGEX = /\b(?:string|number|integer|boolean)\b/;
5-
const BUILD_IN_TS_TYPE_REGEX = /^(?:string|number|integer|boolean|null|undefined|any|Object|Date|File|Blob)\b/;
5+
const BUILD_IN_TS_TYPE_REGEX = /^(?:string|number|integer|boolean|null|undefined|any|Object|Date|File|Blob)\b/i;
66

77
export function toCamelCase(text: string = '', lowerFirst: boolean = true): string {
88
text = removeDuplicateWords(text);
@@ -74,7 +74,11 @@ export function toTypescriptType(type: string | undefined): string {
7474
}
7575

7676
export function typeName(name: string = 'any', isArray: boolean = false): string {
77-
const type = BUILD_IN_TS_TYPE_REGEX.test(name) ? name : toCamelCase(name, false);
77+
const type = BASIC_TS_TYPE_REGEX.test(name)
78+
? /\binteger\b/.test(name)
79+
? 'number'
80+
: name
81+
: toCamelCase(name, false);
7882

7983
return `${type}${isArray ? '[]' : ''}`;
8084
}

src/parser.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
Property,
1616
Render,
1717
RenderFileName,
18-
ResponseType
1918
} from './types';
2019
import {
2120
BASIC_TS_TYPE_REGEX,
@@ -320,21 +319,27 @@ function defineInterface(schema: Schema, definitionKey: string): Definition {
320319
};
321320
}
322321

323-
function determineResponseType(response: Response): ResponseType {
322+
function determineResponseType(response: Response): {
323+
readonly type: string;
324+
readonly name?: string;
325+
} {
324326
if (response == null) { // TODO: check non-200 response codes
325327
logWarn('200 or 201 response not specified; `any` will be used');
326328
return {name: 'any', type: 'any'};
327329
}
328330

329331
const {schema} = response;
332+
330333
if (schema == null) {
331334
logWarn('200 or 201 response schema not specified; `any` will be used');
332335
return {name: 'any', type: 'any'};
333336
}
334337

335338
const nullable = (schema as Schema & { 'x-nullable'?: boolean })['x-nullable'] || false;
339+
336340
if (schema.type === 'array') {
337341
const {items} = schema;
342+
338343
if (items == null) {
339344
logWarn('`items` field not present; `any[]` will be used');
340345
return {name: 'any', type: 'any[]'};
@@ -356,6 +361,11 @@ function determineResponseType(response: Response): ResponseType {
356361
return {name, type};
357362
}
358363

364+
if (schema.type != null) {
365+
const type = nullable ? `${typeName(schema.type)} | null` : typeName(schema.type);
366+
return {name: type, type};
367+
}
368+
359369
return {name: 'any', type: 'any'};
360370
}
361371

src/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ export interface Method {
7070
readonly responseTypeName?: string; // method return type without prefix
7171
}
7272

73-
export interface ResponseType {
74-
readonly type: string;
75-
readonly name?: string;
76-
}
77-
7873
/**
7974
* Options for generator
8075
*/

tests/custom/api/api-client.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface APIClientInterface {
2020
page: number, // page number
2121
},
2222
requestHttpOptions?: HttpOptions
23-
): Observable<any>;
23+
): Observable<Object>;
2424

2525
getPetsId(
2626
args: {

tests/custom/api/api-client.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class APIClient implements APIClientInterface {
6666
page: number, // page number
6767
},
6868
requestHttpOptions?: HttpOptions
69-
): Observable<any> {
69+
): Observable<Object> {
7070
const path = `/itemModels`;
7171
const options: APIHttpOptions = {...this.options, ...requestHttpOptions};
7272

@@ -76,7 +76,7 @@ export class APIClient implements APIClientInterface {
7676
if ('page' in args) {
7777
options.params = options.params.set('page', String(args.page));
7878
}
79-
return this.sendRequest<any>('GET', path, options);
79+
return this.sendRequest<Object>('GET', path, options);
8080
}
8181

8282
getPetsId(

tests/esquare/api/api-client.interface.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ export interface APIClientInterface {
1111
body: models.AuthForm, // Structure entity object that needs to be added
1212
},
1313
requestHttpOptions?: HttpOptions
14-
): Observable<any>;
14+
): Observable<Object>;
1515

1616
authRef(
1717
requestHttpOptions?: HttpOptions
18-
): Observable<any>;
18+
): Observable<Object>;
1919

2020
passwordRestoreRequest(
2121
args: {
2222
body: models.RestoreForm, // Structure entity object that needs to be added
2323
},
2424
requestHttpOptions?: HttpOptions
25-
): Observable<any>;
25+
): Observable<Object>;
2626

2727
passwordRestoreEmailRequest(
2828
args: {
2929
body: models.RestoreRequestForm, // Structure entity object that needs to be added
3030
},
3131
requestHttpOptions?: HttpOptions
32-
): Observable<any>;
32+
): Observable<Object>;
3333

3434
passwordRestoreCheckRestoreGuid(
3535
args: {
3636
restoreGuid: string, // RestoreGuid for check
3737
},
3838
requestHttpOptions?: HttpOptions
39-
): Observable<any>;
39+
): Observable<Object>;
4040

4141
getAclList(
4242
requestHttpOptions?: HttpOptions
@@ -81,7 +81,7 @@ export interface APIClientInterface {
8181
order?: models.Order, // (optional) - asc - desc
8282
},
8383
requestHttpOptions?: HttpOptions
84-
): Observable<any>;
84+
): Observable<Object>;
8585

8686
/**
8787
* [Screenshot from design](http://prntscr.com/hywkd5)
@@ -107,7 +107,7 @@ export interface APIClientInterface {
107107
order?: models.Order, // (optional) - asc - desc
108108
},
109109
requestHttpOptions?: HttpOptions
110-
): Observable<any>;
110+
): Observable<Object>;
111111

112112
/**
113113
* [Screenshot from design](http://prntscr.com/i3ym4j)
@@ -130,7 +130,7 @@ export interface APIClientInterface {
130130
file: File, // file to upload
131131
},
132132
requestHttpOptions?: HttpOptions
133-
): Observable<any>;
133+
): Observable<number>;
134134

135135
/**
136136
* [Screenshot from design](http://prntscr.com/hy52hi)
@@ -187,7 +187,7 @@ export interface APIClientInterface {
187187
all?: boolean, // (optional) Indicator of downloading data(all or errors only)
188188
},
189189
requestHttpOptions?: HttpOptions
190-
): Observable<any>;
190+
): Observable<File>;
191191

192192
/**
193193
* [Screenshot from design](http://prntscr.com/hy57nj)
@@ -209,7 +209,7 @@ export interface APIClientInterface {
209209
id: number, // Id of current import
210210
},
211211
requestHttpOptions?: HttpOptions
212-
): Observable<any>;
212+
): Observable<File>;
213213

214214
/**
215215
* [Screenshot from design](http://prntscr.com/hy5ae7)
@@ -220,7 +220,7 @@ export interface APIClientInterface {
220220
id: number, // Id of current import
221221
},
222222
requestHttpOptions?: HttpOptions
223-
): Observable<any>;
223+
): Observable<File>;
224224

225225
/**
226226
* [Screenshot from design](http://prntscr.com/hy5aqq)
@@ -271,7 +271,7 @@ export interface APIClientInterface {
271271
order?: models.Order, // (optional) - asc - desc
272272
},
273273
requestHttpOptions?: HttpOptions
274-
): Observable<any>;
274+
): Observable<Object>;
275275

276276
/**
277277
* [Screenshot from design](http://prntscr.com/i4byyx)
@@ -287,7 +287,7 @@ export interface APIClientInterface {
287287
order?: models.Order, // (optional) - asc - desc
288288
},
289289
requestHttpOptions?: HttpOptions
290-
): Observable<any>;
290+
): Observable<Object>;
291291

292292
getUsersList(
293293
args: {
@@ -301,7 +301,7 @@ export interface APIClientInterface {
301301
unassignedFromRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9z16)
302302
},
303303
requestHttpOptions?: HttpOptions
304-
): Observable<any>;
304+
): Observable<Object>;
305305

306306
createUser(
307307
args: {
@@ -408,7 +408,7 @@ export interface APIClientInterface {
408408
order?: models.Order, // (optional) - asc - desc
409409
},
410410
requestHttpOptions?: HttpOptions
411-
): Observable<any>;
411+
): Observable<Object>;
412412

413413
/**
414414
* [Screenshot from design](http://prntscr.com/ibac47) |
@@ -440,7 +440,7 @@ export interface APIClientInterface {
440440
order?: models.Order, // (optional) - asc - desc
441441
},
442442
requestHttpOptions?: HttpOptions
443-
): Observable<any>;
443+
): Observable<Object>;
444444

445445
enableNotification(
446446
args: {
@@ -476,7 +476,7 @@ export interface APIClientInterface {
476476
body?: models.NotificationEditable,
477477
},
478478
requestHttpOptions?: HttpOptions
479-
): Observable<any>;
479+
): Observable<number>;
480480

481481
/**
482482
* [Screenshot from design](http://prntscr.com/ijzt2b)

0 commit comments

Comments
 (0)