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

Add configurable default value for ThrowOnError #961

Open
MrLepage opened this issue Aug 20, 2024 · 5 comments
Open

Add configurable default value for ThrowOnError #961

MrLepage opened this issue Aug 20, 2024 · 5 comments
Labels
feature 🚀 New feature or request

Comments

@MrLepage
Copy link

MrLepage commented Aug 20, 2024

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:

  • Add a new configuration option in the configuration file to set the default ThrowOnError value.
  • Update the code generation logic to use this configured default when generating API request functions.

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:

  • Improved code consistency across the application.
  • Reduced boilerplate by eliminating the need to specify or throwOnError: true for each API call.
  • Better alignment with different error handling strategies preferred by different development teams.
  • Maintains flexibility by still allowing per-request override of the behavior.
@MrLepage MrLepage added the feature 🚀 New feature or request label Aug 20, 2024
@snarky-puppy
Copy link

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
Copy link

smiley-uriux commented Aug 24, 2024

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
Copy link

jpenna commented Sep 6, 2024

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!');
    });
  });
}

@amiryadid
Copy link

Would be really glad for this kind of feature, in general passing a client-per-call is a little cumbersome but together with this config it adds a lot of overhead to each call

@modosc
Copy link

modosc commented Oct 4, 2024

we are using "@hey-api/client-axios and in addition to the previously mentioned changes we also had to change RequestOptionsBase<false> and Config<false> to true in client/types.ts.

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

No branches or pull requests

6 participants