npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

clapient

v0.0.3

Published

API Client, the easy way

Downloads

1

Readme

clapient

Create your API client in typescript with clapient.

clapient uses axios as its HTTP client

Why clapient?

While developing frontent for webapps(like Angular), we use normal http client library to send AJAX requests. The structure of the AJAX client is nothing but a class with some methods that corresponds to the API endpoints we use. Most of the time we do not care about these methods but it seriously jeopardize the future development because changing a small chunk of code generates a series of errors that we do not understand.

Here, clapient can provide not only a safer development practice but also leads to less erroneous AJAX calls to your API backend.

Install

$ npm install clapient

Concept

APIClient

The APIClient class is used to define api client both by extending or instantiating.

Define Routes

Routes are basically object with key:value pair(in TypeScript's standard library, there is a type called Record) like following:

{
    getusers: 'https://reqres.in/api/users',
    getauser: 'https://reqres.in/api/users/{id}'
}

Use curly braces for parameters in your request url.

You can define routes via any of the following:

  • Via constructor
  • Via setRoutes() method

Request types

  • getRequest(name: string, requestParams?: any, queryParams?: any)
  • postRequest(name: string, payload: any, requestParams?: any, queryParams?: any)
  • putRequest(name: string, payload: any, requestParams?: any, queryParams?: any)
  • patchRequest(name: string, payload: any, requestParams?: any, queryParams?: any)
  • deleteRequest(name: string, payload: any, requestParams?: any, queryParams?: any)
  • headRequest(name: string, requestParams?: any, queryParams?: any)

Parameters

  • name - Key name of a route.
  • requestParams - Object with key : value pair. Example: { id: 2 }
  • queryParams - Similar to requestParams
  • payload - JSON object to be sent.

Note: when withFile() is used, FormData will be sent instead of normal json object.

Usage

import { APIClient } from 'clapient';

interface User{
    id: number;
    first_name: string;
    last_name: string;
}

let api: APIClient = new APIClient({
    getusers: 'https://reqres.in/api/users',
    getauser: 'https://reqres.in/api/users/{id}'
});

api
.getRequest('getusers', null, { page: 2 })//queryParams
.then((response)=>{
    let users: User[] = response.data.data;
    console.log(users[1].first_name);
})
.catch((e)=>{
    console.log(e.responseJSON)
});

api
.getRequest('getauser', { id: 2 })//requestParams
.then((response)=>{
    let user: User = response.data.data;
    console.log( user.first_name );
})
.catch((e)=>{
    console.log(e.responseJSON)
});

File Upload

Use withFile() method to indicate that the content type of the following request will be multipart/form-data

api
.withFile()
.postRequest('endpointX', {
    first_name: "Peter Parker",
    photo: yourfileobject
})
.then((response)=>{
    console.log( response.data );
})
.catch((e)=>{
    console.log(e.responseJSON)
});

Transform data before sending

Use withTypeTransform() method to transform some specific value in the input json. Example:

api
.withTypeTransform('personal_details.info.dob', v =>moment(v).format('YYYY-MM-DD') )
.postRequest('save_user', data)