Skip to content

Commit

Permalink
- v2 generation ready, including javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdikoomen committed Nov 22, 2019
1 parent cb1668f commit 4bfe976
Show file tree
Hide file tree
Showing 57 changed files with 566 additions and 27,591 deletions.
7 changes: 7 additions & 0 deletions src/client/interfaces/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { Schema } from './Schema';

export interface Model extends Schema {
name: string;
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface';
type: string;
base: string;
template: string | null;
link: Model | null;
description: string | null;
isProperty: boolean;
isReadOnly: boolean;
isRequired: boolean;
isNullable: boolean;
imports: string[];
extends: string[];
enum: Enum[];
enums: Model[];
Expand Down
13 changes: 4 additions & 9 deletions src/client/interfaces/OperationParameter.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Enum } from './Enum';
import { Schema } from './Schema';
import { Model } from './Model';

export interface OperationParameter extends Schema {
prop: string;
export interface OperationParameter extends Model {
in: 'path' | 'query' | 'header' | 'formData' | 'body';
name: string;
default: any;
isRequired: boolean;
isNullable: boolean;
enum: Enum[];
prop: string;
default?: any;
}
4 changes: 2 additions & 2 deletions src/client/interfaces/OperationResponse.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema } from './Schema';
import { Model } from './Model';

export interface OperationResponse extends Schema {
export interface OperationResponse extends Model {
code: number;
}
11 changes: 0 additions & 11 deletions src/client/interfaces/Schema.ts

This file was deleted.

17 changes: 14 additions & 3 deletions src/openApi/v2/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getEnum } from './getEnum';
import { getEnumType } from './getEnumType';
import { getEnumFromDescription } from './getEnumFromDescription';
import { getModel } from './getModel';
import { getOperationParameterDefault } from './getOperationParameterDefault';

