astro-resume/package/Serialize.astro

37 lines
979 B
Text

---
export interface Props {
/**
* The id that the client script will pass to the `deserialize()` function
*/
id: string;
/**
* The data to be serialized and accessed in the client script with `deserialize()`
*/
data: Record<string, any>;
/**
* Custom serializer to be used
* @param data
*/
use?: (data: Record<string, any>) => string;
}
const { id, data, use } = Astro.props;
let serializedData = "{}";
try {
serializedData = use ? use(data) : JSON.stringify(data);
} catch (err) {
/**
* ERR: data is unserializable
* - You might need a custom serializer/parser for complex data
* - Usage examples in 👉 https://ayco.io/gh/astro-resume#usage
*/
throw Error(
`astro-resume ERR: Data unserializable
- You might need a custom serializer/parser for complex data
- Usage examples in 👉 https://ayco.io/gh/astro-resume#usage
`,
err,
);
}
---
<script type="application/json" id={id} set:html={serializedData} />