UNPKG

@halsp/http

Version:

支持 Halsp HTTP 请求

179 lines 8.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initResultHandler = void 0; const http_status_codes_1 = require("http-status-codes"); const core_1 = require("@halsp/core"); function initResultHandler(target, getRes) { function setResult(status, body) { const res = getRes.bind(this)(); res.body = body; res.status = status; return this; } function setError(status, error) { const res = getRes.bind(this)(); res.status = status; const obj = { status: status, message: (0, core_1.isString)(error) ? error : (0, http_status_codes_1.getReasonPhrase)(status), }; if (error && !(0, core_1.isString)(error)) { res.body = Object.assign(obj, error); } else { res.body = obj; } return this; } target.ok = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.OK, body); }; target.created = function (location, body) { setResult.bind(this)(http_status_codes_1.StatusCodes.CREATED, body); const res = getRes.bind(this)(); res.set("location", location); return this; }; target.accepted = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.ACCEPTED, body); }; target.noContent = function () { return setResult.bind(this)(http_status_codes_1.StatusCodes.NO_CONTENT); }; target.partialContent = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.PARTIAL_CONTENT, body); }; target.redirect = function (location, code = http_status_codes_1.StatusCodes.MOVED_TEMPORARILY) { const res = getRes.bind(this)(); res.setStatus(code).set("location", location); return this; }; target.badRequest = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.BAD_REQUEST, body); }; target.badRequestMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.BAD_REQUEST, error); }; target.unauthorized = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.UNAUTHORIZED, body); }; target.unauthorizedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.UNAUTHORIZED, error); }; target.forbidden = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.FORBIDDEN, body); }; target.forbiddenMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.FORBIDDEN, error); }; target.notFound = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.NOT_FOUND, body); }; target.notFoundMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.NOT_FOUND, error); }; target.methodNotAllowed = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.METHOD_NOT_ALLOWED, body); }; target.methodNotAllowedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.METHOD_NOT_ALLOWED, error); }; target.notAcceptable = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.NOT_ACCEPTABLE, body); }; target.notAcceptableMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.NOT_ACCEPTABLE, error); }; target.requestTimeout = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.REQUEST_TIMEOUT, body); }; target.requestTimeoutMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.REQUEST_TIMEOUT, error); }; target.conflict = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.CONFLICT, body); }; target.conflictMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.CONFLICT, error); }; target.gone = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.GONE, body); }; target.goneMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.GONE, error); }; target.preconditionFailed = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.PRECONDITION_FAILED, body); }; target.preconditionFailedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.PRECONDITION_FAILED, error); }; target.requestTooLong = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.REQUEST_TOO_LONG, body); }; target.requestTooLongMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.REQUEST_TOO_LONG, error); }; target.unsupportedMediaType = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.UNSUPPORTED_MEDIA_TYPE, body); }; target.unsupportedMediaTypeMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.UNSUPPORTED_MEDIA_TYPE, error); }; target.imATeapot = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.IM_A_TEAPOT, body); }; target.imATeapotMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.IM_A_TEAPOT, error); }; target.misdirected = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.MISDIRECTED_REQUEST, body); }; target.misdirectedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.MISDIRECTED_REQUEST, error); }; target.unprocessableEntity = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.UNPROCESSABLE_ENTITY, body); }; target.unprocessableEntityMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.UNPROCESSABLE_ENTITY, error); }; target.internalServerError = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR, body); }; target.internalServerErrorMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR, error); }; target.notImplemented = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.NOT_IMPLEMENTED, body); }; target.notImplementedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.NOT_IMPLEMENTED, error); }; target.badGateway = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.BAD_GATEWAY, body); }; target.badGatewayMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.BAD_GATEWAY, error); }; target.serviceUnavailable = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.SERVICE_UNAVAILABLE, body); }; target.serviceUnavailableMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.SERVICE_UNAVAILABLE, error); }; target.gatewayTimeout = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.GATEWAY_TIMEOUT, body); }; target.gatewayTimeoutMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.GATEWAY_TIMEOUT, error); }; target.httpVersionNotSupported = function (body) { return setResult.bind(this)(http_status_codes_1.StatusCodes.HTTP_VERSION_NOT_SUPPORTED, body); }; target.httpVersionNotSupportedMsg = function (error) { return setError.bind(this)(http_status_codes_1.StatusCodes.HTTP_VERSION_NOT_SUPPORTED, error); }; } exports.initResultHandler = initResultHandler; //# sourceMappingURL=result-handler.js.map