Overview
When running adapters in the browser, Cross-Origin Resource Sharing (CORS) restrictions can prevent fetching data from third-party APIs. The CORS proxy utilities automatically detect and handle these restrictions by wrapping URLs with a CORS proxy when needed. All functions are exported fromutils/cors.ts.
Why Use CORS Proxy?
Adapters in the Points Adapters SDK are designed to run in the browser. Many third-party APIs don’t include theAccess-Control-Allow-Origin: * header required for browser-based requests. The CORS proxy utilities solve this by:
- Detecting when an API doesn’t support CORS
- Automatically routing requests through a CORS proxy
- Maintaining compatibility with server-side environments (Deno, Node.js)
Functions
maybeWrapCORSProxy
Intelligently wraps a URL with a CORS proxy only when necessary.url(string): The API URL to potentially wrap
Promise<string>- The original URL or proxy-wrapped URL
- In server environments (Deno, Node.js): Returns the original URL unchanged
- In browser with FAST_LOAD=true: Returns proxy-wrapped URL immediately
- In browser with FAST_LOAD=false (default):
- Tests if the API supports CORS by making a request
- Returns original URL if CORS is supported
- Returns proxy-wrapped URL if CORS is blocked
wrapCORSProxy
Directly wraps a URL with the CORS proxy without any detection.url(string): The URL to wrap
string- The proxy-wrapped URL
- You know for certain an API doesn’t support CORS
- You want to skip the CORS detection check for performance
- You’re testing CORS proxy behavior
isGoodCORS
Tests if an API endpoint supports CORS by checking response headers.url(string): The API URL to test
Promise<boolean>-trueif CORS is supported,falseotherwise
- Makes a GET request to the URL
- Checks if the response includes
Access-Control-Allow-Origin: *header - Returns
falseif aTypeErroris caught (CORS blocked the request) - Re-throws other error types
Configuration
CORS_PROXY_URL
The CORS proxy server URL used to wrap requests."https://c-proxy.dorime.org/"
Environment variable configuration:
You can customize the CORS proxy URL using environment variables:
FAST_LOAD Mode
When enabled, skips CORS detection and immediately wraps all URLs with the proxy. Environment variable:- Development environments where you know CORS will be an issue
- Reducing initial load time by skipping CORS checks
- Testing proxy behavior
- Faster: Skips the CORS detection request
- Less efficient: Routes all requests through proxy, even if CORS is supported
Implementation Details
Environment Detection
The utilities detect the runtime environment to determine appropriate behavior:- Browser: Applies CORS proxy logic
- Server (Deno/Node.js): Returns original URLs unchanged
Environment Variable Loading
Supports multiple runtime environments:Common Patterns
Basic Adapter with CORS Proxy
Multiple API Endpoints
From Harmonix adapter (adapters/harmonix.ts:4):GraphQL Endpoints
From Doma adapter (adapters/doma.ts:6):Error Handling
CORS Detection Errors
Proxy Failures
If the CORS proxy is down or unreachable:Best Practices
-
Always use top-level await: Call
maybeWrapCORSProxyat the module level, not inside the fetch function - Wrap all external APIs: Any third-party API should be wrapped to ensure browser compatibility
-
Use FAST_LOAD in development: Enable
FAST_LOAD=trueduring development to skip CORS checks - Test both environments: Verify your adapter works in both browser and server environments
- Handle proxy failures gracefully: The CORS proxy is a third-party service - implement proper error handling
Limitations
- The CORS proxy adds latency to requests
- Depends on the availability of the CORS proxy service
- Some APIs may have rate limiting that applies to the proxy IP
- POST/PUT requests with large payloads may fail through some proxies
See Also
- Creating an Adapter - Complete guide to building adapters
- MDN: CORS - Understanding Cross-Origin Resource Sharing
- CORS Handling - Understanding browser compatibility and CORS