import { API } from "../../types" ; import { officialCollection , UNSPLASH_API_KEY } from "./constants" ; import { Data , Image } from "./types" ; type Config = Pick < Data , "by" | "collections" | "featured" | "search" > ; export async function getImage ( config , loader ) { loader . push ( ) ; const res = await fetchImageMeta ( config ) ; const data = await fetchImageData ( res . urls . raw ) ; loader . pop ( ) ; return { data , image_link : res . links . html , location_title : res . location ? res . location . title : null , user_name : res . user . name , user_link : res . user . links . html , } ; } async function fetchImageMeta ( { by , collections , featured , search } ) { const url = "https://api.unsplash.com/photos/random" ; const params = new URLSearchParams ( ) ; const headers = new Headers ( { Authorization : `Client-ID ${ UNSPLASH_API_KEY }` , } ) ; switch ( by ) { case "collections" : params . set ( "collections" , collections ) ; break ; case "search" : params . set ( "orientation" , "landscape" ) ; if ( featured ) params . set ( "featured" , "true" ) ; if ( search ) params . set ( "query" , search ) ; break ; default : params . set ( "collections" , String ( officialCollection ) ) ; } const res = await fetch ( `${ url }?${ params }` , { headers } ) ; return res . json ( ) ; } async function fetchImageData ( url ) { const quality = 85 ; const width = calculateWidth ( window . innerWidth ) ; const params = new URLSearchParams ( { q : String ( quality ) , w : String ( width ) , } ) ; return await ( await fetch ( url + params ) ) . blob ( ) ; } /**
 * Calculate width to fetch image, tuned for Unsplash cache performance.
 */ export function calculateWidth ( screenWidth = 1920 ) { screenWidth = Math . max ( screenWidth , 1920 ) ; screenWidth = Math . min ( screenWidth , 3840 ) ; screenWidth = Math . ceil ( screenWidth / 240 ) * 240 ; return screenWidth ; }