All Projects → ahmadnassri → node-api-problem

ahmadnassri / node-api-problem

Licence: MIT license
HTTP Problem Utility

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to node-api-problem

AltiumLibrary
Useful Altium pcb library (3D)
Stars: ✭ 33 (+37.5%)
Mutual labels:  lib
metalsmith-request
Metalsmith plugin to grab content from the web and expose the results to metadata
Stars: ✭ 12 (-50%)
Mutual labels:  lib
teams-api
Unofficial Microsoft Teams Library
Stars: ✭ 92 (+283.33%)
Mutual labels:  lib
metalsmith-imagemin
Metalsmith plugin to minify images
Stars: ✭ 17 (-29.17%)
Mutual labels:  lib
epub-parser
A powerful yet easy-to-use epub parser
Stars: ✭ 103 (+329.17%)
Mutual labels:  lib
cortexm-AES
high performance AES implementations optimized for cortex-m microcontrollers
Stars: ✭ 18 (-25%)
Mutual labels:  lib
impromptu.nvim
Create prompts fast and easy
Stars: ✭ 39 (+62.5%)
Mutual labels:  lib
menu-hamburger
🍔 A clean, simple and easy to use library to create a Menu Hamburger
Stars: ✭ 17 (-29.17%)
Mutual labels:  lib
ssh
golang ssh lib 远程执行命令,文件上传下载 模仿rsync和cp
Stars: ✭ 29 (+20.83%)
Mutual labels:  lib
fs-pochta-api
Библиотека для работы с API Почты России
Stars: ✭ 15 (-37.5%)
Mutual labels:  lib
gostreamer
Go example that uses channels to build an execution pipeline
Stars: ✭ 75 (+212.5%)
Mutual labels:  lib
SunriseSunset
Java utility to calculate the time of sunrise and sunset for a given location.
Stars: ✭ 42 (+75%)
Mutual labels:  lib
metalsmith-paths
Metalsmith plugin that adds file path values to metadata
Stars: ✭ 19 (-20.83%)
Mutual labels:  lib
tgcalls
Voice chats, private incoming and outgoing calls in Telegram for Developers
Stars: ✭ 408 (+1600%)
Mutual labels:  lib
jigjs
🧩 A front-end framework
Stars: ✭ 22 (-8.33%)
Mutual labels:  lib
Mal4J
Java wrapper for the official MyAnimeList API
Stars: ✭ 23 (-4.17%)
Mutual labels:  lib
cheatengine-threadstack-finder
List all thread's base address based on process id
Stars: ✭ 39 (+62.5%)
Mutual labels:  lib
react-lite
A simple implementation of react
Stars: ✭ 51 (+112.5%)
Mutual labels:  lib
liblex
C library for Lexical Analysis
Stars: ✭ 25 (+4.17%)
Mutual labels:  lib
babelfish
🐡 Straightforward library for translations and dictionaries
Stars: ✭ 47 (+95.83%)
Mutual labels:  lib

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://httpstatuses.com/404' }

// override defaults
new Problem(404, 'Oops! Page Not Found')
//=> { status: '404', title: 'Oops! Page Not Found', type: 'https://httpstatuses.com/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://httpstatuses.com/404', 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 respnse 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].