Skip to content

Commit

Permalink
fix: add empty url case
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 24, 2018
1 parent 49b3ea0 commit e8d2d94
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/react/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const REGEX_LOCALHOST = /http:\/\/localhost/

export const isNil = value => value == null

export const getUrlPath = data => (data && typeof data === 'object' ? data.url : data)
export const getUrlPath = data =>
data && typeof data === 'object' ? data.url : data

export const someProp = (data, props) =>
data[props.find(prop => data[prop] !== null && data[prop] !== undefined)]
Expand Down Expand Up @@ -33,9 +34,18 @@ export const defaultApiParameters = {
}

export const createApiUrl = props => {
const { apiKey, url: targetUrl, screenshot, prerender, contrast, video } = props
const {
apiKey,
url: targetUrl,
screenshot,
prerender,
contrast,
video
} = props
const alias = apiKey ? 'pro' : 'api'
let url = `https://${alias}.microlink.io/?url=${encodeURIComponent(targetUrl)}`
let url = `https://${alias}.microlink.io/?url=${encodeURIComponent(
targetUrl
)}`
if (!isNil(video)) url = `${url}&${apiValue('video', video)}`
if (!isNil(contrast) && contrast !== defaultApiParameters.contrast) {
url = `${url}&${apiValue('palette', contrast)}`
Expand All @@ -62,7 +72,9 @@ export const fetchFromApi = props => {
export const isLarge = cardSize => cardSize === 'large'

export const imageProxy = url => {
return REGEX_LOCALHOST.test(url) || REGEX_HTTPS.test(url)
? url
: `https://images.weserv.nl/?url=${encodeURIComponent(url).replace('http://', '')}`
if (!url || REGEX_LOCALHOST.test(url) || REGEX_HTTPS.test(url)) return url
return `https://images.weserv.nl/?url=${encodeURIComponent(url).replace(
'http://',
''
)}`
}

0 comments on commit e8d2d94

Please sign in to comment.