-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.ts.handlebars
149 lines (133 loc) · 6.3 KB
/
client.ts.handlebars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { Inject, Injectable, Optional, InjectionToken } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpUrlEncodingCodec } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import * as schemas from '../schemas';
{{#if components.securitySchemes}}import { SecurityOptions } from '../schemas';{{/if}}
export const BASE_PATH: InjectionToken<string> = new InjectionToken('BASE_PATH');
@Injectable()
export class {{className info.title}} {
private basePath: string = '';
private defaultHeaders = new HttpHeaders();
{{#if components.securitySchemes}} readonly securityOptions: SecurityOptions = {};{{/if}}
constructor(
protected httpClient: HttpClient,
@Optional() @Inject(BASE_PATH) basePath: string,
) {
if (basePath) {
this.basePath = basePath.replace(/\/+$/, '');
}
}
{{#each paths as |operations path|}}
{{~#with operations.get}}{{>operation method="GET" path=path name=(coalesce operationId (concat 'get' (className @path)))}}{{/with}}
{{~#with operations.post}}{{>operation method="POST" path=path name=(coalesce operationId (concat 'post' (className @path)))}}{{/with}}
{{~#with operations.put}}{{>operation method="PUT" path=path name=(coalesce operationId (concat 'put' (className @path)))}}{{/with}}
{{~#with operations.delete}}{{>operation method="DELETE" path=path name=(coalesce operationId (concat 'delete' (className @path)))}}{{/with}}
{{~/each}}
// @TODO: different encoding styles
protected _encodeHttpParameter(value: any) {
if (Array.isArray(value)) {
return value.map(_ => this._encodeHttpParameter(_)).join(',');
} else if (value !== null && value !== undefined) {
return String(value);
} else {
return '';
}
}
// @TODO: different encoding styles
protected _encodeHttpBody(value: any, encoding: string) {
switch (encoding) {
case 'application/json':
return value;
case 'application/x-www-form-urlencoded':
const formData = new FormData();
Object.keys(value).forEach(key => {
formData.append(key, this._encodeHttpParameter(value[key]));
});
return formData;
default:
throw new Error(`Encoding not supported: ${encoding}`);
}
}
}
{{#*inline "operation"}}
{{#if summary}}/** {{summary}} */{{/if}}
public {{name}}(
{{#each parameters}}{{#with (deref this @root)}}{{#unless (eq? in "cookie")~}}
{{valueName name}}{{#unless required}}?{{/unless}}: {{>schema (coalesce schema content.schema) namespace="schemas"}},
{{/unless}}{{/with}}{{/each~}}
{{#with (deref requestBody @root)}}body{{#unless required}}?{{/unless}}: {{#first content}}{{>schema schema namespace="schemas"}},{{/first}}{{/with}}
{{#each (coalesce security @root.security)}}{{#each .}}{{@key}}: SecurityOptions.{{schemaRefToTypeName @key}} = this.securityOptions.{{@key}},{{/each}}{{/each}}
): Observable<{{#each (successResponses responses)~}}
{{#with (deref this @root)}}
{{~#unless content}}void{{/unless}}
{{~#each content}}{{>schema schema namespace="schemas"}}{{~#unless @last}} | {{/unless}}{{/each}}
{{~/with}}
{{~#unless @last}} | {{/unless}}
{{~/each}}> {
let path = '{{path}}';
let params = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
let headers = this.defaultHeaders;
{{~#with (deref requestBody @root)}}{{#first content}}
headers.set('Content-Type', '{{@key}}');{{/first}}{{/with}}
{{#each parameters}}
{{#with (deref this @root)}}{{#switch in}}
{{#case "query"}}{{#unless required}}if ({{valueName name}} !== undefined) {{/unless}}params = params.set('{{name}}', this._encodeHttpParameter({{valueName name}}));{{/case}}
{{#case "header"}}{{#unless required}}if ({{valueName name}} !== undefined) {{/unless}}headers = headers.set('{{name}}', this._encodeHttpParameter({{valueName name}}));{{/case}}
{{#case "path"}}path = path.replace('{{concat "{" name "}"}}', this._encodeHttpParameter({{valueName name}}));{{/case}}
{{/switch}}{{/with}}
{{/each}}
{{#each (coalesce security @root.security)}}{{#first .}}
if ({{@key}}) {
{{#with (deref (concat "#/components/securitySchemes/" @key) @root)}}{{#switch type}}
{{#case "apiKey"}}{{#switch in}}
{{#case "query"}}params = params.set('{{@key}}', this._encodeHttpParameter({{@key}}.key));{{/case}}
{{#case "header"}}headers = headers.set('{{@key}}', this._encodeHttpParameter({{@key}}.key));{{/case}}
{{/switch}}{{/case}}
{{#case "http"}}{{#switch scheme}}
{{#case "bearer"}}headers = headers.set('Authorization', this._encodeHttpParameter('Bearer ' + {{@key}}.token));{{/case}}
{{#case "basic"}}headers = headers.set('Authorization', this._encodeHttpParameter({{@key}}.login + ':' + {{@key}}.password));{{/case}}
{{/switch}}{{/case}}
{{/switch}}{{/with}}
}
{{/first}}{{/each}}
{{#each responses}}
{{#if @first}}
{{#with (deref this)}}
{{#first content}}
{{#if (eq? @key "application/json")}}
return this.httpClient.request<any>('{{../../method}}', `${this.basePath}${path}`, {
{{#with (deref ../../requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}}
responseType: 'json',
params,
headers,
});
{{/if}}
{{#if (contains? @key "text")}}
return this.httpClient.request('{{../../method}}', `${this.basePath}${path}`, {
{{#with (deref ../../requestBody @root)}}body: this._encodeHttpBody(body, {{#first content}}'{{@key}}'{{/first}}),{{/with}}
responseType: 'text',
params,
headers,
});
{{/if}}
{{/first}}
{{/with}}
{{/if}}
{{/each}}
}
{{/inline}}
/**
* CustomHttpUrlEncodingCodec
* Fix plus sign (+) not encoding, so sent as blank space
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
*/
export class CustomHttpUrlEncodingCodec extends HttpUrlEncodingCodec {
encodeKey(k: string): string {
k = super.encodeKey(k);
return k.replace(/\+/gi, '%2B');
}
encodeValue(v: string): string {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}