Coder Social home page Coder Social logo

yesttp's Introduction

Yesttp: Lightweight HTTP Client

This is a lightweight HTTP client, with basic support for request and response interceptors.

By default, it uses JSON serialization and deserialization for both request and response bodies.

Installation

npm install yesttp

yarn add yesttp

Quick Example

import { Yesttp } from 'yesttp';

const response = await new Yesttp().post('https://api.backend.com/users', {
  body: {
    name: 'Bob',
    age: 42,
  },
});

// Assuming the backend JSON response contains an `id` field
const userId = response.body.id;

Creating an instance

import { Yesttp } from 'yesttp';

const yesttp = new Yesttp();

The class can also be instantiated with a configuration object:

const yesttp = new Yesttp({
  baseUrl: 'https://api.backend.com',
  requestInterceptor: (request) => Promise.resolve(request),
  responseErrorIntercepter: (request, response) => Promise.reject({ request, response }),
  responseSuccessInterceptor: (request, response) => Promise.resolve(response),
});

Instance Request API

yesttp.get('/users');

yesttp.post('/users', {
  // Request options
});

Here's an overview of the available request options:

export type GetOptions = {
  searchParams?: Record<string, string | undefined>;
  headers?: Record<string, string | undefined>;
};

export type RequestOptions = {
  searchParams?: Record<string, string | undefined>;
  headers?: Record<string, string | undefined>;
  body?: any;
  bodyRaw?: any;
};

Instance Response API

const response = await yesttp.get<User>('/users/123');

Here, the response is an object with the following properties:

type Response<T> = {
  status: number;
  headers: Record<string, string>;
  body: T;
  bodyRaw: string;
};

yesttp's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.