hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.01k stars 83 forks source link

Question about generated classes with static methods #965

Open nekman opened 3 weeks ago

nekman commented 3 weeks ago

Hi, coming from openapi-typescript-codegen where a generated class have the BaseHttpRequest as a dependency in the constructor.

Example:

  export class CustomerService {
    constructor(public readonly httpRequest: BaseHttpRequest) {}

    public getCustomerById({ id }) {
        ...
    }
  }

This makes it very flexible when it comes to testing, I can just replace the httpRequest with a mocked instance.

Generating the same API using this project will however only create functions or a class with static methods: Example:

export class CustomerService {
   public static getCustomerById<ThrowOnError extends boolean = false>(...) {
      ...
   }
}

This makes it harder to test without writing a wrapper. Is it possible to generate something similar as we get with the openapi-typescript-codegen, or have I missed some other settings?

mrlubos commented 3 weeks ago

@nekman can you share your configuration?

nekman commented 3 weeks ago

@mrlubos Sure, here:

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

export default defineConfig({
  client: '@hey-api/client-fetch',
  input: 'openapi/customer-api.yaml',
  output: {
    format: 'prettier',
    path: 'generated/client/customer-api',
    lint: 'eslint'
  },
  services: {
    asClass: true
  },
  schemas: false
});
mrlubos commented 3 weeks ago

@nekman yeah that's not going to generate any httpRequest or constructor with new clients. Can you mock the @hey-api/client-fetch module instead?