You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-7Lines changed: 47 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,20 +25,60 @@
25
25
26
26
# Introduction
27
27
28
-
ts-rest provides an RPC-like client side interface over your existing REST APIs, as well as allowing you define a _separate_ contract implementation rather than going for a 'implementation is the contract' approach, which is best suited for smaller or simpler APIs.
28
+
ts-rest offers a simple way to define a contract for your API, which can be both consumed and implemented by your application, giving you end to end type safety without the hassle or code generation.
29
29
30
-
If you have non typescript consumers, a public API, or maybe want to add type safety to your existing REST API? ts-rest is what you're looking for!
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).
3
+
By default, not specifying an `api` will use the default `tsRestFetchApi` which uses fetch under the hood.
4
+
5
+
```typescript
6
+
const client =initQueryClient(postsApi, {
7
+
baseUrl: 'http://localhost:5003',
8
+
baseHeaders: {},
9
+
// Uses `tsRestFetchApi` by default
10
+
});
11
+
```
12
+
13
+
## Adding a Custom API
14
+
15
+
If you want a custom api, you can reuse the internal `tsRestFetchApi` to add logging/custom logic to your requests!
16
+
17
+
```typescript
18
+
const client =initQueryClient(postsApi, {
19
+
baseUrl: 'http://localhost:5003',
20
+
baseHeaders: {},
21
+
api: async (args) => {
22
+
// Add anything you want here!
23
+
24
+
returntsRestFetchApi(args);
25
+
},
26
+
});
27
+
```
28
+
29
+
### Extra Query Arguments
30
+
31
+
By default when you make a ts-rest request you can pass in `params`, `query`, `body`, `headers` etc. However, sometimes you may want to pass in extra arguments to your custom api. You can do this by extending the type of the `args` parameter in your custom api.
The magical bit, is this is now fully typed and will work with your IDE's autocomplete! 🤯
48
+
49
+
One note here, any extra args which are provided here but aren't typed correctly - e.g. if you've `@ts-expect-error`'d, **will still be passed to your api**. This is because the `args` parameter is a spread of all the other arguments you pass in to your api.
50
+
51
+
```typescript
52
+
client.getPosts({
53
+
query: { skip: 0, take: 10 },
54
+
myCustomArg: 'hello',
55
+
// ^-- autocomplete will work here, allowing you to extend ts-rest however you want
56
+
});
57
+
```
58
+
59
+
:::tip
60
+
You can use this to accomplish loads of patterns, such as adding a `cache` argument to your api, or adding a `logger` argument to your api - maybe you want to add an `onUploadProgress` argument to your api to track upload progress? You can do all of this with the `args` parameter!
6
61
:::
7
62
8
63
## Using Axios (custom api override)
9
64
10
-
By default ts-rest ships with an incredibly simple fetch
65
+
By default ts-rest ships with an incredibly simple fetch
11
66
implementation for data fetching, because fetch requires zero extra
12
-
dependencies and works perfectly for most use cases, however,
67
+
dependencies and works perfectly for most use cases, however,
13
68
sometimes you may want to use Axios, or another data fetching strategy, for that
14
69
you can pass a `api` attribute to the `initClient` or `initQueryClient`.
15
70
71
+
:::info
72
+
The `credentials` option has no effect when using a custom client. Make sure you handle credentials in your custom client
0 commit comments