0.1.1 • Published 5 years ago

@respondex/core v0.1.1

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

@respondex/core Build Status Coverage Status Maintainability

This is a simple node package that helps developers package HTTP responses into simple reusable methods, without having to worry about setting the headers and the right status codes.

All you need to do is call the right method with all the information required along with the express HTTP response object and the response is properly formated for you in line with REST API Guidelines.

Express is a powerful framework that helps handle HTTP requests and responses on the server. However, it can become a bit repetitive to have to respond to various request the same way. This would help take all that repetitive "res" code and stow it away in the node_modules. 😄

Getting Started

Your REST API must be using express as its HTTP handler.

  • Download the package as a development dependency with
npm install @respondex/core @respondex/apierror
  • Import the package into your controller, middleware or wherever you wish to send a response from.
import RespondEx from '@respondex/core'
  • Use by simply sending a response as demonstrated below for a newly created product resource.
RespondEx.createdResource(
  'Successful sign-in',
  {
    name: 'Chair',
    quantity: 100,
    description: 'A comfortable seat for your afternoon tea.',
  },
  'https://fakeecommerce.com/api/v1/products/1',
  res,
);

Documentation

Methods

createdResource: Sends a HTTP response for a newly created resource. Response will contain a 201 status code, message and data in the body. It would also contain the URL to the newly created resource in the header of the response.

ParametersTypeDescriptionExample
messagestringThe message to be sent with the response"Product successfully created."
dataobjectNewly created resource data{ name: "Chair" }
locationstringThe URL of the newly created resource"https://fakeecommerce.com/api/v1/products/1"
resobjectThe express http response object
options (optional)objectSome extra headers{ contentType: "application/json" }

Example

RespondEx.createdResource(
  'Successful sign-in',
  {
    name: 'Chair',
    quantity: 100,
    description: 'A comfortable seat for your afternoon tea.'
  },
  'https://fakeecommerce.com/api/v1/products/1',
  res,
);

successWithData: Sends a HTTP response for a successful request with some returned data. Response will contain a 200 status code, message and data in the body.

ParametersTypeDescriptionExample
messagestringThe message to be sent with the response"Product successfully created."
dataobjectNewly created resource data{ name: "Chair" }
resobjectThe express http response object
options (optional)objectSome extra headers{ contentType: "application/json" }

Example

RespondEx.successWithData(
  'Successful sign-in',
  {
    name: 'Chair',
    quantity: 100,
    description: 'A comfortable seat for your afternoon tea.'
  },
  res,
);

successWithoutData: Sends a HTTP response for a successful request without an data. Response will contain a 200 status code and message body.

ParametersTypeDescriptionExample
messagestringThe message to be sent with the response"Product successfully created."
resobjectThe express http response object
options (optional)objectSome extra headers{ contentType: "application/json" }

Example

RespondEx.successWithData(
  'Successful sign-in',
  res,
);

error: Sends a HTTP response error using an instance of the APIError class. This method will also handle unforseen server errors that occur at runtime.

ParameterTypeDescriptionExample
errorAPIErrorAn instance of the APIError object
resobjectThe express http response object
options (optional)objectSome extra headers{ contentType: "application/json" }

Examples

import APIError from "@respondex/apierror"

const error = new APIError(...);
RespondEx.error(
  error,
  res,
);

The below example would cause a 500 server error response.

const error = new Error(...);
RespondEx.error(
  error,
  res,
);

errorByType: Sends a HTTP response error by providing an error type.

ParameterTypeDescriptionExample
typestringThe type of error to be sentNotFound
resobjectThe express http response object
message (optional)objectThe message to be sent in the body"Some error has occurred"
possibleErrors (optional)object[]The express http response object
options (optional)objectSome extra headers{ contentType: "application/json" }

Example

RespondEx.errorByType(
  'NotFound',
  res,
);

Options

This is an optional parameter that can be provided to the methods to configure some of the information in the header. Currently there is just one.

PropertyTypeExample
contentTypestring"application/json"

Errors

This is the list of error types

ErrorcodeDescription
ClientError400For general client errors
Unauth­orized401For unauthorized access errors
Forbidden403For for forbidden access errors
NotFound404Resource not found error
default500For general server errors

Contributors

Opeoluwa Iyi-Kuyoro: 👨🏿Profile - WebSite

Contributions

This project is very open to contributions from the community. If you find this package useful, and you feel there are a new things you would like to add or nasty bugs that you just want to get rid of, please feel free to fork, modify and raise a PR.

In doing so, I would like you to follow the conventions.

Commit styles: This project uses the Angular commit style and commitplease has been set up to enforce that.

PR Style: Please use the template you find in the PR message to compose one.

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago