ethanent / phin

Node HTTP client
MIT License
576 stars 33 forks source link

Export IWithData and IWithForm types #65

Closed jdforsythe closed 3 years ago

jdforsythe commented 3 years ago

Fixes #63

Exports IWithData and IWithForm types Explicitly makes generic extend IOptionsBase Changes IWithData's data property to be string, Buffer, or object (allows strings, Buffers, objects, or arrays)

No behavior is affected so no new tests were written

Example usage - you can now construct a typed options object using the IWithData or IWithForm types.

import * as phin from 'phin';

export const app = async () => {
  const stringOpts: phin.IWithData<phin.IOptions> = {
    url: 'http://localhost:8008',
    parse: 'none',
    data: '1',
  };

  const bufferOpts: phin.IWithData<phin.IOptions> = {
    url: 'http://localhost:8008',
    parse: 'none',
    data: Buffer.from('1'),
  };

  const objectOpts: phin.IWithData<phin.IOptions> = {
    url: 'http://localhost:8008',
    parse: 'none',
    data: { val: 1 },
  };

  const arrayOpts: phin.IWithData<phin.IOptions> = {
    url: 'http://localhost:8008',
    parse: 'none',
    data: [{ val: 1 }],
  };

  const res1 = await phin(stringOpts);
  const res2 = await phin(bufferOpts);
  const res3 = await phin(objectOpts);
  const res4 = await phin(arrayOpts);

  console.log(res1.body);
  console.log(res2.body);
  console.log(res3.body);
  console.log(res4.body);
};

If you think something should be added to documentation, let me know.

ethanent commented 3 years ago

Thank you very much.