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

standard-json-api-connectors

v0.3.1

Published

Standardized HTTP JSON requests with standardized errors.

Downloads

402

Readme

standard-json-api-connectors

Standardized HTTP JSON requests with standardized errors, which assumes the following response format from the server:

In case of successful requests:

{
  "status": 200,
  "result": {
    "something": "here"
  }
}

In case of errors:

{
  "status": 500,
  "error": {
    "name": "name of the error",
    "message": "details about the error"
  }
}

Whenewer the response is in the HTTP 400-599 range, an error is thrown based on standard-api-errors.

The lib also works with non-JSON error repsonses, then the status comes from the HTTP status code, the name of the error will be the HTTP status text and the message will be the respone's body.

Usage

import {
  createGetConnector,
  createPostConnector,
  createPutConnector,
  createPatchConnector,
  createDeleteConnector
} from 'standard-json-api-connectors'

const apiUrl = 'https://example-api.com/'
const generateRoute = (params) => {
  return `/v1/user/${params.userId}/projects/${params.id ? params.id : ''}`
}
const generateAdditionalHeaders = (params) => {
  return { Authorization: 'Bearer mybearertoken' }
}

const get = createGetConnector(fetch, apiUrl, generateRoute, generateAdditionalHeaders)
const post = createPostConnector(fetch, apiUrl, generateRoute, generateAdditionalHeaders)
const put = createPutConnector(fetch, apiUrl, generateRoute, generateAdditionalHeaders)
const patch = createPatchConnector(fetch, apiUrl, generateRoute, generateAdditionalHeaders)
const del = createDeleteConnector(fetch, apiUrl, generateRoute, generateAdditionalHeaders)

// after this, you can invoke the returned functions

All of the creator functions have the following parameters:

  • fetch: A reference to a Fetch API compatible function.
  • apiUrl: The root URL of your API. The route will be concatenated to this.
  • generateRoute: This function has to generate route based on the params object it gets as an argument.
  • generateAdditionalHeaders: By default, "Content-Type": "application/json" header is added to every request. With this callback, you can add extra headers to the request, for example an authorization header. You can generate different headers based on the params object it gets as an argument.

get

const result = await get({ userId: 2 }, { find: { name: 'awesome' }, skip: 10, limit: 5 })
// GET https://example-api.com/v1/users/2/projects/?find[name]=awesome&skip=10&limit=5

Parameters:

  • params: an object representing the parameters that will be used in the route.
  • query: an object representing the query string

post

const result = await post({ userId: 2 }, { name: 'new project' })
// POST https://example-api.com/v1/users/2/projects/

Parameters:

  • params: an object representing the parameters that will be used in the route.
  • body: an object representing the body of the request

put

const result = await put({ userId: 2 }, { name: 'updated project' })
// PUT https://example-api.com/v1/users/2/projects/

Parameters:

  • params: an object representing the parameters that will be used in the route.
  • body: an object representing the body of the request

patch

const result = await patch({ userId: 2 }, { name: 'updated project' })
// PATCH https://example-api.com/v1/users/2/projects/

Parameters:

  • params: an object representing the parameters that will be used in the route.
  • body: an object representing the body of the request

delete

const result = await del({ userId: 2 })
// DELETE https://example-api.com/v1/users/2/projects/

Parameters:

  • params: an object representing the parameters that will be used in the route.