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

Types? #43

Open
gBasil opened this issue Jun 10, 2024 · 5 comments · May be fixed by #44
Open

Types? #43

gBasil opened this issue Jun 10, 2024 · 5 comments · May be fixed by #44
Labels
enhancement New feature or request

Comments

@gBasil
Copy link

gBasil commented Jun 10, 2024

Would it be feasible to have TypeScript types (.d.ts) available for this package? There doesn't seem to be anything on DefinitelyTyped.

@zachleat
Copy link
Member

Happy to merge a PR!

@zachleat zachleat added the enhancement New feature or request label Jun 10, 2024
@gBasil
Copy link
Author

gBasil commented Jun 10, 2024

I tried to write some types, but I was defeated by TypeScript features I don't fully grasp (namely overloading the method in a way that would set the right output type depending on type option).

@Silvenga
Copy link

This is what I've been using:

declare module "@11ty/eleventy-fetch" {

    type FetchType =
        | "json"
        | "buffer"
        | "text";

    type EleventyFetchOptionsBase<TType extends FetchType> = {
        type: TType;
        directory?: string;
        concurrency?: number;
        fetchOptions?: RequestInit;
        dryRun?: boolean;
        removeUrlQueryParams?: boolean;
        verbose?: boolean;
        hashLength?: number;
        duration?: string;
        formatUrlForDisplay?: (url: string) => string;
    }

    type EleventyFetch = <TReturn, TType extends FetchType = "json">(url: string, options: EleventyFetchOptionsBase<TType>) =>
        Promise<
            TType extends "json" ? TReturn :
            TType extends "buffer" ? Buffer :
            TType extends "text" ? string :
            never
        >;

    const fetch: EleventyFetch;
    export default fetch;
}

I think it handles most scenarios. @gBasil do you want to see if that handles at least your use-case?

@gBasil
Copy link
Author

gBasil commented Jun 16, 2024

I ended up switching off of @11ty/eleventy-fetch and writing my own basic implementation, but I hope your types are useful for others who stumble upon this issue in the future :)

@Silvenga
Copy link

No worries @gBasil! I'll make a pr either way.

@Silvenga Silvenga linked a pull request Jun 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants