Coder Social home page Coder Social logo

Allow client option for custom dispatcher into fetch requests (e.g. to disable certificate validation) about openapi-typescript HOT 6 OPEN

mellster2012 avatar mellster2012 commented on June 5, 2024
Allow client option for custom dispatcher into fetch requests (e.g. to disable certificate validation)

from openapi-typescript.

Comments (6)

drwpow avatar drwpow commented on June 5, 2024

Thanks for opening, but can you describe what a “dispatcher” is? Is this a concept from another library? Why won’t the custom fetch or middleware work? What prior art are you referencing?

I’m not opposed to adding this, but I want to be 100% sure before we commit to adding breaking changes to the API there’s not a simpler solution to solve the problem (and I do not understand the problem)

from openapi-typescript.

mellster2012 avatar mellster2012 commented on June 5, 2024

Thanks for the comments!

  • Previously you could provide an Agent to override default configurations, such as not failing on self-signed certificates. Node's global fetch does not use the http/https stack, but an internally wired undici client and you cannot replace the global agent easily, see here: https://stackoverflow.com/questions/73817412/why-is-the-agent-option-not-available-in-node-native-fetch
  • Replacing fetch with a custom implementation is a much larger change than providing a custom dispatcher - which is an undici specific Agent different from Node's http(s) Agent. Also we've ran into signature issues/incompatibilities when swapping out fetch. So this seems like the simplest solution (as suggested in the linked SO thread above) to pass custom configurations.
  • We already forked openapi-fetch for this, there must be many others with the same need. AFAIK adding an optional dispatcher to the configuration option should not be breaking changes to the (public facing) API, but I could be wrong.

from openapi-typescript.

mellster2012 avatar mellster2012 commented on June 5, 2024

There actually seems to be an option to modify the globalFetch Dispatcher:
globalThis[Symbol.for('undici.globalDispatcher.1')] = yourDispatcher;
Will verify this. You can just reject the reworked (according to your comments) PR at (#1636) if you think it's not useful enough, but the above is certainly a bit of a hack and I would prefer the option to inject a dispatcher per client.

from openapi-typescript.

mellster2012 avatar mellster2012 commented on June 5, 2024

The above doesn't work, neither does setGlobalDispatcher, so we will still rely on our fork in the meantime. Note that you can also pass a dispatcher as init parameter to fetch, however openapi-fetch does discard this additional parameter here:
let response = await fetch(request); with

ObjectDefineProperty(globalThis, 'fetch', {
    __proto__: null,
    configurable: true,
    enumerable: true,
    set,
    get() {
      function fetch(input, init = undefined) {
        // Loading undici alone lead to promises which breaks lots of tests so we
        // have to load it really lazily for now.
        const { fetch: impl } = require('internal/deps/undici/undici');
        return impl(input, init);
      }
      set(fetch);
      return fetch;
    },
  });

where init can take a dispatcher object.

from openapi-typescript.

mellster2012 avatar mellster2012 commented on June 5, 2024

Specifying a custom fetch is tricky as the globalFetch does not use undici's exposed fetch method, so you have to do what node does and require the internal dependencies which necessitates the --expose-internals flag (or write it from scratch):

function fetch(input: any, init = { dispatcher }) {
  const { fetch: impl } = require('internal/deps/undici/undici');
  return impl(input, init);
}

from openapi-typescript.

mellster2012 avatar mellster2012 commented on June 5, 2024

Ok so this does work, still not really clean, but using this override plus ts-expect-error providing a custom fetch is sufficient.

function myFetch(input: RequestInfo) {
  // @ts-expect-error 'dispatcher does not exist in type RequestInit'
  return globalThis.fetch(input, { dispatcher });
}

Feel free to close this and the related PR at (#1636), or modify the proposed changes as you see fit. Thanks!

from openapi-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.