typescript-node.api.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
import request = require('request');
import http = require('http');
{{^supportsES6}}
import Promise = require('bluebird');
{{/supportsES6}}
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}}{{classname}}.{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
{{/vars}}
}
{{#hasEnums}}
export namespace {{classname}} {
{{#vars}}
{{#isEnum}}
export enum {{datatypeWithEnum}} {
{{#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: request.Options): void;
}
export class HttpBasicAuth implements Authentication {
public username: string;
public password: string;
applyToRequest(requestOptions: request.Options): void {
requestOptions.auth = {
username: this.username, password: this.password
}
}
}
export class ApiKeyAuth implements Authentication {
public apiKey: string;
constructor(private location: string, private paramName: string) {
}
applyToRequest(requestOptions: request.Options): void {
if (this.location == "query") {
(requestOptions.qs)[this.paramName] = this.apiKey;
} else if (this.location == "header") {
requestOptions.headers[this.paramName] = this.apiKey;
}
}
}
export class OAuth implements Authentication {
public accessToken: string;
applyToRequest(requestOptions: request.Options): void {
requestOptions.headers["Authorization"] = "Bearer " + this.accessToken;
}
}
export class VoidAuth implements Authentication {
public username: string;
public password: string;
applyToRequest(requestOptions: request.Options): 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 _useQuerystring : boolean = false;
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
}
}
}
set useQuerystring(value: boolean) {
this._useQuerystring = value;
}
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: T1, objB: T2) {
for(let key in objB){
if(objB.hasOwnProperty(key)){
objA[key] = objB[key];
}
}
return objA;
}
{{#operation}}
/**
* {{summary}}
* {{notes}}
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
let formParams: 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) {
queryParameters['{{baseName}}'] = {{paramName}};
}
{{/queryParams}}
{{#headerParams}}
headerParams['{{baseName}}'] = {{paramName}};
{{/headerParams}}
let useFormData = false;
{{#formParams}}
if ({{paramName}} !== undefined) {
formParams['{{baseName}}'] = {{paramName}};
}
{{#isFile}}
useFormData = true;
{{/isFile}}
{{/formParams}}
let requestOptions: request.Options = {
method: '{{httpMethod}}',
qs: queryParameters,
headers: headerParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
json: true,
{{#bodyParam}}
body: {{paramName}},
{{/bodyParam}}
};
{{#authMethods}}
this.authentications.{{name}}.applyToRequest(requestOptions);
{{/authMethods}}
this.authentications.default.applyToRequest(requestOptions);
if (Object.keys(formParams).length) {
if (useFormData) {
(requestOptions).formData = formParams;
} else {
requestOptions.form = formParams;
}
}
return new Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>((resolve, reject) => {
request(requestOptions, (error, response, body) => {
if (error) {
reject(error);
} else {
if (response.statusCode >= 200 && response.statusCode <= 299) {
resolve({ response: response, body: body });
} else {
reject({ response: response, body: body });
}
}
});
});
}
{{/operation}}
}
{{/operations}}
{{/apis}}
{{/apiInfo}}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy