-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
AngularClient.liquid
121 lines (112 loc) · 6.47 KB
/
AngularClient.liquid
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
{% if HasOperations -%}
{% if GenerateClientInterfaces -%}
{% if ExportTypes %}export {% endif %}interface I{{ Class }} {
{% for operation in Operations -%}
{% template Client.Method.Documentation %}
{{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false%}, {% endif %}{% endfor %}): Observable<{{ operation.ResultType }}>;
{% endfor -%}}
{%- endif -%}
{% if Framework.Angular.UseSingletonProvider -%}
@Injectable({
providedIn: 'root'
})
{% else -%}
@Injectable()
{%- endif -%}
{% if ExportTypes %}export {% endif %}class {{ Class }} {% if HasBaseClass %}extends {{ BaseClass }} {% endif %}{% if GenerateClientInterfaces %}implements I{{ Class }} {% endif %}{
private http: {{ Framework.Angular.HttpClass }};
private baseUrl: string;
protected jsonParseReviver: {% if SupportsStrictNullChecks %}((key: string, value: any) => any) | undefined{% else %}(key: string, value: any) => any{% endif %} = undefined;
{%- if HasExtendedConstructor == false -%}
constructor({% if HasConfigurationClass %}@Inject({{ ConfigurationClass }}) configuration: {{ ConfigurationClass }}, {% endif %}@Inject({{ Framework.Angular.HttpClass }}) http: {{ Framework.Angular.HttpClass }}, @Optional() @Inject({{ Framework.Angular.BaseUrlTokenName }}) baseUrl?: string) {
{%- if HasBaseClass -%}
super({% if HasConfigurationClass %}configuration{% endif %});
{%- endif -%}
this.http = http;
{%- if UseGetBaseUrlMethod -%}
this.baseUrl = baseUrl ?? this.getBaseUrl("{{ BaseUrl }}");
{%- else -%}
this.baseUrl = baseUrl ?? "{{ BaseUrl }}";
{%- endif -%}
}
{%- endif -%}
{% if HasExtensionCode -%}
{{ ExtensionCode }}
{%- endif -%}
{% for operation in Operations %}
{% template Client.Method.Documentation %}
{{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false or IncludeHttpContext == true%}, {% endif %}{% endfor %}{% if IncludeHttpContext%}httpContext?: HttpContext{% endif %}): Observable<{{ operation.ResultType }}> {
{% template Client.RequestUrl %}
{%- if operation.HasBody -%}
{% template Client.RequestBody %}
{%- endif -%}
let options_ : any = {
{%- if operation.HasBody -%}
body: content_,
{%- endif -%}
{%- if Framework.Angular.UseHttpClient -%}
observe: "response",
responseType: "blob",
{%- if Framework.Angular.WithCredentials -%}
withCredentials: true,
{%- endif -%}
{%- else -%}
method: "{{ operation.HttpMethodLower }}",
{%- endif -%}
{%- if operation.IsFile and Framework.Angular.UseHttpClient == false -%}
responseType: ResponseContentType.Blob,
{%- endif -%}
{%- if IncludeHttpContext -%}
context: httpContext,
{%- endif -%}
headers: new {% if Framework.Angular.UseHttpClient %}HttpHeaders{% else %}Headers{% endif %}({
{%- for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{%- endfor -%}
{%- if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{%- endif -%}
{%- if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
"Accept": "{{ operation.Produces }}"
{%- endif -%}
})
};
{% if UseTransformOptionsMethod -%}
return {{ Framework.RxJs.ObservableFromMethod }}(this.transformOptions(options_)).{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}(transformedOptions_ => {
return this.http.request({% if Framework.Angular.UseHttpClient %}"{{ operation.HttpMethodLower }}", {% endif %}url_, transformedOptions_);
}){% if Framework.UseRxJs6 %}){% endif %}.{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}((response_: any) => {
{%- else -%}
return this.http.request({% if Framework.Angular.UseHttpClient %}"{{ operation.HttpMethodLower }}", {% endif %}url_, options_).{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableMergeMapMethod }}((response_ : any) => {
{%- endif -%}
{%- if UseTransformResultMethod -%}
return this.transformResult(url_, response_, (r) => this.process{{ operation.ActualOperationNameUpper }}(r as any));
{%- else -%}
return this.process{{ operation.ActualOperationNameUpper }}(response_);
{%- endif -%}
}){% if Framework.UseRxJs6 %}){% endif %}.{% if Framework.UseRxJs6 %}pipe({% endif %}{{ Framework.RxJs.ObservableCatchMethod }}((response_: any) => {
if (response_ instanceof {% if Framework.Angular.UseHttpClient %}HttpResponseBase{% else %}Response{% endif %}) {
try {
{%- if UseTransformResultMethod -%}
return this.transformResult(url_, response_, (r) => this.process{{ operation.ActualOperationNameUpper }}(r as any));
{%- else -%}
return this.process{{ operation.ActualOperationNameUpper }}(response_ as any);
{%- endif -%}
} catch (e) {
return {{ Framework.RxJs.ObservableThrowMethod }}(e) as any as Observable<{{ operation.ResultType }}>;
}
} else
return {{ Framework.RxJs.ObservableThrowMethod }}(response_) as any as Observable<{{ operation.ResultType }}>;
}){% if Framework.UseRxJs6 %}){% endif %};
}
protected process{{ operation.ActualOperationNameUpper }}(response: {% if Framework.Angular.UseHttpClient %}HttpResponseBase{% else %}Response{% endif %}): Observable<{{ operation.ResultType }}> {
const status = response.status;
{%- if Framework.Angular.UseHttpClient -%}
const responseBlob =
response instanceof HttpResponse ? response.body :
(response as any).error instanceof Blob ? (response as any).error : undefined;
{%- endif -%}
{% template Client.ProcessResponse %}
}
{% endfor -%}
}
{%- endif -%}