export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParameter): OperationParameter {
const operationParameter: OperationParameter = {
Expand All @@ -21,11 +22,16 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
template: null,
link: null,
description: getComment(parameter.description),
default: parameter.default,
default: getOperationParameterDefault(parameter.default),
isProperty: false,
isReadOnly: false,
isRequired: parameter.required || false,
isNullable: false,
imports: [],
extends: [],
enum: [],
enums: [],
properties: [],
};

if (parameter.$ref) {
Expand Down Expand Up @@ -77,6 +83,7 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
operationParameter.base = items.base;
operationParameter.template = items.template;
operationParameter.imports.push(...items.imports);
operationParameter.imports.push('Dictionary');
return operationParameter;
}

Expand All @@ -91,12 +98,16 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
return operationParameter;
} else {
const model = getModel(openApi, parameter.schema);
operationParameter.export = 'interface';
operationParameter.export = model.export;
operationParameter.type = model.type;
operationParameter.base = model.base;
operationParameter.template = model.template;
operationParameter.link = model.link;
operationParameter.imports.push(...model.imports);
operationParameter.link = model;
operationParameter.extends.push(...model.extends);
operationParameter.enum.push(...model.enum);
operationParameter.enums.push(...model.enums);
operationParameter.properties.push(...model.properties);
return operationParameter;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/openApi/v2/parser/getOperationParameterDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getOperationParameterDefault(value: any): string | null {
if (value === null) {
return 'null';
}

switch (typeof value) {
case 'number':
case 'boolean':
return JSON.stringify(value);
case 'string':
return `'${value}'`;
}

return null;
}
15 changes: 14 additions & 1 deletion src/openApi/v2/parser/getOperationResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ export function getOperationResponses(openApi: OpenApi, responses: OpenApiRespon
// free to do their own casting if needed.
if (responseCode) {
const operationResponse: OperationResponse = {
name: '',
code: responseCode,
description: getComment(response.description)!,
export: 'generic',
type: PrimaryType.OBJECT,
base: PrimaryType.OBJECT,
template: null,
link: null,
isProperty: false,
isReadOnly: false,
isRequired: false,
isNullable: false,
imports: [],
extends: [],
enum: [],
enums: [],
properties: [],
};

// If this response has a schema, then we need to check two things:
Expand All @@ -53,8 +62,12 @@ export function getOperationResponses(openApi: OpenApi, responses: OpenApiRespon
operationResponse.type = model.type;
operationResponse.base = model.base;
operationResponse.template = model.template;
operationResponse.link = model.link;
operationResponse.imports.push(...model.imports);
operationResponse.link = model;
operationResponse.extends.push(...model.extends);
operationResponse.enum.push(...model.enum);
operationResponse.enums.push(...model.enums);
operationResponse.properties.push(...model.properties);
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/openApi/v2/parser/getOperationResults.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { PrimaryType } from './constants';
import { OperationResponse } from '../../../client/interfaces/OperationResponse';
import { Schema } from '../../../client/interfaces/Schema';
import { Model } from '../../../client/interfaces/Model';

function areEqual(a: Schema, b: Schema): boolean {
return a.type === b.type && a.base === b.base && a.template === b.template;
function areEqual(a: Model, b: Model): boolean {
const equal = a.type === b.type && a.base === b.base && a.template === b.template;
if (equal && a.link && b.link) {
return areEqual(a.link, b.link);
}
return equal;
}

export function getOperationResults(operationResponses: OperationResponse[]): OperationResponse[] {
Expand All @@ -17,14 +21,23 @@ export function getOperationResults(operationResponses: OperationResponse[]): Op

if (!operationResults.length) {
operationResults.push({
name: '',
code: 200,
description: '',
export: 'interface',
type: PrimaryType.OBJECT,
base: PrimaryType.OBJECT,
template: null,
imports: [],
link: null,
isProperty: false,
isReadOnly: false,
isRequired: false,
isNullable: false,
imports: [],
extends: [],
enum: [],
enums: [],
properties: [],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

export let OpenAPI;
(function (OpenAPI) {
OpenAPI.BASE = '';
OpenAPI.BASE = '{{{server}}}';
OpenAPI.VERSION = '{{{version}}}';
OpenAPI.CLIENT = '{{{httpClient}}}';
OpenAPI.TOKEN = '';
OpenAPI.VERSION = '{VERSION}';
})(OpenAPI || (OpenAPI = {}));
2 changes: 1 addition & 1 deletion src/templates/javascript/core/isSuccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
* @param status Status code
*/
export function isSuccess(status) {
return (status >= 200 && status < 300);
return status >= 200 && status < 300;
}
27 changes: 15 additions & 12 deletions src/templates/javascript/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@
/* eslint-disable */
/* prettier-ignore */

import { getFormData } from './getFormData';
import { getQueryString } from './getQueryString';
import { OpenAPI } from './OpenAPI';
import { requestUsingFetch } from './requestUsingFetch';
import {getFormData} from './getFormData';
import {getQueryString} from './getQueryString';
import {OpenAPI} from './OpenAPI';
import {requestUsingFetch} from './requestUsingFetch';
import {requestUsingXHR} from './requestUsingXHR';

/**
* Create the request.
* @param options Request method options.
* @returns Result object (see above)
*/
export async function request<T>(options) {
export async function request(options) {

// Create the request URL
let url = `${OpenAPI.BASE}${options.path}`;

// Create request headers
const headers = new Headers({
...options.headers,
Accept: 'application/json',
Accept: 'application/json'
});

// Create request settings
const request = {
headers,
method: options.method,
credentials: 'same-origin',
credentials: 'same-origin'
};

// If we have a bearer token then we set the authentication header.
Expand All @@ -43,7 +44,6 @@ export async function request<T>(options) {
// Append formData as body
if (options.formData) {
request.body = getFormData(options.formData);

} else if (options.body) {

// If this is blob data, then pass it directly to the body and set content type.
Expand All @@ -60,16 +60,19 @@ export async function request<T>(options) {
}

try {

return await requestUsingFetch(url, request);

switch (OpenAPI.CLIENT) {
case 'xhr':
return await requestUsingXHR(url, request);
default:
return await requestUsingFetch(url, request);
}
} catch (error) {
return {
url,
ok: false,
status: 0,
statusText: '',
body: error,
body: error
};
}
}
5 changes: 2 additions & 3 deletions src/templates/javascript/core/requestUsingFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
*/
export async function requestUsingFetch<T>(url, request) {
export async function requestUsingFetch(url, request) {

// Fetch response using fetch API.
const response = await fetch(url, request);
Expand All @@ -20,7 +20,7 @@ export async function requestUsingFetch<T>(url, request) {
ok: response.ok,
status: response.status,
statusText: response.statusText,
body: null,
body: null
};

// Try to parse the content for any response status code.
Expand All @@ -29,7 +29,6 @@ export async function requestUsingFetch<T>(url, request) {
const contentType = response.headers.get('Content-Type');
if (contentType) {
switch (contentType.toLowerCase()) {

case 'application/json':
case 'application/json; charset=utf-8':
result.body = await response.json();
Expand Down
14 changes: 6 additions & 8 deletions src/templates/javascript/core/requestUsingXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
/* eslint-disable */
/* prettier-ignore */

import { isSuccess } from "./isSuccess";
import { isSuccess } from './isSuccess';

/**
* Request content using the new legacy XMLHttpRequest API. This method is usefull
* Request content using the new legacy XMLHttpRequest API. This method is useful
* when we want to request UTF-16 content, since it natively supports loading UTF-16.
* We could do the same with the Fetch API, but then we will need to conver the
* We could do the same with the Fetch API, but then we will need to convert the
* content using JavaScript... And that is very very slow.
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
*/
export async function requestUsingXHR<T>(url, request) {
return new Promise(resole => {

export async function requestUsingXHR(url, request) {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();

// Open the request, remember to do this before adding any headers,
Expand All @@ -37,7 +36,7 @@ export async function requestUsingXHR<T>(url, request) {
ok: isSuccess(xhr.status),
status: xhr.status,
statusText: xhr.statusText,
body: null,
body: null
};

// Try to parse the content for any response status code.
Expand All @@ -46,7 +45,6 @@ export async function requestUsingXHR<T>(url, request) {
const contentType = xhr.getResponseHeader('Content-Type');
if (contentType) {
switch (contentType.toLowerCase()) {

case 'application/json':
case 'application/json; charset=utf-8':
result.body = JSON.parse(xhr.responseText);
Expand Down
5 changes: 3 additions & 2 deletions src/templates/javascript/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
/* prettier-ignore */

export { ApiError } from './core/ApiError';
export { isSuccess } from './core/isSuccess';
export { OpenAPI } from './core/OpenAPI';
{{#if models}}

{{#each models}}
export { {{{basename}}} } from './models/{{{basename}}}';
{{#notEquals this 'Dictionary'}}export { {{{this}}} } from './models/{{{this}}}';{{/notEquals}}
{{/each}}
{{/if}}
{{#if services}}

{{#each services}}
export { {{{name}}} } from './services/{{{name}}}';
export { {{{this}}} } from './services/{{{this}}}';
{{/each}}
{{/if}}
Loading

0 comments on commit 4bfe976

Please sign in to comment.