javascriptnode.jsapimiddlewarenode.js-got

What does got.extend do?


What does got.extend do and is there an equivalent function by undici?

I have tried reading the npm documentation in gitHub but am unsure on what got.extend does?

I have the following code, which I would like to write using undici

function initializeClient(clientName) {

  log.info('Begin Initializing REST for %s', clientName);

  const config = rest[clientName]; // eslint-disable-line security/detect-object-injection

  // @ts-ignore

  const client = got.extend(config.base);

  clients[clientName] = client; // eslint-disable-line security/detect-object-injection

  log.info('REST Initialization Complete for %s : %j', clientName, Object.keys(client));

  return client;

}

module.exports.initializeClient = initializeClient;

I have tried undici.fetch but that gives

TypeError: Failed to parse URL from [object Object]

Solution

  • const instance = got.extend({
      option1 : value1,
      option2 : value2,
      option3 : value3,
    })
    const response1 = await instance(URL1);
    const response2 = await instance(URL2);
    

    is equivalent to:

    const response1 = await got(URL1, {
      option1 : value1,
      option2 : value2,
      option3 : value3,  
    });
    const response2 = await got(URL2, {
      option1 : value1,
      option2 : value2,
      option3 : value3,  
    })
    

    So if you need to use the same options for each request you make, you can create an instance that has those options set as defaults (so you don't have to pass them each time).

    It doesn't look like undici has something similar.