|
1 | 1 | import { TupleArrayBuilder } from './tuple-array.builder'; |
2 | 2 |
|
3 | 3 | export class RequestHeaderBuilder { |
4 | | - metadata: Array<[index: number, key: string | undefined]> = []; |
| 4 | + metadata: Array< |
| 5 | + [ |
| 6 | + index: number, |
| 7 | + value: [key: string | undefined, defaultValue: string | undefined], |
| 8 | + ] |
| 9 | + > = []; |
5 | 10 |
|
6 | | - constructor(index: number, key?: string) { |
7 | | - this.add(index, key); |
| 11 | + constructor(index: number, key?: string, defaultValue?: string) { |
| 12 | + this.add(index, key, defaultValue); |
8 | 13 | } |
9 | 14 |
|
10 | | - add(index: number, key?: string): void { |
11 | | - this.metadata.push([index, key]); |
| 15 | + add(index: number, key?: string, defaultValue?: string): void { |
| 16 | + this.metadata.push([index, [key, defaultValue]]); |
12 | 17 | } |
13 | 18 |
|
14 | 19 | build(args: any[]): HeadersInit { |
15 | | - return this.metadata.reduce<Record<string, string>>((acc, [index, key]) => { |
16 | | - if (key != null) { |
17 | | - acc[key] = String(args[index]); |
18 | | - return acc; |
19 | | - } |
| 20 | + return this.metadata.reduce<Record<string, string>>( |
| 21 | + (acc, [index, [key, defaultValue]]) => { |
| 22 | + if (key != null) { |
| 23 | + acc[key] = String(args[index] ?? defaultValue ?? ''); |
| 24 | + return acc; |
| 25 | + } |
20 | 26 |
|
21 | | - TupleArrayBuilder.of<string, unknown>(args[index]).forEach( |
22 | | - ([key, value]) => { |
23 | | - acc[key] = String(value); |
24 | | - }, |
25 | | - ); |
| 27 | + TupleArrayBuilder.of<string, unknown>(args[index]).forEach( |
| 28 | + ([key, value]) => { |
| 29 | + acc[key] = String(value); |
| 30 | + }, |
| 31 | + ); |
26 | 32 |
|
27 | | - return acc; |
28 | | - }, {}); |
| 33 | + return acc; |
| 34 | + }, |
| 35 | + {}, |
| 36 | + ); |
29 | 37 | } |
30 | 38 | } |
0 commit comments