HTTP Requests

HTTP utility is in libs/console-core/src/utils/http.ts.

Auto-Attached Headers

Every request automatically includes:

Authorization: Bearer {localStorage.token}
x-tenant-id: {localStorage.tenant_id}
Content-Type: application/json

Basic Usage

import { http } from '@readystart/console-core'

// GET
const res = await http.get<User>('/v1/users/info')

// POST
const res = await http.post<Todo>('/v1/todos/create', { title: 'Hello' })

// PUT
const res = await http.put<Todo>('/v1/todos/update', { id: '...', title: 'Updated' })

// DELETE
const res = await http.delete('/v1/todos/delete')

Response Format

All endpoints return a unified format:

interface ApiResponse<T> {
  statusCode: number  // 200 = success
  message: string
  data: T
}

Error Handling

When statusCode !== 200, a toast error is shown automatically.

Use the silent option to disable auto-toast:

const res = await http.post('/v1/some-api', body, { silent: true })
if (res.statusCode !== 200) {
  // Handle error yourself
}

Set Base URL

import { setBaseUrl } from '@readystart/console-core'

setBaseUrl('https://api.example.com')

Full Example

import { useState, useEffect } from 'react'
import { http } from '@readystart/console-core'

interface Todo {
  id: string
  title: string
  completed: boolean
}

export default function TodosPage() {
  const [todos, setTodos] = useState<Todo[]>([])

  useEffect(() => {
    http.post<Todo[]>('/v1/todos/list').then(res => {
      if (res.statusCode === 200) setTodos(res.data)
    })
  }, [])

  return (
    <ul>
      {todos.map(t => <li key={t.id}>{t.title}</li>)}
    </ul>
  )
}