Skip to content

thaitype/rest-client

Repository files navigation

@thaitype/rest-client

Lightweight REST client helper function wrapping with type and Axios

CI

Introduction

Lightweight REST client helper function with basic type support and Axios wrapper. However, if you need more type-safety, we recommend considering untypeable.

Alternatively, if you have control over both the server and client types, options like tRPC or GraphQL are great choices. So go ahead and choose the option that works best for your needs!

Installation

npm i @thaitype/rest-client

Examples

import { RestClient } from '@thaitype/rest-client';

async function main() {
  const client = new RestClient();
  const res = await client.request('GET https://jsonplaceholder.typicode.com/posts/{postId}/comments', {
    params: {
      postId: 1,
    },
  });
  console.log(res.data);
}

main();

Use with Axios Retry

import { RestClient } from '@thaitype/rest-client';
import axios from 'axios';
import axiosRetry from 'axios-retry';

// Create an an axios instance and apply axiosRetry
const axiosInstance = axios.create();
axiosRetry(axiosInstance, { retries: 3 });

// Using axiosInstance in RestClient
const client = new RestClient({
  axiosInstance,
});
// This will retry 3 times
await client.request('GET http://domain.com');

API

to be add later