Route /auth and /config to local API; everything else uses API_URL
This commit is contained in:
@@ -11,19 +11,19 @@ test('uses basePath with the default api path', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('uses a relative API_URL', () => {
|
test('routes calls through a relative API_URL', () => {
|
||||||
expect(getApiUrl('/websites', { apiUrl: '/backend/api', basePath: '' })).toBe(
|
expect(getApiUrl('/websites', { apiUrl: '/backend/api', basePath: '' })).toBe(
|
||||||
'/backend/api/websites',
|
'/backend/api/websites',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('uses basePath with a relative API_URL', () => {
|
test('routes calls through basePath with a relative API_URL', () => {
|
||||||
expect(getApiUrl('/websites', { apiUrl: '/backend/api', basePath: '/analytics' })).toBe(
|
expect(getApiUrl('/websites', { apiUrl: '/backend/api', basePath: '/analytics' })).toBe(
|
||||||
'/analytics/backend/api/websites',
|
'/analytics/backend/api/websites',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('uses an absolute API_URL directly', () => {
|
test('routes calls through an absolute API_URL', () => {
|
||||||
expect(getApiUrl('/websites', { apiUrl: 'https://api.example.com/api', basePath: '' })).toBe(
|
expect(getApiUrl('/websites', { apiUrl: 'https://api.example.com/api', basePath: '' })).toBe(
|
||||||
'https://api.example.com/api/websites',
|
'https://api.example.com/api/websites',
|
||||||
);
|
);
|
||||||
@@ -34,3 +34,24 @@ test('leaves absolute request URLs unchanged', () => {
|
|||||||
'https://example.com/custom',
|
'https://example.com/custom',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('keeps /auth/* on the default api path', () => {
|
||||||
|
expect(
|
||||||
|
getApiUrl('/auth/login', { apiUrl: 'https://api.example.com/api', basePath: '' }),
|
||||||
|
).toBe('/api/auth/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('keeps /config on the default api path', () => {
|
||||||
|
expect(getApiUrl('/config', { apiUrl: 'https://api.example.com/api', basePath: '' })).toBe(
|
||||||
|
'/api/config',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('keeps /auth/* on the default api path with basePath', () => {
|
||||||
|
expect(
|
||||||
|
getApiUrl('/auth/verify', {
|
||||||
|
apiUrl: 'https://api.example.com/api',
|
||||||
|
basePath: '/analytics',
|
||||||
|
}),
|
||||||
|
).toBe('/analytics/api/auth/verify');
|
||||||
|
});
|
||||||
+12
-1
@@ -3,6 +3,11 @@ type ApiUrlOptions = {
|
|||||||
basePath?: string;
|
basePath?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const APP_ROUTE_PATTERNS: RegExp[] = [
|
||||||
|
/^\/auth(\/|$)/,
|
||||||
|
/^\/config(\/|$)/,
|
||||||
|
];
|
||||||
|
|
||||||
function trimTrailingSlash(value: string) {
|
function trimTrailingSlash(value: string) {
|
||||||
return value.replace(/\/+$/, '');
|
return value.replace(/\/+$/, '');
|
||||||
}
|
}
|
||||||
@@ -23,13 +28,19 @@ function isAbsoluteUrl(url: string) {
|
|||||||
return /^https?:\/\//i.test(url);
|
return /^https?:\/\//i.test(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isAppRoute(url: string) {
|
||||||
|
const path = `/${trimLeadingSlash(url.split('?')[0])}`;
|
||||||
|
return APP_ROUTE_PATTERNS.some(re => re.test(path));
|
||||||
|
}
|
||||||
|
|
||||||
export function getApiUrl(url: string, options: ApiUrlOptions = {}) {
|
export function getApiUrl(url: string, options: ApiUrlOptions = {}) {
|
||||||
if (isAbsoluteUrl(url)) {
|
if (isAbsoluteUrl(url)) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { apiUrl = process.env.apiUrl || '', basePath = process.env.basePath || '' } = options;
|
const { apiUrl = process.env.apiUrl || '', basePath = process.env.basePath || '' } = options;
|
||||||
const baseUrl = apiUrl
|
const useApiUrl = apiUrl && !isAppRoute(url);
|
||||||
|
const baseUrl = useApiUrl
|
||||||
? isAbsoluteUrl(apiUrl)
|
? isAbsoluteUrl(apiUrl)
|
||||||
? apiUrl
|
? apiUrl
|
||||||
: joinPath(basePath, apiUrl)
|
: joinPath(basePath, apiUrl)
|
||||||
|
|||||||
Reference in New Issue
Block a user