@codecraftkit
Documentation
The useApiRequest is used to make api calls using a custom api.
npm i @codecraftkit/use-api-request
it can be imported from hooks.
import { useApiRequest } from "@codecraftkit/hooks";
or alone.
import { useApiRequest } from "@codecraftkit/use-api-request";
To config the apis used in this hooks, look at this example, in the folder _app
import Api from '@codecraftkit/api-nextjs'global['customApi'] = Api({url: process.env.NEXT_PUBLIC_API_FEED_URL,secret: process.env.NEXT_PUBLIC_API_SECRET,tokenKey: process.env.NEXT_PUBLIC_COOKIE_NAME})
This hook allows you to pass and api as an argument, to make fetch request with it
const { data, call, loading, error } = useApiRequest(customApi, '/tasks')
This hook automatically sets the token from the cookies when making requests
You can pass an option object as an argument as well
When destructuring the result you can change the item's key names as shown below
const {data: tasks,call: getTasks,loading: loadingTasks,error: errorTasks} = useApiRequest(customApi, '/tasks', {method: "POST"})
api
Api
yes
The custom api where the requests will be made
path
String
yes
The path of the request
options
Object
no
api custom configuration
defaultValue
String
no
data default value
verbose
String
no
Show requests logs in the console
data
any
Query's response inside the collection field
call
function
Function to execute the query supplied, it receive the query params as arguments
loading
boolean
Request loading state
error
String
Response Error