Automatically create shortlinks for your Astro site
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: worker-links shortlinker

+46
+46
src/worker-links.ts
··· 1 + import type { Shortlinker } from "./types.js"; 2 + 3 + export interface WorkerLinksOptions { 4 + domain: string; 5 + secret: string | undefined; 6 + } 7 + 8 + /** 9 + * Shortlinker to create links on a https://github.com/erisa/worker-links instance. 10 + * If the provided secret is empty or missing, it will skip over creating the links. 11 + */ 12 + export const workerLinks = ({ 13 + domain, 14 + secret, 15 + }: WorkerLinksOptions): Shortlinker => { 16 + return { 17 + name: "worker-links", 18 + async run(mappings, logger) { 19 + if (!secret) { 20 + logger.warn("Empty secret provided, skipping creation of shortlinks."); 21 + return false; 22 + } 23 + 24 + const body = JSON.stringify( 25 + Object.fromEntries( 26 + mappings.map((x) => [x.shortlink.replace(/\/$/, ""), x.longlink]) 27 + ) 28 + ); 29 + 30 + const response = await fetch(domain, { 31 + method: "POST", 32 + headers: { 33 + Authorization: secret, 34 + "Content-Type": "application/json", 35 + }, 36 + body, 37 + }); 38 + 39 + if (!response.ok) { 40 + const err = await response.text(); 41 + logger.error(`Failed to create shortlinks: ${err}`); 42 + return false; 43 + } 44 + }, 45 + }; 46 + };