Skip to content

Type errors when compiling with the --axios option #301

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

Closed
Isaac-Leonard opened this issue Oct 2, 2021 · 5 comments · Fixed by #294
Closed

Type errors when compiling with the --axios option #301

Isaac-Leonard opened this issue Oct 2, 2021 · 5 comments · Fixed by #294
Assignees
Labels
bug Something isn't working next release

Comments

@Isaac-Leonard
Copy link

Isaac-Leonard commented Oct 2, 2021

I get these errors when running tsc after generating the api with the axios option.
The normal fetch option works fine however I am targeting node and node-fetch doesn't want to work with it.
The compilation errors I get are:
src/api/data-portal.ts:471:7 - error TS2532: Object is possibly 'undefined'.

471 requestParams.headers.common = { Accept: "/" };
~~~~~~~~~~~~~~~~~~~~~

src/api/data-portal.ts:471:7 - error TS2322: Type '{ Accept: string; }' is not assignable to type 's
tring'.

471 requestParams.headers.common = { Accept: "/" };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/api/data-portal.ts:472:7 - error TS2532: Object is possibly 'undefined'.

472 requestParams.headers.post = {};
~~~~~~~~~~~~~~~~~~~~~

src/api/data-portal.ts:472:7 - error TS2322: Type '{}' is not assignable to type 'string'.

472 requestParams.headers.post = {};
~~~~~~~~~~~~~~~~~~~~~~~~~~

src/api/data-portal.ts:473:7 - error TS2532: Object is possibly 'undefined'.

473 requestParams.headers.put = {};
~~~~~~~~~~~~~~~~~~~~~

src/api/data-portal.ts:473:7 - error TS2322: Type '{}' is not assignable to type 'string'.

473 requestParams.headers.put = {};
~~~~~~~~~~~~~~~~~~~~~~~~~

Found 6 errors.
I generate the api successfully with the command:
npx swagger-typescript-api --path src/un-data-portal.json -o ./src/api/ -n data-portal.ts --axios
And then run:
npx tsc
If there is no easy fix for this does anyone know how to get node-fetch working with the default version instead?
In case it matters I'm using the latest versions of axios and swagger-typescript-api and using node v16.8.0 with typescript Version 4.4.3 .

@Isaac-Leonard
Copy link
Author

Isaac-Leonard commented Oct 2, 2021

Heres the relevant lines in the outputted file starting from line 470:
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
requestParams.headers.common = { Accept: "/" };
requestParams.headers.post = {};
requestParams.headers.put = {};

  body = this.createFormData(body as Record<string, unknown>);
}

@kompasbank-niels
Copy link

I have the same issue and worked around it by adding // @ts-ignore before those 3 lines.

@Isaac-Leonard
Copy link
Author

I've done that for now but its not really a sustainable option if I end up sticking with my current project.
I could look into making a pull request with a proper fix but atm I'm not sure what is meant to actually be happening there.

@js2me js2me self-assigned this Oct 4, 2021
@js2me js2me linked a pull request Oct 4, 2021 that will close this issue
@js2me js2me added bug Something isn't working next release labels Oct 4, 2021
@tiagoskaneta
Copy link
Contributor

tiagoskaneta commented Oct 12, 2021

TLDR: stick with [email protected] for now I guess.

There have been changes to typing in [email protected] and 0.23.0
https://github.com/axios/axios/pull/3021/files
https://github.com/axios/axios/pull/4140/files

This construction is no longer possible (not sure it had any effect in the first place TBH).

requestParams.headers.common = { Accept: "*/*" };
requestParams.headers.post = {};
requestParams.headers.put = {};

As the common, post and put are only available in the default headers, i.e. something like this:

this.instance.defaults.headers.common = { Accept: "*/*" };
this.instance.defaults.headers.post = {};
this.instance.defaults.headers.put = {};

Would this be desired effect or was the intention for the changes in the header to be applied only for the current request?

@grandsilence
Copy link

Use the swagger-typescript-api-nextgen fork, it solves this problem. The package is present in the npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working next release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants