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
| import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators';
export class JsonInterceptor implements HttpInterceptor { private utcRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/; private dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)$/; private zoneRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)([+-])(\d{2}):(\d{2})$/;
intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { return next.handle(req).pipe( tap((event: HttpEvent<any>) => { if (event instanceof HttpResponse) { this.convertDates(event.body); } }) ); }
private convertDates(object: Object) { if (!object || !(object instanceof Object)) { return; }
if (object instanceof Array) { for (const item of object) { this.convertDates(item); } }
for (const key of Object.keys(object)) { const value = object[key];
if (value instanceof Array) { for (const item of value) { this.convertDates(item); } }
if (value instanceof Object) { this.convertDates(value); }
if (typeof value === 'string') { let a; if (typeof value === 'string') { a = this.utcRegex.exec(value); if (a) { object[key] = new Date( Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]) ); continue; } a = this.dateRegex.exec(value); if (a) { object[key] = new Date( +a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6] ); continue; } a = this.zoneRegex.exec(value); if (a) { const dir = a[7] === '+' ? -1 : 1; object[key] = new Date( Date.UTC( +a[1], +a[2] - 1, +a[3], +a[4] + dir * a[8], +a[5] + dir * a[9], +a[6] ) ); continue; } } } } } }
|