-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpayload-decoder.svelte
More file actions
102 lines (87 loc) · 2.38 KB
/
payload-decoder.svelte
File metadata and controls
102 lines (87 loc) · 2.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<script lang="ts" module>
export type DecodedPayloadResult = {
decodedValue: ParsedPayload | PayloadContainingObject;
originalValue: Payload | PayloadContainingObject;
}[];
</script>
<script lang="ts">
import { type Snippet } from 'svelte';
import type { Payload, Payloads } from '$lib/types';
import {
decodeEventAttributes,
decodePayloadAndParseDataToJSON,
decodePayloadsAndParseDataToJSON,
isRawPayload,
isRawPayloads,
type ParsedPayload,
type PayloadContainingObject,
} from '$lib/utilities/decode-payload';
type T = $$Generic<PayloadContainingObject>;
const decodePayloadValue = async (
value: Payload,
): Promise<DecodedPayloadResult> => {
const decodedPayload = await decodePayloadAndParseDataToJSON(value, false);
const result = [
{
decodedValue: decodedPayload,
originalValue: value,
},
];
onDecode?.(result);
return result;
};
const decodePayloadsValue = async (
value: Payloads,
): Promise<DecodedPayloadResult> => {
const decodedPayloads = await decodePayloadsAndParseDataToJSON(
value,
false,
);
const result = decodedPayloads.map((decodedPayload, idx) => {
return {
decodedValue: decodedPayload,
originalValue: value.payloads[idx],
};
});
onDecode?.(result);
return result;
};
const decodePayloadContainingObjectValue = async <
T extends PayloadContainingObject,
>(
value: T,
): Promise<DecodedPayloadResult> => {
const decodedValue = await decodeEventAttributes(value);
const result = [
{
decodedValue,
originalValue: value,
},
];
onDecode?.(result);
return result;
};
const decodeValue = (
value: Payload | Payloads | T,
): Promise<DecodedPayloadResult> => {
if (isRawPayload(value)) {
return decodePayloadValue(value);
}
if (isRawPayloads(value)) {
return decodePayloadsValue(value);
}
return decodePayloadContainingObjectValue(value);
};
type Props = {
value: Payload | Payloads | T;
children: Snippet<[DecodedPayloadResult]>;
onDecode?: (result: DecodedPayloadResult) => void;
loading?: Snippet<[]>;
};
let { value, children, onDecode, loading }: Props = $props();
</script>
{#await decodeValue(value)}
{@render loading?.()}
{:then decodeResult}
{@render children(decodeResult)}
{/await}