mirror of
https://github.com/directus/directus.git
synced 2026-02-06 09:05:06 -05:00
Add Axios
This commit is contained in:
19
src/api.test.ts
Normal file
19
src/api.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { getRootPath } from './api';
|
||||
|
||||
describe('API', () => {
|
||||
beforeAll(() => {
|
||||
globalThis.window = Object.create(window);
|
||||
});
|
||||
|
||||
it('Calculates the correct API root URL based on window', () => {
|
||||
Object.defineProperty(window, 'location', {
|
||||
value: {
|
||||
pathname: '/api/nested/admin'
|
||||
},
|
||||
writable: true
|
||||
});
|
||||
|
||||
const result = getRootPath();
|
||||
expect(result).toBe('/api/nested/');
|
||||
});
|
||||
});
|
||||
15
src/api.ts
Normal file
15
src/api.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: getRootPath()
|
||||
});
|
||||
|
||||
export function getRootPath(): string {
|
||||
const path = window.location.pathname;
|
||||
const parts = path.split('/');
|
||||
const adminIndex = parts.indexOf('admin');
|
||||
const rootPath = parts.slice(0, adminIndex).join('/') + '/';
|
||||
return rootPath;
|
||||
}
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user