-
Notifications
You must be signed in to change notification settings - Fork 0
/
detail.field.ts
62 lines (55 loc) · 1.97 KB
/
detail.field.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { ZLogField } from '../formats/log-field.js';
import { ZLogWriter } from '../formats/log-writer.js';
import { ZLogDetails } from '../messages/log-details.js';
/**
* Creates a log field for {@link ZLogWriter.extractDetail extracted} message detail.
*
* Writes the value with {@link ZLogWriter.writeValue} method. Writes nothing if the target detail is undefined.
*
* @param path - A path to details property to extract. Empty path means extracting of all details.
*
* @returns Log detail field.
*/
export function detailZLogField(...path: (keyof ZLogDetails)[]): ZLogField;
/**
* Creates a log field for {@link ZLogWriter.extractDetail extracted} message detail in specific format.
*
* Writes the value with {@link ZLogWriter.writeValue} method. Writes nothing if the target detail is undefined.
*
* @param pathAndWrite - A path to details property to extract and value writer function. Empty path means extracting
* of all details.
*
* @returns Log detail field.
*/
export function detailZLogField<T>(
...pathAndWrite: [
...path: (keyof ZLogDetails)[],
write: (this: void, writer: ZLogWriter, value: T) => void,
]
): ZLogField;
export function detailZLogField<T>(
...pathAndWrite: [
...path: (keyof ZLogDetails)[],
...write: [((this: void, writer: ZLogWriter, value: T) => void)?],
]
): ZLogField {
let path: (keyof ZLogDetails)[];
let write: (this: void, writer: ZLogWriter, value: T) => void;
const writeOrKey = pathAndWrite[pathAndWrite.length - 1];
if (typeof writeOrKey === 'function') {
path = pathAndWrite.slice(0, pathAndWrite.length - 1) as (keyof ZLogDetails)[];
write = writeOrKey;
} else {
path = pathAndWrite as (keyof ZLogDetails)[];
write = detailZLogField$write;
}
return writer => {
const value = writer.extractDetail(...path);
if (value !== undefined) {
write(writer, value as T);
}
};
}
function detailZLogField$write(writer: ZLogWriter, value: unknown): void {
writer.writeValue(value);
}