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

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

There is a newer version: 3.0.0-rc1
Show newest version
{{>licenseInfo}}
/* tslint:disable:no-unused-variable member-ordering */

import { Inject, Injectable, Optional }                      from '@angular/core';
import { Http, Headers, URLSearchParams }                    from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType }                     from '@angular/http';

import { Observable }                                        from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import * as models                                           from '../model/models';
import { BASE_PATH, COLLECTION_FORMATS }                     from '../variables';
import { Configuration }                                     from '../configuration';
{{#withInterfaces}}
import { {{classname}}Interface }                            from './{{classname}}Interface';
{{/withInterfaces}}

{{#operations}}

{{#description}}
/**
 * {{&description}}
 */
{{/description}}
@Injectable()
{{#withInterfaces}}
export class {{classname}} implements {{classname}}Interface {
{{/withInterfaces}}
{{^withInterfaces}}
export class {{classname}} {
{{/withInterfaces}}

    protected basePath = '{{{basePath}}}';
    public defaultHeaders: Headers = new Headers();
    public configuration: Configuration = new Configuration();

    constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
            this.basePath = basePath;
        }
        if (configuration) {
            this.configuration = configuration;
        }
    }

{{#operation}}
    /**
     * {{¬es}}
     {{#summary}}
     * @summary {{&summary}}
     {{/summary}}
     {{#allParams}}* @param {{paramName}} {{description}}
     {{/allParams}}*/
    public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
        return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return response.json() || {};
                }
            });
    }

{{/operation}}

{{#operation}}
    /**
     * {{summary}}
     * {{notes}}
     {{#allParams}}* @param {{paramName}} {{description}}
     {{/allParams}}*/
    public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable {
        const path = this.basePath + '{{{path}}}'{{#pathParams}}
                    .replace('${' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

        let queryParameters = new URLSearchParams();
        let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
{{#hasFormParams}}
        let formParams = new URLSearchParams();

{{/hasFormParams}}
{{#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}}
        {{#isListContainer}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                queryParameters.append('{{baseName}}', element);
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            queryParameters.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        {{/isCollectionFormatMulti}}
        }
        {{/isListContainer}}
        {{^isListContainer}}
        if ({{paramName}} !== undefined) {
        {{#isDateTime}}
            queryParameters.set('{{baseName}}', {{paramName}}.toISOString());
        {{/isDateTime}}
        {{^isDateTime}}
            {{#isDate}}
            queryParameters.set('{{baseName}}', {{paramName}}.toISOString());
            {{/isDate}}
            {{^isDate}}
            queryParameters.set('{{baseName}}', {{paramName}});
            {{/isDate}}
        {{/isDateTime}}
        }
        {{/isListContainer}}

{{/queryParams}}
{{#headerParams}}
        {{#isListContainer}}
        if ({{paramName}}) {
            headers.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        }
        {{/isListContainer}}
        {{^isListContainer}}
        if ({{paramName}} !== undefined && {{paramName}} !== null) {
            headers.set('{{baseName}}', String({{paramName}}));
        }
        {{/isListContainer}}

{{/headerParams}}
        // to determine the Content-Type header
        let consumes: string[] = [
            {{#consumes}}
            '{{{mediaType}}}'{{#hasMore}},{{/hasMore}}
            {{/consumes}}
        ];

        // to determine the Accept header
        let produces: string[] = [
            {{#produces}}
            '{{{mediaType}}}'{{#hasMore}},{{/hasMore}}
            {{/produces}}
        ];

{{#authMethods}}
        // authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInHeader}}
        if (this.configuration.apiKey) {
            headers.set('{{keyParamName}}', this.configuration.apiKey);
        }

{{/isKeyInHeader}}
{{#isKeyInQuery}}
        if (this.configuration.apiKey) {
            queryParameters.set('{{keyParamName}}', this.configuration.apiKey);
        }

{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
        // http basic authentication required
        if (this.configuration.username || this.configuration.password) {
            headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password));
        }

{{/isBasic}}
{{#isOAuth}}
        // oauth required
        if (this.configuration.accessToken) {
            let accessToken = typeof this.configuration.accessToken === 'function'
                ? this.configuration.accessToken()
                : this.configuration.accessToken;
            headers.set('Authorization', 'Bearer ' + accessToken);
        }

{{/isOAuth}}
{{/authMethods}}
{{#hasFormParams}}
        headers.set('Content-Type', 'application/x-www-form-urlencoded');

{{/hasFormParams}}
{{#bodyParam}}
        headers.set('Content-Type', 'application/json');

{{/bodyParam}}
{{#formParams}}
        {{#isListContainer}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                formParams.append('{{baseName}}', element);
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            formParams.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        {{/isCollectionFormatMulti}}
        }
        {{/isListContainer}}
        {{^isListContainer}}
        if ({{paramName}} !== undefined) {
            formParams.set('{{baseName}}', {{paramName}});
        }
        {{/isListContainer}}

{{/formParams}}
        let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: {{httpMethod}},
            headers: headers,
{{#bodyParam}}
            body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
            body: formParams.toString(),
{{/hasFormParams}}
            search: queryParameters,
            withCredentials:this.configuration.withCredentials
        });
        // https://github.com/swagger-api/swagger-codegen/issues/4037
        if (extraHttpRequestParams) {
            requestOptions = (Object).assign(requestOptions, extraHttpRequestParams);
        }

        return this.http.request(path, requestOptions);
    }

{{/operation}}
}
{{/operations}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy