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

fix(typescript-fetch): set prototype to fix instanceof not working #20694

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

petercinibulk
Copy link

Solves this issue: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work

try {
    // API call
} catch (error: unknown) {
    if (error instanceof FetchError) { // Currently this will never be true
        console.log(error.message);
    }
}

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@amakhrov
Copy link
Contributor

Worth mentioning that the error only happens when your compile target is es5. Which is somewhat questionable in year 2025.

@petercinibulk
Copy link
Author

Worth mentioning that the error only happens when your compile target is es5. Which is somewhat questionable in year 2025.

@amakhrov I am targeting ES2020 which confused me. I am also not getting an error but just not able to pass the error instanceof FetchError check.
image

@amakhrov
Copy link
Contributor

@petercinibulk What is the exact command / params for generating the client? And how do you consume it in the app?

It sounds like you might be compiling the client as a separate library (using es5 target) and then import into your app - is it the case?

@petercinibulk
Copy link
Author

@amakhrov You correct. However it seems like I am using the default configuration and it is generating es5 target. Should that be the default behavior?

docker run --rm \
    -v ${PWD}:/local \
    --user="$(id -u):$(id -g)" \
    openapitools/openapi-generator-cli generate \
    -i /local/openapi.json \
    -g typescript-fetch \
    -o /local/typescript \
    --additional-properties=npmName=ironpipe-client,npmVersion=$VERSION

@amakhrov
Copy link
Contributor

amakhrov commented Feb 20, 2025

Oh I see. It's in the tsconfig produced by the generator: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/tsconfig.mustache#L4

I would argue that supportsES6 flag should go for good. Would setting it to true in your project fix the issue you observe?

If we want to fix the generator in a backward-compatible way, we can make your changes (with prototype) conditional, based on supportsES6. But I really feel it just builds up tech debt for the mode that is long obsolete

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

Successfully merging this pull request may close these issues.

2 participants