| 120 | let _toJSONDepth = 0; |
| 121 | |
| 122 | export class RequestContext<Values extends Record<string, any> | unknown = unknown> { |
| 123 | private registry = new Map<string, unknown>(); |
| 124 | |
| 125 | constructor( |
| 126 | iterable?: Values extends Record<string, any> |
| 127 | ? RecordToTuple<Partial<Values>> |
| 128 | : Iterable<readonly [string, unknown]>, |
| 129 | ) { |
| 130 | if (iterable && typeof iterable === 'object' && typeof (iterable as any)[Symbol.iterator] !== 'function') { |
| 131 | this.registry = new Map(Object.entries(iterable)); |
| 132 | } else { |
| 133 | this.registry = new Map(iterable); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * set a value with strict typing if `Values` is a Record and the key exists in it. |
| 139 | */ |
| 140 | public set<K extends Values extends Record<string, any> ? keyof Values : string>( |
| 141 | key: K, |
| 142 | value: Values extends Record<string, any> ? (K extends keyof Values ? Values[K] : never) : unknown, |
| 143 | ): void { |
| 144 | // The type assertion `key as string` is safe because K always extends string ultimately. |
| 145 | this.registry.set(key as string, value); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get a value with its type |
| 150 | */ |
| 151 | public get< |
| 152 | K extends Values extends Record<string, any> ? keyof Values : string, |
| 153 | R = Values extends Record<string, any> ? (K extends keyof Values ? Values[K] : never) : unknown, |
| 154 | >(key: K): R { |
| 155 | return this.registry.get(key as string) as R; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Check if a key exists in the container |
| 160 | */ |
| 161 | public has<K extends Values extends Record<string, any> ? keyof Values : string>(key: K): boolean { |
| 162 | return this.registry.has(key); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Delete a value by key |
| 167 | */ |
| 168 | public delete<K extends Values extends Record<string, any> ? keyof Values : string>(key: K): boolean { |
| 169 | return this.registry.delete(key); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Clear all values from the container |
| 174 | */ |
| 175 | public clear(): void { |
| 176 | this.registry.clear(); |
| 177 | } |
| 178 | |
| 179 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…