Skip to content

Commit 91dd581

Browse files
committed
- Cleanup of generator
1 parent 8149bd2 commit 91dd581

File tree

13 files changed

+118
-44
lines changed

13 files changed

+118
-44
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export interface OperationResponse {
22
code: number;
33
text: string;
4+
type: string;
5+
base: string;
6+
template: string | null;
7+
imports: string[];
48
}

src/openApi/v2/parser/getArrayType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export function getArrayType(items: OpenApiItems): ArrayType {
88
let itemsType = 'any';
99
let itemsBase = 'any';
1010
let itemsTemplate: string | null = null;
11-
const itemsImports: string[] = [];
11+
let itemsImports: string[] = [];
1212

1313
// If the parameter has a type than it can be a basic or generic type.
1414
if (items.type) {
1515
const itemsData: Type = getType(items.type);
1616
itemsType = itemsData.type;
1717
itemsBase = itemsData.base;
1818
itemsTemplate = itemsData.template;
19-
itemsImports.push(...itemsData.imports);
19+
itemsImports = [...itemsData.imports];
2020

2121
// If the parameter is an Array type, we check for the child type,
2222
// so we can create a typed array, otherwise this will be a "any[]".

src/openApi/v2/parser/getOperation.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { getComment } from './getComment';
1010
import { getOperationResponses } from './getOperationResponses';
1111
import { OperationParameters } from '../../../client/interfaces/OperationParameters';
1212
import { OperationResponse } from '../../../client/interfaces/OperationResponse';
13+
import { getOperationResponse } from './getOperationResponse';
14+
import { getOperationErrors } from './getOperationErrors';
15+
import { OperationError } from '../../../client/interfaces/OperationError';
1316

1417
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation): Operation {
1518
const serviceName = (op.tags && op.tags[0]) || 'Service';
@@ -53,11 +56,13 @@ export function getOperation(openApi: OpenApi, url: string, method: string, op:
5356
// Parse the operation responses.
5457
if (op.responses) {
5558
const responses: OperationResponse[] = getOperationResponses(openApi, op.responses);
56-
// const result: OperationResponse = getOperationResult(responses);
57-
// const errors = getOperationErrors(responses);
58-
// operation.imports.push(...result.imports);
59-
// operation.errors = errors;
60-
// operation.result = result.type;
59+
const response: OperationResponse = getOperationResponse(responses);
60+
const errors: OperationError[] = getOperationErrors(responses);
61+
operation.imports.push(...response.imports);
62+
operation.errors = errors;
63+
operation.result = response.type;
64+
65+
console.log(operation.result);
6166
}
6267

6368
return operation;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { OperationResponse } from '../../../client/interfaces/OperationResponse';
2+
3+
export function getOperationResponse(responses: OperationResponse[]): OperationResponse {
4+
let responseCode = 200;
5+
let responseText = '';
6+
let responseType = 'any';
7+
let responseBase = 'any';
8+
let responseTemplate: string | null = null;
9+
let responseImports: string[] = [];
10+
11+
// Fetch the first valid (2XX range) response code and return that type.
12+
const result: OperationResponse | undefined = responses.find(response => response.code && response.code >= 200 && response.code < 300);
13+
14+
if (result) {
15+
responseCode = result.code;
16+
responseText = result.text;
17+
responseType = result.type;
18+
responseBase = result.base;
19+
responseTemplate = result.template;
20+
responseImports = [...result.imports];
21+
}
22+
23+
return {
24+
code: responseCode,
25+
text: responseText,
26+
type: responseType,
27+
base: responseBase,
28+
template: responseTemplate,
29+
imports: responseImports,
30+
};
31+
}

src/openApi/v2/parser/getOperationResponses.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { OpenApiResponse } from '../interfaces/OpenApiResponse';
55
import { OpenApiReference } from '../interfaces/OpenApiReference';
66
import { getRef } from './getRef';
77
import { OpenApi } from '../interfaces/OpenApi';
8+
import { getType } from './getType';
9+
import { Type } from '../../../client/interfaces/Type';
10+
import { Schema } from '../../../client/interfaces/Schema';
11+
import { getSchema } from './getSchema';
12+
import { OpenApiSchema } from '../interfaces/OpenApiSchema';
813

914
export function getOperationResponses(openApi: OpenApi, responses: OpenApiResponses): OperationResponse[] {
1015
const result: OperationResponse[] = [];
@@ -17,16 +22,35 @@ export function getOperationResponses(openApi: OpenApi, responses: OpenApiRespon
1722
const response: OpenApiResponse = getRef<OpenApiResponse>(openApi, responseOrReference);
1823
const responseCode: number | null = getOperationResponseCode(code);
1924
const responseText: string = response.description || '';
25+
let responseType = 'any';
26+
let responseBase = 'any';
27+
let responseTemplate: string | null = null;
28+
let responseImports: string[] = [];
2029

21-
// TODO:
2230
if (response.schema) {
23-
console.log('response.schema', response.schema);
31+
if (response.schema.$ref) {
32+
const schemaReference: Type = getType(response.schema.$ref);
33+
responseType = schemaReference.type;
34+
responseBase = schemaReference.base;
35+
responseTemplate = schemaReference.template;
36+
responseImports = [...schemaReference.imports];
37+
} else {
38+
const schema: Schema = getSchema(openApi, response.schema as OpenApiSchema);
39+
responseType = schema.type;
40+
responseBase = schema.base;
41+
responseTemplate = schema.template;
42+
responseImports = [...schema.imports];
43+
}
2444
}
2545

2646
if (responseCode) {
2747
result.push({
2848
code: responseCode,
2949
text: responseText,
50+
type: responseType,
51+
base: responseBase,
52+
template: responseTemplate,
53+
imports: responseImports,
3054
});
3155
}
3256
}

src/openApi/v2/parser/getOperationResult.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/openApi/v2/parser/getParameter.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { ArrayType } from '../../../client/interfaces/ArrayType';
99
import { getEnumType } from './getEnumType';
1010
import { getEnumTypeFromDescription } from './getEnumTypeFromDescription';
1111
import { getComment } from './getComment';
12+
import { getSchema } from './getSchema';
13+
import { Schema } from '../../../client/interfaces/Schema';
14+
import { OpenApiSchema } from '../interfaces/OpenApiSchema';
1215

1316
export function getParameter(openApi: OpenApi, parameter: OpenApiParameter): Parameter {
1417
let parameterType = 'any';
@@ -35,10 +38,24 @@ export function getParameter(openApi: OpenApi, parameter: OpenApiParameter): Par
3538
}
3639
}
3740

38-
// If this parameter has a schema, then we should treat it as an embedded parameter.
39-
// We can just parse the schema name ($ref) and use that as the parameter type.
41+
// If this parameter has a schema, then we need to check two things:
42+
// if this is a reference then the parameter is just the 'name' of
43+
// this reference type. Otherwise it might be a complex schema and
44+
// then we need to parse the schema!
4045
if (parameter.schema) {
41-
// TODO: console.log('parameter.schema', parameter.schema);
46+
if (parameter.schema.$ref) {
47+
const schemaReference: Type = getType(parameter.schema.$ref);
48+
parameterType = schemaReference.type;
49+
parameterBase = schemaReference.base;
50+
parameterTemplate = schemaReference.template;
51+
parameterImports.push(...schemaReference.imports);
52+
} else {
53+
const schema: Schema = getSchema(openApi, parameter.schema as OpenApiSchema);
54+
parameterType = schema.type;
55+
parameterBase = schema.base;
56+
parameterTemplate = schema.template;
57+
parameterImports.push(...schema.imports);
58+
}
4259
}
4360

4461
// If the param is a enum then return the values as an inline type.

src/openApi/v2/parser/getSchema.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { OpenApi } from '../interfaces/OpenApi';
2+
import { Schema } from '../../../client/interfaces/Schema';
3+
import { OpenApiSchema } from '../interfaces/OpenApiSchema';
4+
5+
export function getSchema(openApi: OpenApi, schema: OpenApiSchema): Schema {
6+
return {
7+
type: 'todo',
8+
base: 'todo',
9+
template: null,
10+
imports: [],
11+
};
12+
}

src/openApi/v2/parser/getType.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import { getMappedType, hasMappedType } from './getMappedType';
77
* @param value String value like "integer" or "Link[Model]".
88
* @param template Optional template class from parent (needed to process generics)
99
*/
10-
export function getType(value: string, template: string | null = null): Type {
10+
export function getType(value: string | undefined, template: string | null = null): Type {
1111
let propertyType = 'any';
1212
let propertyBase = 'any';
1313
let propertyTemplate: string | null = null;
1414
let propertyImports: string[] = [];
1515

1616
// Remove definitions prefix and cleanup string.
17-
const valueTrimmed: string = stripNamespace(value || '');
17+
const valueClean: string = stripNamespace(value || '');
1818

1919
// Check of we have an Array type or generic type, for instance: "Link[Model]".
20-
if (/\[.*\]$/g.test(valueTrimmed)) {
21-
const matches: RegExpMatchArray | null = valueTrimmed.match(/(.*?)\[(.*)\]$/);
20+
if (/\[.*\]$/g.test(valueClean)) {
21+
const matches: RegExpMatchArray | null = valueClean.match(/(.*?)\[(.*)\]$/);
2222
if (matches) {
2323
// Both of the types can be complex types so parse each of them.
2424
const match1: Type = getType(matches[1]);
@@ -46,14 +46,14 @@ export function getType(value: string, template: string | null = null): Type {
4646
propertyImports.push(...match1.imports);
4747
propertyImports.push(...match2.imports);
4848
}
49-
} else if (hasMappedType(valueTrimmed)) {
50-
const mapped: string = getMappedType(valueTrimmed);
49+
} else if (hasMappedType(valueClean)) {
50+
const mapped: string = getMappedType(valueClean);
5151
propertyType = mapped;
5252
propertyBase = mapped;
5353
} else {
54-
propertyType = valueTrimmed;
55-
propertyBase = valueTrimmed;
56-
propertyImports.push(valueTrimmed);
54+
propertyType = valueClean;
55+
propertyBase = valueClean;
56+
propertyImports.push(valueClean);
5757
}
5858

5959
// If the property that we found matched the parent template class

src/templates/typescript/core/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Result } from './Result';
1414
* @param options Request method options.
1515
* @returns Result object (see above)
1616
*/
17-
export async function request<T>(options: Readonly<RequestOptions>): Promise<Result<T>> {
17+
export async function request<T = any>(options: Readonly<RequestOptions>): Promise<Result<T>> {
1818
// Create the request URL
1919
let url: string = `${OpenAPI.BASE}${options.path}`;
2020

src/templates/typescript/core/requestUsingFetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Result } from './Result';
1111
* @param url The url to request.
1212
* @param request The request object, containing method, headers, body, etc.
1313
*/
14-
export async function requestUsingFetch<T>(url: string, request: Readonly<RequestInit>): Promise<Result<T>> {
14+
export async function requestUsingFetch<T = any>(url: string, request: Readonly<RequestInit>): Promise<Result<T>> {
1515
// Fetch response using fetch API.
1616
const response: Response = await fetch(url, request);
1717

src/templates/typescript/core/requestUsingXHR.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { isSuccess } from './isSuccess';
1313
* @param url The url to request.
1414
* @param request The request object, containing method, headers, body, etc.
1515
*/
16-
export async function requestUsingXHR<T>(url: string, request: Readonly<RequestInit>): Promise<Result<T>> {
16+
export async function requestUsingXHR<T = any>(url: string, request: Readonly<RequestInit>): Promise<Result<T>> {
1717
return new Promise(resolve => {
1818
const xhr: XMLHttpRequest = new XMLHttpRequest();
1919

src/templates/typescript/service.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class {{{name}}} {
4141
{{/each}}
4242
{{/if}}
4343

44-
const result: Result<{{{result}}}> = await request<{{{result}}}>({
44+
const result: Result = await request({
4545
method: '{{{method}}}',
4646
path: `{{{path}}}`,{{#if parametersHeader}}
4747
headers: {

0 commit comments

Comments
 (0)