/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * A token used to manipulate and access values stored in `HttpContext`. * * @publicApi */ export class HttpContextToken { constructor(public readonly defaultValue: () => T) {} } /** * Http context stores arbitrary user defined values and ensures type safety without * actually knowing the types. It is backed by a `Map` and guarantees that keys do not clash. * * This context is mutable and is shared between cloned requests unless explicitly specified. * * @usageNotes * * ### Usage Example * * ```typescript * // inside cache.interceptors.ts * export const IS_CACHE_ENABLED = new HttpContextToken(() => false); * * export class CacheInterceptor implements HttpInterceptor { * * intercept(req: HttpRequest, delegate: HttpHandler): Observable> { * if (req.context.get(IS_CACHE_ENABLED) === true) { * return ...; * } * return delegate.handle(req); * } * } * * // inside a service * * this.httpClient.get('/api/weather', { * context: new HttpContext().set(IS_CACHE_ENABLED, true) * }).subscribe(...); * ``` * * @publicApi */ export class HttpContext { private readonly map = new Map, unknown>(); /** * Store a value in the context. If a value is already present it will be overwritten. * * @param token The reference to an instance of `HttpContextToken`. * @param value The value to store. * * @returns A reference to itself for easy chaining. */ set(token: HttpContextToken, value: T): HttpContext { this.map.set(token, value); return this; } /** * Retrieve the value associated with the given token. * * @param token The reference to an instance of `HttpContextToken`. * * @returns The stored value or default if one is defined. */ get(token: HttpContextToken): T { if (!this.map.has(token)) { this.map.set(token, token.defaultValue()); } return this.map.get(token) as T; } /** * Delete the value associated with the given token. * * @param token The reference to an instance of `HttpContextToken`. * * @returns A reference to itself for easy chaining. */ delete(token: HttpContextToken): HttpContext { this.map.delete(token); return this; } /** * @returns a list of tokens currently stored in the context. */ keys(): IterableIterator> { return this.map.keys(); } }