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

isomorphic-api-client

v0.1.4

Published

Simple, minimalistic, isomorphic generic client to make requests to an api

Downloads

2

Readme

isomorphic-api-client

Simple, minimalistic, isomorphic generic client to make requests to an api

Installation

npm install --save isomorphic-api-client

Usage

First configure the api communication, you need do it once

import Api from 'isomorphic-api-client'

// our API url is: https://myhost.com:3000/api/v2

Api({
  protocol: 'https',
  host: 'myhost.com',
  port: 3000 ,
  basePath: '/api',
  version: 2
})

make request to the api:

import {Client} from 'isomorphic-api-client'

const skirtl = {
  username: 'squirtlesquirtle',
  email: '[email protected]'
}

const squirtle = {
  email: '[email protected]'
}

let apiClient = new Client();

let user = await apiClient.get('/users/1')
let theNew = await apiClient.post('/users', skirtl)
theNew = await apiClient.update('/users/1', squirtle)
await apiClient.delete('users/1')

or you can do it more easy:

import {Resources} from 'isomorphic-api-client'

const Users = new Resources('/users')

let user = await Users.get('1') // GET https://myhost.com:3000/api/v2/users/1
let theNew = await Users.post(skirtl) // POST https://myhost.com:3000/api/v2/users
theNew = await apiClient.update(2, squirtle) // UPDATE https://myhost.com:3000/api/v2/users/2
await apiClient.delete('2') // DELETE https://myhost.com:3000/api/v2/users/2

And you can do it with Auth:

import {Resources, Client} from 'isomorphic-api-client'
import {ClientError} from 'isomorphic-api-client/errors'

const Users = new Resources('/users')
let apiClient = new Client()

try {
  let user = await Users.get('1/secret') // GET https://myhost.com:3000/api/v2/users/1/secret and FAIL!
} catch (error) {
  if (error instanceof ClientError && error.code == 401) {     
    // do it log in
    let token = await apiClient.post('/auth/login', {username: 'hugox123', password: 'automaticRobot234'}) // now get the token
    apiClient.setAuthToken(token) //set the token
    let user = await Users.get('1/secret') // GET https://myhost.com:3000/api/v2/users/1/secret and.. EUREKA!!
  }
}

ToDo:

  • [X] Tests and readme for Auth token requests
  • [ ] Create most common Error Clases (NotFound, Unauthorized, InternalServerError, ...)
  • [ ] Methods for special cases, like catch Auth token of headers and not of body
  • [ ] Methods to simplify isomorphic Auth

License

MIT Licensed. Copyright (c) Futurecommerce 2016.