Skip to content

corsfix/client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Corsfix JavaScript Library

A JavaScript library to use Corsfix CORS Proxy without manually adding the proxy URL. Just import the library and fetch APIs without CORS error.

Usage

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.

Script Tag (CDN)

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>

NPM Module

Install the package:

npm install corsfix

Using the default import:

import corsfix from "corsfix";

corsfix.fetch("https://example.com");

Using named imports:

import { fetch } from "corsfix";

fetch("https://example.com");

TypeScript Support

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
}

Example with Corsfix Options

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",
    },
  },
});

License

MIT

About

Corsfix client library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published