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

Export VerbOptions and other important types #1590

Closed
clragon opened this issue Aug 19, 2024 · 5 comments · Fixed by #1824
Closed

Export VerbOptions and other important types #1590

clragon opened this issue Aug 19, 2024 · 5 comments · Fixed by #1824
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@clragon
Copy link

clragon commented Aug 19, 2024

What are the steps to reproduce this issue?

  1. create an orval.config.ts
  2. use defineConfig to create your config
  3. specify input and output transformers
  4. refactor your transformers and move them into their own functions / files

Because we are using typescript and want to type our function parameters,
we need access to both OpenAPIObject which is what is passed to an InputTransformer and VerbOptions which is passed to an OutputTransformer.

However, Orval does not expose these types.
The OpenAPIObject type can be accessed by adding and importing the openapi3-ts/oas30 package.
The VerbOptions can only be accessed if we employ longwinded and arcane typescript unpacking magic.
This is the code I have written to gain access to VerbOptions:

import { defineConfig } from 'orval';

type ConfigType = Parameters<typeof defineConfig>[0];

type ValueOf<T> = T[keyof T];

type UnwrappedConfigValueType = Unwrap<ValueOf<Unwrap<ConfigType>>>;

type Unwrap<T> = T extends Promise<infer U> ? Unwrap<U> : T extends () => infer U ? Unwrap<U> : T;

type OutputOptionsType = ExtractOutput<UnwrappedConfigValueType>;

type ExtractOutput<T> = T extends { output?: infer O }
  ? O extends string | undefined
    ? never
    : O
  : never;

type OverrideOutputType = ExtractOverride<OutputOptionsType>;

type ExtractOverride<T> = T extends { override?: infer O }
  ? O extends undefined
    ? never
    : O
  : never;

type ExtractTransformer<T> = T extends { transformer?: infer O }
  ? O extends (options: infer U) => unknown
    ? U
    : never
  : never;

export type VerbOptions = ExtractTransformer<OverrideOutputType>;

What happens?

I have to add and import extra packages as well as write a vast amount of boiler plate code so that my transformer functions can be typed safely and correctly.

What were you expecting to happen?

I expect the orval package to export these types so I can use them right away.

Any logs, error output, etc?

N/A

Any other comments?

These types are inferred, but that only works as long as the transformer function is inline.
As soon as we refactor because we have a larger amount of configuration / extras, we run into this issue.

What versions are you using?

  System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
    Memory: 2.71 GB / 15.78 GB
  npmPackages:
    orval: ^7.0.1 => 7.0.1
    react: ^18.3.1 => 18.3.1
@melloware
Copy link
Collaborator

PR is welcome !

@melloware melloware added the enhancement New feature or request label Aug 19, 2024
@AllieJonsson
Copy link
Contributor

Hi, I think this is already exported as GeneratorVerbOptions from @orval/core:
image
Please let me know if I misunderstood!

@clragon
Copy link
Author

clragon commented Jan 14, 2025

You are right, these types are exported by @orval/core!
@orval/core was not in my direct dependencies and the installation docs didn't specify I need to install it,
and therefore it didnt show up in the autocomplete of my IDE.

It doesn't look like I am meant to directly depend on @orval/core,
and importing things from an indirect dependency is discouraged.

Do you think it would be sensible to re-export these types in orval, considering its public functions use it?

@melloware
Copy link
Collaborator

cc @anymaniax thoughts?

@anymaniax
Copy link
Collaborator

The Orval package combines all the dependencies, and for me, it's not an issue to re-export the types from it. All the other packages were meant to enable more advanced functionality, such as using a specific version of a client or performing custom generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants