WIP - additional fixes for multitenant
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
export const useApi = () => {
|
||||
const config = useRuntimeConfig()
|
||||
const apiBaseUrl = config.public.apiBaseUrl
|
||||
|
||||
// Use current domain for API calls (same subdomain routing)
|
||||
const getApiBaseUrl = () => {
|
||||
if (import.meta.client) {
|
||||
// In browser, use current hostname but with port 3000 for API
|
||||
const currentHost = window.location.hostname
|
||||
const protocol = window.location.protocol
|
||||
return `${protocol}//${currentHost}:3000`
|
||||
}
|
||||
// Fallback for SSR
|
||||
return config.public.apiBaseUrl
|
||||
}
|
||||
|
||||
const getHeaders = () => {
|
||||
const headers: Record<string, string> = {
|
||||
@@ -25,7 +36,7 @@ export const useApi = () => {
|
||||
|
||||
const api = {
|
||||
async get(path: string) {
|
||||
const response = await fetch(`${apiBaseUrl}/api${path}`, {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
headers: getHeaders(),
|
||||
})
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
@@ -33,7 +44,7 @@ export const useApi = () => {
|
||||
},
|
||||
|
||||
async post(path: string, data: any) {
|
||||
const response = await fetch(`${apiBaseUrl}/api${path}`, {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
@@ -43,7 +54,7 @@ export const useApi = () => {
|
||||
},
|
||||
|
||||
async put(path: string, data: any) {
|
||||
const response = await fetch(`${apiBaseUrl}/api${path}`, {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
method: 'PUT',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
@@ -53,7 +64,7 @@ export const useApi = () => {
|
||||
},
|
||||
|
||||
async delete(path: string) {
|
||||
const response = await fetch(`${apiBaseUrl}/api${path}`, {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
method: 'DELETE',
|
||||
headers: getHeaders(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user