redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

RTK Query - How can I use a API client as base query and provide a custom configuration.

Open sebastienlabine opened this issue 3 years ago • 0 comments

Hi,

I have developed a Node JS API Client that is being used in several of my apps. I now want to wrap this client inside hooks.

I use a fakeBaseQuery in order to make my call to my JS client.

const merchantService = new MerchantService();
export const merchantAPI = smartbillsAPI
    .injectEndpoints({
        endpoints: (builder) => ({
            getMerchants: builder.query<PaginatedResult<SBMerchant>, GetMerchantsRequest>({
                queryFn: async (request, query, params) => {
                    try {
                        var response = await merchantService.get(request);
                        return { data: response.data };
                    } catch (error) {
                        return { error: error as AxiosError };
                    }
                },
                providesTags: [SmartbillsApiTags.MERCHANTS_TAG],
            }),
        })
    });

However, my service allows for configuration options, for example endpoint configuration, accessToken and API key. This configuration is settable by clients using our hooks library. It should also allow a dynamic configuration, meaning the accessToken can be updated if it expires.

const merchantService = new MerchantService({
     endpoint: "https://localhost:3000",
     accessToken: "123465",
})

My question is how can I achive this.

sebastienlabine avatar Jul 22 '22 20:07 sebastienlabine