0.0.7 • Published 1 year ago

jsonder v0.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Jsonder

Provides a simple interface for creating unified JSON API responses in Express.js apps.

Installation

You can install Jsonder using NPM:

npm install jsonder

Basic usage

import jsonder from 'jsonder'.

const api = jsonder();

Jsonder provides two basic methods for handling API responses: sendSuccess and sendFail. These methods take a response object res and either a resource to send or an error, respectively.

Successful response:

api.sendSuccess(res, { id: '123456', greeting: 'Hello, World!' });

Each resource must have an id field of type string.

You can also provid an array of resources to send.

api.sendSuccess(res, [
  { id: '123456', greeting: 'Hello, World!' },
  { id: '123457', greeting: 'Hi, World!' },
]);

Error Response:

Each error must have these fields:

  • status: a HTTP status of the error
  • code: a human readable code of the error as a string,
  • detail: a detailed human readable explanation of the error.
jsonder.sendFail(res, { 
  status: 400,
  code: 'invalid_body',
  detail: 'you forgot to provide a body'
});

You can provide an array of errors. Jsonder will choose the most general HTTP status code for the whole reqeust.

0.0.3

1 year ago

0.0.2

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.1

1 year ago