-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy pathjsonDoc.server.ts
164 lines (134 loc) Β· 3.46 KB
/
jsonDoc.server.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { customRandom } from "nanoid";
import safeFetch from "./utilities/safeFetch";
import createFromRawXml from "./utilities/xml/createFromRawXml";
import isXML from "./utilities/xml/isXML";
type BaseJsonDocument = {
id: string;
title: string;
readOnly: boolean;
};
export type RawJsonDocument = BaseJsonDocument & {
type: "raw";
contents: string;
};
export type UrlJsonDocument = BaseJsonDocument & {
type: "url";
url: string;
};
export type CreateJsonOptions = {
ttl?: number;
readOnly?: boolean;
injest?: boolean;
metadata?: any;
};
export type JSONDocument = RawJsonDocument | UrlJsonDocument;
export async function createFromUrlOrRawJson(
urlOrJson: string,
title?: string
): Promise<JSONDocument | undefined> {
if (isUrl(urlOrJson)) {
return createFromUrl(new URL(urlOrJson), title);
}
if (isJSON(urlOrJson)) {
return createFromRawJson("Untitled", urlOrJson);
}
// Wrapper for createFromRawJson to handle XML
// TODO ? change from urlOrJson to urlOrJsonOrXml
if (isXML(urlOrJson)) {
return createFromRawXml("Untitled", urlOrJson);
}
}
export async function createFromUrl(
url: URL,
title?: string,
options?: CreateJsonOptions
): Promise<JSONDocument> {
if (options?.injest) {
const response = await safeFetch(url.href);
if (!response.ok) {
throw new Error(`Failed to injest ${url.href}`);
}
return createFromRawJson(title || url.href, await response.text(), options);
}
const docId = createId();
const doc: JSONDocument = {
id: docId,
type: <const>"url",
url: url.href,
title: title ?? url.hostname,
readOnly: options?.readOnly ?? false,
};
await DOCUMENTS.put(docId, JSON.stringify(doc), {
expirationTtl: options?.ttl ?? undefined,
metadata: options?.metadata ?? undefined,
});
return doc;
}
export async function createFromRawJson(
filename: string,
contents: string,
options?: CreateJsonOptions
): Promise<JSONDocument> {
const docId = createId();
const doc: JSONDocument = {
id: docId,
type: <const>"raw",
contents,
title: filename,
readOnly: options?.readOnly ?? false,
};
JSON.parse(contents);
await DOCUMENTS.put(docId, JSON.stringify(doc), {
expirationTtl: options?.ttl ?? undefined,
metadata: options?.metadata ?? undefined,
});
return doc;
}
export async function getDocument(
slug: string
): Promise<JSONDocument | undefined> {
const doc = await DOCUMENTS.get(slug);
if (!doc) return;
return JSON.parse(doc);
}
export async function updateDocument(
slug: string,
title: string
): Promise<JSONDocument | undefined> {
const document = await getDocument(slug);
if (!document) return;
const updated = { ...document, title };
await DOCUMENTS.put(slug, JSON.stringify(updated));
return updated;
}
export async function deleteDocument(slug: string): Promise<void> {
await DOCUMENTS.delete(slug);
}
function createId(): string {
const nanoid = customRandom(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn
46E7
opqrstuvwxyz",
12,
(bytes: number): Uint8Array => {
const array = new Uint8Array(bytes);
crypto.getRandomValues(array);
return array;
}
);
return nanoid();
}
function isUrl(possibleUrl: string): boolean {
try {
new URL(possibleUrl);
return true;
} catch {
return false;
}
}
function isJSON(possibleJson: string): boolean {
try {
JSON.parse(possibleJson);
return true;
} catch (e: any) {
throw new Error(e.message);
}
}