mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 15:53:24 +00:00
Write some pages
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
# Fetchers
|
||||
|
||||
When making an instance of the library using `makeProviders()`. It will immediately make a fetcher.
|
||||
When creating provider controls, it will need you to configure a fetcher.
|
||||
This comes with some considerations depending on the environment youre running.
|
||||
|
||||
## Using `fetch()`
|
||||
In most cases, you can use the `fetch()` API. This will work in newer versions of Node.js (18 and above) and on the browser.
|
||||
|
||||
```ts
|
||||
const fetcher = makeDefaultFetcher(fetch);
|
||||
const fetcher = makeStandardFetcher(fetch);
|
||||
```
|
||||
|
||||
If you using older version of Node.js. You can use the npm package `node-fetch` to polyfill fetch:
|
||||
@@ -15,7 +15,7 @@ If you using older version of Node.js. You can use the npm package `node-fetch`
|
||||
```ts
|
||||
import fetch from "node-fetch";
|
||||
|
||||
const fetcher = makeDefaultFetcher(fetch);
|
||||
const fetcher = makeStandardFetcher(fetch);
|
||||
```
|
||||
|
||||
## Using fetchers on the browser
|
||||
@@ -29,7 +29,7 @@ const fetcher = makeSimpleProxyFetcher("https://your.proxy.workers.dev/", fetch)
|
||||
|
||||
If you aren't able to use this specific proxy and need to use a different one, you can make your own fetcher in the next section.
|
||||
|
||||
## Making a custom fetcher
|
||||
## Making a derived fetcher
|
||||
|
||||
In some rare cases, a custom fetcher will need to be made. This can be quite difficult to do from scratch so it's recommended to base it off an existing fetcher and building your own functionality around it.
|
||||
|
||||
@@ -37,6 +37,7 @@ In some rare cases, a custom fetcher will need to be made. This can be quite dif
|
||||
export function makeCustomFetcher(): Fetcher {
|
||||
const fetcher = makeStandardFetcher(f);
|
||||
const customFetcher: Fetcher = (url, ops) => {
|
||||
// Do something with the options and url here
|
||||
return fetcher(url, ops);
|
||||
};
|
||||
|
||||
@@ -44,4 +45,30 @@ export function makeCustomFetcher(): Fetcher {
|
||||
}
|
||||
```
|
||||
|
||||
If you need to make your own fetcher for a proxy. Make sure you make it compatible with the following headers: `Cookie`, `Referer`, `Origin`. Proxied fetchers need to be able to write those headers when making a request.
|
||||
If you need to make your own fetcher for a proxy. Make sure you make it compatible with the following headers: `Set-Cookie`, `Cookie`, `Referer`, `Origin`. Proxied fetchers need to be able to write/read those headers when making a request.
|
||||
|
||||
|
||||
## Making a fetcher from scratch
|
||||
|
||||
In some even rare cases, you need to make one completely from scratch.
|
||||
This is the list of features it needs:
|
||||
- Send/read every header
|
||||
- Parse JSON, otherwise parse as text
|
||||
- Send JSON, Formdata or normal strings
|
||||
- get final destination url
|
||||
|
||||
It's not recommended to do this at all, but if you have to. You can base your code on the original implementation of `makeStandardFetcher`. Check the source code for it.
|
||||
|
||||
Here is a basic template on how to make your own custom fetcher:
|
||||
|
||||
```ts
|
||||
const myFetcher: Fetcher = (url, ops) => {
|
||||
// Do some fetching
|
||||
return {
|
||||
body: {},
|
||||
finalUrl: "",
|
||||
headers: new Headers(), // should only contain headers from ops.readHeaders
|
||||
statusCode: 200,
|
||||
};
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user