OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.76k stars 6.32k forks source link

[BUG][Typescript-axios] The context of `this` is lost when dereferencing a class method #18822

Closed Halu89 closed 1 month ago

Halu89 commented 1 month ago

Hi folks, I ran in an issue when attempting to add a wrapper function to my api calls with the class api. We access this.configuration, but this is undefined when attempting to call the function

Example:

const api = new PetApi(undefined);

function logWrapper <T extends unknown[], U> (fn: (...args: T) => U, ...args: T) {
    console.log("Calling api with args: ", args);
    // `this` is undefined in this call
    return fn(...args);
}

logWrapper(api.getPetById, 1).then((result) => {
    console.log("Result: ", result);
});

I added a test case that reproduces the bug, and type annotations for the other apis that do not reference this for eslint.

References:

Technical Committee:

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

PR checklist