Skip to content

Commit c8b401d

Browse files
📚 docs(CORS): New CORS docs added to documentation
1 parent 9bc84d3 commit c8b401d

3 files changed

Lines changed: 107 additions & 154 deletions

File tree

docs/endpoints/cors.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# CORS
6+
7+
CORS, or Cross-Origin Resource Sharing, is a security feature implemented by web browsers to prevent unauthorized access to resources on a different domain. When a web application makes a request to a different domain, the browser will block the request unless the server responds with the appropriate CORS headers.
8+
9+
## Enabling CORS
10+
11+
The `cors()` method allows you to configure CORS settings for your Yelix application. Here's a basic example:
12+
13+
```ts title="main.ts"
14+
import { Yelix } from 'jsr:@murat/yelix';
15+
16+
export async function startServer() {
17+
const app = new Yelix();
18+
19+
// highlight-start
20+
app.cors({
21+
origin: '*',
22+
allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
23+
allowHeaders: ['Content-Type'],
24+
});
25+
// highlight-end
26+
27+
app.serve();
28+
}
29+
30+
await startServer();
31+
```
32+
33+
## Configuration Options
34+
35+
The CORS middleware accepts the following options:
36+
37+
### `origin`
38+
- Type: `string | string[] | ((origin: string, c: Context) => string | undefined | null)`
39+
- Required: Yes
40+
- Description: Configures the `Access-Control-Allow-Origin` CORS header
41+
- Examples:
42+
```ts
43+
// Allow all origins
44+
origin: '*'
45+
46+
// Allow specific origins
47+
origin: ['https://example.com', 'https://api.example.com']
48+
49+
// Dynamic origin validation
50+
origin: (origin, ctx) => {
51+
return origin.endsWith('.example.com') ? origin : null;
52+
}
53+
```
54+
55+
### `allowMethods`
56+
- Type: `string[]`
57+
- Optional
58+
- Description: Configures the `Access-Control-Allow-Methods` CORS header
59+
- Default: `['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE']`
60+
61+
### `allowHeaders`
62+
- Type: `string[]`
63+
- Optional
64+
- Description: Configures the `Access-Control-Allow-Headers` CORS header
65+
- Example: `['Content-Type', 'Authorization']`
66+
67+
### `maxAge`
68+
- Type: `number`
69+
- Optional
70+
- Description: Configures the `Access-Control-Max-Age` CORS header (in seconds)
71+
- Example: `86400` (24 hours)
72+
73+
### `credentials`
74+
- Type: `boolean`
75+
- Optional
76+
- Description: Configures the `Access-Control-Allow-Credentials` CORS header
77+
- Default: `false`
78+
79+
### `exposeHeaders`
80+
- Type: `string[]`
81+
- Optional
82+
- Description: Configures the `Access-Control-Expose-Headers` CORS header
83+
- Example: `['Content-Length', 'X-Custom-Header']`
84+
85+
### `affectRoute`
86+
- Type: `string`
87+
- Optional
88+
- Description: Specifies which routes should be affected by CORS. Uses path pattern matching.
89+
- Default: `"*"` (all routes)
90+
- Example: `"/api/*"` (only routes starting with /api/)
91+
92+
## Advanced Example
93+
94+
Here's a more comprehensive CORS configuration:
95+
96+
```ts
97+
app.cors({
98+
origin: ['https://app.example.com', 'https://admin.example.com'],
99+
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
100+
allowHeaders: ['Content-Type', 'Authorization', 'X-Custom-Header'],
101+
maxAge: 86400,
102+
credentials: true,
103+
exposeHeaders: ['Content-Length', 'X-Request-Id'],
104+
affectRoute: '/api/*'
105+
});
106+
```

docs/endpoints/endpoint-routing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Creating a basic endpoint in Yelix is straightforward. Below is an example of a
1414
import type { Ctx } from "jsr:@murat/yelix";
1515

1616
export async function GET(ctx: Ctx) {
17+
// highlight-next-line
1718
// highlight-next-line
1819
return await ctx.text('Hello World!', 200);
1920
}

docs/endpoints/introduction-to-first-endpoint.md

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)