1.0.0 • Published 3 months ago

@supersourcing/response-handler v1.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
3 months ago

response-handler

Version npm It is a simple response message package.

Installation

To install the project, you need to have NodeJS and Npm pre-installed in your system. Only then, you can run the following commands:

       npm install response-handler

Description

  • index.js - This file contains the code for error and response handle.

  • notFoundError - It indicates that the URL you used in your request doesn’t exist on the API server, or origin server.this can also indicate a server problem. Sometimes API URL paths change after a version update, but sometimes they change because something on the server went wrong.

          notFoundError('Your message!', yourData); // 404
  • forbiddenError - The forbidden status indicates that you don’t have permission to request that URL. You’re authenticated, but the user or role you’re authenticated for isn’t permitted to make the API request.This also occurs when you have an authentication issue, like when using the wrong API key or trying to access features your subscription plan doesn’t allow for.

          forbiddenError('Your message!', yourData); // 403
  • unauthorizedError - This status code means you haven’t yet authenticated against the API. The API doesn’t know who you are and it won’t serve you. For most APIs you need to sign up and get an API key. This key is then used inside an HTTP header field when you send a request, telling the API who you are.

          unauthorizedError('Your message!', yourData); // 401
  • preconditionFailedError - The client has indicated preconditions in its headers which the server does not meet.

          preconditionFailedError('Your message!', yourData); // 412
  • tooManyRequestsError - Most API subscription plans have limits — the cheaper the plan, the fewer requests per second are allowed for your API key.

          tooManyRequestsError('Your message!', yourData); // 429
  • internalServerError - This HTTP status code can mean anything really, but it usually indicates the API server crashed. It could have been caused by something related to your API call.

          internalServerError('Your message!', yourData); // 500
  • notImplementedError - Usually, an HTTP request with an inappropriate method simply results in a 404 not found status. A not-implemented status implies that the method isn’t implemented “yet.” The API creator can use this status to tell the clients that this method will be available to them in future requests.

          notImplementedError('Your message!', yourData); // 501
  • badGatewayError - This response tells you that the server you were calling wasn’t the actual API server, but a gateway or proxy. The proxy server tries to call the API server in your name. This error response also indicates that the API server didn’t answer. This could be related to a network problem, or simply because the API server crashed, or was down for maintenance

          badGatewayError('Your message!', yourData); // 502
  • serviceUnavailableError - The 503 Service Unavailable Status indicates a server error. Too many API requests were sent and now the API can’t handle any more of them. This problem solves itself when clients send fewer future requests, but it could also mean that the API provider didn’t plan enough resources for all of its customers.

          serviceUnavailableError('Your message!', yourData); // 503
  • gatewayTimedOutError - Like the 502 Bad Gateway status, this response code tells you that the server you were calling is a proxy for the real API server. This time, the problem is the API server’s slow response. This could be related to high network latency between the proxy and the API server. It could also mean that the API server takes too long to process your request.To solve this problem, check if your request’s content could be related to that timeout. If you are requesting too much data or a calculation that takes too long, you should try and reduce it. If you think your request is reasonable and the status doesn’t go away, contact support.

          gatewayTimedOutError('Your message!', yourData); // 504
  • badRequestError - The request could not be understood by the server due to incorrect syntax. The client SHOULD NOT repeat the request without modifications.

          badRequestError('Your message!', yourData); // 400
  • Unprocessable Entity - The server understands the content type and syntax of the request entity, but still server is unable to process the request for some reason.

          unProcessableEntity('Your message!', yourData); // 422
  • Ok Response - OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

          okResponse(yourData, 'Your message!'); // 200

Usage

  • In your file

    const responseFunction = require('@mayank_supersourcing/response-handler');
    
    const your_function_name = async () => {
        try {
            const data = your_conditions;
            if (data) {
                return responseFunction.okResponse(data, 'Your message!');
        } catch (error) {
            return responseFunction.unProcessableEntity(error.message);
        }
    };
  • Ok Response

    {
        "success": true,
        "statusCode": 200,
        "data": [],
        "message": "Your message!"
    }
  • Un Processable Entity Response

    {
        "statusCode": 422,
        "message": "Your message!"
    }
1.0.0

3 months ago