mirror of
https://github.com/strapi/strapi.git
synced 2025-11-07 21:58:23 +00:00
test the axios.create argument
This commit is contained in:
parent
3c3cab3cad
commit
feab74b178
@ -1,39 +1,44 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { auth } from '@strapi/helper-plugin';
|
import { auth } from '@strapi/helper-plugin';
|
||||||
|
|
||||||
export const instance = axios.create({
|
// eslint-disable-next-line import/no-mutable-exports
|
||||||
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
export let instance;
|
||||||
});
|
|
||||||
|
|
||||||
instance.interceptors.request.use(
|
|
||||||
async (config) => {
|
|
||||||
config.headers = {
|
|
||||||
Authorization: `Bearer ${auth.getToken()}`,
|
|
||||||
Accept: 'application/json',
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
Promise.reject(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
instance.interceptors.response.use(
|
|
||||||
(response) => response,
|
|
||||||
(error) => {
|
|
||||||
// whatever you want to do with the error
|
|
||||||
if (error?.response?.status === 401) {
|
|
||||||
auth.clearAppStorage();
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export const getFetchClient = () => {
|
export const getFetchClient = () => {
|
||||||
|
if (!instance) {
|
||||||
|
instance = axios.create({
|
||||||
|
baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
instance.interceptors.request.use(
|
||||||
|
async (config) => {
|
||||||
|
config.headers = {
|
||||||
|
Authorization: `Bearer ${auth.getToken()}`,
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
instance.interceptors.response.use(
|
||||||
|
(response) => response,
|
||||||
|
(error) => {
|
||||||
|
// whatever you want to do with the error
|
||||||
|
if (error?.response?.status === 401) {
|
||||||
|
auth.clearAppStorage();
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get: (url, config) => instance.get(url, config),
|
get: (url, config) => instance.get(url, config),
|
||||||
put: (url, data, config) => instance.put(url, data, config),
|
put: (url, data, config) => instance.put(url, data, config),
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { auth } from '@strapi/helper-plugin';
|
import { auth } from '@strapi/helper-plugin';
|
||||||
import { getFetchClient } from '../getFetchClient';
|
import { getFetchClient, instance } from '../getFetchClient';
|
||||||
|
|
||||||
const token = 'coolToken';
|
const token = 'coolToken';
|
||||||
auth.getToken = jest.fn().mockReturnValue(token);
|
auth.getToken = jest.fn().mockReturnValue(token);
|
||||||
auth.clearAppStorage = jest.fn().mockReturnValue(token);
|
auth.clearAppStorage = jest.fn().mockReturnValue(token);
|
||||||
|
process.env.STRAPI_ADMIN_BACKEND_URL = 'http://localhost:1337';
|
||||||
|
|
||||||
describe('ADMIN | utils | getFetchClient', () => {
|
describe('ADMIN | utils | getFetchClient', () => {
|
||||||
it('should return the 4 HTTP methods to call GET, POST, PUT and DELETE apis', () => {
|
it('should return the 4 HTTP methods to call GET, POST, PUT and DELETE apis', () => {
|
||||||
@ -32,13 +33,11 @@ describe('ADMIN | utils | getFetchClient', () => {
|
|||||||
expect(auth.clearAppStorage).toHaveBeenCalledTimes(1);
|
expect(auth.clearAppStorage).toHaveBeenCalledTimes(1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
it('should respond with status 200 to a known API', async () => {
|
it('should respond with status 200 to a known API and create the instance with the correct base URL', async () => {
|
||||||
const response = getFetchClient();
|
const response = getFetchClient();
|
||||||
try {
|
const getData = await response.get('/admin/project-type');
|
||||||
const getData = await response.get('/admin/project-type');
|
expect(getData.status).toBe(200);
|
||||||
expect(getData.status).toBe(200);
|
|
||||||
} catch (err) {
|
expect(instance.defaults.baseURL).toBe(process.env.STRAPI_ADMIN_BACKEND_URL);
|
||||||
console.log('err', err);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user