Installation
npm install @akadenia/api --save
Import the Axios Client
import { AxiosApiClient } from '@akadenia/api'
Instantiate the Axios Client
const headers = {
"Content-Type": "application/json"
}
const client = new AxiosApiClient('https://baseurl', headers)
Making a GET request
type User = {
id: number
firstname: string
lastname: string
}
const response = await client.get<User>("/users/1")
if (response.success) {
console.log(response.data.firstname)
console.log(response.data.lastname)
} else {
console.log(response.error)
}
Making a POST request
type User = {
id: number
firstname: string
lastname: string
}
const payload = {
firstname: "John",
lastname: "Doe"
}
const response = await client.post<User>("/users", payload)
if (response.success) {
console.log(response.data)
} else {
console.log(response.error)
}
console.log(response.message)
Making a PUT request
const payload = {
firstname: "Lisa",
lastname: "Smith"
}
const response = await client.put("/users/1", payload)
if (response.success) {
console.log(response.data)
} else {
console.log(response.error)
}
console.log(response.message)
Making a DELETE request
const response = await client.delete("/users/1")
if (response.success) {
console.log(response.message)
} else {
console.log(response.error)
}
Default response structure
{
success: false
message: string
data?: any
error: any
}