Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eric-crowell opened this issue Jul 16, 2023 · 1 comment
Open

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

eric-crowell opened this issue Jul 16, 2023 · 1 comment
Labels
enhancement New feature or request question Further information is requested rtk-query
Milestone

Comments

@eric-crowell
Copy link
Contributor

eric-crowell commented Jul 16, 2023

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

@markerikson markerikson added this to the 2.0 milestone Aug 16, 2023
@markerikson markerikson added the linear Created by Linear-GitHub Sync label Sep 22, 2023
@markerikson markerikson changed the title Sharing a set of endpoints between the two Apis? [RED-15] Sharing a set of endpoints between the two Apis? Sep 22, 2023
@markerikson markerikson modified the milestones: 2.0, Post 2.0 Oct 1, 2023
Copy link
Collaborator

This feels like two separate requests:

  • Extract endpoint definitions outside of createApi
  • Add React hooks to an already created API instance

(Remember that endpoint definitions don't know anything about hooks - it's the createApi module definition looking at the endpoints and doing something additional with those definitions.

We've had some requests for the ability to add React hooks from a UI-agnostic API instance, and there's an early POC over at #4128 .
For extracting endpoints, your current snippet looks pretty plausible - is there something about that that doesn't work right now?

@markerikson markerikson added enhancement New feature or request question Further information is requested rtk-query and removed linear Created by Linear-GitHub Sync labels Feb 6, 2024 — with Volta.net
@markerikson markerikson closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
@markerikson markerikson reopened this Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested rtk-query
Projects
None yet
Development

No branches or pull requests

2 participants