UNPKG

omnichron

Version:

Unified interface for web archive providers

55 lines (53 loc) 1.95 kB
import { createErrorResponse, createFetchOptions, createSuccessResponse, mergeOptions, normalizeDomain } from "./_utils.mjs"; import { $fetch } from "ofetch"; //#region src/providers/webcite.ts /** * Create a WebCite archive provider. * * Note: WebCite is currently not accepting new archiving requests, but existing * archives remain accessible. * * @param initOptions - Initial archive options for WebCite queries. * @returns ArchiveProvider instance for fetching snapshots from WebCite. */ function webcite(initOptions = {}) { return { name: "WebCite", slug: "webcite", async getSnapshots(domain, reqOptions = {}) { const options = await mergeOptions(initOptions, reqOptions); const baseUrl = "https://www.webcitation.org"; const cleanDomain = normalizeDomain(domain, false); const fetchOptions = await createFetchOptions(baseUrl, { url: encodeURIComponent(cleanDomain) }, { timeout: options.timeout ?? 3e4 }); try { const queryPath = "/query"; try { const response = await $fetch(queryPath, fetchOptions); const isNotFound = typeof response === "string" && response.includes("We are currently not accepting archiving requests"); const pages = []; if (!isNotFound && response) pages.push({ url: cleanDomain, timestamp: new Date().toISOString(), snapshot: `${baseUrl}/query?url=${encodeURIComponent(cleanDomain)}`, _meta: { requestId: "webcite-archive", provider: "webcite" } }); return createSuccessResponse(pages, "webcite", { domain: cleanDomain, empty: pages.length === 0, queryParams: fetchOptions.params, isAvailable: !isNotFound }); } catch (fetchError) { return createErrorResponse(fetchError, "webcite", { domain: cleanDomain }); } } catch (error) { return createErrorResponse(error, "webcite", { domain: cleanDomain }); } } }; } //#endregion export { webcite as default };