1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | /**
|
---|
9 | * A token used to manipulate and access values stored in `HttpContext`.
|
---|
10 | *
|
---|
11 | * @publicApi
|
---|
12 | */
|
---|
13 | export class HttpContextToken {
|
---|
14 | constructor(defaultValue) {
|
---|
15 | this.defaultValue = defaultValue;
|
---|
16 | }
|
---|
17 | }
|
---|
18 | /**
|
---|
19 | * Http context stores arbitrary user defined values and ensures type safety without
|
---|
20 | * actually knowing the types. It is backed by a `Map` and guarantees that keys do not clash.
|
---|
21 | *
|
---|
22 | * This context is mutable and is shared between cloned requests unless explicitly specified.
|
---|
23 | *
|
---|
24 | * @usageNotes
|
---|
25 | *
|
---|
26 | * ### Usage Example
|
---|
27 | *
|
---|
28 | * ```typescript
|
---|
29 | * // inside cache.interceptors.ts
|
---|
30 | * export const IS_CACHE_ENABLED = new HttpContextToken<boolean>(() => false);
|
---|
31 | *
|
---|
32 | * export class CacheInterceptor implements HttpInterceptor {
|
---|
33 | *
|
---|
34 | * intercept(req: HttpRequest<any>, delegate: HttpHandler): Observable<HttpEvent<any>> {
|
---|
35 | * if (req.context.get(IS_CACHE_ENABLED) === true) {
|
---|
36 | * return ...;
|
---|
37 | * }
|
---|
38 | * return delegate.handle(req);
|
---|
39 | * }
|
---|
40 | * }
|
---|
41 | *
|
---|
42 | * // inside a service
|
---|
43 | *
|
---|
44 | * this.httpClient.get('/api/weather', {
|
---|
45 | * context: new HttpContext().set(IS_CACHE_ENABLED, true)
|
---|
46 | * }).subscribe(...);
|
---|
47 | * ```
|
---|
48 | *
|
---|
49 | * @publicApi
|
---|
50 | */
|
---|
51 | export class HttpContext {
|
---|
52 | constructor() {
|
---|
53 | this.map = new Map();
|
---|
54 | }
|
---|
55 | /**
|
---|
56 | * Store a value in the context. If a value is already present it will be overwritten.
|
---|
57 | *
|
---|
58 | * @param token The reference to an instance of `HttpContextToken`.
|
---|
59 | * @param value The value to store.
|
---|
60 | *
|
---|
61 | * @returns A reference to itself for easy chaining.
|
---|
62 | */
|
---|
63 | set(token, value) {
|
---|
64 | this.map.set(token, value);
|
---|
65 | return this;
|
---|
66 | }
|
---|
67 | /**
|
---|
68 | * Retrieve the value associated with the given token.
|
---|
69 | *
|
---|
70 | * @param token The reference to an instance of `HttpContextToken`.
|
---|
71 | *
|
---|
72 | * @returns The stored value or default if one is defined.
|
---|
73 | */
|
---|
74 | get(token) {
|
---|
75 | if (!this.map.has(token)) {
|
---|
76 | this.map.set(token, token.defaultValue());
|
---|
77 | }
|
---|
78 | return this.map.get(token);
|
---|
79 | }
|
---|
80 | /**
|
---|
81 | * Delete the value associated with the given token.
|
---|
82 | *
|
---|
83 | * @param token The reference to an instance of `HttpContextToken`.
|
---|
84 | *
|
---|
85 | * @returns A reference to itself for easy chaining.
|
---|
86 | */
|
---|
87 | delete(token) {
|
---|
88 | this.map.delete(token);
|
---|
89 | return this;
|
---|
90 | }
|
---|
91 | /**
|
---|
92 | * @returns a list of tokens currently stored in the context.
|
---|
93 | */
|
---|
94 | keys() {
|
---|
95 | return this.map.keys();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvbW1vbi9odHRwL3NyYy9jb250ZXh0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOzs7O0dBSUc7QUFDSCxNQUFNLE9BQU8sZ0JBQWdCO0lBQzNCLFlBQTRCLFlBQXFCO1FBQXJCLGlCQUFZLEdBQVosWUFBWSxDQUFTO0lBQUcsQ0FBQztDQUN0RDtBQ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