Forked from martinobordin/AngularRxJs5DateHttpInterceptor.ts
Last active
October 30, 2021 09:26
-
-
Save aml360/7e8efc275b0adb46bd5bb1b555b42467 to your computer and use it in GitHub Desktop.
An Angular interceptor to parse string dates (ISO8601 format) from server response to JS Date Object. There are both RxJs 5 and RxJs 6 versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse, HttpResponse } from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/operator/do'; | |
@Injectable() | |
export class AngularDateHttpInterceptor implements HttpInterceptor { | |
// Migrated from AngularJS https://raw.githubusercontent.com/Ins87/angular-date-interceptor/master/src/angular-date-interceptor.js | |
iso8601 = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(([+-]\d\d:\d\d)|Z)?$/; | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
return next.handle(req).do((event: HttpEvent<any>) => { | |
if (event instanceof HttpResponse) { | |
const body = event.body; | |
this.convertToDate(body); | |
} | |
}, (err: any) => { | |
if (err instanceof HttpErrorResponse) { | |
if (err.status === 401) { | |
} | |
} | |
}); | |
} | |
convertToDate(body) { | |
if (body === null || body === undefined) { | |
return body; | |
} | |
if (typeof body !== 'object') { | |
return body; | |
} | |
for (const key of Object.keys(body)) { | |
const value = body[key]; | |
if (this.isIso8601(value)) { | |
body[key] = new Date(value); | |
} else if (typeof value === 'object') { | |
this.convertToDate(value); | |
} | |
} | |
} | |
isIso8601(value) { | |
if (value === null || value === undefined) { | |
return false; | |
} | |
return this.iso8601.test(value); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
HttpRequest, | |
HttpHandler, | |
HttpEvent, | |
HttpInterceptor, | |
HttpErrorResponse, | |
HttpResponse | |
} from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { tap } from 'rxjs/operators'; | |
export class AngularDateHttpInterceptor implements HttpInterceptor { | |
// Migrated from AngularJS https://raw.githubusercontent.com/Ins87/angular-date-interceptor/master/src/angular-date-interceptor.js | |
iso8601 = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(([+-]\d\d:\d\d)|Z)?$/; | |
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
return next.handle(req).pipe( | |
tap({ | |
next: (event: HttpEvent<any>) => { | |
if (event instanceof HttpResponse) { | |
const body = event.body; | |
this.convertToDate(body); | |
} | |
}, | |
error: (err: any) => { | |
if (err instanceof HttpErrorResponse) { | |
if (err.status === 401) { | |
} | |
} | |
}, | |
}), | |
); | |
} | |
convertToDate(body) { | |
if (typeof body === 'object' && body !== null) { | |
const casted = body as { [key: string]: unknown }; | |
for (const key of Object.keys(casted)) { | |
const value = casted[key]; | |
if (this.isIso8601(value)) { | |
casted[key] = new Date(value); | |
} else if (typeof value === 'object' && value !== null) { | |
this.convertToDate(value); | |
} | |
} | |
} else { | |
return body; | |
} | |
} | |
isIso8601(value: unknown): value is string { | |
return typeof value === 'string' ? this.iso8601.test(value) : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment