9.0.2 • Published 8 months ago

api-problem v9.0.2

Weekly downloads
1,479
License
MIT
Repository
github
Last release
8 months ago

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])

nametyperequireddefaultdescriptionreferece
statusStringN/AThe HTTP status code generated by the origin server for this occurrence of the problemSection 3.1
titleStringHTTP status phraseA short, human-readable summary of the problem typeSection 3.1
typeStringabout:blankA URI reference that identifies the problem typeSection 3.1
detailsObjectN/Aadditional details to attach to objectSection 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

9.0.2

8 months ago

9.0.1

1 year ago

9.0.0

1 year ago

7.0.4

2 years ago

8.0.0

2 years ago

7.0.3

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.0.0

7 years ago

3.0.0

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago