3.1.0 • Published 1 year ago

@mimik/response-helper v3.1.0

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
1 year ago

responseHelper

Example

const responseHelper = require('@mimik/response-helper');

responseHelper~cleanObj(mObj, properties) ⇒ object

Clean a mongoose object

Kind: inner method of responseHelper
Returns: object - The clean Mongoose object or the object if null or not an array of Object or not an Object.
Category: sync

ParamTypeDescription
mObjobjectMongoose object to clean. If null or not an Array of Object or not an Object return mObj.
propertiesarrayProperties to remove from the object. Top level only. If null or not an Array the defautl '_id, '__v' will be used.

responseHelper~sendError(error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters) ⇒

The error has the following format:

{
   "statusCode": "http code for the response",
   "title": "http title associated with the http code",
   "message": "error.message or `no error message` if the error does not exist",
   "info": "information contains in the error if the error is not an Error",
   "error": "extra information associated with the error if error is an Error"
}

The swagger options object has the following properties:

{
   "swagger": {
     "method": "method of the request",
     "path": "path of the request",
     "operationId": "operation defined for that route"
   },
   "correlationId": "correlationId including in the header of the request if present otherwise UUID"
}

The parameters object has the following optional properties:

{
   "rfc": "to indicate the error need to be in rfc format and could be in the error itself, if present the value is 7807",
   "headers": "object with propertied that need to be added to the header of the response"
}

If an error of statusCode abouve 500 is to be sent, and if logger is enable but not set, a error level log will be generated, otherwise a warning level log will be generated.

Kind: inner method of responseHelper
Returns: null.
Category: sync
Requires: module:@mimik/sumologic-winston-logger

ParamTypeDescription
errorobjectThe error to include in the response.
responseobjectThe http response object.
otherErrorStatusCodenumberStatusCode overriding the statusCode of the error to associated with the response.
swaggerOptionsobjectObject containing the swagger values to be used to setup the error.
logLevelstringTo indicate if the response will be logged on not (false or undefined or invalid will indicate that the log is done with error level). If set to true, will indicate that no log should be done.
parametersobjectParameters to add or configure the response

responseHelper~sendResult(result, statusCode, response, swaggerOptions, logLevel, parameters) ⇒

If the result contains a data fields the response is sent as is, otherwise the result is encapsulated under data property. The swagger options object has the following properties:

{
   "swagger": {
     "method": "method of the request",
     "path": "path of the request",
     "operationId": "operation defined for that route"
   },
   "correlationId": "correlationId including in the header of the request if present otherwise UUID"
}

The parameters object has the following optional properties:

{
   "notTouch": "to indicate the error need to be in rfc format and could be in the error itself, if present the value is 7807",
   "headers": "object with properties that need to be added to the header of the response",
   "cleanUp": array of string describing the property to remov at the top level of the object in data",
}

Kind: inner method of responseHelper
Returns: null.
Category: sync
Requires: module:@mimik/sumologic-winston-logger, module:@mimik/lib-filters

ParamTypeDescription
resultobjectActual result to be send.
statusCodenumberHttp statusCode to send as part of the response.
responseobjectThe http response object.
swaggerOptionsobjectObject containing the values to be passed.
logLevelstringTo indicate if the response will be logged on not (false or undefined or invalid will indicate that the log is done with error level). If set to true, will indicate that no log should be done.
parametersobjectParamters to add or configure the response

responseHelper~getRichError(val, message, info, err, logLevel, correlationId) ⇒ object

Create a rich error.

Kind: inner method of responseHelper
Returns: object - The rich error.

{
   "name": "name of the error",
   "info": "info of the error",
   "cause": "encapsulated error",
   "message": "message of the error"
}

Category: sync
Requires: module:@mimik/sumologic-winston-logger

ParamTypeDescription
valobjectIf it is a string it represents the name of the error, if it is a number it represents the code of the error (e.g. 400, 500, ....). Invalid string or number will result to a 500 Internal Error title.
messagestringMessage to associated with the error.
infoobjectInfo as a JSON object to associate to the error.
errobjectError to encapsulate in the created error as a cause.
logLevelstringIndicates if the error needs to be log at creation with a specific level. If logLevel is false a error log will be created with `error level.
correlationIdstringCorrelationId to be added to the log when log is enabled.
3.1.0

1 year ago

3.0.1

2 years ago

3.0.0

2 years ago

2.6.3

2 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

4 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.0.0

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago