Add Axios

This commit is contained in:
rijkvanzanten
2020-02-06 16:58:48 -05:00
parent 60e8447482
commit c4f90b724a
2 changed files with 34 additions and 0 deletions

19
src/api.test.ts Normal file
View 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
View 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;