Merge pull request #924 from bigcapitalhq/20260201-180532-f578

fix(webapp): normalize api path
This commit is contained in:
Ahmed Bouhuolia
2026-02-01 18:06:51 +02:00
committed by GitHub
3 changed files with 22 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ import { useQuery } from 'react-query';
import { castArray, defaultTo } from 'lodash';
import { useAuthOrganizationId } from './state';
import useApiRequest from './useRequest';
import { normalizeApiPath } from '../utils';
import { useRef } from 'react';
/**
@@ -19,7 +20,11 @@ export function useRequestQuery(query, axios, props) {
const states = useQuery(
query,
() => apiRequest.http({ ...axios, url: `/api/${axios.url}` }),
() =>
apiRequest.http({
...axios,
url: `/api/${normalizeApiPath(axios.url)}`,
}),
props,
);
// Momerize the default data.

View File

@@ -7,7 +7,7 @@ import {
useSetGlobalErrors,
useAuthToken,
} from './state';
import { getCookie } from '../utils';
import { getCookie, normalizeApiPath } from '../utils';
export default function useApiRequest() {
const setGlobalErrors = useSetGlobalErrors();
@@ -93,27 +93,27 @@ export default function useApiRequest() {
http,
get(resource, params) {
return http.get(`/api/${resource}`, params);
return http.get(`/api/${normalizeApiPath(resource)}`, params);
},
post(resource, params, config) {
return http.post(`/api/${resource}`, params, config);
return http.post(`/api/${normalizeApiPath(resource)}`, params, config);
},
update(resource, slug, params) {
return http.put(`/api/${resource}/${slug}`, params);
return http.put(`/api/${normalizeApiPath(resource)}/${slug}`, params);
},
put(resource, params) {
return http.put(`/api/${resource}`, params);
return http.put(`/api/${normalizeApiPath(resource)}`, params);
},
patch(resource, params, config) {
return http.patch(`/api/${resource}`, params, config);
return http.patch(`/api/${normalizeApiPath(resource)}`, params, config);
},
delete(resource, params) {
return http.delete(`/api/${resource}`, params);
return http.delete(`/api/${normalizeApiPath(resource)}`, params);
},
}),
[http],
@@ -130,22 +130,22 @@ export function useAuthApiRequest() {
() => ({
http,
get(resource, params) {
return http.get(`/api/${resource}`, params);
return http.get(`/api/${normalizeApiPath(resource)}`, params);
},
post(resource, params, config) {
return http.post(`/api/${resource}`, params, config);
return http.post(`/api/${normalizeApiPath(resource)}`, params, config);
},
update(resource, slug, params) {
return http.put(`/api/${resource}/${slug}`, params);
return http.put(`/api/${normalizeApiPath(resource)}/${slug}`, params);
},
put(resource, params) {
return http.put(`/api/${resource}`, params);
return http.put(`/api/${normalizeApiPath(resource)}`, params);
},
patch(resource, params, config) {
return http.patch(`/api/${resource}`, params, config);
return http.patch(`/api/${normalizeApiPath(resource)}`, params, config);
},
delete(resource, params) {
return http.delete(`/api/${resource}`, params);
return http.delete(`/api/${normalizeApiPath(resource)}`, params);
},
}),
[http],

View File

@@ -13,6 +13,9 @@ import jsCookie from 'js-cookie';
import { deepMapKeys } from './map-key-deep';
export * from './deep';
/** Strips leading slash from a path segment to avoid double slashes when joining with a base (e.g. `/api/` + path). */
export const normalizeApiPath = (path) => (path || '').replace(/^\//, '');
export const getCookie = (name, defaultValue) =>
_.defaultTo(jsCookie.get(name), defaultValue);