Newer
Older
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
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;
}
}