Skip to content

Commit 30e2662

Browse files
committed
- Added core template files
- Working on v2 parsing
1 parent c4bc1e1 commit 30e2662

File tree

127 files changed

+1459
-697
lines changed

Some content is hidden

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

127 files changed

+1459
-697
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"eslint": "6.6.0",
7575
"eslint-config-prettier": "6.5.0",
7676
"eslint-plugin-prettier": "3.1.1",
77+
"glob": "7.1.6",
7778
"handlebars": "4.5.1",
7879
"jest": "24.9.0",
7980
"jest-cli": "24.9.0",

src/client/interfaces/ArrayType.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface ArrayType {
2+
type: string;
3+
base: string;
4+
template: string | null;
5+
default?: any;
6+
imports: string[];
7+
}

src/client/interfaces/Client.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Model } from './Model';
22
import { Service } from './Service';
3-
import { Schema } from './Schema';
43

54
export interface Client {
65
version: string;
76
server: string;
87
models: Map<string, Model>;
9-
schemas: Map<string, Schema>;
108
services: Map<string, Service>;
119
}

src/client/interfaces/Model.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface Model {
55
name: string;
66
base: string;
77
type: string;
8-
template?: string;
8+
template: string | null;
99
description?: string;
1010
extends: string[];
1111
imports: string[];

src/client/interfaces/ModelProperty.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface ModelProperty {
22
name: string;
33
type: string;
44
base: string;
5-
template?: string;
5+
template: string | null;
66
description?: string;
77
default?: any;
88
required: boolean;

src/client/interfaces/Operation.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { OperationError } from './OperationError';
2+
import { Parameter } from './Parameter';
3+
4+
export interface Operation {
5+
service: string;
6+
name: string;
7+
summary?: string;
8+
description?: string;
9+
deprecated?: boolean;
10+
method: string;
11+
path: string;
12+
parameters: Parameter[];
13+
parametersPath: Parameter[];
14+
parametersQuery: Parameter[];
15+
parametersForm: Parameter[];
16+
parametersHeader: Parameter[];
17+
parametersBody: Parameter | null;
18+
errors: OperationError[];
19+
result: string;
20+
imports: string[];
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface OperationError {
2+
code: number;
3+
text: string;
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Parameter } from './Parameter';
2+
3+
export interface OperationParameters {
4+
imports: string[];
5+
parameters: Parameter[];
6+
parametersPath: Parameter[];
7+
parametersQuery: Parameter[];
8+
parametersForm: Parameter[];
9+
parametersHeader: Parameter[];
10+
parametersBody: Parameter | null;
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface OperationResponse {
2+
code: number;
3+
text: string;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface OperationResult {
2+
type: string;
3+
imports: string[];
4+
}

src/client/interfaces/Parameter.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface Parameter {
2+
prop: string;
3+
in: 'path' | 'query' | 'header' | 'formData' | 'body';
4+
name: string;
5+
type: string;
6+
base: string;
7+
template: string | null;
8+
description?: string;
9+
default?: any;
10+
required: boolean;
11+
nullable: boolean;
12+
imports: string[];
13+
}

src/client/interfaces/Schema.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export interface Schema {
2-
name: string;
2+
type: string;
33
base: string;
4-
imports: [];
4+
template: string | null;
5+
default?: any;
6+
imports: string[];
57
}

src/client/interfaces/Service.d.ts

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

33
export interface Service {
44
name: string;
5-
operations: ServiceOperation[];
5+
operations: Operation[];
66
imports: string[];
77
}

src/client/interfaces/ServiceOperation.d.ts

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

src/client/interfaces/ServiceOperationError.d.ts

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

src/client/interfaces/ServiceOperationParameter.ts

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

src/client/interfaces/ServiceOperationResponse.ts

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

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export function generate(input: string, output: string, language: Language = Lan
3131
const inputPath = path.resolve(process.cwd(), input);
3232
const outputPath = path.resolve(process.cwd(), output);
3333

34-
console.log(chalk.bold.green('Generate:'));
35-
console.log(chalk.grey(' Input:'), input);
36-
console.log(chalk.grey(' Output:'), output);
37-
console.log(chalk.grey(' Language:'), language);
38-
console.log(chalk.grey(' HTTP client:'), httpClient);
39-
console.log(os.EOL);
34+
// console.log(chalk.bold.green('Generate:'));
35+
// console.log(chalk.grey(' Input:'), input);
36+
// console.log(chalk.grey(' Output:'), output);
37+
// console.log(chalk.grey(' Language:'), language);
38+
// console.log(chalk.grey(' HTTP client:'), httpClient);
39+
// console.log(os.EOL);
4040

4141
try {
4242
// Load the specification, read the OpenAPI version and load the

src/openApi/v2/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Client } from '../../client/interfaces/Client';
33
import { getServer } from './parser/getServer';
44
import { getServices } from './parser/getServices';
55
import { getModels } from './parser/getModels';
6-
import { getSchemas } from './parser/getSchemas';
76

87
/**
98
* Parse the OpenAPI specification to a Client model that contains
@@ -15,7 +14,6 @@ export function parse(openApi: OpenApi): Client {
1514
version: openApi.info.version,
1615
server: getServer(openApi),
1716
models: getModels(openApi),
18-
schemas: getSchemas(openApi),
1917
services: getServices(openApi),
2018
};
2119
}

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[];
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[];
2121
multipleOf?: number;
2222
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { OpenApiItems } from './OpenApiItems';
22
import { OpenApiSchema } from './OpenApiSchema';
3+
import { OpenApiReference } from './OpenApiReference';
34

45
/**
56
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
@@ -9,8 +10,8 @@ export interface OpenApiParameter {
910
in: 'path' | 'query' | 'header' | 'formData' | 'body';
1011
description?: string;
1112
required?: boolean;
12-
schema?: OpenApiSchema;
13-
type?: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'file';
13+
schema?: OpenApiSchema & OpenApiReference;
14+
type?: string;
1415
format?: 'int32' | 'int64' | 'float' | 'double' | 'string' | 'boolean' | 'byte' | 'binary' | 'date' | 'date-time' | 'password';
1516
allowEmptyValue?: boolean;
1617
items?: OpenApiItems;
@@ -26,6 +27,6 @@ export interface OpenApiParameter {
2627
maxItems?: number;
2728
minItems?: number;
2829
uniqueItems?: boolean;
29-
enum?: (string | number)[];
30+
enum?: string[];
3031
multipleOf?: number;
3132
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Dictionary } from '../../../utils/types';
22
import { OpenApiExample } from './OpenApiExample';
33
import { OpenApiHeader } from './OpenApiHeader';
44
import { OpenApiSchema } from './OpenApiSchema';
5+
import { OpenApiReference } from './OpenApiReference';
56

67
/**
78
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responseObject
89
*/
910
export interface OpenApiResponse {
1011
description: string;
11-
schema?: OpenApiSchema;
12+
schema?: OpenApiSchema & OpenApiReference;
1213
headers?: Dictionary<OpenApiHeader>;
1314
examples?: OpenApiExample;
1415
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import { OpenApiResponse } from './OpenApiResponse';
77
export interface OpenApiResponses {
88
[httpcode: string]: OpenApiResponse & OpenApiReference;
99

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface OpenApiSchema {
2525
maxProperties?: number;
2626
minProperties?: number;
2727
required?: string[];
28-
enum?: (string | number)[];
28+
enum?: string[];
2929
type?: string;
3030
items?: OpenApiSchema & OpenApiReference;
3131
allOf?: (OpenApiSchema & OpenApiReference)[];

src/openApi/v2/parser/getArrayType.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { getType } from './getType';
2+
import { Type } from '../../../client/interfaces/Type';
3+
import { OpenApiItems } from '../interfaces/OpenApiItems';
4+
import { getEnumType } from './getEnumType';
5+
import { ArrayType } from '../../../client/interfaces/ArrayType';
6+
7+
export function getArrayType(items: OpenApiItems): ArrayType {
8+
let itemsType = 'any';
9+
let itemsBase = 'any';
10+
let itemsTemplate: string | null = null;
11+
const itemsImports: string[] = [];
12+
13+
// If the parameter has a type than it can be a basic or generic type.
14+
if (items.type) {
15+
const itemsData: Type = getType(items.type);
16+
itemsType = itemsData.type;
17+
itemsBase = itemsData.base;
18+
itemsTemplate = itemsData.template;
19+
itemsImports.push(...itemsData.imports);
20+
21+
// If the parameter is an Array type, we check for the child type,
22+
// so we can create a typed array, otherwise this will be a "any[]".
23+
if (items.type === 'array' && items.items) {
24+
console.log('templated array', items.items);
25+
// Parse the child types and create a correct Array type, for example "string[]" or "ActivityData[]"
26+
// const child: ParsedProperty = parseProperty(parameter.items, template);
27+
// parameterType = `${child.type}[]`;
28+
// parameterBase = child.base;
29+
// parameterTemplate = child.template;
30+
// parameterImports.push(...child.imports);
31+
}
32+
}
33+
34+
if (items.enum) {
35+
itemsType = getEnumType(items.enum, true);
36+
}
37+
38+
return {
39+
type: itemsType,
40+
base: itemsBase,
41+
template: itemsTemplate,
42+
default: items.default,
43+
imports: itemsImports,
44+
};
45+
}

src/openApi/v2/parser/getComment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function getComment(comment: string | undefined): string | undefined {
2+
if (comment) {
3+
return comment.replace(/(\r\n|\n|\r)+/g, '$1 * ');
4+
}
5+
return undefined;
6+
}

src/openApi/v2/parser/getEnumType.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function getEnumType(values?: string[], addParentheses = false): string {
2+
if (Array.isArray(values)) {
3+
// Filter out empty and double enum values.
4+
// Plus make sure we put quotes around strings!
5+
const entries: string[] = values
6+
.filter(name => name)
7+
.filter((name, index, arr) => arr.indexOf(name) === index)
8+
.map(value => `'${String(value)}'`);
9+
10+
// Add grouping parentheses if needed. This can be handy if enum values
11+
// are used in Arrays, so that you will get the following definition:
12+
// const myArray: ('EnumValue1' | 'EnumValue2' | 'EnumValue3')[];
13+
if (entries.length > 1 && addParentheses) {
14+
return `(${entries.join(' | ')})`;
15+
}
16+
17+
return entries.join(' | ');
18+
}
19+
return 'string';
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function getEnumTypeFromDescription(description: string, addParentheses = false): string | null {
2+
// Check if we can find this special format string:
3+
// None=0,Something=1,AnotherThing=2
4+
const matches: RegExpMatchArray | null = description.match(/((\w+)=([0-9]+)(?:,|$))/g);
5+
if (matches) {
6+
// Grab the values from the description
7+
const values: number[] = [];
8+
for (let i = 0, n = matches.length; i < n; i++) {
9+
const value = parseInt(matches[i].split('=')[1].replace(/[^0-9]/g, ''));
10+
if (Number.isInteger(value)) {
11+
values.push(value);
12+
}
13+
}
14+
15+
// Filter and sort the values
16+
const entries: string[] = values
17+
.sort()
18+
.filter((name, index, arr) => arr.indexOf(name) === index)
19+
.map(value => String(value));
20+
21+
// Add grouping parentheses if needed. This can be handy if enum values
22+
// are used in Arrays, so that you will get the following definition:
23+
// const myArray: ('EnumValue1' | 'EnumValue2' | 'EnumValue3')[];
24+
if (entries.length > 1 && addParentheses) {
25+
return `(${entries.join(' | ')})`;
26+
}
27+
28+
return entries.join(' | ');
29+
}
30+
31+
return null;
32+
}

0 commit comments

Comments
 (0)