Add getApiUrl helper

This commit is contained in:
Sam Carlton 2022-05-21 11:25:31 -05:00
parent ef065423cc
commit d96332730d

View file

@ -1,3 +1,4 @@
export function getSiteUrl () {
// console.log( 'import.meta.site', import.meta.env )
@ -38,6 +39,25 @@ export function getSiteUrl () {
throw new Error('Could not find site URL')
}
export function getApiUrl () {
const hasImportMeta = typeof import.meta !== 'undefined'
const hasImportMetaEnv = hasImportMeta && typeof import.meta.env !== 'undefined'
// Try PUBLIC_API_DOMAIN
if ( typeof process.env.PUBLIC_API_DOMAIN !== 'undefined' ) {
// console.log('Has env.PUBLIC_API_DOMAIN')
return process.env.PUBLIC_API_DOMAIN
}
if ( hasImportMetaEnv && typeof import.meta.env.PUBLIC_API_DOMAIN !== 'undefined' ) {
// console.log('Has PUBLIC_API_DOMAIN')
return import.meta.env.PUBLIC_API_DOMAIN
}
throw new Error('Could not find API URL')
}
export function getPartPartsFromUrl ( urlString ) {
if ( typeof urlString !== 'string' ) throw new Error('urlString must be a string')