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

js-tinyapi

v0.3.0

Published

A simple and lightweight API helper.

Downloads

3

Readme

js-tinyapi

npm version Downloads

Installing

yarn add js-tinyapi

Setup

Create a custom api object containing all the endpoints you create

import API from 'js-tinyapi'
const api = new API()

Example

Perform a GET request to an endpoint

// make endpoint
api.makeEndpoint('people', '/api/people', 'GET')

// call the endpoint
api.people()
  .then(data => {
    console.log(data)
  })
  .catch(error => {
    console.log(error)
  })

Perform a POST request an endpoint

// make endpoint
api.makeEndpoint('people', '/api/people', 'POST')

// call the endpoint passing in the post payload
api.people({
  payload: {name: 'Mary'}
})
  .then(data => {
    console.log(data)
  })
  .catch(error => {
    console.log(error)
  })

Perform a custom request

  options = {
    method: 'GET',
    path: '/api/people',
    params: {},
    type: 'json',
    payload: undefined,
    contentType: undefined,
    include: []
  }

  // call the endpoint with options
  api.request(null, options)
    .then(data => {
      console.log(data)
    })
    .catch(error => {
      console.log(error)
    })

Create CRUD endpoints

api.makeCrudEndpoints('people', '/api/')

api.peopleList() // GET /api/people
api.peopleCreate(payload) // POST /api/people with payload
api.peopleDetail(123) // GET /api/people?id=123
api.peopleUpdate(123, payload) // PATCH /api/people?id=123 with payload
api.peopleRemove(123) // DELETE /api/people?id=123
api.peopleOptions() // OPTIONS /api/people

Merge in new endpoints

Merge in POST and/or GET endpoints

api.merge({
  api: {
    people: {
      GET: {
        name: 'peopleGet'
      },
      POST: {
        name: 'peoplePost'
      }
    }
  }
})

api.peopleGet() // GET /api/people
api.peoplePost(payload) // POST /api/people with payload

Merge in a CRUD endpoint. The result of this merge is equivalent to the above Create CRUD endpoints example.

api.merge({
  api: {
    people: {
      CRUD: {
        name: 'people'
      }
    }
  }
})
// OR
api.merge({
  api: {
    people: 'CRUD'
  }
})

// equivalent to
api.makeCrudEndpoints('people', '/api/')

Middleware

A middleware layer is provided in order to easily alter the characteristics of requests made through js-tinyapi. Three kinds of middleware may be created:

  1. Request altering middleware.

  2. Response altering middleware.

  3. Fetch middleware.

The first, request altering middleware, is able to modify a request prior to being fetched. The second, response altering middleware, is able to modify a response after having been returned. The last, fetch middleware, is able to alter how each request is sent to a server.

To create a basic middleware for modifying a request, inherit from the provided middleware baseclass and override the process method:

import Middleware from './middleware'

// Add an extra slash to all request URLs.
class AddSlash extends Middleware {
  process = request => {
    return {
      ...request,
      url: request.url + '/'
    }
  }
}

The above middleware returns a new request with an extra slash added to the URL.

To create a middleware that performs a fetch, simply return a promise that will be resolved once the fetch has completed:

import Middleware from './middleware'

// Add an extra slash to all request URLs.
class DelayedFetch extends Middleware {
  process = request => {
    return new Promise( (resolve, reject) => {
      setTimeout( () => {
        this.submit( request )
            .then( r => resolve( r ) )
      }, 500 )
    })
  }
}

The above will add a 500ms delay to all requests. Notice the use of this.submit; this is a helper method to submit the supplied request object.

As an example, a batching middleware is provided. It can be enabled as such:

import API, { Batch } from 'js-tinyapi'
const api = new API()
// prepare API as usual
api.pushMiddleware(
  new Batch({
    batchUrl: 'http://your.domain/api/batch/',
    timeout: 50
  })
)

This causes each incoming request to be "held" for up to 50 milliseconds, waiting for further requests to be made. Once the timeout has expired, all collected requests are sent to the batch endpoint simultaneously.