Added generic enum

This commit is contained in:
Tino Fuhrmann 2018-08-09 17:36:24 +02:00
parent 06d9556f13
commit 05f64c6732
4 changed files with 74 additions and 2 deletions

View File

@ -10,9 +10,17 @@ export enum HttpMethod {
PATCH = "PATCH"
}
export interface FormEntry {
contentType: string;
value: string | Blob;
}
export type FormData = { [key: string]: FormEntry };
export class Request {
public headers: { [key: string]: string } = {};
public body: string = "";
public body: string | FormData = "";
public constructor(public url: string, public httpMethod: HttpMethod) {

View File

@ -7,9 +7,19 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public send(request: Request): Promise<Response> {
let method = request.httpMethod.toString();
let body: string | FormData = "";
if (typeof request.body === "string") {
body = request.body;
} else {
body = new FormData();
for (const key in request.body) {
body.append(key, request.body[key].value);
}
}
return fetch(request.url, {
method: method,
body: request.body,
body: body,
headers: request.headers,
credentials: "same-origin"
}).then((resp) => {
@ -23,5 +33,6 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
return new Response(resp.status, headers, body)
});
});
}
}

View File

@ -0,0 +1,12 @@
/**
* {{{description}}}
* @export
* @enum {string}
*/
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}

View File

@ -0,0 +1,41 @@
/**
* {{{description}}}
* @export
* @interface {{classname}}
*/
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#additionalPropertiesType}}
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}};
{{/additionalPropertiesType}}
{{#vars}}
/**
* {{{description}}}
* @type {{=<% %>=}}{<%&datatype%>}<%={{ }}=%>
* @memberof {{classname}}
*/
{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}}
}{{#hasEnums}}
/**
* @export
* @namespace {{classname}}
*/
export namespace {{classname}} {
{{#vars}}
{{#isEnum}}
/**
* @export
* @enum {string}
*/
export enum {{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/vars}}
}{{/hasEnums}}