Last active
November 8, 2020 15:44
-
-
Save AllanJeremy/53f3b5aca8f0f19ee110df3b1688f9f4 to your computer and use it in GitHub Desktop.
A simple API response helper. Formats API responses in a consistent manner. Concepts and format borrowed from the telegram API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Get an API response | |
* @param {String} message Message to return as part of the response | |
* @param {Object} data An object representing data returned as part of the API response. Default: `null` | |
* @param {Number} statusCode HTTP status code to be returned with this response. Default: `200` | |
* @return {Object} An API response object | |
*/ | |
const getResponse = (ok, message, data = null, statusCode = 200) => { | |
let response = { | |
ok, | |
message, | |
data, | |
statusCode | |
}; | |
return response; | |
}; | |
/** Get an error response | |
* @param {String} message The error message to return as part of the response | |
* @param {Object<Error>} error The error object containing the error's data | |
* @param {Number} statusCode HTTP status code to be returned with this response | |
* @return {Object} An API response object | |
*/ | |
const getError = (message, error, statusCode = 500) => { | |
statusCode = statusCode; | |
let response = this.getResponse(false, message, null, statusCode); | |
response.error = error; | |
return response; | |
}; | |
/** Get an unauthorized error response.Ideally used when a user does not have permission to access a resource. | |
* @param {String} message The error message to return as part of the response | |
* @param {Object<Error>} error The error object containing the error's data | |
* @return {Object} An API response object | |
*/ | |
const getUnauthorizedError = ( | |
message = "Permission denied! You are not authorized to do that.", | |
error = Error("Permission denied! You are not authorized to do that.") | |
) => { | |
let response = getError(message, error, 401); | |
response.error = error; | |
return response; | |
}; | |
/** Attaches an error handler to an `async` function | |
* @param {Object} res A HTTP resource object. Usually the first parameter of an Express middleware function | |
* @param {Function} fn The `async` function to attach the error message to | |
* @return A promise with an error handler attached to it | |
*/ | |
const attachErrorHandler = (res, fn) => { //? Intended to be called in middleware | |
return fn.catch(err => { | |
let apiResponse = this.getError(err.message, err); | |
console.log(err); | |
console.error(err.message); | |
res.status(500).json(apiResponse); | |
}); | |
}; | |
/** Prints an API response. Used to return the API response to the client. | |
* @param {Object} res Express result. Usually the first parameter in an express middleware function | |
* @param {Object} apiResponse An API response generated by the API util. Expects a specific format | |
* @param {Function} next A next function, passes over execution to the next middleware if set. | |
*/ | |
const printApiResponse = (res, apiResponse, next = null) => { | |
res.status(apiResponse.statusCode).json(apiResponse); | |
// Only run the next function if an actual function is provided | |
if (next && typeof next === 'function') { | |
next(); | |
} | |
}; | |
module.exports = { | |
getResponse, | |
getError, | |
getUnauthorizedError, | |
attachErrorHandler, | |
printApiResponse | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment