hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.43k stars 106 forks source link

`@tanstack/react-query` is not compatible with `asClass` services #1090

Closed prenaissance closed 2 months ago

prenaissance commented 2 months ago

Description

Turning on both asClass services and the tanstack query plugin resulting in wrong imports in the react-query.gen.ts file:

import {
  client,
  getFlatServiceEp1, /* Module '"../services.gen"' has no exported member 'getFlatServiceEp1' */
  getFlatServiceEp2, /* Module '"../services.gen"' has no exported member 'getFlatServiceEp2' */
} from "../services.gen";

Reproducible example or configuration

import { defineConfig } from "@hey-api/openapi-ts";

export default defineConfig({
  client: "@hey-api/client-fetch",
  input: "http://localhost:3080/swagger/json",
  output: {
    path: "src/client",
    format: "prettier",
    lint: "eslint",
  },
  services: {
    asClass: true,
  },
  plugins: ["@tanstack/react-query"],
});

OpenAPI specification (optional)

No response

System information (optional)

OS - Ubuntu 22.04 on WSL2, Package manager - pnpm 9.10.0 Node version - 22.9.0 openapi-ts version - 0.53.4 typescript version - 5.6.2

mrlubos commented 2 months ago

Hey! What would you expect the resulting code to look like?

prenaissance commented 2 months ago

Hey! What would you expect the resulting code to look like?

Hello. I would expect the grouped services to be imported instead. The generated services look the following way:

export class MyService {
  public static getEp1<
    ThrowOnError extends boolean = false,
  >(options) {/*...*/}

  public static getEp2<
    ThrowOnError extends boolean = false,
  >(options) {/*...*/}
}

so the import should be:

import {
  client,
  MyService
} from "../services.gen";

I would expect the static classes to be imported. Instead, the plugin always assumes a flat structure.

mrlubos commented 2 months ago

@prenaissance Do you expect the TanStack functions to be flat? Grouped?

prenaissance commented 2 months ago

@mrlubos I don't have a preference about that. The issues is that react-query.gen.ts imports non-existings functions from services.gen.ts when the asClass option is turned on. I can try to make a sandbox example if it's not clear.

mrlubos commented 2 months ago

No need, that much is clear. Why do you have a preference for using asClass: true but no preference for TanStack functions?