Importing CORS
To use the CORS functionality in your Joor application, you need to import thecors function from the joor package:
cors function takes an options object with these properties:
| Property | Type | Default | Description |
|---|---|---|---|
| origins | string[] | ["*"] | An array of allowed URLs. Supports patterns like https://*.example.com. |
| methods | string[] | ["*"] | An array of uppercase HTTP methods (e.g., ["GET", "POST"]). |
| allowedHeaders | string[] | ["*"] | An array of acceptable headers. Use capitalized headers, e.g., Content-Type. |
| exposedHeaders | string[] or string | undefined | A string or array listing headers exposed to clients. Optional. |
| allowsCookies | boolean | false | A boolean indicating whether credentials are allowed. |
| maxAge | number | 0 | A number (in seconds) that sets how long a preflight response is cached. |
["*"]. Currently, only global CORS is supported.
Pattern Matching in Origins
Theorigins property supports pattern matching to allow multiple subdomains or specific URL patterns. For example:
https://*.example.comallows any subdomain ofexample.com.
Example Usage
Basic Example
Example with All Defaults
Example with Custom Headers and Methods
CORS_OPTIONS is a type alias covering all supported CORS properties. If the options provided are invalid or not an object, a cors-options-invalid exception is thrown. If the options object lacks valid required data, defaults are applied.
