Skip to content

The api-response-utils package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.

License

Notifications You must be signed in to change notification settings

jesusgraterol/api-response-utils

Repository files navigation

API Response Utils

The api-response-utils package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.


Getting Started

Install the package:

$ npm install -S api-response-utils

Usage

Building a successful response:

import { buildResponse } from 'api-response-utils';

buildResponse();
// {
//   success: true,
//   data: undefined,
//   error: undefined
// }

// building a successful response w/ data:
buildResponse({ id: 1, nickname: 'Jane Doe' });
// {
//   success: true,
//   data: { id: 1, nickname: 'Jane Doe' },
//   error: undefined
// }

Building an unsuccessful response:

import { buildResponse } from 'api-response-utils';

buildResponse(undefined, new Error('The user was not found in the db.'));
// {
//   success: false,
//   data: undefined,
//   error: 'The user was not found in the db.'
// }

Checking if a value is a response object:

import { isResponse } from 'api-response-utils';

isResponse({ 
  success: true,
  data: { id: 1, nickname: 'Jane Doe' },
  error: undefined,
});
// true

isResponse({ foo: 'bar' });
// false

Built With

  • TypeScript

Running the Tests

$ npm run test:unit

License

MIT


Acknowledgments

  • ...

@TODOS

  • ...

Deployment

Install dependencies:

$ npm install

Build the library:

$ npm start

Publish to npm:

$ npm publish

About

The api-response-utils package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published