mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-22 23:31:12 +02:00
Fixed breaking changes
This commit is contained in:
parent
ddb718ff4c
commit
75329e5b41
1 changed files with 35 additions and 25 deletions
|
|
@ -1,25 +1,15 @@
|
||||||
const NO_TRAILING_SLASH_ENDPOINTS = [
|
|
||||||
'users/me',
|
|
||||||
'api/v1/chats',
|
|
||||||
'api/v1/search-spaces'
|
|
||||||
];
|
|
||||||
|
|
||||||
function shouldSkipTrailingSlash(url: string): boolean {
|
|
||||||
const basePath = url.includes('?') ? url.split('?')[0] : url;
|
|
||||||
|
|
||||||
return NO_TRAILING_SLASH_ENDPOINTS.some(endpoint =>
|
|
||||||
basePath.includes(endpoint)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API client with Next.js caching and trailing slash handling
|
* List of API endpoints that require specific trailing slash handling
|
||||||
*/
|
*/
|
||||||
export async function fetchWithCache(url: string, options: RequestInit & {
|
const ENDPOINTS_CONFIG = {
|
||||||
revalidate?: number | false,
|
'users/me': false,
|
||||||
} = {}) {
|
'api/v1/chats': false,
|
||||||
const { revalidate, ...fetchOptions } = options;
|
'api/v1/documents': true,
|
||||||
|
'api/v1/searchspaces': true
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function formatUrlPath(url: string): string {
|
||||||
let basePath = url;
|
let basePath = url;
|
||||||
let queryParams = '';
|
let queryParams = '';
|
||||||
|
|
||||||
|
|
@ -27,14 +17,34 @@ export async function fetchWithCache(url: string, options: RequestInit & {
|
||||||
[basePath, queryParams] = url.split('?');
|
[basePath, queryParams] = url.split('?');
|
||||||
queryParams = `?${queryParams}`;
|
queryParams = `?${queryParams}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let needsTrailingSlash = true;
|
||||||
|
|
||||||
if (!shouldSkipTrailingSlash(basePath)) {
|
for (const [endpoint, shouldHaveSlash] of Object.entries(ENDPOINTS_CONFIG)) {
|
||||||
basePath = basePath.endsWith('/') ? basePath : `${basePath}/`;
|
if (basePath.includes(endpoint)) {
|
||||||
} else {
|
needsTrailingSlash = shouldHaveSlash;
|
||||||
basePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const formattedUrl = basePath + queryParams;
|
if (needsTrailingSlash && !basePath.endsWith('/')) {
|
||||||
|
basePath = `${basePath}/`;
|
||||||
|
} else if (!needsTrailingSlash && basePath.endsWith('/')) {
|
||||||
|
basePath = basePath.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return basePath + queryParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API client with Next.js caching and careful URL handling
|
||||||
|
*/
|
||||||
|
export async function fetchWithCache(url: string, options: RequestInit & {
|
||||||
|
revalidate?: number | false,
|
||||||
|
} = {}) {
|
||||||
|
const { revalidate, ...fetchOptions } = options;
|
||||||
|
|
||||||
|
const formattedUrl = formatUrlPath(url);
|
||||||
|
|
||||||
const response = await fetch(formattedUrl, {
|
const response = await fetch(formattedUrl, {
|
||||||
...fetchOptions,
|
...fetchOptions,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue