UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

69 lines (61 loc) 1.54 kB
import { STATUS_CREATED, STATUS_FORBIDDEN, STATUS_NOT_FOUND, STATUS_OK, STATUS_OUT_OF_SYNC, } from './Result.js'; function getDebugHttpStatusCode(stringToVerify, allowedStatusCodes) { const regexResult = /HTTP-([0-9]{3})(?:-(on-delete))?/g.exec( stringToVerify ); if (regexResult === null) { return null; } const httpStatusCode = parseInt(regexResult[1], 10); if (!allowedStatusCodes.includes(httpStatusCode)) { return null; } if (regexResult[2] === 'on-delete') { return { statusCode: httpStatusCode, onDelete: true }; } return { statusCode: httpStatusCode, onDelete: false }; } /** * Maps the values present in the status field of an AnnotationResult to a HTTP status code. * * @param {string} status * @param {boolean} hasContent * @return {number} */ function mapAnnotationResultStatusToHttpStatusCode(status, hasContent) { switch (status) { case STATUS_OK: return hasContent ? 200 : 204; case STATUS_CREATED: return 201; case STATUS_FORBIDDEN: return 403; case STATUS_NOT_FOUND: return 404; case STATUS_OUT_OF_SYNC: return 412; default: return 500; } } /** * Sends an HTTP status code for testing purposes. Helper function. * * @param {Object} res * @param {Object} foundHttpStatusCode */ function sendDebugHttpResponse(res, foundHttpStatusCode) { const httpStatusCode = foundHttpStatusCode.statusCode; res.status(httpStatusCode).end(); } export default { getDebugHttpStatusCode, mapAnnotationResultStatusToHttpStatusCode, sendDebugHttpResponse, };