Created
February 13, 2023 11:15
-
-
Save Mustafa-Omran/40b79793ebb857f4bc93ed26521d06a5 to your computer and use it in GitHub Desktop.
Caching - Passing Context to HTTP Interceptors
This file contains hidden or 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
const CACHE_IT = new HttpContextToken<boolean>(() => false); | |
export function cacheIt() { | |
return new HttpContext().set(CACHE_IT, true); | |
} | |
@Injectable() | |
export class CacheInterceptor implements HttpInterceptor { | |
intercept(request: HttpRequest, next: HttpHandler): Observable<HttpEvent> { | |
if(request.context.get(CACHE_IT)) { | |
return; | |
} | |
return next.handle(request); | |
} | |
} |
This file contains hidden or 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 { cacheIt } from './cache.interceptor'; | |
@Injectable() | |
export class UsersService { | |
constructor(private http: HttpClient) {} | |
getUsers() { | |
return this.http.get('....', { | |
context: cacheIt() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment