Skip to content

Instantly share code, notes, and snippets.

@S-codes14
Created March 25, 2022 18:41
Show Gist options
  • Save S-codes14/ce159e5da5e69467aed12716ad2d93c0 to your computer and use it in GitHub Desktop.
Save S-codes14/ce159e5da5e69467aed12716ad2d93c0 to your computer and use it in GitHub Desktop.
how to get random pictures from different providers
interface ImageObject {
url: string;
author?: string;
width?: number;
height?: number;
}
const PROVIDERS = [
'picsum-photos',
]
async function RandomPicture (): Promise<ImageObject> {
// Get random provider
const provider = PROVIDERS[Math.floor(Math.random() * PROVIDERS.length)]
if (provider === 'picsum-photos') {
const { Picsum } = await import('picsum-photos')
const image = await Picsum.random()
return {
url: image.download_url,
author: image.author,
width: image.width,
height: image.height,
}
}
throw new Error('Image provider not implemented: ' + provider)
}
export { RandomPicture }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment