From 1024544e5f89d3eb6de50d6199e990aa6e71ce98 Mon Sep 17 00:00:00 2001 From: Prasad Nayak Date: Sun, 16 Jun 2019 18:09:05 +0530 Subject: [PATCH] refactor(unsplash): switch to unsplash http api --- src/image.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/image.ts b/src/image.ts index 625e598..a6de955 100644 --- a/src/image.ts +++ b/src/image.ts @@ -1,4 +1,50 @@ -export const getUnsplashUrl = (tags: string) => - `https://source.unsplash.com/1920x1080/?nature,${encodeURIComponent( - tags.split(' ').join(',') - )}`; +import axios from 'axios'; +import config from './config'; + +export interface IUnsplashResponse { + id: string; + created_at: string; + updated_at: string; + width: number; + height: number; + color: string; + downloads: number; + likes: number; + liked_by_user: boolean; + description: string; + urls: Record< + 'raw' | 'full' | 'regular' | 'small' | 'thumb' | 'custom', + string + >; + links: Record<'self' | 'html' | 'download' | 'download_location', string>; + user: { + id: string; + updated_at: string; + username: string; + name: string; + portfolio_url: string; + bio: string; + location: string; + total_likes: number; + total_photos: number; + total_collections: number; + instagram_username: string; + twitter_username: string; + links: Record<'self' | 'html' | 'photos' | 'likes' | 'portfolio', string>; + }; +} + +export const getUnsplashPhoto = async (tags: string) => { + return axios.get( + `https://api.unsplash.com/photos/random`, + { + params: { + client_id: config.UNSPLASH_ACCESS_KEY, + query: ['nature', ...tags.split(' ')].join(','), + orientation: 'landscape', + w: 1920, + h: 1080, + }, + } + ); +};