Skip to content

Commit b775b44

Browse files
committed
feat(model): objects generated as key value maps
Signed-off-by: Vojtech Mašek <[email protected]>
1 parent 7214f2b commit b775b44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+99
-91
lines changed

src/helper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Reference } from 'swagger-schema-official';
12
import { FileInfix } from './types';
23

34
export const BASIC_TS_TYPE_REGEX = /\b(?:string|number|integer|boolean)\b/;
@@ -63,7 +64,7 @@ export function toTypescriptType(type: string | undefined): string {
6364
} else if (/^string|boolean$/i.test(type)) {
6465
return type.toLocaleLowerCase();
6566
} else if (/^object$/i.test(type)) {
66-
return 'any';
67+
return '{ [key: string]: any }';
6768
} else if (/^array$/i.test(type)) {
6869
logWarn('Support for nested arrays is limited, using any[] as type');
6970
return 'any[]';
@@ -106,3 +107,7 @@ export async function flattenAll<T>(promises: Promise<T[]>[]): Promise<T[]> {
106107
export function compareStringByKey<T>(key: keyof T): (a: T, b: T) => number {
107108
return (a, b) => a[key] && b[key] ? `${a[key]}`.localeCompare(`${b[key]}`) : -1;
108109
}
110+
111+
export function isReference(param: any | Reference): param is Reference { // tslint:disable-line no-any
112+
return !!(param as Reference).$ref;
113+
}

src/parser.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
typeName,
2929
logWarn,
3030
compareStringByKey,
31+
isReference,
3132
} from './helper';
3233

3334
interface Parameters {
@@ -84,17 +85,24 @@ export function determineDomain({schemes, host, basePath}: Swagger): string {
8485
return `${protocol}${domain}${base}`;
8586
}
8687

87-
function parseMethods({paths, security, parameters}: Swagger, swaggerTag?: string): Method[] {
88+
function parseMethods({paths, security, parameters, responses = {}}: Swagger, swaggerTag?: string): Method[] {
8889
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
8990

9091
return ([] as Method[]).concat(...Object.entries(paths)
9192
.map(([pathName, pathDef]: [string, Path]) =>
92-
Object.entries(pathDef).filter(([methodType, operation]) => { // tslint:disable-line:whitespace
93+
Object.entries(pathDef).filter(([methodType, operation]) => {
9394
const op = (<Operation>operation);
9495
return supportedMethods.indexOf(methodType.toUpperCase()) !== -1 && // skip unsupported methods
9596
(!swaggerTag || (op.tags && op.tags.includes(swaggerTag))); // if tag is defined take only paths including this tag
9697
}).map(([methodType, operation]: [string, Operation]) => {
97-
const responseType = determineResponseType((operation as any).responses);
98+
const okResponse: Response | Reference = operation.responses['200'] || operation.responses['201'];
99+
100+
const responseType = determineResponseType(
101+
okResponse && isReference(okResponse)
102+
? responses[dereferenceType(okResponse.$ref)]
103+
: okResponse
104+
);
105+
98106
return {
99107
hasJsonResponse: true,
100108
isSecure: security !== undefined || operation.security !== undefined,
@@ -305,15 +313,13 @@ function defineInterface(schema: Schema, definitionKey: string): Definition {
305313
};
306314
}
307315

308-
function determineResponseType(responses: { [responseName: string]: Response }): ResponseType {
309-
const okResponse = responses['200'] || responses['201'];
310-
311-
if (okResponse == null) { // TODO: check non-200 response codes
316+
function determineResponseType(response: Response): ResponseType {
317+
if (response == null) { // TODO: check non-200 response codes
312318
logWarn('200 or 201 response not specified; `any` will be used');
313319
return {name: 'any', type: 'any'};
314320
}
315321

316-
const {schema} = okResponse;
322+
const {schema} = response;
317323
if (schema == null) {
318324
logWarn('200 or 201 response schema not specified; `any` will be used');
319325
return {name: 'any', type: 'any'};

tests/gcloud-firestore/api/models/status.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@
8484
*/
8585
export interface Status {
8686
code: number; // The status code, which should be an enum value of google.rpc.Code.
87-
details: any[]; // A list of messages that carry the error details. There is a common set ofmessage types for APIs to use.
87+
details: { [key: string]: any }[]; // A list of messages that carry the error details. There is a common set ofmessage types for APIs to use.
8888
message: string; // A developer-facing error message, which should be in English. Anyuser-facing error message should be localized and sent in thegoogle.rpc.Status.details field, or localized by the client.
8989
}

tests/github/api/models/asset.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export interface Asset {
1010
size: number;
1111
state: string;
1212
updated_at: string;
13-
uploader: any;
13+
uploader: { [key: string]: any };
1414
url: string;
1515
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable */
22

33
export interface Branch {
4-
_links: any;
5-
commit: any;
4+
_links: { [key: string]: any };
5+
commit: { [key: string]: any };
66
name: string;
77
}

tests/github/api/models/commit-comments.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export interface CommitComments {
1111
position: number;
1212
updated_at: string; // ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ
1313
url: string;
14-
user: any;
14+
user: { [key: string]: any };
1515
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* tslint:disable */
22

33
export interface Commit {
4-
author: any;
5-
commit: any;
6-
committer: any;
7-
files: any[];
8-
parents: any[];
4+
author: { [key: string]: any };
5+
commit: { [key: string]: any };
6+
committer: { [key: string]: any };
7+
files: { [key: string]: any }[];
8+
parents: { [key: string]: any }[];
99
sha: string;
10-
stats: any;
10+
stats: { [key: string]: any };
1111
url: string;
1212
}

tests/github/api/models/compare-commits.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
export interface CompareCommits {
44
ahead_by: number;
5-
base_commit: any;
5+
base_commit: { [key: string]: any };
66
behind_by: number;
7-
commits: any[];
7+
commits: { [key: string]: any }[];
88
diff_url: string;
9-
files: any[];
9+
files: { [key: string]: any }[];
1010
html_url: string;
1111
patch_url: string;
1212
permalink_url: string;

tests/github/api/models/contents-path.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable */
22

33
export interface ContentsPath {
4-
_links: any;
4+
_links: { [key: string]: any };
55
content: string;
66
encoding: string;
77
git_url: string;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable */
22

33
export interface CreateFileBody {
4-
committer: any;
4+
committer: { [key: string]: any };
55
content: string;
66
message: string;
77
}

0 commit comments

Comments
 (0)