2024-01-12 14:43:28 +00:00
|
|
|
import {getDomain, getHost, customDomainUsed} from "~/lib/utils.js";
|
2023-12-16 18:21:03 +00:00
|
|
|
|
|
|
|
function addAuthHeader(request, options) {
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
if (authStore.token) {
|
|
|
|
options.headers = {Authorization: `Bearer ${authStore.token}`, ...options.headers}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addPasswordToFormRequest(request, options) {
|
|
|
|
const url = request.url
|
|
|
|
if (!url || !url.startsWith('/api/forms/')) return
|
|
|
|
|
|
|
|
const slug = url.split('/')[3]
|
|
|
|
const passwordCookie = useCookie('password-' + slug, {maxAge: 60 * 60 * 24 * 30}) // 30 days
|
|
|
|
if (slug !== undefined && slug !== '' && passwordCookie.value !== undefined) {
|
|
|
|
options.headers['form-password'] = passwordCookie.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:43:28 +00:00
|
|
|
/**
|
|
|
|
* Add custom domain header if custom domain is used
|
|
|
|
*/
|
|
|
|
function addCustomDomainHeader(request, options) {
|
|
|
|
if (!customDomainUsed()) return
|
|
|
|
options.headers['x-custom-domain'] = getDomain(getHost())
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:21:03 +00:00
|
|
|
export function getOpnRequestsOptions(request, opts) {
|
2024-01-11 13:07:27 +00:00
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
2023-12-16 18:21:03 +00:00
|
|
|
opts.headers = {accept: 'application/json', ...opts.headers}
|
|
|
|
|
2024-01-11 13:07:27 +00:00
|
|
|
// Authenticate requests coming from the server
|
|
|
|
if (process.server && config.apiSecret) {
|
|
|
|
opts.headers['x-api-secret'] = config.apiSecret
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:21:03 +00:00
|
|
|
addAuthHeader(request, opts)
|
|
|
|
addPasswordToFormRequest(request, opts)
|
2024-01-12 14:43:28 +00:00
|
|
|
addCustomDomainHeader(request, opts)
|
2023-12-16 18:21:03 +00:00
|
|
|
|
|
|
|
return {
|
2024-01-03 16:38:11 +00:00
|
|
|
baseURL: config.public.apiBase,
|
2023-12-16 18:21:03 +00:00
|
|
|
onResponseError({response}) {
|
|
|
|
const authStore = useAuthStore()
|
2024-01-12 14:43:28 +00:00
|
|
|
console.log(response)
|
2023-12-16 18:21:03 +00:00
|
|
|
const {status} = response
|
|
|
|
|
2024-01-12 14:43:28 +00:00
|
|
|
if (status === 401) {
|
|
|
|
if (response.body.error && response.body.error === 'invalid_domain' && process.client) {
|
|
|
|
// If invalid domain, redirect to main domain
|
|
|
|
window.location.href = config.public.appUrl + '?utm_source=failed_custom_domain_redirect'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authStore.check) {
|
|
|
|
console.log("Logging out due to 401")
|
|
|
|
authStore.logout()
|
|
|
|
useRouter().push({name: 'login'})
|
|
|
|
}
|
2023-12-16 18:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= 500) {
|
|
|
|
console.error('Request error', status)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:46:55 +00:00
|
|
|
export const opnFetch = (request, opts = {}) => {
|
|
|
|
return $fetch(request, getOpnRequestsOptions(request, opts))
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:21:03 +00:00
|
|
|
export const useOpnApi = (request, opts = {}) => {
|
|
|
|
return useFetch(request, getOpnRequestsOptions(request, opts))
|
|
|
|
}
|