import type { DialogSet } from "./types"; /** * Returns true, if input is a valid name of a dialogSet. * Intended to protect from loading anything other than dialogSet json. * @param input input to check */ function isValidDialogSetName(input: unknown) { if (typeof input !== "string") { return false; } if (input.length === 0) { return false; } if (input.length > 50) { return false; } return !/[^a-zA-Z0-9\-\_]/i.test(input); } function getHashValue(): string { return window.location.hash.substr(1); } /** * Fetch a dialogSet based on the hash value */ export async function fetchDialogSet(): Promise<DialogSet> { const setName = getHashValue(); if (!isValidDialogSetName(setName)) throw new Error("Name is not valid dialogSet name"); const dialogSetUrl = "./dialogs/" + setName + ".json"; try { const dialogSet: DialogSet = await (await fetch(dialogSetUrl)).json(); return dialogSet; } catch (error) { throw error; } }