-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathdecode-payload-value.ts
More file actions
44 lines (41 loc) · 1.35 KB
/
decode-payload-value.ts
File metadata and controls
44 lines (41 loc) · 1.35 KB
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
import type { Memo, Payload as RawPayload } from '$lib/types';
import type { EventAttribute, WorkflowEvent } from '$lib/types/events';
import {
decodeEventAttributes,
parsePayloadAttributes,
type PotentiallyDecodable,
} from '$lib/utilities/decode-payload';
import { stringifyWithBigInt } from '$lib/utilities/parse-with-big-int';
export type DecodableValue =
| PotentiallyDecodable
| EventAttribute
| WorkflowEvent
| Memo
| RawPayload
| null
| undefined;
export const getInitialPayloadValue = (
value: DecodableValue,
fieldName: string,
): string => {
if (!value) return stringifyWithBigInt(value);
const keyedValue = fieldName && value?.[fieldName] ? value[fieldName] : value;
return stringifyWithBigInt(keyedValue);
};
export const decodePayloadValue = async (
value: DecodableValue,
fieldName: string,
): Promise<string> => {
const convertedAttributes = await decodeEventAttributes(
value as PotentiallyDecodable | EventAttribute | WorkflowEvent | Memo,
);
const decodedAttributes = parsePayloadAttributes(
convertedAttributes,
) as object;
const keyExists = fieldName && decodedAttributes?.[fieldName];
let finalValue = keyExists ? decodedAttributes[fieldName] : decodedAttributes;
if (Array.isArray(finalValue) && finalValue.length === 1) {
finalValue = finalValue[0];
}
return stringifyWithBigInt(finalValue);
};