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

endpointDefinitions not exported from @reduxjs/toolkit/query

Open zudenkovas opened this issue 1 year ago • 9 comments

Hello I am using some types from endpointDefinitions to define transformedResponse return type in my endpoint enhancing, the problem is that after updating @reduxjs/toolkit to version 2.0.1 I am not able to import them from @reduxjs/toolkit/query, and this url is deepest I am allowed to go down the tree.

"@reduxjs/toolkit": "^2.0.1",

import {
  DefinitionsFromApi,
  OverrideResultType,
  TagTypesFromApi
} from '@reduxjs/toolkit/query';

zudenkovas avatar Dec 08 '23 12:12 zudenkovas

Can you show how (and why) you're using that type?

markerikson avatar Dec 08 '23 14:12 markerikson

Can you show how (and why) you're using that type?

I am using code generation to generate endpoints from OpenAPI using @rtk-query/codegen-openapi. Once the endpoints are generated I am enhancing them adding the transformResponse but I keep on getting the error because typing is not correct and I do not get the right return type from the hook. So I am using this solution #3506 to type it.

zudenkovas avatar Dec 11 '23 08:12 zudenkovas

I'm experiencing the same exact issue when trying to enhance a codegened API.

If it helps in the interim, setting moduleResolution to Node in your tsconfig.json allows you to do a deeper import like I'm doing below. Vite sets it to Bundler by default starting with Vite 5 as Rollup 4 requires it so it probably makes sense to export the type.

import { EntityState, createEntityAdapter } from '@reduxjs/toolkit';
import { DefinitionsFromApi, OverrideResultType } from '@reduxjs/toolkit/dist/query/endpointDefinitions';

import { FindAllCatsApiResponse, Cat, baseApi } from './base-api';

type Definitions = DefinitionsFromApi<typeof baseApi>;
type TagTypes = (typeof tags)[number];

type UpdatedFindAllCatsDef = OverrideResultType<
  Definitions['findAllCats'],
  EntityState<Cat, number>
>;

type UpdatedDefinitions = Omit<Definitions, 'findAllCats'> & {
  findAllCats: UpdatedFindAllCatsDef;
};

const tags = ['cats'] as const;

export const catsAdapter = createEntityAdapter<Cat, number>({
  selectId: (entity) => entity.id,
  sortComparer: (a, b) => a.createdOn - b.createdOn,
});

export const animalApi = baseApi.enhanceEndpoints<TagTypes, UpdatedDefinitions>({
  addTagTypes: tags,
  endpoints: {
    findAllCats: {
      providesTags: ['cats'],
      transformResponse: (response: FindAllCatsApiResponse) =>
        catsAdapter.setAll(catsAdapter.getInitialState(), response),
    },
  },
});

KasimAhmic avatar Jan 07 '24 16:01 KasimAhmic

As of 2.1.0, the types exported from that path (DefinitionsFromApi, OverrideResultType ) are all resolving to the type "never" and my overrides that worked pre-2.0 are no longer working.

mattoni avatar Feb 13 '24 00:02 mattoni

which path are you using? you shouldn't be importing from /dist - as of v2.2.0 we should be exporting those types from the normal entry point.

EskiMojo14 avatar Feb 13 '24 00:02 EskiMojo14

image

Those types don't exist at the 'normal' entry point :(

mattoni avatar Feb 13 '24 00:02 mattoni

try a reinstall - we only released v2.2.0 in the last couple hours :)

EskiMojo14 avatar Feb 13 '24 00:02 EskiMojo14

@mattoni the normal RTK Query entry point :) @reduxjs/toolkit/query or @reduxjs/toolkit/query/react .

markerikson avatar Feb 13 '24 00:02 markerikson

Got it, just figured that out as you commented :) works fine from the new path!

mattoni avatar Feb 13 '24 00:02 mattoni