UNPKG

omnichron

Version:

Unified interface for web archive providers

61 lines (59 loc) 2.14 kB
import { createErrorResponse, createFetchOptions, createSuccessResponse, mergeOptions, normalizeDomain } from "./_utils.mjs"; import { cleanDoubleSlashes } from "ufo"; import { $fetch } from "ofetch"; //#region src/providers/permacc.ts /** * Create a Perma.cc archive provider. * * @param initOptions - Initial Perma.cc options including required `apiKey` and cache settings. * @returns ArchiveProvider instance for fetching snapshots from Perma.cc. */ function permacc(initOptions = {}) { return { name: "Perma.cc", slug: "permacc", async getSnapshots(domain, reqOptions = {}) { const options = await mergeOptions(initOptions, reqOptions); if (!options.apiKey) throw new Error("API key is required for Perma.cc"); const baseUrl = "https://api.perma.cc"; const snapshotUrl = "https://perma.cc"; const { apiKey } = options; const cleanDomain = normalizeDomain(domain, false); const fetchOptions = await createFetchOptions(baseUrl, { limit: options?.limit ?? 100, url: cleanDomain }, { headers: { "Authorization": `ApiKey ${apiKey}` } }); try { const response = await $fetch("/v1/public/archives/", fetchOptions); if (!response.objects || response.objects.length === 0) return createSuccessResponse([], "permacc", { queryParams: fetchOptions.params }); const pages = response.objects.filter((item) => { return item.url && item.url.includes(cleanDomain); }).map((item) => { const cleanedUrl = cleanDoubleSlashes(item.url); const snapUrl = `${snapshotUrl}/${item.guid}`; const timestamp = item.creation_timestamp ?? new Date().toISOString(); const page = { url: cleanedUrl, timestamp, snapshot: snapUrl, _meta: { guid: item.guid, title: item.title, status: item.status, created_by: item.created_by?.id } }; return page; }); return createSuccessResponse(pages, "permacc", { queryParams: fetchOptions.params, meta: response.meta ?? {} }); } catch (error) { return createErrorResponse(error, "permacc"); } } }; } //#endregion export { permacc as default };