A JavaScript library to use Corsfix CORS Proxy without manually adding the proxy URL. Just import the library and fetch APIs without CORS error.
The library extends the default fetch method to perform requests via the Corsfix proxy, automatically bypassing CORS restrictions.
The API is identical to the native Fetch API, with optional additional configuration for Corsfix-specific options.
Using the corsfix global variable:
<script src="https://unpkg.com/corsfix"></script>
<script>
corsfix
.fetch("https://example.com")
.then((response) => response.json())
.then((data) => console.log(data));
</script>Using fetch override:
<script src="https://unpkg.com/corsfix/fetch.js"></script>
<script>
fetch("https://example.com")
.then((response) => response.json())
.then((data) => console.log(data));
</script>Install the package:
npm install corsfixUsing the default import:
import corsfix from "corsfix";
corsfix.fetch("https://example.com");Using named imports:
import { fetch } from "corsfix";
fetch("https://example.com");Corsfix is fully typed and extends the native Fetch API types. The fetch function accepts the same parameters as the standard fetch, with an additional optional corsfix property in the request init options:
interface CorsfixOptions {
cache?: boolean; // Enable caching for requests
headers?: Record<string, string>; // Additional headers for the proxy
}
interface CorsfixRequestInit extends RequestInit {
corsfix?: CorsfixOptions; // Optional Corsfix-specific configuration
}import { fetch } from "corsfix";
fetch("https://example.com/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ data: "example" }),
// Corsfix-specific options
corsfix: {
cache: true,
headers: {
"X-Custom-Header": "value",
},
},
});MIT