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

get-scrud

v4.0.0

Published

Client for SCRUD style rest APIs

Downloads

18

Readme

get-scrud NPM version js-standard-style Dependency Status

Client for SCRUD style rest APIs

Install

$ npm install --save get-scrud

2.0.0

This is a very breaking change, switching to ES Modules and eliminating babel. If you are targeting older browsers you will need to include this module in any babel-ification you're doing.

API

Module ships single function which accepts options and returns main function

const apiCall = require('get-scrud')(*opts)

  • opts [object - required]
    • host [string - required] (ex. 'jsonplaceholder.typicode.com')
    • port [integer - optional - default: 443] (ex. 80)
    • protocol [string - optional - default is based on port] (ex. http)
    • basePath [string - optional - default: null] (ex. 'api' or '/api')
    • timeout [string | integer - optional - default: '1m' | 60000] (ex. '3m')
    • jwt [string - optional - default: null] (ex. 'abc123')
    • cache [boolean - optional - default: false] use cached instance instead of a fresh one on each invocation of top-level exported function
    • throttle [boolean | object - optional - default: false] throttle API calls, in object form uses the below options, if true the defaults are applied
      • interval [string | integer - default: '45s' | 45000] reset call counter every X ms
      • threshold [integer - default: 45] max calls per interval
      • exclude [array - default: null] calls that are ignored by throttle
        • Array can contain objects, arrays, or strings
        • The following are all equivalent
          • [{ api: 'user', action: 'read' }]
          • [['user', 'read']]
          • ['user:read']

Main function returns Promise that resolves with JSON parsed response data

apiCall(*resource, *action, [*id, *body, *jwt])

  • resource [string - required] (ex. 'posts')
  • action [string - required] (ex. 'search')
  • id [integer - optional - default: null] (ex. 1)
  • body [object - optional - default: null] (ex. {userId: 5})
  • jwt [string - optional - default: null] (ex. 'abc123')

Alternatively address each action with apiCall[action](...)

apiCall.[search, create, read, update, delete](*resource, [*id, *body, *jwt])

  • resource [string - required] (ex. 'posts')
  • id [integer - optional - default: null] (ex. 1)
  • body [object - optional - default: null] (ex. {userId: 5})
  • jwt [string - optional - default: null] (ex. 'abc123')

Change options on an instance

If you need to change any options on an existing instance, you can call the main function with a single argument, which is the new options you want to merge (Object.assign) with the original. That call will be synchronous.

apiCall(*opts)

  • opts [object - required] (same as above)

Usage

const opts = {
  host: 'jsonplaceholder.typicode.com',
  port: 443,
  timeout: '3m', // or '180s' or 180000
  jwt: `abc123`
}
const apiCall = require('get-scrud')(opts)
const body = {userId: 1, title: `get scrud yo`, body: `you're scrud`}
const jwt = `def456`

async function callApis () {

  /* no jwt passed, uses jwt set in init if one was set */

  // SEARCH
  let search = await apiCall('posts', 'search', {userId: 1})
  // CREATE
  let create = await apiCall('posts', 'create', body)
  // READ
  let read = await apiCall('posts', 'read', 1)
  // UPDATE
  let update = await apiCall('posts', 'update', 1, {userId: 5})
  // DELETE
  let deleted = !!(await apiCall('posts', 'delete', 2))

  /* passing in jwt to call, overrides jwt set in init if one was set */

  // SEARCH
  let search = await apiCall('posts', 'search', {userId: 1}, jwt)
  // CREATE
  let create = await apiCall('posts', 'create', body, jwt)
  // READ
  let read = await apiCall('posts', 'read', 1, jwt)
  // UPDATE
  let update = await apiCall('posts', 'update', 1, {userId: 5}, jwt)
  // DELETE
  let deleted = !!(await apiCall('posts', 'delete', 2, jwt))

}

Related

scrud - Super opinionated, minimalistic, PG centric API fabric

License

MIT © Andrew Carpenter