/modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache

https://gitlab.com/akkhil2012/swagger-codegen · Mustache · 260 lines · 228 code · 32 blank · 0 comment · 22 complexity · c85e2bfbc0128bcab74425a922b52cd9 MD5 · raw file

  1. import request = require('request');
  2. import promise = require('bluebird');
  3. import http = require('http');
  4. // ===============================================
  5. // This file is autogenerated - Please do not edit
  6. // ===============================================
  7. /* tslint:disable:no-unused-variable */
  8. {{#models}}
  9. {{#model}}
  10. {{#description}}
  11. /**
  12. * {{{description}}}
  13. */
  14. {{/description}}
  15. export class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
  16. {{#vars}}
  17. {{#description}}
  18. /**
  19. * {{{description}}}
  20. */
  21. {{/description}}
  22. "{{name}}": {{#isEnum}}{{classname}}.{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
  23. {{/vars}}
  24. }
  25. {{#hasEnums}}
  26. export namespace {{classname}} {
  27. {{#vars}}
  28. {{#isEnum}}
  29. export enum {{datatypeWithEnum}} { {{#allowableValues}}{{#values}}
  30. {{.}} = <any> '{{.}}'{{^-last}},{{/-last}}{{/values}}{{/allowableValues}}
  31. }
  32. {{/isEnum}}
  33. {{/vars}}
  34. }
  35. {{/hasEnums}}
  36. {{/model}}
  37. {{/models}}
  38. interface Authentication {
  39. /**
  40. * Apply authentication settings to header and query params.
  41. */
  42. applyToRequest(requestOptions: request.Options): void;
  43. }
  44. class HttpBasicAuth implements Authentication {
  45. public username: string;
  46. public password: string;
  47. applyToRequest(requestOptions: request.Options): void {
  48. requestOptions.auth = {
  49. username: this.username, password: this.password
  50. }
  51. }
  52. }
  53. class ApiKeyAuth implements Authentication {
  54. public apiKey: string;
  55. constructor(private location: string, private paramName: string) {
  56. }
  57. applyToRequest(requestOptions: request.Options): void {
  58. if (this.location == "query") {
  59. (<any>requestOptions.qs)[this.paramName] = this.apiKey;
  60. } else if (this.location == "header") {
  61. requestOptions.headers[this.paramName] = this.apiKey;
  62. }
  63. }
  64. }
  65. class OAuth implements Authentication {
  66. public accessToken: string;
  67. applyToRequest(requestOptions: request.Options): void {
  68. requestOptions.headers["Authorization"] = "Bearer " + this.accessToken;
  69. }
  70. }
  71. class VoidAuth implements Authentication {
  72. public username: string;
  73. public password: string;
  74. applyToRequest(requestOptions: request.Options): void {
  75. // Do nothing
  76. }
  77. }
  78. {{#apiInfo}}
  79. {{#apis}}
  80. {{#operations}}
  81. {{#description}}
  82. /**
  83. * {{&description}}
  84. */
  85. {{/description}}
  86. export class {{classname}} {
  87. protected basePath = '{{basePath}}';
  88. protected defaultHeaders : any = {};
  89. public authentications = {
  90. 'default': <Authentication>new VoidAuth(),
  91. {{#authMethods}}
  92. {{#isBasic}}
  93. '{{name}}': new HttpBasicAuth(),
  94. {{/isBasic}}
  95. {{#isApiKey}}
  96. '{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{^isKeyInHeader}}'query'{{/isKeyInHeader}}, '{{keyParamName}}'),
  97. {{/isApiKey}}
  98. {{#isOAuth}}
  99. '{{name}}': new OAuth(),
  100. {{/isOAuth}}
  101. {{/authMethods}}
  102. }
  103. constructor(basePath?: string);
  104. {{#authMethods}}
  105. {{#isBasic}}
  106. constructor(username: string, password: string, basePath?: string);
  107. {{/isBasic}}
  108. {{/authMethods}}
  109. constructor(basePathOrUsername: string, password?: string, basePath?: string) {
  110. if (password) {
  111. {{#authMethods}}
  112. {{#isBasic}}
  113. this.username = basePathOrUsername;
  114. this.password = password
  115. {{/isBasic}}
  116. {{/authMethods}}
  117. if (basePath) {
  118. this.basePath = basePath;
  119. }
  120. } else {
  121. if (basePathOrUsername) {
  122. this.basePath = basePathOrUsername
  123. }
  124. }
  125. }
  126. {{#authMethods}}
  127. {{#isBasic}}
  128. set username(username: string) {
  129. this.authentications.{{name}}.username = username;
  130. }
  131. set password(password: string) {
  132. this.authentications.{{name}}.password = password;
  133. }
  134. {{/isBasic}}
  135. {{#isApiKey}}
  136. set apiKey(key: string) {
  137. this.authentications.{{name}}.apiKey = key;
  138. }
  139. {{/isApiKey}}
  140. {{#isOAuth}}
  141. set accessToken(token: string) {
  142. this.authentications.{{name}}.accessToken = token;
  143. }
  144. {{/isOAuth}}
  145. {{/authMethods}}
  146. private extendObj<T1,T2>(objA: T1, objB: T2) {
  147. for(let key in objB){
  148. if(objB.hasOwnProperty(key)){
  149. objA[key] = objB[key];
  150. }
  151. }
  152. return <T1&T2>objA;
  153. }
  154. {{#operation}}
  155. /**
  156. * {{summary}}
  157. * {{notes}}
  158. {{#allParams}}* @param {{paramName}} {{description}}
  159. {{/allParams}}*/
  160. public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
  161. const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
  162. .replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
  163. let queryParameters: any = {};
  164. let headerParams: any = this.extendObj({}, this.defaultHeaders);
  165. let formParams: any = {};
  166. {{#allParams}}{{#required}}
  167. // verify required parameter '{{paramName}}' is set
  168. if (!{{paramName}}) {
  169. throw new Error('Missing required parameter {{paramName}} when calling {{nickname}}');
  170. }
  171. {{/required}}{{/allParams}}
  172. {{#queryParams}}
  173. if ({{paramName}} !== undefined) {
  174. queryParameters['{{baseName}}'] = {{paramName}};
  175. }
  176. {{/queryParams}}
  177. {{#headerParams}}
  178. headerParams['{{baseName}}'] = {{paramName}};
  179. {{/headerParams}}
  180. let useFormData = false;
  181. {{#formParams}}
  182. if ({{paramName}} !== undefined) {
  183. formParams['{{baseName}}'] = {{paramName}};
  184. }
  185. {{#isFile}}
  186. useFormData = true;
  187. {{/isFile}}
  188. {{/formParams}}
  189. let localVarDeferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>();
  190. let requestOptions: request.Options = {
  191. method: '{{httpMethod}}',
  192. qs: queryParameters,
  193. headers: headerParams,
  194. uri: localVarPath,
  195. json: true,
  196. {{#bodyParam}}
  197. body: {{paramName}},
  198. {{/bodyParam}}
  199. }
  200. {{#authMethods}}
  201. this.authentications.{{name}}.applyToRequest(requestOptions);
  202. {{/authMethods}}
  203. this.authentications.default.applyToRequest(requestOptions);
  204. if (Object.keys(formParams).length) {
  205. if (useFormData) {
  206. (<any>requestOptions).formData = formParams;
  207. } else {
  208. requestOptions.form = formParams;
  209. }
  210. }
  211. request(requestOptions, (error, response, body) => {
  212. if (error) {
  213. localVarDeferred.reject(error);
  214. } else {
  215. if (response.statusCode >= 200 && response.statusCode <= 299) {
  216. localVarDeferred.resolve({ response: response, body: body });
  217. } else {
  218. localVarDeferred.reject({ response: response, body: body });
  219. }
  220. }
  221. });
  222. return localVarDeferred.promise;
  223. }
  224. {{/operation}}
  225. }
  226. {{/operations}}
  227. {{/apis}}
  228. {{/apiInfo}}