( url: URL, title?: string, options?: CreateJsonOptions )
| 48 | } |
| 49 | |
| 50 | export async function createFromUrl( |
| 51 | url: URL, |
| 52 | title?: string, |
| 53 | options?: CreateJsonOptions |
| 54 | ): Promise<JSONDocument> { |
| 55 | if (options?.injest) { |
| 56 | const response = await safeFetch(url.href); |
| 57 | |
| 58 | if (!response.ok) { |
| 59 | throw new Error(`Failed to injest ${url.href}`); |
| 60 | } |
| 61 | |
| 62 | return createFromRawJson(title || url.href, await response.text(), options); |
| 63 | } |
| 64 | |
| 65 | const docId = createId(); |
| 66 | |
| 67 | const doc: JSONDocument = { |
| 68 | id: docId, |
| 69 | type: <const>"url", |
| 70 | url: url.href, |
| 71 | title: title ?? url.hostname, |
| 72 | readOnly: options?.readOnly ?? false, |
| 73 | }; |
| 74 | |
| 75 | await DOCUMENTS.put(docId, JSON.stringify(doc), { |
| 76 | expirationTtl: options?.ttl ?? undefined, |
| 77 | metadata: options?.metadata ?? undefined, |
| 78 | }); |
| 79 | |
| 80 | return doc; |
| 81 | } |
| 82 | |
| 83 | export async function createFromRawJson( |
| 84 | filename: string, |
no test coverage detected