octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

Form-data 5.0.0 types no longer compatible with node-fetch 3.2.1 #60

Closed Nkmol closed 2 years ago

Nkmol commented 2 years ago

Contrary to the readme, the FormData object is no longer type-compatible with the body parameter of node-fetch 3.2.1.

Type 'FormData' is not assignable to type 'BodyInit | null | undefined'. Type 'import("/Users/sandermol/www/tpg/devops/monitor/server/scraper/docker/node/node_modules/formdata-node/@type/FormData", { assert: { "resolution-mode": "import" } }).FormData' is not assignable to type 'FormData'. The types returned by 'get(...)' are incompatible between these types. Type 'import("/Users/sandermol/www/tpg/devops/monitor/server/scraper/docker/node/node_modules/formdata-node/@type/FormData", { assert: { "resolution-mode": "import" } }).FormDataEntryValue | null' is not assignable to type 'FormDataEntryValue | null'. Type 'File' is not assignable to type 'FormDataEntryValue | null'. Type 'import("/Users/sandermol/www/tpg/devops/monitor/server/scraper/docker/node/node_modules/formdata-node/@type/File", { assert: { "resolution-mode": "import" } }).File' is not assignable to type 'File'. The types returned by 'slice(...).stream()' are incompatible between these types. Type 'ReadableStream' is missing the following properties from type 'ReadableStream': readable, read, setEncoding, pause, and 21 more.ts(2322)

    const form = new FormData();
    form.set('grant_type', 'client_credentials');

    const request = await fetch('https://xxxx', {
      method: 'POST',
      body: form,
    });

Workaround is using the form-data-encoder.

octet-stream commented 2 years ago

Duplicate of https://github.com/octet-stream/form-data/issues/57

Hi, thank you for reaching me out with this issue. I'm not sure why is this happening, but it seem like TS maps ReadableStream to NodeJS.ReadableStream an I don't understand why.

Please, keep further discussion by the issue I mentioned.

octet-stream commented 1 year ago

v6 is released and should be compatible with node-fetch BodyInit type (tested with node-fetch v3.3.2 on TypeScript 5.2.2), check out release notes for more information: https://github.com/octet-stream/form-data/releases/tag/v6.0.0