Now supports running under a sub path
Just set the SPOOLMAN_BASE_PATH environment variable and it should work. Resolves #95
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import dayjs from "dayjs";
|
||||
import { getAPIURL } from "./url";
|
||||
|
||||
export enum FieldType {
|
||||
text = "text",
|
||||
@@ -34,11 +35,10 @@ export interface Field extends FieldParameters {
|
||||
}
|
||||
|
||||
export function useGetFields(entity_type: EntityType) {
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useQuery<Field[]>({
|
||||
queryKey: ["fields", entity_type],
|
||||
queryFn: async () => {
|
||||
const response = await fetch(`${apiEndpoint}/field/${entity_type}`);
|
||||
const response = await fetch(`${getAPIURL()}/field/${entity_type}`);
|
||||
return response.json();
|
||||
},
|
||||
});
|
||||
@@ -47,10 +47,9 @@ export function useGetFields(entity_type: EntityType) {
|
||||
export function useSetField(entity_type: EntityType) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useMutation<Field[], unknown, { key: string; params: FieldParameters }, { previousFields?: Field[] }>({
|
||||
mutationFn: async ({ key, params }) => {
|
||||
const response = await fetch(`${apiEndpoint}/field/${entity_type}/${key}`, {
|
||||
const response = await fetch(`${getAPIURL()}/field/${entity_type}/${key}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -110,10 +109,9 @@ export function useSetField(entity_type: EntityType) {
|
||||
export function useDeleteField(entity_type: EntityType) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useMutation<Field[], unknown, string>({
|
||||
mutationFn: async (key) => {
|
||||
const response = await fetch(`${apiEndpoint}/field/${entity_type}/${key}`, {
|
||||
const response = await fetch(`${getAPIURL()}/field/${entity_type}/${key}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { getAPIURL } from "./url";
|
||||
|
||||
interface SettingResponseValue {
|
||||
value: string;
|
||||
@@ -11,22 +12,20 @@ interface SettingsResponse {
|
||||
}
|
||||
|
||||
export function useGetSettings() {
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useQuery<SettingsResponse>({
|
||||
queryKey: ["settings"],
|
||||
queryFn: async () => {
|
||||
const response = await fetch(`${apiEndpoint}/setting`);
|
||||
const response = await fetch(`${getAPIURL()}/setting/`);
|
||||
return response.json();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetSetting(key: string) {
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useQuery<SettingResponseValue>({
|
||||
queryKey: ["settings", key],
|
||||
queryFn: async () => {
|
||||
const response = await fetch(`${apiEndpoint}/setting/${key}`);
|
||||
const response = await fetch(`${getAPIURL()}/setting/${key}`);
|
||||
return response.json();
|
||||
},
|
||||
});
|
||||
@@ -35,10 +34,9 @@ export function useGetSetting(key: string) {
|
||||
export function useSetSetting() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const apiEndpoint = import.meta.env.VITE_APIURL;
|
||||
return useMutation<SettingResponseValue, unknown, { key: string; value: unknown }>({
|
||||
mutationFn: async ({ key, value }) => {
|
||||
const response = await fetch(`${apiEndpoint}/setting/${key}`, {
|
||||
const response = await fetch(`${getAPIURL()}/setting/${key}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
||||
35
client/src/utils/url.ts
Normal file
35
client/src/utils/url.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
declare global {
|
||||
interface Window {
|
||||
SPOOLMAN_BASE_PATH: string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base path of the application.
|
||||
*
|
||||
* If a base path is set, this returns e.g. "/spoolman". If none is set, it returns "".
|
||||
*
|
||||
* @return {string} The base path of the application. If the `SPOOLMAN_BASE_PATH`
|
||||
* window variable is set and not empty, it is returned. Otherwise, the
|
||||
* default base path "" is returned.
|
||||
*/
|
||||
export function getBasePath(): string {
|
||||
if (window.SPOOLMAN_BASE_PATH && window.SPOOLMAN_BASE_PATH.length > 0) {
|
||||
return window.SPOOLMAN_BASE_PATH;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A function that returns the Spoolman API URL
|
||||
* This returns e.g. "/spoolman/api/v1" if the base path is "/spoolman"
|
||||
*
|
||||
* @return {string} The API URL
|
||||
*/
|
||||
export function getAPIURL(): string {
|
||||
if (!import.meta.env.VITE_APIURL) {
|
||||
throw new Error("VITE_APIURL is not set");
|
||||
}
|
||||
return getBasePath() + import.meta.env.VITE_APIURL;
|
||||
}
|
||||
Reference in New Issue
Block a user