Last active
March 29, 2019 16:13
-
-
Save VicAv99/914a65c426eb13068e4469f960cf7bed to your computer and use it in GitHub Desktop.
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
// src/core/interceptors/pizza.interceptor.ts | |
import { Injectable } from ‘@angular/core’; | |
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from ‘@angular/common/http’; | |
import { Observable } from ‘rxjs’; | |
@Injectable({ | |
providedIn: ‘root’ | |
}) | |
export class PizzaInterceptor implements HttpInterceptor { | |
constructor () { } | |
// An outgoing HTTP request with an optional typed body. | |
// `HttpRequest` represents an outgoing request, including URL, method, | |
// headers, body, and other request configuration options. Instances should be | |
// assumed to be immutable. To modify a `HttpRequest`, the `clone` | |
// method should be used. | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
if (req.body) { | |
let { description } = req.body; | |
description = description.replace(/pizza/g, ‘🍕’); | |
const pizzaRequest = req.clone({ body: { description } }); | |
return next.handle(pizzaRequest); | |
} | |
return next.handle(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment