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

RTK Query Generation: Append some code at the top of the file.

Open yassinebridi opened this issue 3 years ago • 2 comments

This is my openapi-config.ts

import type { ConfigFile } from '@rtk-query/codegen-openapi';

const config: ConfigFile = {
  schemaFile: `http://localhost:3000/api-docs?type=json`,
  apiFile: './src/redux/emptyAPI.ts',
  apiImport: 'emptySplitApi',
  outputFile: './src/redux/myApi.ts',
  exportName: 'myApi',
  hooks: {
    lazyQueries: true,
    mutations: true,
    queries: true,
  },
};
export default config;

and it generates something like this.

import { emptySplitApi as api } from './emptyAPI';
const injectedRtkApi = api.injectEndpoints({
  endpoints: (build) => ({
    postApiUserLogout: build.mutation<PostApiUserLogoutApiResponse, PostApiUserLogoutApiArg>({
      query: (queryArg) => ({
        url: `/api/user/logout`,
        method: 'POST',
        body: queryArg.body,
        headers: { 'Content-Type': queryArg['Content-Type'], Accept: queryArg.accept },
      }),
    }),
...

There is two issues with this.

  • i have to manually add export in line 2 after every generation(i wish there is an option to that automatically)
  • There are some typing issues, that i know wouldn't make any issues, but i just want to @ts-nocheck at the top of the file, is this possible?

yassinebridi avatar Oct 21 '22 11:10 yassinebridi

You should see something like export { injectedRtkApi as myApi } on the line after that function. No need to add the export keyword on line 2.

blwinters avatar Dec 09 '22 15:12 blwinters

@yassinebridi Hello, can you provide your json as well ? I am curious how did you manage to generate endpoint where you can set headers (from what I saw it seems that it does not care about what the api consumes or produces, it is not reflected in the generated file), Thank you

Lasto13 avatar Feb 06 '24 21:02 Lasto13