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

typescript-node.api-single.mustache Maven / Gradle / Ivy

There is a newer version: 7.7.0
Show newest version
{{>licenseInfo}}

import localVarRequest from 'request';
import http from 'http';

/* tslint:disable:no-unused-locals */
{{#imports}}
import { {{classname}} } from '{{filename}}';
{{/imports}}

import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
{{#hasAuthMethods}}
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
{{/hasAuthMethods}}

import { HttpError, RequestFile } from './apis';

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

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

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

export class {{classname}} {
    protected _basePath = defaultBasePath;
    protected _defaultHeaders : any = {};
    protected _useQuerystring : boolean = false;

    protected authentications = {
        'default': new VoidAuth(),
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
        '{{name}}': new HttpBasicAuth(),
{{/isBasicBasic}}
{{#isBasicBearer}}
        '{{name}}': new HttpBearerAuth(),
{{/isBasicBearer}}
{{#isApiKey}}
        '{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}{{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}, '{{keyParamName}}'),
{{/isApiKey}}
{{#isOAuth}}
        '{{name}}': new OAuth(),
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
    }

    protected interceptors: Interceptor[] = [];

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

    set useQuerystring(value: boolean) {
        this._useQuerystring = value;
    }

    set basePath(basePath: string) {
        this._basePath = basePath;
    }

    set defaultHeaders(defaultHeaders: any) {
        this._defaultHeaders = defaultHeaders;
    }

    get defaultHeaders() {
        return this._defaultHeaders;
    }

    get basePath() {
        return this._basePath;
    }

    public setDefaultAuthentication(auth: Authentication) {
        this.authentications.default = auth;
    }

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

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

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

    set accessToken(accessToken: string | (() => string)) {
        this.authentications.{{name}}.accessToken = accessToken;
    }
{{/isBasicBearer}}
{{#isOAuth}}

    set accessToken(token: string) {
        this.authentications.{{name}}.accessToken = token;
    }
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}

    public addInterceptor(interceptor: Interceptor) {
        this.interceptors.push(interceptor);
    }

{{#operation}}
    /**
     * {{¬es}}
     {{#summary}}
     * @summary {{&summary}}
     {{/summary}}
     {{#allParams}}
     * @param {{paramName}} {{description}}
     {{/allParams}}
     */
    public async {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; {{#returnType}}body: {{{.}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
        const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
            .replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
        let localVarQueryParameters: any = {};
        let localVarHeaderParams: any = (Object).assign({}, this._defaultHeaders);
{{#hasProduces}}
        const produces = [{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}];
        // give precedence to 'application/json'
        if (produces.indexOf('application/json') >= 0) {
            localVarHeaderParams.Accept = 'application/json';
        } else {
            localVarHeaderParams.Accept = produces.join(',');
        }
{{/hasProduces}}
        let localVarFormParams: any = {};

{{#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) {
            localVarQueryParameters['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
        }

{{/queryParams}}
{{#headerParams}}
        localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/headerParams}}
        (Object).assign(localVarHeaderParams, options.headers);

        let localVarUseFormData = false;

{{#formParams}}
        if ({{paramName}} !== undefined) {
            {{#isFile}}
            localVarFormParams['{{baseName}}'] = {{paramName}};
            {{/isFile}}
            {{^isFile}}
            localVarFormParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
            {{/isFile}}
        }
{{#isFile}}
        localVarUseFormData = true;
{{/isFile}}

{{/formParams}}
        let localVarRequestOptions: localVarRequest.Options = {
            method: '{{httpMethod}}',
            qs: localVarQueryParameters,
            headers: localVarHeaderParams,
            uri: localVarPath,
            useQuerystring: this._useQuerystring,
{{^isResponseFile}}
            json: true,
{{/isResponseFile}}
{{#isResponseFile}}
            encoding: null,
{{/isResponseFile}}
{{#bodyParam}}
            body: ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}")
{{/bodyParam}}
        };

        let authenticationPromise = Promise.resolve();
{{#authMethods}}
    {{#isApiKey}}
        if (this.authentications.{{name}}.apiKey) {
            authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
        }
    {{/isApiKey}}
    {{#isBasicBasic}}
        if (this.authentications.{{name}}.username && this.authentications.{{name}}.password) {
            authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
        }
    {{/isBasicBasic}}
    {{#isBasicBearer}}
        if (this.authentications.{{name}}.accessToken) {
            authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
        }
    {{/isBasicBearer}}
    {{#isOAuth}}
        if (this.authentications.{{name}}.accessToken) {
            authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
        }
    {{/isOAuth}}
{{/authMethods}}
        authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));

        let interceptorPromise = authenticationPromise;
        for (const interceptor of this.interceptors) {
            interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
        }

        return interceptorPromise.then(() => {
            if (Object.keys(localVarFormParams).length) {
                if (localVarUseFormData) {
                    (localVarRequestOptions).formData = localVarFormParams;
                } else {
                    localVarRequestOptions.form = localVarFormParams;
                }
            }
            return new Promise<{ response: http.IncomingMessage; {{#returnType}}body: {{{.}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>((resolve, reject) => {
                localVarRequest(localVarRequestOptions, (error, response, body) => {
                    if (error) {
                        reject(error);
                    } else {
                        if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
                            {{#returnType}}
                            body = ObjectSerializer.deserialize(body, "{{{.}}}");
                            {{/returnType}}
                            resolve({ response: response, body: body });
                        } else {
                            reject(new HttpError(response, body, response.statusCode));
                        }
                    }
                });
            });
        });
    }
{{/operation}}
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy