Skip to content

Commit 59b8d29

Browse files
committed
feat: add credentials option for fetch client
1 parent ddac9e3 commit 59b8d29

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/core': minor
3+
---
4+
5+
Add credentials option for fetch client

apps/docs/docs/core/custom.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Custom Client
22

3+
:::info
4+
The `credentials` option has no effect when using a custom client. Make sure you handle credentials in your custom client
5+
(e.g., setting `withCredentials` in axios).
6+
:::
7+
38
## Using Axios (custom api override)
49

510
By default ts-rest ships with an incredibly simple fetch
@@ -94,4 +99,4 @@ export class SampleAPI {
9499
}
95100
```
96101

97-
:::
102+
:::

apps/docs/docs/core/fetch.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ const data: {
5454
body: unknown;
5555
}
5656
```
57-
:::
57+
:::
58+
59+
60+
## Credentials (sending cookies)
61+
62+
The `fetch()` function used by ts-rest does not send cookies in cross-origin requests by default unless the `credentials`
63+
option is set to `include`.
64+
65+
```typescript
66+
const client = initClient(contract, {
67+
baseUrl: "http://localhost:3333/api",
68+
baseHeaders: {},
69+
credentials: 'include'
70+
})
71+
```

apps/docs/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const sidebars = {
3939
items: [
4040
{ type: 'doc', id: 'core/core' },
4141
{ type: 'doc', id: 'core/fetch' },
42+
{ type: 'doc', id: 'core/custom' },
4243
{ type: 'doc', id: 'core/errors' },
4344
{ type: 'doc', id: 'core/form-data' },
4445
],

libs/ts-rest/core/src/lib/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,25 @@ export interface ClientArgs {
7474
baseUrl: string;
7575
baseHeaders: Record<string, string>;
7676
api?: ApiFetcher;
77+
credentials?: RequestCredentials;
7778
}
7879

7980
type ApiFetcher = (args: {
8081
path: string;
8182
method: string;
8283
headers: Record<string, string>;
8384
body: FormData | string | null | undefined;
85+
credentials?: RequestCredentials;
8486
}) => Promise<{ status: number; body: unknown }>;
8587

8688
export const defaultApi: ApiFetcher = async ({
8789
path,
8890
method,
8991
headers,
9092
body,
93+
credentials,
9194
}) => {
92-
const result = await fetch(path, { method, headers, body });
95+
const result = await fetch(path, { method, headers, body, credentials });
9396

9497
try {
9598
return {
@@ -130,6 +133,7 @@ export const fetchApi = (
130133
return apiFetcher({
131134
path,
132135
method: route.method,
136+
credentials: clientArgs.credentials,
133137
headers: {
134138
...clientArgs.baseHeaders,
135139
},
@@ -140,6 +144,7 @@ export const fetchApi = (
140144
return apiFetcher({
141145
path,
142146
method: route.method,
147+
credentials: clientArgs.credentials,
143148
headers: {
144149
...clientArgs.baseHeaders,
145150
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)