|
| 1 | +import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'; |
| 2 | +import { |
| 3 | + HttpErrorResponse, |
| 4 | + HttpEvent, |
| 5 | + HttpHeaders, |
| 6 | + HttpRequest, |
| 7 | + HttpResponse, |
| 8 | + XhrFactory |
| 9 | +} from '@angular/common/http'; |
| 10 | +import { Observable, from, throwError } from 'rxjs'; |
| 11 | +import { catchError, map } from 'rxjs/operators'; |
| 12 | +import { NSFileSystem, NsHttpBackEnd } from '@nativescript/angular'; |
| 13 | +import { |
| 14 | + Headers, |
| 15 | + HttpsRequestObject, |
| 16 | + HttpsRequestOptions, |
| 17 | + HttpsResponse, |
| 18 | + request as httpsRequest |
| 19 | +} from '../request'; |
| 20 | +import { ExcludedService } from './excluded.service'; |
| 21 | + |
| 22 | +/** Https request default options. */ |
| 23 | +export type HttpsRequestDefaultOptions |
| 24 | + = Pick<HttpsRequestOptions, 'timeout' | 'allowLargeResponse' | 'cachePolicy' | 'cookiesEnabled'> |
| 25 | + & { useLegacy?: boolean; }; |
| 26 | + |
| 27 | +/** Page size injection token. */ |
| 28 | +export const HTTPS_REQUEST_DEFAULT_OPTIONS |
| 29 | + = new InjectionToken<HttpsRequestDefaultOptions>('HTTPS_REQUEST_DEFAULT_OPTIONS'); |
| 30 | + |
| 31 | +@Injectable() |
| 32 | +export class NativeScriptHttpXhrBackend extends NsHttpBackEnd { |
| 33 | + constructor( |
| 34 | + xhrFactory: XhrFactory, |
| 35 | + nsFileSystem: NSFileSystem, |
| 36 | + private readonly _excludedService: ExcludedService, |
| 37 | + @Optional() |
| 38 | + @Inject(HTTPS_REQUEST_DEFAULT_OPTIONS) |
| 39 | + private readonly _defaults?: HttpsRequestDefaultOptions |
| 40 | + ) { |
| 41 | + super(xhrFactory, nsFileSystem); |
| 42 | + } |
| 43 | + |
| 44 | + public handle(req: HttpRequest<any>): Observable<HttpEvent<any>> { |
| 45 | + let result: Observable<HttpEvent<any>>; |
| 46 | + if (this._isLocalRequest(req.url) || this._excludedService.skipSslPinning(req)) { |
| 47 | + result = super.handle(req); |
| 48 | + } else { |
| 49 | + result = this._request(req) |
| 50 | + .pipe( |
| 51 | + map((response: HttpsResponse) => { |
| 52 | + return new HttpResponse({ |
| 53 | + body: response.content, |
| 54 | + headers: new HttpHeaders(response.headers), |
| 55 | + status: response.statusCode, |
| 56 | + statusText: response.reason, |
| 57 | + url: req.url |
| 58 | + }); |
| 59 | + }), |
| 60 | + catchError((error) => { |
| 61 | + return throwError(() => new HttpErrorResponse({ |
| 62 | + status: error.status, |
| 63 | + headers: error.headers, |
| 64 | + statusText: error.statusText, |
| 65 | + url: req.url |
| 66 | + })); |
| 67 | + }) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private _isLocalRequest(url: string): boolean { |
| 75 | + return url.indexOf('~') === 0 || url.indexOf('/') === 0; |
| 76 | + } |
| 77 | + |
| 78 | + private _request(request: HttpRequest<any>) { |
| 79 | + const method = request.method as ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD'); |
| 80 | + return from(httpsRequest({ |
| 81 | + url: request.url, |
| 82 | + method, |
| 83 | + headers: this._mapHeaders(request), |
| 84 | + params: this._mapParams(request), |
| 85 | + body: request.body, |
| 86 | + timeout: this._defaults?.timeout ?? 3 * 60, |
| 87 | + allowLargeResponse: this._defaults?.allowLargeResponse ?? true, |
| 88 | + cachePolicy: this._defaults?.cachePolicy ?? 'noCache', |
| 89 | + cookiesEnabled: this._defaults?.cookiesEnabled ?? false |
| 90 | + }, this._defaults?.useLegacy ?? true)); |
| 91 | + } |
| 92 | + |
| 93 | + private _mapHeaders(request: HttpRequest<any>): Headers { |
| 94 | + const headerKeys = request.headers.keys(); |
| 95 | + const headers = headerKeys.reduce<Headers>((accumulator, key) => { |
| 96 | + const values = request.headers.getAll(key); |
| 97 | + if (values !== null && values !== undefined) { |
| 98 | + accumulator[key] = values.length > 1 ? values.join(' ') : values[0]; |
| 99 | + } |
| 100 | + return accumulator; |
| 101 | + }, { }); |
| 102 | + |
| 103 | + if (Object.keys(headers).length) { |
| 104 | + return headers; |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + 'Content-Type': 'application/json', |
| 109 | + 'Accept': 'application/json' |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private _mapParams(request: HttpRequest<any>): HttpsRequestObject { |
| 114 | + const paramKeys = request.params.keys(); |
| 115 | + const params = paramKeys.reduce<HttpsRequestObject>((accumulator, key) => { |
| 116 | + const values = request.params.getAll(key); |
| 117 | + if (values !== null && values !== undefined) { |
| 118 | + accumulator[key] = values.length > 1 ? values : values[0]; |
| 119 | + } |
| 120 | + return accumulator; |
| 121 | + }, { }); |
| 122 | + return params; |
| 123 | + } |
| 124 | +} |
0 commit comments