hey-api / openapi-ts

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

Add configurable default value for ThrowOnError #961

Open MrLepage opened 3 weeks ago

MrLepage commented 3 weeks ago

Currently, services generated code sets the default value of ThrowOnError to false for all API request functions. This default behavior requires developers to explicitly set ThrowOnError to true for each individual API call when they want exceptions to be thrown on errors.

I propose adding a configuration option that allows setting the default value of ThrowOnError globally. This enhancement would significantly improve developer experience and code consistency for projects that prefer a "throw-by-default" error handling strategy.

Proposed changes:

Example configuration:

export default defineConfig({
  // ... other configuration options
  services: {
    throwOnError: true, // New option
  },
});

With this configuration, all generated API request functions would default to throwing errors unless explicitly overridden.

Benefits:

snarky-puppy commented 3 weeks ago

Agree that this should be a generator option.

Meanwhile my work around is to add this post-generator step:

find ./src/clients -type f | xargs perl -pi -e 's/ThrowOnError extends boolean = false/ThrowOnError extends boolean = true/'
smiley-uriux commented 3 weeks ago

This was the first blocker I hit. When reviewing the docs I just assumed there was a way to configure this globally since the hard part appears to have been done (i.e. altering typescript types and allowing the config value to be specified at the individual method level). It's even more confusing because you can actually configure it with client.setConfig({ throwOnError: true}) but that won't alter the typescript types obviously. Ideally this configuration would live at the top-level as OP suggested and removed from client setConfig() method to avoid confusion.

@snarky-puppy thanks for the snippet. That looks like it would address the inferred return types only so those finding this should be aware that client.setConfig({ throwOnError: true }) also needs to called somewhere.

Otherwise love with this library is going!

jpenna commented 1 week ago

Liked the solution presented by the OP. This should be included in the CLI too.

In the meantime, using the solution proposed by @snarky-puppy above, but in a JS script:

function replaceThrowOnError() {
  const clientFile = './src/client/services.gen.ts';
  fs.readFile(clientFile, 'utf8', (err, data) => {
    if (err) {
      console.error('Error:', err);
      return;
    }
    const result = data.replace(
      /ThrowOnError extends boolean = false/g,
      'ThrowOnError extends boolean = true',
    );
    fs.writeFile(clientFile, result, 'utf8', (err) => {
      if (err) {
        console.error('Error:', err);
        return;
      }
      console.info('ThrowOnError default replaced successfully!');
    });
  });
}