All Downloads are FREE. Search and download functionalities are using the official Maven repository.

typescript-jquery.api.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
import * as $ from 'jquery';

let defaultBasePath = '{{{basePath}}}';

// ===============================================
// This file is autogenerated - Please do not edit
// ===============================================

/* tslint:disable:no-unused-variable */

{{#models}}
{{#model}}
{{#description}}
    /**
    * {{{description}}}
    */
{{/description}}
export class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#vars}}
    {{#description}}
        /**
        * {{{description}}}
        */
    {{/description}}
    '{{name}}': {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
{{/vars}}
}

{{#hasEnums}}
export namespace {{classname}} {
{{#vars}}
{{#isEnum}}
    export enum {{enumName}} {
    {{#allowableValues}}
        {{#enumVars}}
            {{name}} =  {{{value}}}{{^-last}},{{/-last}}
        {{/enumVars}}
    {{/allowableValues}}
    }
{{/isEnum}}
{{/vars}}
}
{{/hasEnums}}
{{/model}}
{{/models}}

export interface Authentication {
    /**
    * Apply authentication settings to header and query params.
    */
    applyToRequest(requestOptions: JQueryAjaxSettings): void;
}

export class HttpBasicAuth implements Authentication {
    public username: string;
    public password: string;
    applyToRequest(requestOptions: any): void {
        requestOptions.username = this.username;
        requestOptions.password = this.password;
    }
}

export class ApiKeyAuth implements Authentication {
    public apiKey: string;

    constructor(private location: string, private paramName: string) {
    }

    applyToRequest(requestOptions: JQueryAjaxSettings): void {
        requestOptions.headers[this.paramName] = this.apiKey;
    }
}

export class OAuth implements Authentication {
    public accessToken: string;

    applyToRequest(requestOptions: JQueryAjaxSettings): void {
        requestOptions.headers["Authorization"] = "Bearer " + this.accessToken;
    }
}

export class VoidAuth implements Authentication {
    public username: string;
    public password: string;
    applyToRequest(requestOptions: JQueryAjaxSettings): void {
        // Do nothing
    }
}

{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#description}}
/**
* {{&description}}
*/
{{/description}}
export enum {{classname}}ApiKeys {
{{#authMethods}}
{{#isApiKey}}
    {{name}},
{{/isApiKey}}
{{/authMethods}}
}

export class {{classname}} {
    protected basePath = defaultBasePath;
    protected defaultHeaders : any = {};

    protected authentications = {
        'default': new VoidAuth(),
{{#authMethods}}
{{#isBasic}}
        '{{name}}': new HttpBasicAuth(),
{{/isBasic}}
{{#isApiKey}}
        '{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{^isKeyInHeader}}'query'{{/isKeyInHeader}}, '{{keyParamName}}'),
{{/isApiKey}}
{{#isOAuth}}
        '{{name}}': new OAuth(),
{{/isOAuth}}
{{/authMethods}}
    }

    constructor(basePath?: string);
{{#authMethods}}
{{#isBasic}}
    constructor(username: string, password: string, basePath?: string);
{{/isBasic}}
{{/authMethods}}
    constructor(basePathOrUsername: string, password?: string, basePath?: string) {
        if (password) {
{{#authMethods}}
{{#isBasic}}
            this.username = basePathOrUsername;
            this.password = password
{{/isBasic}}
{{/authMethods}}
            if (basePath) {
                this.basePath = basePath;
            }
        } else {
            if (basePathOrUsername) {
                this.basePath = basePathOrUsername
            }
        }
    }

    public setApiKey(key: {{classname}}ApiKeys, value: string) {
        this.authentications[{{classname}}ApiKeys[key]].apiKey = value;
    }
{{#authMethods}}
{{#isBasic}}

    set username(username: string) {
        this.authentications.{{name}}.username = username;
    }

    set password(password: string) {
        this.authentications.{{name}}.password = password;
    }
{{/isBasic}}
{{#isOAuth}}

    set accessToken(token: string) {
        this.authentications.{{name}}.accessToken = token;
    }
{{/isOAuth}}
{{/authMethods}}
    private extendObj(objA: T2, objB: T2): T1|T2 {
        for(let key in objB){
            if(objB.hasOwnProperty(key)){
            objA[key] = objB[key];
            }
        }
        return objA;
    }

{{#operation}}
    /**
     * {{¬es}}
     {{#summary}}
     * @summary {{&summary}}
     {{/summary}}
     {{#allParams}}* @param {{paramName}} {{description}}
     {{/allParams}}*/
    public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : JQueryPromise<{ response: JQueryXHR; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
        let localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
            .replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
        let queryParameters: any = {};
        let headerParams: any = this.extendObj({}, this.defaultHeaders);

{{#allParams}}{{#required}}
        // verify required parameter '{{paramName}}' is not null or undefined
        if ({{paramName}} === null || {{paramName}} === undefined) {
            throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
        }
{{/required}}{{/allParams}}
{{#queryParams}}
        if ({{paramName}} !== undefined) {
            queryParameters['{{baseName}}'] = {{paramName}};
        }

{{/queryParams}}

        localVarPath = localVarPath + "?" + $.param(queryParameters);

{{#headerParams}}
        headerParams['{{baseName}}'] = {{paramName}};

{{/headerParams}}

{{^bodyParam}}
        let reqHasFile = false;
        let reqDict = {};
        let reqFormData = new FormData();
{{#formParams}}
{{#isFile}}
        reqHasFile = true;
{{/isFile}}
        if ({{paramName}} !== undefined) {
            reqFormData.append('{{baseName}}', {{paramName}});
            reqDict['{{baseName}}'] = {{paramName}};
        }

{{/formParams}}
{{/bodyParam}}
{{#bodyParam}}
        let reqDict = {{paramName}};
        let reqFormData = new FormData();
        reqFormData.append('{{paramName}}', {{paramName}});
{{#isFile}}
        let reqHasFile = true;
{{/isFile}}
{{^isFile}}
        let reqHasFile = false;
{{/isFile}}
{{/bodyParam}}

        let requestOptions: JQueryAjaxSettings = {
            url: localVarPath,
            type: '{{httpMethod}}',
            headers: headerParams,
            processData: false
        };

        if (Object.keys(reqDict).length) {
            requestOptions.data = reqHasFile ? reqFormData : JSON.stringify(reqDict);
            requestOptions.contentType = reqHasFile ? false : 'application/json; charset=utf-8';
        }

{{#authMethods}}
        this.authentications.{{name}}.applyToRequest(requestOptions);

{{/authMethods}}
        this.authentications.default.applyToRequest(requestOptions);

        let dfd = $.Deferred();
        $.ajax(requestOptions).then(
            (data: {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}any{{/returnType}}, textStatus: string, jqXHR: JQueryXHR) =>
                dfd.resolve({ response: jqXHR, body: data }),
            (xhr: JQueryXHR, textStatus: string, errorThrown: string) =>
                dfd.reject({ response: xhr, body: errorThrown })
        );
        return dfd.promise();
    }
{{/operation}}
}
{{/operations}}
{{/apis}}
{{/apiInfo}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy