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

api-problem

v9.0.2

Published

HTTP Problem Utility

Downloads

11,232

Readme

API Problem

RFC 7807 - Problem Details for HTTP APIs

license release super linter test semantic

RFC 7807 - Problem Details for HTTP APIs

Install

npm install api-problem

API

Constructor: Problem(status[, title][, type][, members])

| name | type | required | default | description | referece | |---------------|----------|----------|--------------------|----------------------------------------------------------------------------------------|------------------| | status | String | | N/A | The HTTP status code generated by the origin server for this occurrence of the problem | Section 3.1 | | title | String | | HTTP status phrase | A short, human-readable summary of the problem type | Section 3.1 | | type | String | | about:blank | A URI reference that identifies the problem type | Section 3.1 | | details | Object | | N/A | additional details to attach to object | Section 3.1 |

import Problem from 'api-problem'

// HTTP defaults
new Problem(404)
//=> { status: '404', title: 'Not Found', type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404' }

// override defaults
new Problem(404, 'Oops! Page Not Found')
//=> { status: '404', title: 'Oops! Page Not Found', type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404' }

// custom values
new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit')
//=> { status: '403', title: 'You do not have enough credit', type: 'https://example.com/probs/out-of-credit' }

// additional details
new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit', {
  detail: 'Your current balance is 30, but that costs 50.',
  instance: '/account/12345/msgs/abc',
  balance: 30,
  accounts: ['/account/12345', '/account/67890']
})

//=> { status: '403', title: 'You do not have enough credit', type: 'https://example.com/probs/out-of-credit', detail: 'Your current balance is 30, but that costs 50.', instance: '/account/12345/msgs/abc', balance: 30, accounts: ['/account/12345', '/account/67890'] }

// HTTP defaults + Details
new Problem(403, {
  detail: 'Account suspended',
  instance: '/account/12345',
  date: '2016-01-15T06:47:01.175Z',
  account_id: '12345'
})

//=> { status: '403', title: 'Forbidden', type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403', detail: 'Account suspended', instance: '/account/12345', account_id: 12345, 'date: 2016-01-15T06:47:01.175Z' }

Method : <object> toObject()

returns an object containing all the properties including: (status, title, type, members)

const prob = new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit', { user_id: 'x123' })

prob.toObject() //=> { status: 403, title: 'You do not have enough credit', type: 'https://example.com/probs/out-of-credit', user_id: 'x123' }

Method : <string> toString()

returns a simplified, human-readable string representation

const prob = new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit')

prob.toString() //=> [403] You do not have enough credit ('https://example.com/probs/out-of-credit')

Method : <void> send(response)

uses response.writeHead and response.end to send an appropriate error response message with the Content-Type response header to application/problem+json

import http from 'http'
import Problem from 'api-problem'

let response = new http.ServerResponse()
Problem.send(response)

Express Middleware

A standard connect middleware is provided. The middleware intercepts Problem objects thrown as exceptions and serializes them appropriately in the HTTP response while setting the Content-Type response header to application/problem+json

import express from 'express'
import Problem from 'api-problem'
import Middleware from 'api-problem/lib/middleware'

const app = express()

app.get('/', (req, res) => {
  throw new Problem(403)
})

app.use(Middleware)

Author: Ahmad Nassri • Twitter: @AhmadNassri