refactor: making unsplash api request from api folder (#482)

* refractor: added params to fetch key

* feat: create views directly from views list page

fix: selected filter not showing up in multi-level dropdown, refactor: arranged imports

* refactor: making unsplash api request from api folder

to hide acces key from client side
This commit is contained in:
Dakshesh Jain 2023-03-21 16:31:01 +05:30 committed by GitHub
parent 5869c91d70
commit bf09673d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -0,0 +1,23 @@
import type { NextApiRequest, NextApiResponse } from "next";
// TODO: remove NEXT_PUBLIC_ prefix from env variable
const unsplashKey = process.env.NEXT_PUBLIC_UNSPLASH_ACCESS;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { query, page, per_page = 20 } = req.query;
const url = query
? `https://api.unsplash.com/search/photos/?client_id=${unsplashKey}&query=${query}&page=${page}&per_page=${per_page}`
: `https://api.unsplash.com/photos/?client_id=${unsplashKey}&page=${page}&per_page=${per_page}`;
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
res.status(200).json(data);
}

View File

@ -65,14 +65,16 @@ class FileServices extends APIService {
}
async getUnsplashImages(page: number = 1, query?: string): Promise<UnSplashImage[]> {
const clientId = process.env.NEXT_PUBLIC_UNSPLASH_ACCESS;
const url = query
? `https://api.unsplash.com/search/photos/?client_id=${clientId}&query=${query}&page=${page}&per_page=20`
: `https://api.unsplash.com/photos/?client_id=${clientId}&page=${page}&per_page=20`;
const url = "/api/unsplash";
return this.request({
method: "get",
url,
params: {
page,
per_page: 20,
query,
},
})
.then((response) => response?.data?.results ?? response?.data)
.catch((error) => {