2024-04-15 19:39:03 +02:00
|
|
|
import { getDomain, getHost, customDomainUsed } from "~/lib/utils.js"
|
2023-12-16 19:21:03 +01:00
|
|
|
|
|
|
|
|
function addAuthHeader(request, options) {
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
if (authStore.token) {
|
2024-04-15 19:39:03 +02:00
|
|
|
options.headers = {
|
|
|
|
|
Authorization: `Bearer ${authStore.token}`,
|
|
|
|
|
...options.headers,
|
|
|
|
|
}
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addPasswordToFormRequest(request, options) {
|
2024-04-15 19:39:03 +02:00
|
|
|
if (!request || !request.startsWith("/forms/")) return
|
2024-01-16 13:27:54 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
const slug = request.split("/")[2]
|
2023-12-16 19:21:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
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
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 15:43:28 +01:00
|
|
|
/**
|
|
|
|
|
* Add custom domain header if custom domain is used
|
|
|
|
|
*/
|
|
|
|
|
function addCustomDomainHeader(request, options) {
|
|
|
|
|
if (!customDomainUsed()) return
|
2024-04-15 19:39:03 +02:00
|
|
|
options.headers["x-custom-domain"] = getDomain(getHost())
|
2024-01-12 15:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
export function getOpnRequestsOptions(request, opts) {
|
2024-01-11 14:07:27 +01:00
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
2024-01-13 18:17:24 +01:00
|
|
|
if (opts.body && opts.body instanceof FormData) {
|
|
|
|
|
opts.headers = {
|
2024-04-15 19:39:03 +02:00
|
|
|
charset: "utf-8",
|
2024-01-13 18:17:24 +01:00
|
|
|
...opts.headers,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
opts.headers = { accept: "application/json", ...opts.headers }
|
2023-12-16 19:21:03 +01:00
|
|
|
|
2024-01-11 14:07:27 +01:00
|
|
|
// Authenticate requests coming from the server
|
2024-03-28 17:59:41 +01:00
|
|
|
if (import.meta.server && config.apiSecret) {
|
2024-04-15 19:39:03 +02:00
|
|
|
opts.headers["x-api-secret"] = config.apiSecret
|
2024-01-11 14:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
addAuthHeader(request, opts)
|
|
|
|
|
addPasswordToFormRequest(request, opts)
|
2024-01-12 15:43:28 +01:00
|
|
|
addCustomDomainHeader(request, opts)
|
2023-12-16 19:21:03 +01:00
|
|
|
|
2024-04-15 16:02:28 +02:00
|
|
|
if (!opts.baseURL) opts.baseURL = config.privateApiBase || config.public.apiBase
|
2024-01-13 19:57:39 +01:00
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
return {
|
2024-04-15 19:39:03 +02:00
|
|
|
async onResponseError({ response }) {
|
2023-12-16 19:21:03 +01:00
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
const { status } = response
|
2024-01-12 15:43:28 +01:00
|
|
|
if (status === 401) {
|
|
|
|
|
if (authStore.check) {
|
|
|
|
|
console.log("Logging out due to 401")
|
|
|
|
|
authStore.logout()
|
2024-04-15 19:39:03 +02:00
|
|
|
useRouter().push({ name: "login" })
|
2024-01-12 15:43:28 +01:00
|
|
|
}
|
2024-01-12 17:32:10 +01:00
|
|
|
} else if (status === 420) {
|
|
|
|
|
// If invalid domain, redirect to main domain
|
2024-04-15 19:39:03 +02:00
|
|
|
window.location.href =
|
|
|
|
|
config.public.appUrl + "?utm_source=failed_custom_domain_redirect"
|
2024-01-12 17:32:10 +01:00
|
|
|
} else if (status >= 500) {
|
2024-04-15 19:39:03 +02:00
|
|
|
console.error("Request error", status)
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
},
|
2024-04-15 19:39:03 +02:00
|
|
|
...opts,
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 13:46:55 +01:00
|
|
|
export const opnFetch = (request, opts = {}) => {
|
|
|
|
|
return $fetch(request, getOpnRequestsOptions(request, opts))
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
export const useOpnApi = (request, opts = {}) => {
|
|
|
|
|
return useFetch(request, getOpnRequestsOptions(request, opts))
|
|
|
|
|
}
|