Skip to content

Commit

Permalink
Merge pull request #23 from dorelljames/issue-22
Browse files Browse the repository at this point in the history
  • Loading branch information
dorelljames authored Apr 4, 2023
2 parents 5963a72 + 892ef9e commit 9f000bf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanity-plugin-asset-source-pexels",
"version": "3.0.1",
"version": "3.0.2",
"description": "> Be careful with your *API key*. If you use this Sanity plugin, it's a good idea to make your repository private. Technically, the said API key can be accessed inside of the JS-bundle if someone knows the domain for the studio.",
"keywords": [
"sanity",
Expand Down
17 changes: 14 additions & 3 deletions src/proxyClient.ts → src/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const proxyClient = () => {
const baseURL = 'https://sanity-plugin-asset-source-pexels-api-proxy.dorelljames.com'
export const createClient = (API_KEY?: string) => {
const baseURL = API_KEY
? 'https://api.pexels.com/v1'
: 'https://sanity-plugin-asset-source-pexels-api-proxy.dorelljames.com'
const headers = API_KEY
? {
headers: {
Authorization: API_KEY,
},
}
: {}

const getPhotos = (path: string, params: unknown) => {
return fetch(`${baseURL}${path}?${new URLSearchParams(params as URLSearchParams)}`)
return fetch(`${baseURL}${path}?${new URLSearchParams(params as URLSearchParams)}`, {
...headers,
})
.then((res) => {
if (!res.ok) {
return {error: res.statusText}
Expand Down
10 changes: 3 additions & 7 deletions src/hooks/usePhotos.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/* eslint-disable camelcase */
import React from 'react'
import {Photos, createClient} from 'pexels'
import {proxyClient} from '../proxyClient'
import {Photos} from 'pexels'
import {createClient} from '../createClient'
import useDebounce from './useDebounce'
import type {PexelsAssetSourceConfig} from '../types'

export function usePhotos(config: PexelsAssetSourceConfig) {
const client = React.useMemo(() => {
if (config?.useProxyClient) {
return proxyClient()
}

return createClient(config?.API_KEY)
}, [config?.API_KEY, config?.useProxyClient])
}, [config?.API_KEY])
const perPage = config?.results?.perPage!

const [state, setState] = React.useState('idle') // loading > success | error
Expand Down

0 comments on commit 9f000bf

Please sign in to comment.