forked from nativescript-community/https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathns-http-xhr-backend.ts
More file actions
128 lines (116 loc) · 5.11 KB
/
ns-http-xhr-backend.ts
File metadata and controls
128 lines (116 loc) · 5.11 KB
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
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { XhrFactory } from '@angular/common';
import { HttpErrorResponse, HttpEvent, HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable, from, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { NSFileSystem, NsHttpBackEnd } from '@nativescript/angular';
import { Headers, HttpsRequestObject, HttpsRequestOptions, HttpsResponse, request as httpsRequest } from '@nativescript-community/https';
import { ExcludedService } from './excluded.service';
/** Https request default options. */
export type HttpsRequestDefaultOptions = Pick<HttpsRequestOptions, 'timeout' | 'allowLargeResponse' | 'cachePolicy' | 'cookiesEnabled'> & { useLegacy?: boolean };
/** Page size injection token. */
export const HTTPS_REQUEST_DEFAULT_OPTIONS = new InjectionToken<HttpsRequestDefaultOptions>('HTTPS_REQUEST_DEFAULT_OPTIONS');
@Injectable()
export class NativeScriptHttpXhrBackend extends NsHttpBackEnd {
constructor(
xhrFactory: XhrFactory,
nsFileSystem: NSFileSystem,
private readonly _excludedService: ExcludedService,
@Optional()
@Inject(HTTPS_REQUEST_DEFAULT_OPTIONS)
private readonly _defaults?: HttpsRequestDefaultOptions
) {
super(xhrFactory, nsFileSystem);
}
public handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
let result: Observable<HttpEvent<any>>;
if (this._isLocalRequest(req.url) || this._excludedService.skipSslPinning(req)) {
result = super.handle(req);
} else {
result = this._request(req).pipe(
map((response: HttpsResponse) => {
if (response.statusCode < 200 || response.statusCode > 299) {
throw this._mapHttpErrorResponse(response, req);
}
return new HttpResponse({
body: response.content,
headers: new HttpHeaders(response.headers),
status: response.statusCode,
statusText: response.reason,
url: req.url
});
}),
catchError((error) => throwError(() => error))
);
}
return result;
}
private _isLocalRequest(url: string): boolean {
return url.indexOf('~') === 0 || url.indexOf('/') === 0;
}
private _request(request: HttpRequest<any>) {
const method = request.method as 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
let url = request.url;
if (request.params) {
const params = request.params.toString();
if (params.length) {
const qIdx = url.indexOf('?');
const sep = qIdx === -1 ? '?' : (qIdx < url.length - 1 ? '&' : '');
url += sep + params;
}
}
return from(
httpsRequest(
{
url,
method,
headers: this._mapHeaders(request),
params: this._mapParams(request),
body: request.body,
timeout: this._defaults?.timeout ?? 3 * 60,
allowLargeResponse: this._defaults?.allowLargeResponse ?? true,
cachePolicy: this._defaults?.cachePolicy ?? 'noCache',
cookiesEnabled: this._defaults?.cookiesEnabled ?? false
},
this._defaults?.useLegacy ?? true
)
);
}
private _mapHeaders(request: HttpRequest<any>): Headers {
const headerKeys = request.headers.keys();
const headers = headerKeys.reduce<Headers>((accumulator, key) => {
const values = request.headers.getAll(key);
if (values !== null && values !== undefined) {
accumulator[key] = values.length > 1 ? values.join(' ') : values[0];
}
return accumulator;
}, {});
if (Object.keys(headers).length) {
return headers;
}
return {
'Content-Type': 'application/json',
Accept: 'application/json'
};
}
private _mapParams(request: HttpRequest<any>): HttpsRequestObject {
const paramKeys = request.params.keys();
const params = paramKeys.reduce<HttpsRequestObject>((accumulator, key) => {
const values = request.params.getAll(key);
if (values !== null && values !== undefined) {
accumulator[key] = values.length > 1 ? values : values[0];
}
return accumulator;
}, {});
return params;
}
private _mapHttpErrorResponse(error: HttpsResponse, request: HttpRequest<any>): HttpErrorResponse {
return new HttpErrorResponse({
error: error.content,
status: error.statusCode,
headers: new HttpHeaders(error.headers),
statusText: error.reason || (typeof error.content === 'string' && error.content) || String(error.statusCode),
url: request.url
});
}
}