UNPKG

586 BPlain TextView Raw
1export interface Serialized<Data, Details> {
2 data: Data;
3 details: Details;
4}
5
6export function getSerialized<Data = {}, Details = {}>(
7 id: string,
8): Serialized<Data, Details> {
9 const node = document.getElementById(serializedID(id));
10 if (node == null) {
11 throw new Error(`No serialized data found with the id '${id}'`);
12 }
13
14 const {serializedDetails} = node.dataset;
15
16 return {
17 data: JSON.parse(node.innerHTML),
18 details: serializedDetails ? JSON.parse(serializedDetails) : {},
19 };
20}
21
22export function serializedID(id: string) {
23 return `SerializedData-${id}`;
24}