Skip to content

Instantly share code, notes, and snippets.

@VicAv99
Last active March 29, 2019 16:13
Show Gist options
  • Save VicAv99/914a65c426eb13068e4469f960cf7bed to your computer and use it in GitHub Desktop.
Save VicAv99/914a65c426eb13068e4469f960cf7bed to your computer and use it in GitHub Desktop.
// 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