Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for custom fetch implementations #22

Open
u873838 opened this issue Oct 28, 2024 · 2 comments
Open

Better support for custom fetch implementations #22

u873838 opened this issue Oct 28, 2024 · 2 comments

Comments

@u873838
Copy link

u873838 commented Oct 28, 2024

The current TypeScript typing information makes it difficult to use a custom fetch implementation with certain libraries e.g. undici.

import { fetch as undiciFetch } from "undici";
// ...
const signedFetch = createSignedFetcher({
    region,
    credentials,
    service: "service",
    fetch: (input, init) => {
        return undiciFetch(
            input as unknown as UndiciRequestInfo,
            init as unknown as UndiciRequestInit
        );
    },
}) as typeof undiciFetch;

The createSignedFetcher shouldn't be overly strict with what the arguments and return value the custom implementation has. It should also be generic so that the return type of createSignedFetcher is the same type as SignedFetcherOptions["fetch"]:

const signedFetch = createSignedFetcher({
    region,
    credentials,
    service: "service",
    fetch: undiciFetch,
});
// signedFetch is typeof undiciFetch
@zirkelc
Copy link
Owner

zirkelc commented Oct 28, 2024

Hey @u873838 thanks for bringing this to my attention. I actually didn't think there would be any type issue, because this library is using the official fetch types from TypeScript DOM lib and unidici implements the Fetch standard.

I'll see what I can do and will come back to you soon!

@u873838
Copy link
Author

u873838 commented Oct 28, 2024

You would think, but this is the error that I get when trying to use undici as the fetch option:

file.ts:10:7 - error TS2322: Type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>' is not assignable to type '(input: string | URL | Request, init?: RequestInit | undefined) => Promise<Response>'.
  Types of parameters 'input' and 'input' are incompatible.
    Type 'string | URL | Request' is not assignable to type 'RequestInfo'.
      Type 'Request' is not assignable to type 'RequestInfo'.
        Property 'referrer' is missing in type 'import("<project>/node_modules/undici-types/fetch").Request' but required in type 'import("<project>/node_modules/undici/types/fetch").Request'.

64       fetch: undiciFetch,
         ~~~~~

  node_modules/undici/types/fetch.d.ts:166:12
    166   readonly referrer: string
                   ~~~~~~~~
    'referrer' is declared here.
  node_modules/aws-sigv4-fetch/dist/index.d.cts:7:5
    7     fetch?: typeof fetch;
          ~~~~~
    The expected type comes from property 'fetch' which is declared here on type 'SignedFetcherOptions'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants