|
| 1 | +// TODO: better import syntax? |
| 2 | +import { BaseAPIRequestFactory, RequiredError } from './baseapi'; |
| 3 | +import {Configuration} from '../configuration'; |
| 4 | +import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http'; |
| 5 | +{{#platforms}} |
| 6 | +{{#node}} |
| 7 | +import * as FormData from "form-data"; |
| 8 | +{{/node}} |
| 9 | +{{/platforms}} |
| 10 | +import {ObjectSerializer} from '../models/ObjectSerializer'; |
| 11 | +import {ApiException} from './exception'; |
| 12 | +import {isCodeInRange} from '../util'; |
| 13 | +{{#useInversify}} |
| 14 | +import { injectable } from "inversify"; |
| 15 | +{{/useInversify}} |
| 16 | + |
| 17 | +{{#imports}} |
| 18 | +import { {{classname}} } from '..{{filename}}'; |
| 19 | +{{/imports}} |
| 20 | +{{#operations}} |
| 21 | + |
| 22 | +/** |
| 23 | + * {{#description}}{{{description}}}{{/description}}{{^description}}no description{{/description}} |
| 24 | + */ |
| 25 | +{{#useInversify}} |
| 26 | +@injectable() |
| 27 | +{{/useInversify}} |
| 28 | +export class {{classname}}RequestFactory extends BaseAPIRequestFactory { |
| 29 | + |
| 30 | + {{#operation}} |
| 31 | + /** |
| 32 | + {{#notes}} |
| 33 | + * {{¬es}} |
| 34 | + {{/notes}} |
| 35 | + {{#summary}} |
| 36 | + * {{&summary}} |
| 37 | + {{/summary}} |
| 38 | + {{#allParams}} |
| 39 | + * @param {{paramName}} {{description}} |
| 40 | + {{/allParams}} |
| 41 | + */ |
| 42 | + public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration): Promise<RequestContext> { |
| 43 | + let config = options || this.configuration; |
| 44 | + {{#allParams}} |
| 45 | + |
| 46 | + {{#required}} |
| 47 | + // verify required parameter '{{paramName}}' is not null or undefined |
| 48 | + if ({{paramName}} === null || {{paramName}} === undefined) { |
| 49 | + throw new RequiredError('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.'); |
| 50 | + } |
| 51 | + |
| 52 | + {{/required}} |
| 53 | + {{/allParams}} |
| 54 | + |
| 55 | + // Path Params |
| 56 | + const localVarPath = '{{{path}}}'{{#pathParams}} |
| 57 | + .replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}}; |
| 58 | + |
| 59 | + // Make Request Context |
| 60 | + const requestContext = config.baseServer.makeRequestContext(localVarPath, HttpMethod.{{httpMethod}}); |
| 61 | + requestContext.setHeaderParam("Accept", "application/json, */*;q=0.8") |
| 62 | + |
| 63 | + // Query Params |
| 64 | + {{#queryParams}} |
| 65 | + if ({{paramName}} !== undefined) { |
| 66 | + requestContext.setQueryParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "{{dataFormat}}")); |
| 67 | + } |
| 68 | + {{/queryParams}} |
| 69 | + |
| 70 | + // Header Params |
| 71 | + {{#headerParams}} |
| 72 | + requestContext.setHeaderParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "{{dataFormat}}")); |
| 73 | + {{/headerParams}} |
| 74 | + |
| 75 | + // Form Params |
| 76 | + {{#hasFormParams}} |
| 77 | + let localVarFormParams = new FormData(); |
| 78 | + {{/hasFormParams}} |
| 79 | + |
| 80 | + {{#formParams}} |
| 81 | + {{#isListContainer}} |
| 82 | + if ({{paramName}}) { |
| 83 | + {{#isCollectionFormatMulti}} |
| 84 | + {{paramName}}.forEach((element) => { |
| 85 | + localVarFormParams.append('{{baseName}}', element as any); |
| 86 | + }) |
| 87 | + {{/isCollectionFormatMulti}} |
| 88 | + {{^isCollectionFormatMulti}} |
| 89 | + // TODO: replace .append with .set |
| 90 | + localVarFormParams.append('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"])); |
| 91 | + {{/isCollectionFormatMulti}} |
| 92 | + } |
| 93 | + {{/isListContainer}} |
| 94 | + {{^isListContainer}} |
| 95 | + if ({{paramName}} !== undefined) { |
| 96 | + // TODO: replace .append with .set |
| 97 | + {{^isFile}} |
| 98 | + localVarFormParams.append('{{baseName}}', {{paramName}} as any); |
| 99 | + {{/isFile}} |
| 100 | + {{#isFile}} |
| 101 | + {{#platforms}} |
| 102 | + {{#node}} |
| 103 | + localVarFormParams.append('{{baseName}}', {{paramName}}.data, {{paramName}}.name); |
| 104 | + {{/node}} |
| 105 | + {{#browser}} |
| 106 | + localVarFormParams.append('{{baseName}}', {{paramName}}, {{paramName}}.name); |
| 107 | + {{/browser}} |
| 108 | + {{/platforms}} |
| 109 | + {{/isFile}} |
| 110 | + } |
| 111 | + {{/isListContainer}} |
| 112 | + {{/formParams}} |
| 113 | + {{#hasFormParams}} |
| 114 | + requestContext.setBody(localVarFormParams); |
| 115 | + {{/hasFormParams}} |
| 116 | + |
| 117 | + // Body Params |
| 118 | + {{#bodyParam}} |
| 119 | + const contentType = ObjectSerializer.getPreferredMediaType([{{#consumes}} |
| 120 | + "{{{mediaType}}}"{{#hasMore}},{{/hasMore}} |
| 121 | + {{/consumes}}]); |
| 122 | + requestContext.setHeaderParam("Content-Type", contentType); |
| 123 | + const serializedBody = ObjectSerializer.stringify( |
| 124 | + ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "{{dataFormat}}"), |
| 125 | + contentType |
| 126 | + ); |
| 127 | + requestContext.setBody(serializedBody); |
| 128 | + {{/bodyParam}} |
| 129 | + |
| 130 | + {{#hasAuthMethods}} |
| 131 | + let authMethod = null; |
| 132 | + {{/hasAuthMethods}} |
| 133 | + // Apply auth methods |
| 134 | + {{#authMethods}} |
| 135 | + authMethod = config.authMethods["{{name}}"] |
| 136 | + if (authMethod) { |
| 137 | + await authMethod.applySecurityAuthentication(requestContext); |
| 138 | + } |
| 139 | + {{/authMethods}} |
| 140 | + |
| 141 | + return requestContext; |
| 142 | + } |
| 143 | + |
| 144 | + {{/operation}} |
| 145 | +} |
| 146 | +{{/operations}} |
| 147 | + |
| 148 | + |
| 149 | +{{#operations}} |
| 150 | + |
| 151 | +{{#useInversify}} |
| 152 | +@injectable() |
| 153 | +{{/useInversify}} |
| 154 | +export class {{classname}}ResponseProcessor { |
| 155 | +
|
| 156 | + {{#operation}} |
| 157 | + /** |
| 158 | + * Unwraps the actual response sent by the server from the response context and deserializes the response content |
| 159 | + * to the expected objects |
| 160 | + * |
| 161 | + * @params response Response returned by the server for a request to {{nickname}} |
| 162 | + * @throws ApiException if the response code was not in [200, 299] |
| 163 | + */ |
| 164 | + public async {{nickname}}(response: ResponseContext): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> { |
| 165 | + const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]); |
| 166 | + {{#responses}} |
| 167 | + if (isCodeInRange("{{code}}", response.httpStatusCode)) { |
| 168 | + {{#dataType}} |
| 169 | + {{#isBinary}} |
| 170 | + const body: {{{dataType}}} = await response.getBodyAsFile() as any as {{{returnType}}}; |
| 171 | + {{/isBinary}} |
| 172 | + {{^isBinary}} |
| 173 | + const body: {{{dataType}}} = ObjectSerializer.deserialize( |
| 174 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 175 | + "{{{dataType}}}", "{{returnFormat}}" |
| 176 | + ) as {{{dataType}}}; |
| 177 | + {{/isBinary}} |
| 178 | + {{#is2xx}} |
| 179 | + return body; |
| 180 | + {{/is2xx}} |
| 181 | + {{^is2xx}} |
| 182 | + throw new ApiException<{{{dataType}}}>({{code}}, body); |
| 183 | + {{/is2xx}} |
| 184 | + {{/dataType}} |
| 185 | + {{^dataType}} |
| 186 | + {{#is2xx}} |
| 187 | + return; |
| 188 | + {{/is2xx}} |
| 189 | + {{^is2xx}} |
| 190 | + throw new ApiException<string>(response.httpStatusCode, "{{message}}"); |
| 191 | + {{/is2xx}} |
| 192 | + {{/dataType}} |
| 193 | + } |
| 194 | + {{/responses}} |
| 195 | + |
| 196 | + // Work around for missing responses in specification, e.g. for petstore.yaml |
| 197 | + if (response.httpStatusCode >= 200 && response.httpStatusCode <= 299) { |
| 198 | + {{#returnType}} |
| 199 | + {{#isBinary}} |
| 200 | + const body: {{{returnType}}} = await response.getBodyAsFile() as any as {{{returnType}}}; |
| 201 | + {{/isBinary}} |
| 202 | + {{^isBinary}} |
| 203 | + const body: {{{returnType}}} = ObjectSerializer.deserialize( |
| 204 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 205 | + "{{{returnType}}}", "{{returnFormat}}" |
| 206 | + ) as {{{returnType}}}; |
| 207 | + {{/isBinary}} |
| 208 | + return body; |
| 209 | + {{/returnType}} |
| 210 | + {{^returnType}} |
| 211 | + return; |
| 212 | + {{/returnType}} |
| 213 | + } |
| 214 | + |
| 215 | + let body = response.body || ""; |
| 216 | + throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\""); |
| 217 | + } |
| 218 | + |
| 219 | + {{/operation}} |
| 220 | +} |
| 221 | +{{/operations}} |
0 commit comments