Implemented a Zustand-based project_id store, expanded project selection/switching to persist project_id,
and centralized backend requests via api/apiFetch (including data provider updates) to inject X-Project-ID.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import axios from "axios";
|
||||
import { config } from "@config/config";
|
||||
import { useProjectStore } from "@/store/projectStore";
|
||||
|
||||
export const API_URL = process.env.NEXT_PUBLIC_API_URL || config.BACKEND_URL;
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
});
|
||||
|
||||
api.interceptors.request.use((request) => {
|
||||
const projectId = useProjectStore.getState().currentProjectId;
|
||||
if (projectId) {
|
||||
request.headers = request.headers ?? {};
|
||||
request.headers["X-Project-ID"] = projectId;
|
||||
}
|
||||
return request;
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
import { useProjectStore } from "@/store/projectStore";
|
||||
|
||||
export const apiFetch = (input: RequestInfo | URL, init: RequestInit = {}) => {
|
||||
const projectId = useProjectStore.getState().currentProjectId;
|
||||
const headers = new Headers(init.headers ?? {});
|
||||
if (projectId) {
|
||||
headers.set("X-Project-ID", projectId);
|
||||
}
|
||||
return fetch(input, { ...init, headers });
|
||||
};
|
||||
Reference in New Issue
Block a user