11---
22title : Fetcher
3- description : Fetch data from the server.
3+ description : Fetch data with type-safe API
44---
55
6- ## Usage
6+ Our fetcher has RPC (Remote Procedure Call) style API. It allows you to call any API endpoint with type-safe API.
77
88<Callout type = " warn" emoji = " ⚠️" title = " Server-side only" >
99 The ` fetcher() ` function is only server-side. You cannot use it on the
1010 client-side.
1111</Callout >
1212
13- import { Step , Steps } from ' fumadocs-ui/components/steps' ;
14-
15- <Steps >
16- <Step >
17-
18- ### Initial client
13+ ## Usage
1914
20- ` fetcher() ` is a function to create client. Pass generic type with the response type and pass the ` plugin ` and ` module ` as arguments .
15+ To use the ` fetcher() ` function, you need to import it from the ` vitnode/lib/fetcher ` module. You also need to import the module you want to use .
2116
22- As an example, we will pass ` UsersTypes ` as a generic type .
17+ More about modules can be found in the [ Modules ] ( /docs/dev/modules ) section .
2318
2419``` ts
2520import { fetcher } from ' vitnode/lib/fetcher' ;
26- import { UsersTypes } from ' vitnode/api/modules/users/users.module' ;
21+ import { usersModule } from ' vitnode/api/modules/users/users.module' ;
2722```
2823
29- ``` tsx
30- const client = await fetcher <UsersTypes >({
31- plugin: ' core' ,
24+ ``` ts
25+ const res = await fetcher (usersModule , {
26+ path: ' /session' ,
27+ method: ' get' ,
3228 module: ' users' ,
3329});
3430```
3531
36- #### Options
32+ The response from ` fetcher() ` is compatible with the standard ` fetch ` Response API. Here are common way to work with the response:
33+
34+ ``` ts
35+ // Basic response handling
36+ if (res .ok ) {
37+ const data = await res .json ();
38+ console .log (data );
39+ }
40+ ```
41+
42+ ### Options
3743
38- You can pass the ` options ` object to modify the ` fetch ` function. For example we can pass ` cache ` option to enable the cache.
44+ You can pass the ` options ` object to modify the ` fetch ` function. For example we can pass ` cache ` option to enable the cache from ` next ` :
3945
40- ``` tsx
41- const client = await fetcher <UsersTypes >({
42- plugin: ' core' ,
46+ ``` ts
47+ const res = await fetcher (usersModule , {
48+ path: ' /session' ,
49+ method: ' get' ,
4350 module: ' users' ,
4451 // [!code ++]
4552 options: {
@@ -50,167 +57,28 @@ const client = await fetcher<UsersTypes>({
5057});
5158```
5259
53- </Step >
54-
55- <Step >
56-
57- ### Fetch data
58-
59- Call ` client.{path}.{method} ` with the data you want to send to the server as an argument.
60-
61- ``` tsx
62- const data = await client .sign_in .$post (input );
63- ```
64-
65- </Step >
66- </Steps >
67-
68- ## Server Functions
69-
70- Methods like ` POST ` , ` PUT ` and ` DELETE ` require to use [ Server Functions] ( https://react.dev/reference/rsc/server-functions ) . You can create ` mutation-api.ts ` file and use it to call the ` fetcher() ` function with ` FetcherInput ` type.
71-
72- ``` ts title="mutation-api.ts"
73- ' use server' ;
74-
75- import { UsersTypes } from ' vitnode/api/modules/users/users.module' ;
76- import { fetcher , FetcherInput } from ' vitnode/lib/fetcher' ;
77-
78- export const mutationApi = async (
79- input : FetcherInput <UsersTypes , ' /sign_in' , ' post' >,
80- ) => {
81- const res = await fetcher <UsersTypes >({
82- plugin: ' core' ,
83- module: ' users' ,
84- });
85-
86- await res .sign_in .$post (input );
87- };
88- ```
89-
90- Now you can call the ` mutationApi() ` function on the server-side.
91-
92- ``` ts title="useForm.ts"
93- export const useForm = () => {
94- const onSubmit = async (values : z .infer <typeof formSchema >) => {
95- // [!code ++]
96- await mutationApi ({
97- // [!code ++]
98- json: values ,
99- // [!code ++]
100- });
101- };
102-
103- return {
104- onSubmit ,
105- };
106- };
107- ```
108-
109- ### Handling errors
110-
111- You can handle errors by checking the ` status ` property of the response.
112-
113- ``` ts title="mutation-api.ts"
114- export const mutationApi = async (
115- input : FetcherInput <UsersTypes , ' /sign_in' , ' post' >,
116- ) => {
117- const res = await fetcher <UsersTypes >({
118- plugin: ' core' ,
119- module: ' users' ,
120- });
121-
122- // [!code --]
123- await res .sign_in .$post (input );
124- // [!code ++]
125- const data = await res .sign_in .$post (input );
126- // [!code ++]
127-
128- // [!code ++]
129- if (data .status !== 200 ) {
130- // [!code ++]
131- return { message: await data .text () };
132- // [!code ++]
133- }
134- };
135- ```
136-
137- ``` ts title="useForm.ts"
138- // [!code ++]
139- import { useTranslations } from ' next-intl' ;
140- // [!code ++]
141- import { toast } from ' sonner' ;
142-
143- export const useForm = () => {
144- // [!code ++]
145- const t = useTranslations (' core.global.errors' );
146-
147- const onSubmit = async (values : z .infer <typeof formSchema >) => {
148- const mutation = await mutationApi ({
149- json: values ,
150- });
151-
152- // [!code ++]
153- if (! mutation ?.message ) return ;
154- // [!code ++]
155-
156- // [!code ++]
157- toast .error (t (' title' ), {
158- // [!code ++]
159- description: t (' internal_server_error' ),
160- // [!code ++]
161- });
162- };
163-
164- return {
165- onSubmit ,
166- };
167- };
168- ```
169-
170- ### Handling set-cookies
60+ ### Handle set-cookies
17161
172- React Server Components cannot handle ` set-cookies ` headers. You need to handle them by using the ` handleCookiesFetcher() ` function.
62+ React Server Components cannot handle ` set-cookies ` headers from the server. To handle
63+ this, you need to pass the ` allowSaveCookies ` param to the ` fetcher() ` function. This will allow the cookies to be saved in the response.
17364
174- ``` ts title="mutation-api.ts"
175- ' use server' ;
65+ <Callout type = " warn" emoji = " ⚠️" title = " Server-side only" >
66+ The ` allowSaveCookies ` param works only when your request has method different
67+ from ` get ` and response status has 2xx.
68+ </Callout >
17669
177- import { UsersTypes } from ' vitnode/api/modules/users/users.module' ;
178- import {
179- fetcher ,
180- FetcherInput ,
181- // [!code ++]
182- handleSetCookiesFetcher ,
183- } from ' vitnode/lib/fetcher' ;
184-
185- export const mutationApi = async (
186- input : FetcherInput <UsersTypes , ' /sign_in' , ' post' >,
187- ) => {
188- const res = await fetcher <UsersTypes >({
189- plugin: ' core' ,
190- module: ' users' ,
191- });
192-
193- // [!code --]
194- await res .sign_in .$post (input );
195- // [!code ++]
196- const data = await res .sign_in .$post (input );
70+ ``` ts
71+ const res = await fetcher (usersModule , {
72+ path: ' /sign_in' ,
73+ method: ' post' ,
74+ module: ' users' ,
19775 // [!code ++]
198- await handleSetCookiesFetcher (data );
199- };
200- ```
201-
202- ## Client-side
203-
204- If you want to use the ` fetcher() ` on the client-side, you need to use the ` fetcherClient() ` function.
205-
206- ``` tsx
207-
76+ allowSaveCookies: true ,
77+ args: {
78+ body: {
79+ email: ' ' ,
80+ password: ' ' ,
81+ },
82+ },
83+ });
20884```
209-
210- ## Custom fetcher
211-
212- If you want you can create your own ` fetch ` function, but you need to remember to pass headers like:
213-
214- - ` x-forwarded-for ` header - client IP address,
215- - ` Cookie ` header - client cookies,
216- - ` user-agent ` header - client user agent.
0 commit comments