I am currently using createAsyncThunk to create async middlewares for a React-Redux app I am working on in the following manner:
import axios from "axios";
import ApiUrls from "../ApiUrls";
import { createAsyncThunk } from "@reduxjs/toolkit";
class NftMiddleware{
//The middleware
static fetchAllNfts = createAsyncThunk('nft/fetchAllNfts', async () => {
const response = await axios.get(ApiUrls.getAllNftsUrl);
return response.data;
});
}
Now if I want to also create a wrapper function for a fetch request without triggering any dispatch, should I add another static function to the NftMiddleware class like so:
class NftMiddleware{
//The middleware
static fetchAllNfts = createAsyncThunk('nft/fetchAllNfts', async () => {
const response = await axios.get(ApiUrls.getAllNftsUrl);
return response.data;
});
//Not actually middleware, just a simple api call wrapper
static postMetaData = async (data) => {
const response = await axios.post(ApiUrls.postMetaData, data);
return response.data;
}
}
Is this the right way of organizing my code that interacts with the backend? Should these be separate classes? For middleware and then for actual API calls? I can't help but feel I am doing things in an 'unclean' and unintuitive manner, as opposed to a more 'centralized' way.