Skip to content

Commit aec9338

Browse files
committed
- Working on v2 service generation
1 parent ec30f28 commit aec9338

Some content is hidden

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

60 files changed

+34516
-246
lines changed

src/client/interfaces/Model.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export interface Model {
55
name: string;
66
base: string;
77
type: string;
8-
template: string;
9-
description: string | null;
10-
extends: string | null;
8+
template?: string;
9+
description?: string;
10+
extends: string[];
1111
imports: string[];
1212
properties: ModelProperty[];
1313
enums: ModelEnum[];

src/client/interfaces/ModelEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ModelEnumProperty } from './ModelEnumProperty';
1+
import { ModelEnumValue } from './ModelEnumValue';
22

33
export interface ModelEnum {
44
name: string;
55
value: string;
6-
values: ModelEnumProperty[];
6+
values: ModelEnumValue[];
77
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export interface ModelEnumProperty {
1+
export interface ModelEnumValue {
22
type: string;
33
name: string;
4+
description?: string;
45
value: string | number;
56
}

src/client/interfaces/ModelProperties.d.ts

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

src/client/interfaces/ModelProperty.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ export interface ModelProperty {
22
name: string;
33
type: string;
44
base: string;
5-
template: string | null;
6-
description: string | null;
5+
template?: string;
6+
description?: string;
7+
default?: any;
78
required: boolean;
9+
nullable: boolean;
810
readOnly: boolean;
11+
extends: string[];
912
imports: string[];
13+
properties: ModelProperty[];
1014
}

src/client/interfaces/Service.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { ServiceOperation } from './ServiceOperation';
2+
13
export interface Service {
24
name: string;
3-
base: string;
4-
imports: [];
5+
operations: ServiceOperation[];
6+
imports: string[];
57
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ServiceOperationError } from './ServiceOperationError';
2+
import { ServiceOperationParameter } from './ServiceOperationParameter';
3+
import { Model } from './Model';
4+
import { ServiceOperationResponse } from './ServiceOperationResponse';
5+
6+
export interface ServiceOperation {
7+
name: string;
8+
summary?: string;
9+
description?: string;
10+
deprecated?: boolean;
11+
method: string;
12+
path: string;
13+
parameters: ServiceOperationParameter[];
14+
parametersPath: ServiceOperationParameter[];
15+
parametersQuery: ServiceOperationParameter[];
16+
parametersForm: ServiceOperationParameter[];
17+
parametersHeader: ServiceOperationParameter[];
18+
parametersBody: ServiceOperationParameter | null;
19+
models: Model[];
20+
errors: ServiceOperationError[];
21+
response: ServiceOperationResponse | null;
22+
result: string;
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ServiceOperationError {
2+
code: number;
3+
text: string;
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface ServiceOperationParameter {
2+
name: string;
3+
type: string;
4+
base: string;
5+
template: string;
6+
description: string;
7+
default?: any;
8+
required: boolean;
9+
nullable: boolean;
10+
// extends: string[];
11+
// imports: string[];
12+
// properties: ModelProperty[];
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface ServiceOperationResponse {
2+
code: number;
3+
text: string;
4+
property: any;
5+
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export enum HttpClient {
2020

2121
/**
2222
* Generate the OpenAPI client. This method will read the OpenAPI specification and based on the
23-
* given language it will generate the client, including the types models, validation schemas,
23+
* given language it will generate the client, including the typed models, validation schemas,
2424
* service layer, etc.
2525
* @param input The relative location of the OpenAPI spec.
2626
* @param output The relative location of the output directory
@@ -39,6 +39,8 @@ export function generate(input: string, output: string, language: Language = Lan
3939
console.log(os.EOL);
4040

4141
try {
42+
// Load the specification, read the OpenAPI version and load the
43+
// handlebar templates for the given language
4244
const openApi = getOpenApiSpec(inputPath);
4345
const openApiVersion = getOpenApiVersion(openApi);
4446
const templates = readHandlebarsTemplates(language);

src/openApi/v2/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { getServices } from './parser/getServices';
55
import { getModels } from './parser/getModels';
66
import { getSchemas } from './parser/getSchemas';
77

8+
/**
9+
* Parse the OpenAPI specification to a Client model that contains
10+
* all the models, services and schema's we should output.
11+
* @param openApi The OpenAPI spec that we have loaded from disk.
12+
*/
813
export function parse(openApi: OpenApi): Client {
914
return {
1015
version: openApi.info.version,

src/openApi/v2/interfaces/OpenApiHeader.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export interface OpenApiHeader {
2121
maxItems?: number;
2222
minItems?: number;
2323
uniqueItems?: boolean;
24-
enum?: string[] | number[];
24+
enum?: (string | number)[];
2525
multipleOf?: number;
2626
}

src/openApi/v2/interfaces/OpenApiItems.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export interface OpenApiItems {
1717
maxItems?: number;
1818
minItems?: number;
1919
uniqueItems?: number;
20-
enum?: string[] | number[];
20+
enum?: (string | number)[];
2121
multipleOf?: number;
2222
}

src/openApi/v2/interfaces/OpenApiOperation.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface OpenApiOperation {
1515
operationId?: string;
1616
consumes?: string[];
1717
produces?: string[];
18-
parameters?: OpenApiParameter[] | OpenApiReference[];
18+
parameters?: (OpenApiParameter & OpenApiReference)[];
1919
responses: OpenApiResponses;
2020
schemes: ('http' | 'https' | 'ws' | 'wss')[];
2121
deprecated?: boolean;

src/openApi/v2/interfaces/OpenApiParameter.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export interface OpenApiParameter {
2626
maxItems?: number;
2727
minItems?: number;
2828
uniqueItems?: boolean;
29-
enum?: string[] | number[];
29+
enum?: (string | number)[];
3030
multipleOf?: number;
3131
}

src/openApi/v2/interfaces/OpenApiPath.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import { OpenApiReference } from './OpenApiReference';
66
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#pathItemObject
77
*/
88
export interface OpenApiPath {
9-
$ref?: string;
109
get?: OpenApiOperation;
1110
put?: OpenApiOperation;
1211
post?: OpenApiOperation;
1312
delete?: OpenApiOperation;
1413
options?: OpenApiOperation;
1514
head?: OpenApiOperation;
1615
patch?: OpenApiOperation;
17-
parameters?: OpenApiParameter[] | OpenApiReference[];
16+
parameters?: (OpenApiParameter & OpenApiReference)[];
1817
}

src/openApi/v2/interfaces/OpenApiReference.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#referenceObject
33
*/
44
export interface OpenApiReference {
5-
$ref: string;
5+
$ref?: string;
66
}

src/openApi/v2/interfaces/OpenApiResponses.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OpenApiResponse } from './OpenApiResponse';
55
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responsesObject
66
*/
77
export interface OpenApiResponses {
8-
[httpcode: string]: OpenApiResponse | OpenApiReference;
8+
[httpcode: string]: OpenApiResponse & OpenApiReference;
99

10-
default: OpenApiResponse | OpenApiReference;
10+
default: OpenApiResponse & OpenApiReference;
1111
}

src/openApi/v2/interfaces/OpenApiSchema.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { OpenApiXml } from './OpenApiXml';
77
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject
88
*/
99
export interface OpenApiSchema {
10-
$ref?: string;
1110
format?: 'int32' | 'int64' | 'float' | 'double' | 'string' | 'boolean' | 'byte' | 'binary' | 'date' | 'date-time' | 'password';
1211
title?: string;
1312
description?: string;
@@ -26,12 +25,12 @@ export interface OpenApiSchema {
2625
maxProperties?: number;
2726
minProperties?: number;
2827
required?: string[];
29-
enum?: string[] | number[];
28+
enum?: (string | number)[];
3029
type?: string;
31-
items?: OpenApiReference | OpenApiSchema;
32-
allOf?: OpenApiReference[] | OpenApiSchema[];
33-
properties?: Dictionary<OpenApiSchema>;
34-
additionalProperties?: boolean | OpenApiReference | OpenApiSchema;
30+
items?: OpenApiSchema & OpenApiReference;
31+
allOf?: (OpenApiSchema & OpenApiReference)[];
32+
properties?: Dictionary<OpenApiSchema & OpenApiReference>;
33+
additionalProperties?: boolean | (OpenApiSchema & OpenApiReference);
3534
discriminator?: string;
3635
readOnly?: boolean;
3736
xml?: OpenApiXml;
Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,100 @@
11
import { ModelProperty } from '../../../client/interfaces/ModelProperty';
2+
import { OpenApiSchema } from '../interfaces/OpenApiSchema';
3+
import { OpenApiReference } from '../interfaces/OpenApiReference';
4+
import { getType } from './getType';
25

3-
export function parseModelProperty(): ModelProperty {
4-
return {
5-
name: '',
6+
export function getModelProperty(name: string, schema: OpenApiSchema & OpenApiReference): ModelProperty {
7+
/**
8+
$ref?: string;
9+
format?: 'int32' | 'int64' | 'float' | 'double' | 'string' | 'boolean' | 'byte' | 'binary' | 'date' | 'date-time' | 'password';
10+
title?: string;
11+
description?: string;
12+
default?: any;
13+
multipleOf?: number;
14+
maximum?: number;
15+
exclusiveMaximum?: boolean;
16+
minimum?: number;
17+
exclusiveMinimum?: boolean;
18+
maxLength?: number;
19+
minLength?: number;
20+
pattern?: string;
21+
maxItems?: number;
22+
minItems?: number;
23+
uniqueItems?: number;
24+
maxProperties?: number;
25+
minProperties?: number;
26+
required?: string[];
27+
enum?: string[] | number[];
28+
type?: string;
29+
items?: OpenApiReference | OpenApiSchema;
30+
allOf?: OpenApiReference[] | OpenApiSchema[];
31+
properties?: Dictionary<OpenApiSchema>;
32+
additionalProperties?: boolean | OpenApiReference | OpenApiSchema;
33+
discriminator?: string;
34+
readOnly?: boolean;
35+
xml?: OpenApiXml;
36+
externalDocs?: OpenApiExternalDocs;
37+
example?: any;
38+
*/
39+
40+
const prop: ModelProperty = {
41+
name: name,
642
type: '',
743
base: '',
844
template: '',
9-
description: null,
45+
description: schema.description,
46+
default: schema.default,
1047
required: false,
11-
readOnly: false,
48+
nullable: false,
49+
readOnly: schema.readOnly || false,
1250
imports: [],
51+
extends: [],
52+
properties: [],
1353
};
54+
55+
if (schema.$ref) {
56+
// console.log('parse $ref?');
57+
}
58+
59+
if (schema.properties || schema.type === 'object') {
60+
prop.type = 'interface';
61+
// type is interface!?
62+
}
63+
64+
if (schema.enum) {
65+
prop.type = 'enum';
66+
// type is enum!
67+
}
68+
69+
console.log('propertyName:', schema);
70+
console.log('format:', schema.format);
71+
console.log('type:', schema.type);
72+
73+
const properties = schema.properties;
74+
for (const propertyName in properties) {
75+
if (properties.hasOwnProperty(propertyName)) {
76+
const property = properties[propertyName];
77+
console.log('propertyName', propertyName);
78+
// getModelProperty(propertyName, property);
79+
}
80+
}
81+
82+
if (schema.allOf) {
83+
schema.allOf.forEach(parent => {
84+
if (parent.$ref) {
85+
const extend = getType(parent.$ref);
86+
prop.extends.push(extend.type);
87+
prop.imports.push(extend.base);
88+
}
89+
if (parent.properties) {
90+
console.log(parent.properties);
91+
// const properties: ParsedModelProperties = parseModelProperties(modelClass, definition.allOf[1].properties as SwaggerDefinitions, required);
92+
// model.imports.push(...properties.imports);
93+
// model.properties.push(...properties.properties);
94+
// model.enums.push(...properties.enums);
95+
}
96+
});
97+
}
98+
99+
return prop;
14100
}

0 commit comments

Comments
 (0)