Skip to content

alex8088/apexio

Repository files navigation

apexio

Tiny HTTP client for Node.js

license

Features

  • 🚀 Lightweight and fast HTTP client for Node.js
  • 💪 Full TypeScript support
  • 🔥 Promise based
  • ✨ Modern and intuitive API
  • 🛡️ Automatic transforms for JSON data
  • 🔌 Configurable request and response interceptors
  • 📦 Zero dependencies

Installation

$ npm install apexio

Basic Usage

import { Apexio } from 'apexio'

const apexio = new Apexio({
  baseURL: 'https://api.example.com'
})

// Make a GET request
apexio
  .get('/users')
  .then((response) => {
    console.log(response.data)
  })
  .catch((error) => {
    console.error(error)
  })

// Make a POST request with JSON data
apexio.post('/users', {
  name: 'John',
  email: '[email protected]'
})

API

Request Methods

apexio.get<T = any>(url: string, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>
apexio.post<T = any>(url: string, data?: any, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>
apexio.put<T = any>(url: string, data?: any, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>
apexio.delete<T = any>(url: string, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>
apexio.head<T = any>(url: string, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>
apexio.options<T = any>(url: string, options?: ApexioRequestOptions): Promise<ApexioResponse<T>>

Request Configuration

{
  // Base URL for the request
  baseURL?: string

  // Request headers
  headers?: {
    [key: string]: string
  }

  // URL parameters to be appended to the URL
  params?: any

  // Request timeout in milliseconds
  timeout?: number

  // Response type: 'json' | 'text' | 'stream'
  responseType?: ApexioResponseType
}

Response Structure

{
  // Response data
  data: T

  // HTTP status code
  status: number

  // HTTP status message
  statusText: string

  // Response headers
  headers: IncomingHttpHeaders

  // Request configuration used
  config: ApexioRequestConfig
}

Request Data Types

JSON Data

apexio.post('/api/users', {
  name: 'John',
  age: 30
})

URL Search Params

const params = new URLSearchParams()
params.append('name', 'John')
params.append('age', '30')

apexio.post('/api/users', params)

Form Data

import FormData from 'form-data'
import fs from 'node:fs'

const form = new FormData()
form.append('file', fs.createReadStream('path/to/file.txt'))
form.append('field', 'value')

apexio.post('/api/upload', form)

Stream Data

const stream = fs.createReadStream('path/to/file')
apexio.post('/api/upload', stream)

Interceptors

// Request interceptor
apexio.interceptors.request.use(
  (config) => {
    // Modify request config
    config.headers['Authorization'] = 'Bearer token'
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// Response interceptor
apexio.interceptors.response.use(
  (response) => {
    // Handle response data
    return response
  },
  (error) => {
    return Promise.reject(error)
  }
)

Build

$ pnpm build

Test

$ pnpm test

License

MIT

About

Tiny HTTP client for Node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published