-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathpayload-decoder.svelte
More file actions
72 lines (64 loc) · 2.18 KB
/
payload-decoder.svelte
File metadata and controls
72 lines (64 loc) · 2.18 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<script lang="ts">
import { onMount, type Snippet } from 'svelte';
import { page } from '$app/stores';
import type { Memo } from '$lib/types';
import type { EventAttribute, WorkflowEvent } from '$lib/types/events';
import {
cloneAllPotentialPayloadsWithCodec,
decodePayloadAttributes,
type PotentiallyDecodable,
} from '$lib/utilities/decode-payload';
import {
getCodecEndpoint,
getCodecIncludeCredentials,
getCodecPassAccessToken,
} from '$lib/utilities/get-codec';
import { stringifyWithBigInt } from '$lib/utilities/parse-with-big-int';
interface Props {
value: PotentiallyDecodable | EventAttribute | WorkflowEvent | Memo;
key?: string;
onDecode?: (decodedValue: string) => void;
children: Snippet<[decodedValue: string]>;
}
let { children, value, key = '', onDecode }: Props = $props();
let keyedValue = key && value?.[key] ? value[key] : value;
let decodedValue = $state(stringifyWithBigInt(keyedValue));
onMount(() => {
decodePayloads(value);
});
const decodePayloads = async (
_value: PotentiallyDecodable | EventAttribute | WorkflowEvent | Memo,
) => {
const settings = {
...$page.data.settings,
codec: {
...$page.data.settings?.codec,
endpoint: getCodecEndpoint($page.data.settings),
passAccessToken: getCodecPassAccessToken($page.data.settings),
includeCredentials: getCodecIncludeCredentials($page.data.settings),
},
};
try {
const convertedAttributes = await cloneAllPotentialPayloadsWithCodec(
_value,
$page.params.namespace,
settings,
);
const decodedAttributes = decodePayloadAttributes(
convertedAttributes,
) as object;
const keyExists = key && decodedAttributes?.[key];
let finalValue = keyExists ? decodedAttributes[key] : decodedAttributes;
if (Array.isArray(finalValue) && finalValue.length === 1) {
finalValue = finalValue[0];
}
decodedValue = stringifyWithBigInt(finalValue);
if (onDecode) {
onDecode(decodedValue);
}
} catch {
console.error('Could not decode payloads');
}
};
</script>
{@render children(decodedValue)}