Created
March 25, 2022 18:41
-
-
Save S-codes14/ce159e5da5e69467aed12716ad2d93c0 to your computer and use it in GitHub Desktop.
how to get random pictures from different providers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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