elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

Using wretch in deno #74

Closed shian15810 closed 4 years ago

shian15810 commented 4 years ago

Hi there, I have been using wretch for a very long time, and hoping to use it in deno too.

Since wretch is packaged as a esmodule, it is possible to import wretch directly in deno.

import wretch from "https://cdn.pika.dev/wretch";

However, I'm getting some typescript errors in some d.ts files in dist:

error TS2304: Cannot find name 'AbortController'.

► https://cdn.pika.dev/-/wretch@v1.7.1-He7QpmxksyhS3v1eqUdL/dist=es2019,mode=types/dist/resolver.d.ts:24:45

24     setTimeout: (time: number, controller?: AbortController) => ResponseChain;
                                               ~~~~~~~~~~~~~~~

error TS2304: Cannot find name 'RequestInit'.

► https://cdn.pika.dev/-/wretch@v1.7.1-He7QpmxksyhS3v1eqUdL/dist=es2019,mode=types/dist/wretcher.d.ts:4:39

4 export declare type WretcherOptions = RequestInit & {
                                        ~~~~~~~~~~~

error TS2304: Cannot find name 'AbortController'.

► https://cdn.pika.dev/-/wretch@v1.7.1-He7QpmxksyhS3v1eqUdL/dist=es2019,mode=types/dist/wretcher.d.ts:109:24

109     signal(controller: AbortController): Wretcher;
                           ~~~~~~~~~~~~~~~

Found 3 errors.

I can see they are not referenced anywhere in their corresponding files.

Any idea what is causing these errors?

Thank you for this amazing package!

shian15810 commented 4 years ago

Just FYI, wretch().polyfills({ fetch }) is working perfectly with deno-provided fetch.

elbywan commented 4 years ago

Hi @shian15810,

Thank you for this amazing package!

Thanks a bunch!! 😄

Any idea what is causing these errors?

Yes, you need to enable the dom type definitions in the typescript configuration file.

Basically when running deno, specify the location of a tsconfig.json file having the following contents:

{
    "compilerOptions": {
        "lib": [ "es2015", "dom" ],
    },
}

Then, run deno with the following command line arguments:

deno -c tsconfig.deno.json --allow-net program.ts

I tried with the following program and it seemed to work like a charm:

import wretch from 'https://cdn.pika.dev/wretch'

const html = await wretch('https://google.com').get().text()
console.log(html)

Hope this will help! 👍

shian15810 commented 4 years ago

Yeah, thanks for your help!

lib="dom" conflicts with lib="deno.shared_globals", which is very much required to use Deno internals, so it is nearly impossible to utilize lib="dom" in deno ATM.

I guess I will have to wait https://github.com/denoland/deno/issues/3974 for RequestInit and https://github.com/denoland/deno/issues/3345 for AbortController to land on lib="deno.shared_globals".

Thanks again for this amazing library!!!

elbywan commented 4 years ago

lib="dom" conflicts with lib="deno.shared_globals", which is very much required to use Deno internals, so it is nearly impossible to utilize lib="dom" in deno ATM.

Ah thanks for sharing, I did not know that. I'll look into deno a bit more in the future 👍.

Thanks again for this amazing library!!!

🙇

shian15810 commented 4 years ago

As of deno 0.41.0, AbortController and RequestInit are both exposed in the global scope, thus allowing wretch to be imported in deno.

import wretch from "https://cdn.pika.dev/wretch";

wretch().polyfills({ fetch, FormData, URLSearchParams });

await wretch('https://httpstat.us/200').get().text();

The above code can run with no problem in deno 0.41.0.

If you are interested in deno, maybe you can add information about deno support in README.

elbywan commented 4 years ago

If you are interested in deno, maybe you can add information about deno support in README.

@shian15810 I just added a paragraph about Deno, thanks for the suggestion!

Also it turns out that the .polyfills(...) part is not necessary since wretch uses the global values by default which makes it even easier to use with Deno 😄.

shian15810 commented 4 years ago

Yeah, didn't noticed that! The global of deno is window, which is purposely made to be browser compatible.