Skip to content

[RED-15] Sharing a set of endpoints between the two Apis? #3598

Open
@eric-crowell

Description

@eric-crowell

Hello,
I would like to define a set of endpoints I can use between two Api types. That way, if I need to alter or change those endpoints, I only need to modify it once for both Apis

For instance, I am looking to make some modules like so:

- src/
- - todo/
- - - todoEndpoints.ts # Set of endpoints for both `todoApi.ts` and `todoApiReact.ts`
- - - todoApi.ts # @reduxjs/toolkit/query
- - - todoApiReact.ts # @reduxjs/toolkit/query/react

I'd import and use the endpoints in a way like so:

// todoApiReact.ts
import { createApi } from '@reduxjs/toolkit/query/react';
import { todoEndpoints } from './todoEndpoints.ts';

export const todoApiReact = createApi({
   // ...
   endpoints: todoEndpoints,
});

I'm imagining the endpoints module could look something like this:

import type {
  EndpointBuilder,
  BaseQueryFn,
} from '@reduxjs/toolkit/query';

export const todoEndpoints = <T extends EndpointBuilder<BaseQueryFn, string, string>>(
  builder: T,
) => ({
   createTodo: builder.mutation</** ... */>(/** ... */),
   readTodo: builder.query</** ... */>(/** ... */),
});

For my case, it would be especially useful if I could apply React hooks to my non-react api. Maybe like const myReactApi = reactifyApi(myApi);. Maybe that is already possible?

I understand I can create individual definitions with EndpointDefinition(s), but it doesn't save time if I want to add new endpoints. Might be something I'm missing. How would one best share the same endpoints?

RED-15

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions