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

@lindebros/awesome-api-client

v1.0.0

Published

An awesome client to use for communication with an API

Downloads

9

Readme

Awesome-api-client

The awesome-api-client is a nice wrapper around fetch that makes it easier to work with Api integration.

The reason for writing this module is to avoid dublicate code. I felt that I wrote this kind of code or copying and pasting it all the time everytime I started a new project. So with this module, I hope that you can save some time and do other great stuff without having to write Api integration all the time. :=)

Get Started

Install awesome-api-client

npm install @lindebros/awesome-api-client

How to use it

Just include the client anywhere in your project. Use it in your redux actions, in your node express server, server side as well as the browser side.

import ApiClient from '@lindebros/awesome-api-client'

const someFunction = async () => {
  const response = await ApiClient.get('https://api', {
    authToken: 'someToken'
  })
  if (response.isOk) {
    // do something awesome here...
  }
}

Available methods:

GET

ApiClient.get(url, {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

POST

ApiClient.post({string} url, {object} body, {object} options = {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

PUT

ApiClient.put({string} url, {object} body, {object} options = {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

PATCH

ApiClient.patch({string} url, {object} body, {object} options = {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

DELETE

ApiClient.delete(url, {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

UPLOAD

Sends a file upload request.

ApiClient.delete(url, file, {
  headers: {object} custom headers,
  authToken: {string} token that will be set as a propertry of headers object (Authentication: bearer {token})
})

CALL

All methods above are shortcuts of the call function. If none of the methods above suits your needs, you can use the call function to make a request to your API.

ApiClient.call({object} options = {
  url: {string} the url to the endpoint of your api,
  method: {string} the rest method to use,
  headers: {object} headers to send in the request,
  body = {object} the body of your request,
  fetchOptions = {object} options of the fetch function (isomorphic-fetch)
})

Mocking the api-client when unittesting

This client is great when writing unittests in for instance jest. When you write API integration unittests, just mock this module using jest.mock.

Here's an example of how a unittests can look like. Lets say that getBooks are using the get method of the ApiClient, then just let jest mock the get method and fake a response from it:

import ApiClient from '@lindebros/awesome-api-client'
import getBooks from 'getBooks'


jest.mock('@lindebros/awesome-api-client')

test('this code is actually never making a request', () => {
  ApiClient.get = jest.fn(() => ({
    isOk: 400,
    error: {
      'message': 'this is a really bad request',
    }
  }))

  getBooks()

  expect(ApiClient.get.mock.calls[0][0]).toEqual('https://someapiurl')
})

Unittesting a React Redux Action:

The action:

const ERROR = 'Some error event action'
const myErrorAction = createAction(ERROR)
export const MyAwesomeAction = (params) => async (dispatch) => {
  const response = await ApiClient.post('https://myUrl', params)

  if (!response.isOk) {
    // do som great error handling here
    dispatch(myErrorAction(response.data))
  }
}

The unittest:

const mockDispatcher = jest.fn()
ApiClient.post = jest.fn(() => ({
  isOk: 400,
  error: {
    'message': 'this is a really bad request',
  }
}))

const fn = MyAwesomeAction({ a: 'b'})

fn(mockDispatcher)

expect(ApiClient.post.mock.calls.[0]).toEqual({
  type: 'Some error event action',
  payload: 'this is a really bad request',
})