|
| 1 | +<% |
| 2 | + function capitalize(string) { |
| 3 | + return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase(); |
| 4 | + } |
| 5 | +
|
| 6 | + var responseType = 'void'; |
| 7 | + if (method.responseMediaType) { |
| 8 | + switch (method.responseMediaType) { |
| 9 | + case 'text/plain': |
| 10 | + responseType = 'string'; |
| 11 | + break; |
| 12 | + case 'application/json': |
| 13 | + responseType = method.responseType || 'any'; |
| 14 | + break; |
| 15 | + case 'application/octet-stream': |
| 16 | + responseType = 'File'; |
| 17 | + break; |
| 18 | + default: |
| 19 | + responseType = 'ArrayBuffer'; |
| 20 | + } |
| 21 | + } |
| 22 | +
|
| 23 | + var requestType = null; |
| 24 | + if (method.requestMediaType) { |
| 25 | + if (method.requestMediaType === 'application/json') { |
| 26 | + requestType = method.requestType; |
| 27 | + } |
| 28 | + } |
| 29 | +
|
| 30 | + var params = (method.pathParams || []).concat(method.queryParams || []).concat(method.formParams || []); |
| 31 | +
|
| 32 | + var argsVars = params.map(function(pathParam) { |
| 33 | + return pathParam.name + ': ' + pathParam.type; |
| 34 | + }); |
| 35 | +
|
| 36 | + if (requestType) { |
| 37 | + argsVars.push(varPfx + 'request : ' + requestType); |
| 38 | + } |
| 39 | +%> |
| 40 | + |
| 41 | +public <%- method.name %>(<%- argsVars.join(', ') %>): Observable<<%- responseType %>>{ |
| 42 | + const <%- varPfx %>headers = new HttpHeaders({ |
| 43 | + 'Accept': '<%- method.responseMediaType || '*/*' %>'<% if (method.requestMediaType) { %>, |
| 44 | + 'Content-Type': '<%- method.requestMediaType %>' |
| 45 | + <% } %> |
| 46 | + }); |
| 47 | + <% if (method.queryParams) { %> |
| 48 | + const <%- varPfx %>params = new HttpParams({ |
| 49 | + fromObject: { |
| 50 | + <% method.queryParams.forEach(function (param, index) { %> |
| 51 | + '<%- param.name %>': this.<%- varPfx %>toString(<%- param.name %>)<%- index !== method.queryParams.length - 1 ? ',' : '' %> |
| 52 | + <% }); %> |
| 53 | + } |
| 54 | + }); |
| 55 | + <% } %> |
| 56 | + <% if (method.formParams && method.requestMediaType !== 'multipart/form-data') { %> |
| 57 | + const <%- varPfx %>body = new HttpParams({ |
| 58 | + fromObject: { |
| 59 | + <% method.formParams.forEach(function (param, index) { %> |
| 60 | + '<%- param.name %>': this.<%- varPfx %>toString(<%- param.name %>)<%- index !== method.formParams.length - 1 ? ',' : '' %> |
| 61 | + <% }); %> |
| 62 | + } |
| 63 | + }); |
| 64 | + <% } %> |
| 65 | + <% if (method.formParams && method.requestMediaType === 'multipart/form-data') { %> |
| 66 | + const <%- varPfx %>body = new FormData(); |
| 67 | + <% method.formParams.forEach(function (param, index) { %> |
| 68 | + <% if (param.type === 'string' || param.type === 'Blob') { %> |
| 69 | + <%- varPfx %>body.set('<%- param.name %>', <%- param.name %>); |
| 70 | + <% } else { %> |
| 71 | + <%- varPfx %>body.set('<%- param.name %>', this.<%- varPfx %>toString(<%- param.name %>)); |
| 72 | + <% } %> |
| 73 | + <% }); %> |
| 74 | + <% } %> |
| 75 | + return this.<%- varPfx %>http.<%- method.method.toLowerCase() %>(`${this.<%- varPfx %>rootUrl}<%- method.path.replace(/{/g,'${') %>`, |
| 76 | + <% if (method.method === 'PATCH' || method.method === 'POST' || method.method === 'PUT') { %> |
| 77 | + <% if (method.formParams) { %> |
| 78 | + <%- varPfx %>body, |
| 79 | + <% } else if (requestType) { %> |
| 80 | + <%- varPfx %>request, |
| 81 | + <% } else { %> |
| 82 | + null, |
| 83 | + <% } %> |
| 84 | + <% } %> { |
| 85 | + <% if (responseType === 'ArrayBuffer') { %> |
| 86 | + responseType: 'arraybuffer', |
| 87 | + <% } else if (responseType === 'File') { %> |
| 88 | + responseType: 'blob', |
| 89 | + <% } else { %> |
| 90 | + responseType: 'json', |
| 91 | + <% } %> |
| 92 | + <% if (method.queryParams) { %> |
| 93 | + params: <%- varPfx %>params, |
| 94 | + <% } %> |
| 95 | + headers: <%- varPfx %>headers |
| 96 | + }); |
| 97 | +} |
0 commit comments