darseen / amadeus-ts

Node library for the Amadeus travel APIs written in TypeScript
https://npmjs.com/package/amadeus-ts
MIT License
3 stars 0 forks source link

Error parsing email: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array #1

Closed sarbogast closed 6 days ago

sarbogast commented 6 days ago

I'm trying to use amadeus-ts to call the Amadeus API for the first time with this code:

const amadeus = new Amadeus({
    clientId: amadeusApiKey.value(),
    clientSecret: amadeusApiSecret.value(),
  });
  try {
    const response = await amadeus.travel.tripParser.post({
      payload: emailData,
      metadata: {
        encoding: "BASE_64",
      },
    });
    logger.debug(`Amadeus response: ${JSON.stringify(response)}`);
  } catch (e) {
    if (e instanceof ResponseError) {
      logger.error(`Amadeus error: ${e.code}`, e);
    } else {
      logger.error(`Error parsing email: ${e}`, e);
    }
  }

And I double checked that emailData is a base-64 encoded email body. But then I get the following error that seems to come from inside this library:

Error parsing email: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object TypeError: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object
    at new NodeError (node:internal/errors:405:5)
    at write_ (node:_http_outgoing:875:11)
    at ClientRequest.write (node:_http_outgoing:834:15)
    at Client.execute (/workspace/node_modules/amadeus-ts/dist/index.js:822:18)
    at Client.unauthenticatedRequest (/workspace/node_modules/amadeus-ts/dist/index.js:807:10)
    at Client.<anonymous> (/workspace/node_modules/amadeus-ts/dist/index.js:783:19)
    at Generator.next (<anonymous>)
    at fulfilled (/workspace/node_modules/amadeus-ts/dist/index.js:33:24)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Am I doing something wrong?

darseen commented 6 days ago

Hi @sarbogast, thank you for reporting this issue. I have identified and fixed the problem, so you can download version 4.0.2 today when I release it shortly, and everything should be working fine.

The issue was in the library as you correctly assumed. Because the amadeus-ts library aims to eliminate the need for users to manually use JSON.stringify before passing the request body to some of the endpoints.

Example

in amadeus library you would have to pass your request as follows:

amadeus.travel.tripParser.post(JSON.stringify(body));

in amadeus-ts library:

amadeus.travel.tripParser.post(body);

But apparently I forgot to implement this feature to this specific endpoint when I was rewriting the codebase.

Thank you again for reporting this issue.

sarbogast commented 6 days ago

Wow, that was fast. Thanks a lot.