chore: remove axios-instance and fix types for StrapiApp

This commit is contained in:
Josh 2023-02-16 13:51:54 +01:00
parent b026f35fde
commit db4036bc4d
10 changed files with 13 additions and 95 deletions

View File

@ -7,7 +7,9 @@ module.exports = {
*
* This gives you an opportunity to extend code.
*/
register({ strapi }) {},
register(app) {
console.log(app);
},
/**
* An asynchronous bootstrap function that runs before

View File

@ -1,42 +0,0 @@
/**
* axios with a custom config.
*/
import axios from 'axios';
import { auth, wrapAxiosInstance } from '@strapi/helper-plugin';
const 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;
}
);
const wrapper = wrapAxiosInstance(instance);
export default wrapper;

View File

@ -4,7 +4,7 @@
*
*/
import React, { useEffect, useRef } from 'react';
import { useEffect, useRef } from 'react';
import pluginId from '../../pluginId';
type InitializerProps = {

View File

@ -7,6 +7,6 @@
import React from 'react';
import { Puzzle } from '@strapi/icons';
const PluginIcon: React.VoidFunctionComponent = () => <Puzzle />;
const PluginIcon = () => <Puzzle />;
export default PluginIcon;

View File

@ -1,4 +1,5 @@
import { prefixPluginTranslations } from '@strapi/helper-plugin';
import pluginPkg from '../../package.json';
import pluginId from './pluginId';
import Initializer from './components/Initializer';
@ -7,7 +8,7 @@ import PluginIcon from './components/PluginIcon';
const name = pluginPkg.strapi.name;
export default {
register(app) {
register(app: any) {
app.addMenuLink({
to: `/plugins/${pluginId}`,
icon: PluginIcon,
@ -38,12 +39,13 @@ export default {
app.registerPlugin(plugin);
},
bootstrap(app) {},
async registerTrads(app) {
bootstrap(app: any) {},
async registerTrads(app: any) {
const { locales } = app;
const importedTrads = await Promise.all(
locales.map((locale) => {
(locales as any[]).map((locale) => {
return import(`./translations/${locale}.json`)
.then(({ default: data }) => {
return {

View File

@ -11,7 +11,7 @@ import { NotFound } from '@strapi/helper-plugin';
import pluginId from '../../pluginId';
import HomePage from '../HomePage';
const App: React.VoidFunctionComponent = () => {
const App = () => {
return (
<div>
<Switch>

View File

@ -7,7 +7,7 @@
import React from 'react';
import pluginId from '../../pluginId';
const HomePage: React.VoidFunctionComponent = () => {
const HomePage = () => {
return (
<div>
<h1>{pluginId}&apos;s HomePage</h1>

View File

@ -1,42 +0,0 @@
/**
* axios with a custom config.
*/
import axios from 'axios';
import { auth, wrapAxiosInstance } from '@strapi/helper-plugin';
const 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;
}
);
const wrapper = wrapAxiosInstance(instance);
export default wrapper;

View File

@ -12,7 +12,6 @@
"@strapi/design-system": "^1.6.3",
"@strapi/helper-plugin": "^4.6.0",
"@strapi/icons": "^1.6.3",
"axios": "^1.2.2",
"prop-types": "^15.7.2"
},
"devDependencies": {

View File

@ -11,7 +11,6 @@
"@strapi/design-system": "^1.6.3",
"@strapi/helper-plugin": "^4.6.0",
"@strapi/icons": "^1.6.3",
"axios": "^1.2.2",
"prop-types": "^15.7.2"
},
"devDependencies": {