response-objects
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,3 @@ | ||
declare function R_(code: number, body?: any, headers?: any): ResponseObject; | ||
export default R_; | ||
export interface R<Body> extends ResponseObject { | ||
@@ -19,65 +21,129 @@ body: Body; | ||
export declare const MARKER: symbol; | ||
declare function R_(code: number, body?: any, headers?: any): ResponseObject; | ||
export declare const Continue: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const SwitchingProtocols: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const Processing: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const OK: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const Ok: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const Created: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const Accepted: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const NonAuthoritativeInformation: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const NoContent: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const ResetContent: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const PartialContent: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const MultiStatus: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const AlreadyReported: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const IMUsed: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const MultipleChoices: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const MovedPermanently: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const Found: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const SeeOther: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const NotModified: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const UseProxy: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const TemporaryRedirect: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const PermanentRedirect: (body?: any, headers?: object | undefined) => ResponseObject; | ||
export declare const BadRequest: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const Unauthorized: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const PaymentRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const Forbidden: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const NotFound: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const MethodNotAllowed: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const NotAcceptable: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const ProxyAuthenticationRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const RequestTimeout: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const Conflict: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const Gone: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const LengthRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const PreconditionFailed: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const PayloadTooLarge: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const URITooLong: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const UnsupportedMediaType: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const RangeNotSatisfiable: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const ExpectationFailed: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const MisdirectedRequest: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const UnprocessableEntity: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const Locked: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const FailedDependency: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const UnorderedCollection: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const UpgradeRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const PreconditionRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const TooManyRequests: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const RequestHeaderFieldsTooLarge: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const UnavailableForLegalReasons: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const InternalServerError: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const NotImplemented: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const BadGateway: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const ServiceUnavailable: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const GatewayTimeout: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const HTTPVersionNotSupported: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const VariantAlsoNegotiates: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const InsufficientStorage: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const LoopDetected: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const BandwidthLimitExceeded: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const NotExtended: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export declare const NetworkAuthenticationRequired: (body?: any, headers?: object | undefined) => ErrorResponseObject; | ||
export default R_; | ||
export declare type RConstructor = (body?: any, headers?: object) => ResponseObject; | ||
export declare type RErrorConstructor = (body?: any, headers?: object) => ErrorResponseObject; | ||
export declare const Continue: RConstructor; | ||
export declare const SwitchingProtocols: RConstructor; | ||
export declare const Processing: RConstructor; | ||
export declare const OK: RConstructor; | ||
export declare const Ok: RConstructor; | ||
export declare const Created: RConstructor; | ||
export declare const Accepted: RConstructor; | ||
export declare const NonAuthoritativeInformation: RConstructor; | ||
export declare const NoContent: RConstructor; | ||
export declare const ResetContent: RConstructor; | ||
export declare const PartialContent: RConstructor; | ||
export declare const MultiStatus: RConstructor; | ||
export declare const AlreadyReported: RConstructor; | ||
export declare const IMUsed: RConstructor; | ||
export declare const MultipleChoices: RConstructor; | ||
export declare const MovedPermanently: RConstructor; | ||
export declare const Found: RConstructor; | ||
export declare const SeeOther: RConstructor; | ||
export declare const NotModified: RConstructor; | ||
export declare const UseProxy: RConstructor; | ||
export declare const TemporaryRedirect: RConstructor; | ||
export declare const PermanentRedirect: RConstructor; | ||
export declare const BadRequest: RErrorConstructor; | ||
export declare const Unauthorized: RErrorConstructor; | ||
export declare const PaymentRequired: RErrorConstructor; | ||
export declare const Forbidden: RErrorConstructor; | ||
export declare const NotFound: RErrorConstructor; | ||
export declare const MethodNotAllowed: RErrorConstructor; | ||
export declare const NotAcceptable: RErrorConstructor; | ||
export declare const ProxyAuthenticationRequired: RErrorConstructor; | ||
export declare const RequestTimeout: RErrorConstructor; | ||
export declare const Conflict: RErrorConstructor; | ||
export declare const Gone: RErrorConstructor; | ||
export declare const LengthRequired: RErrorConstructor; | ||
export declare const PreconditionFailed: RErrorConstructor; | ||
export declare const PayloadTooLarge: RErrorConstructor; | ||
export declare const URITooLong: RErrorConstructor; | ||
export declare const UnsupportedMediaType: RErrorConstructor; | ||
export declare const RangeNotSatisfiable: RErrorConstructor; | ||
export declare const ExpectationFailed: RErrorConstructor; | ||
export declare const MisdirectedRequest: RErrorConstructor; | ||
export declare const UnprocessableEntity: RErrorConstructor; | ||
export declare const Locked: RErrorConstructor; | ||
export declare const FailedDependency: RErrorConstructor; | ||
export declare const UnorderedCollection: RErrorConstructor; | ||
export declare const UpgradeRequired: RErrorConstructor; | ||
export declare const PreconditionRequired: RErrorConstructor; | ||
export declare const TooManyRequests: RErrorConstructor; | ||
export declare const RequestHeaderFieldsTooLarge: RErrorConstructor; | ||
export declare const UnavailableForLegalReasons: RErrorConstructor; | ||
export declare const InternalServerError: RErrorConstructor; | ||
export declare const NotImplemented: RErrorConstructor; | ||
export declare const BadGateway: RErrorConstructor; | ||
export declare const ServiceUnavailable: RErrorConstructor; | ||
export declare const GatewayTimeout: RErrorConstructor; | ||
export declare const HTTPVersionNotSupported: RErrorConstructor; | ||
export declare const VariantAlsoNegotiates: RErrorConstructor; | ||
export declare const InsufficientStorage: RErrorConstructor; | ||
export declare const LoopDetected: RErrorConstructor; | ||
export declare const BandwidthLimitExceeded: RErrorConstructor; | ||
export declare const NotExtended: RErrorConstructor; | ||
export declare const NetworkAuthenticationRequired: RErrorConstructor; | ||
declare namespace R_ { | ||
const Continue: RConstructor; | ||
const SwitchingProtocols: RConstructor; | ||
const Processing: RConstructor; | ||
const OK: RConstructor; | ||
const Ok: RConstructor; | ||
const Created: RConstructor; | ||
const Accepted: RConstructor; | ||
const NonAuthoritativeInformation: RConstructor; | ||
const NoContent: RConstructor; | ||
const ResetContent: RConstructor; | ||
const PartialContent: RConstructor; | ||
const MultiStatus: RConstructor; | ||
const AlreadyReported: RConstructor; | ||
const IMUsed: RConstructor; | ||
const MultipleChoices: RConstructor; | ||
const MovedPermanently: RConstructor; | ||
const Found: RConstructor; | ||
const SeeOther: RConstructor; | ||
const NotModified: RConstructor; | ||
const UseProxy: RConstructor; | ||
const TemporaryRedirect: RConstructor; | ||
const PermanentRedirect: RConstructor; | ||
const BadRequest: RErrorConstructor; | ||
const Unauthorized: RErrorConstructor; | ||
const PaymentRequired: RErrorConstructor; | ||
const Forbidden: RErrorConstructor; | ||
const NotFound: RErrorConstructor; | ||
const MethodNotAllowed: RErrorConstructor; | ||
const NotAcceptable: RErrorConstructor; | ||
const ProxyAuthenticationRequired: RErrorConstructor; | ||
const RequestTimeout: RErrorConstructor; | ||
const Conflict: RErrorConstructor; | ||
const Gone: RErrorConstructor; | ||
const LengthRequired: RErrorConstructor; | ||
const PreconditionFailed: RErrorConstructor; | ||
const PayloadTooLarge: RErrorConstructor; | ||
const URITooLong: RErrorConstructor; | ||
const UnsupportedMediaType: RErrorConstructor; | ||
const RangeNotSatisfiable: RErrorConstructor; | ||
const ExpectationFailed: RErrorConstructor; | ||
const MisdirectedRequest: RErrorConstructor; | ||
const UnprocessableEntity: RErrorConstructor; | ||
const Locked: RErrorConstructor; | ||
const FailedDependency: RErrorConstructor; | ||
const UnorderedCollection: RErrorConstructor; | ||
const UpgradeRequired: RErrorConstructor; | ||
const PreconditionRequired: RErrorConstructor; | ||
const TooManyRequests: RErrorConstructor; | ||
const RequestHeaderFieldsTooLarge: RErrorConstructor; | ||
const UnavailableForLegalReasons: RErrorConstructor; | ||
const InternalServerError: RErrorConstructor; | ||
const NotImplemented: RErrorConstructor; | ||
const BadGateway: RErrorConstructor; | ||
const ServiceUnavailable: RErrorConstructor; | ||
const GatewayTimeout: RErrorConstructor; | ||
const HTTPVersionNotSupported: RErrorConstructor; | ||
const VariantAlsoNegotiates: RErrorConstructor; | ||
const InsufficientStorage: RErrorConstructor; | ||
const LoopDetected: RErrorConstructor; | ||
const BandwidthLimitExceeded: RErrorConstructor; | ||
const NotExtended: RErrorConstructor; | ||
const NetworkAuthenticationRequired: RErrorConstructor; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const status = require("statuses"); | ||
// in the compilation to JS, `export const x = 1` becomes `exports.x = 1`, so we | ||
// need to make sure `exports` is pointing to the right object here, before assignments | ||
// start | ||
function R_(code, body, headers) { | ||
return (code >= 400 ? | ||
createErrorResponse(code) : | ||
createResponse(code))(body, headers); | ||
} | ||
exports = R_; | ||
exports.default = R_; | ||
let bodyCreator = (code, body) => body != null ? body : (status[code] || `Unknown status for ${code}`); | ||
@@ -12,3 +15,4 @@ exports.setBodyCreator = (fn) => { bodyCreator = fn; }; | ||
const proto = { toJSON, toString, status: 0, statusCode: 0, headers: {}, [exports.MARKER]: true }; | ||
function createResponse(code, name) { | ||
function createResponse(code) { | ||
const name = getName(code); | ||
return _setName(function Response(body, headers) { | ||
@@ -21,3 +25,4 @@ if (body && body[exports.MARKER]) | ||
const errProto = Object.assign(Object.create(Error.prototype), proto); | ||
function createErrorResponse(code, name) { | ||
function createErrorResponse(code) { | ||
const name = getName(code); | ||
return _setName(function ErrorResponse(body, headers) { | ||
@@ -32,2 +37,143 @@ if (body && body[exports.MARKER]) | ||
} | ||
/* | ||
Named module exports | ||
*/ | ||
// 1xx | ||
exports.Continue = createResponse(100); | ||
exports.SwitchingProtocols = createResponse(101); | ||
exports.Processing = createResponse(102); | ||
// 2xx | ||
exports.OK = createResponse(200); | ||
exports.Ok = exports.OK; | ||
exports.Created = createResponse(201); | ||
exports.Accepted = createResponse(202); | ||
exports.NonAuthoritativeInformation = createResponse(203); | ||
exports.NoContent = createResponse(204); | ||
exports.ResetContent = createResponse(205); | ||
exports.PartialContent = createResponse(206); | ||
exports.MultiStatus = createResponse(207); | ||
exports.AlreadyReported = createResponse(208); | ||
exports.IMUsed = createResponse(226); | ||
// 3xx | ||
exports.MultipleChoices = createResponse(300); | ||
exports.MovedPermanently = createResponse(301); | ||
exports.Found = createResponse(302); | ||
exports.SeeOther = createResponse(303); | ||
exports.NotModified = createResponse(304); | ||
exports.UseProxy = createResponse(305); | ||
exports.TemporaryRedirect = createResponse(307); | ||
exports.PermanentRedirect = createResponse(308); | ||
// 4xx | ||
exports.BadRequest = createErrorResponse(400); | ||
exports.Unauthorized = createErrorResponse(401); | ||
exports.PaymentRequired = createErrorResponse(402); | ||
exports.Forbidden = createErrorResponse(403); | ||
exports.NotFound = createErrorResponse(404); | ||
exports.MethodNotAllowed = createErrorResponse(405); | ||
exports.NotAcceptable = createErrorResponse(406); | ||
exports.ProxyAuthenticationRequired = createErrorResponse(407); | ||
exports.RequestTimeout = createErrorResponse(408); | ||
exports.Conflict = createErrorResponse(409); | ||
exports.Gone = createErrorResponse(410); | ||
exports.LengthRequired = createErrorResponse(411); | ||
exports.PreconditionFailed = createErrorResponse(412); | ||
exports.PayloadTooLarge = createErrorResponse(413); | ||
exports.URITooLong = createErrorResponse(414); | ||
exports.UnsupportedMediaType = createErrorResponse(415); | ||
exports.RangeNotSatisfiable = createErrorResponse(416); | ||
exports.ExpectationFailed = createErrorResponse(417); | ||
exports.MisdirectedRequest = createErrorResponse(421); | ||
exports.UnprocessableEntity = createErrorResponse(422); | ||
exports.Locked = createErrorResponse(423); | ||
exports.FailedDependency = createErrorResponse(424); | ||
exports.UnorderedCollection = createErrorResponse(425); | ||
exports.UpgradeRequired = createErrorResponse(426); | ||
exports.PreconditionRequired = createErrorResponse(428); | ||
exports.TooManyRequests = createErrorResponse(429); | ||
exports.RequestHeaderFieldsTooLarge = createErrorResponse(431); | ||
exports.UnavailableForLegalReasons = createErrorResponse(451); | ||
// 5xx | ||
exports.InternalServerError = createErrorResponse(500); | ||
exports.NotImplemented = createErrorResponse(501); | ||
exports.BadGateway = createErrorResponse(502); | ||
exports.ServiceUnavailable = createErrorResponse(503); | ||
exports.GatewayTimeout = createErrorResponse(504); | ||
exports.HTTPVersionNotSupported = createErrorResponse(505); | ||
exports.VariantAlsoNegotiates = createErrorResponse(506); | ||
exports.InsufficientStorage = createErrorResponse(507); | ||
exports.LoopDetected = createErrorResponse(508); | ||
exports.BandwidthLimitExceeded = createErrorResponse(509); | ||
exports.NotExtended = createErrorResponse(510); | ||
exports.NetworkAuthenticationRequired = createErrorResponse(511); | ||
/* | ||
Attach all exports to R_ as well | ||
*/ | ||
(function (R_) { | ||
R_.Continue = createResponse(100); | ||
R_.SwitchingProtocols = createResponse(101); | ||
R_.Processing = createResponse(102); | ||
// 2xx | ||
R_.OK = createResponse(200); | ||
R_.Ok = R_.OK; | ||
R_.Created = createResponse(201); | ||
R_.Accepted = createResponse(202); | ||
R_.NonAuthoritativeInformation = createResponse(203); | ||
R_.NoContent = createResponse(204); | ||
R_.ResetContent = createResponse(205); | ||
R_.PartialContent = createResponse(206); | ||
R_.MultiStatus = createResponse(207); | ||
R_.AlreadyReported = createResponse(208); | ||
R_.IMUsed = createResponse(226); | ||
// 3xx | ||
R_.MultipleChoices = createResponse(300); | ||
R_.MovedPermanently = createResponse(301); | ||
R_.Found = createResponse(302); | ||
R_.SeeOther = createResponse(303); | ||
R_.NotModified = createResponse(304); | ||
R_.UseProxy = createResponse(305); | ||
R_.TemporaryRedirect = createResponse(307); | ||
R_.PermanentRedirect = createResponse(308); | ||
// 4xx | ||
R_.BadRequest = createErrorResponse(400); | ||
R_.Unauthorized = createErrorResponse(401); | ||
R_.PaymentRequired = createErrorResponse(402); | ||
R_.Forbidden = createErrorResponse(403); | ||
R_.NotFound = createErrorResponse(404); | ||
R_.MethodNotAllowed = createErrorResponse(405); | ||
R_.NotAcceptable = createErrorResponse(406); | ||
R_.ProxyAuthenticationRequired = createErrorResponse(407); | ||
R_.RequestTimeout = createErrorResponse(408); | ||
R_.Conflict = createErrorResponse(409); | ||
R_.Gone = createErrorResponse(410); | ||
R_.LengthRequired = createErrorResponse(411); | ||
R_.PreconditionFailed = createErrorResponse(412); | ||
R_.PayloadTooLarge = createErrorResponse(413); | ||
R_.URITooLong = createErrorResponse(414); | ||
R_.UnsupportedMediaType = createErrorResponse(415); | ||
R_.RangeNotSatisfiable = createErrorResponse(416); | ||
R_.ExpectationFailed = createErrorResponse(417); | ||
R_.MisdirectedRequest = createErrorResponse(421); | ||
R_.UnprocessableEntity = createErrorResponse(422); | ||
R_.Locked = createErrorResponse(423); | ||
R_.FailedDependency = createErrorResponse(424); | ||
R_.UnorderedCollection = createErrorResponse(425); | ||
R_.UpgradeRequired = createErrorResponse(426); | ||
R_.PreconditionRequired = createErrorResponse(428); | ||
R_.TooManyRequests = createErrorResponse(429); | ||
R_.RequestHeaderFieldsTooLarge = createErrorResponse(431); | ||
R_.UnavailableForLegalReasons = createErrorResponse(451); | ||
// 5xx | ||
R_.InternalServerError = createErrorResponse(500); | ||
R_.NotImplemented = createErrorResponse(501); | ||
R_.BadGateway = createErrorResponse(502); | ||
R_.ServiceUnavailable = createErrorResponse(503); | ||
R_.GatewayTimeout = createErrorResponse(504); | ||
R_.HTTPVersionNotSupported = createErrorResponse(505); | ||
R_.VariantAlsoNegotiates = createErrorResponse(506); | ||
R_.InsufficientStorage = createErrorResponse(507); | ||
R_.LoopDetected = createErrorResponse(508); | ||
R_.BandwidthLimitExceeded = createErrorResponse(509); | ||
R_.NotExtended = createErrorResponse(510); | ||
R_.NetworkAuthenticationRequired = createErrorResponse(511); | ||
})(R_ || (R_ = {})); | ||
function _decorate(resp, code, body, headers) { | ||
@@ -59,78 +205,2 @@ resp.status = resp.statusCode = code; | ||
} | ||
function R_(code, body, headers) { | ||
const name = getName(code); | ||
const responseCtor = code >= 400 ? | ||
createErrorResponse(code, name) : | ||
createResponse(code, name); | ||
return responseCtor(body, headers); | ||
} | ||
// 1xx | ||
exports.Continue = createResponse(100, "Continue"); | ||
exports.SwitchingProtocols = createResponse(101, "SwitchingProtocols"); | ||
exports.Processing = createResponse(102, "Processing"); | ||
// 2xx | ||
exports.OK = createResponse(200, "OK"); | ||
exports.Ok = exports.OK; | ||
exports.Created = createResponse(201, "Created"); | ||
exports.Accepted = createResponse(202, "Accepted"); | ||
exports.NonAuthoritativeInformation = createResponse(203, "NonAuthoritativeInformation"); | ||
exports.NoContent = createResponse(204, "NoContent"); | ||
exports.ResetContent = createResponse(205, "ResetContent"); | ||
exports.PartialContent = createResponse(206, "PartialContent"); | ||
exports.MultiStatus = createResponse(207, "MultiStatus"); | ||
exports.AlreadyReported = createResponse(208, "AlreadyReported"); | ||
exports.IMUsed = createResponse(226, "IMUsed"); | ||
// 3xx | ||
exports.MultipleChoices = createResponse(300, "MultipleChoices"); | ||
exports.MovedPermanently = createResponse(301, "MovedPermanently"); | ||
exports.Found = createResponse(302, "Found"); | ||
exports.SeeOther = createResponse(303, "SeeOther"); | ||
exports.NotModified = createResponse(304, "NotModified"); | ||
exports.UseProxy = createResponse(305, "UseProxy"); | ||
exports.TemporaryRedirect = createResponse(307, "TemporaryRedirect"); | ||
exports.PermanentRedirect = createResponse(308, "PermanentRedirect"); | ||
// 4xx | ||
exports.BadRequest = createErrorResponse(400, "BadRequest"); | ||
exports.Unauthorized = createErrorResponse(401, "Unauthorized"); | ||
exports.PaymentRequired = createErrorResponse(402, "PaymentRequired"); | ||
exports.Forbidden = createErrorResponse(403, "Forbidden"); | ||
exports.NotFound = createErrorResponse(404, "NotFound"); | ||
exports.MethodNotAllowed = createErrorResponse(405, "MethodNotAllowed"); | ||
exports.NotAcceptable = createErrorResponse(406, "NotAcceptable"); | ||
exports.ProxyAuthenticationRequired = createErrorResponse(407, "ProxyAuthenticationRequired"); | ||
exports.RequestTimeout = createErrorResponse(408, "RequestTimeout"); | ||
exports.Conflict = createErrorResponse(409, "Conflict"); | ||
exports.Gone = createErrorResponse(410, "Gone"); | ||
exports.LengthRequired = createErrorResponse(411, "LengthRequired"); | ||
exports.PreconditionFailed = createErrorResponse(412, "PreconditionFailed"); | ||
exports.PayloadTooLarge = createErrorResponse(413, "PayloadTooLarge"); | ||
exports.URITooLong = createErrorResponse(414, "URITooLong"); | ||
exports.UnsupportedMediaType = createErrorResponse(415, "UnsupportedMediaType"); | ||
exports.RangeNotSatisfiable = createErrorResponse(416, "RangeNotSatisfiable"); | ||
exports.ExpectationFailed = createErrorResponse(417, "ExpectationFailed"); | ||
exports.MisdirectedRequest = createErrorResponse(421, "MisdirectedRequest"); | ||
exports.UnprocessableEntity = createErrorResponse(422, "UnprocessableEntity"); | ||
exports.Locked = createErrorResponse(423, "Locked"); | ||
exports.FailedDependency = createErrorResponse(424, "FailedDependency"); | ||
exports.UnorderedCollection = createErrorResponse(425, "UnorderedCollection"); | ||
exports.UpgradeRequired = createErrorResponse(426, "UpgradeRequired"); | ||
exports.PreconditionRequired = createErrorResponse(428, "PreconditionRequired"); | ||
exports.TooManyRequests = createErrorResponse(429, "TooManyRequests"); | ||
exports.RequestHeaderFieldsTooLarge = createErrorResponse(431, "RequestHeaderFieldsTooLarge"); | ||
exports.UnavailableForLegalReasons = createErrorResponse(451, "UnavailableForLegalReasons"); | ||
// 5xx | ||
exports.InternalServerError = createErrorResponse(500, "InternalServerError"); | ||
exports.NotImplemented = createErrorResponse(501, "NotImplemented"); | ||
exports.BadGateway = createErrorResponse(502, "BadGateway"); | ||
exports.ServiceUnavailable = createErrorResponse(503, "ServiceUnavailable"); | ||
exports.GatewayTimeout = createErrorResponse(504, "GatewayTimeout"); | ||
exports.HTTPVersionNotSupported = createErrorResponse(505, "HTTPVersionNotSupported"); | ||
exports.VariantAlsoNegotiates = createErrorResponse(506, "VariantAlsoNegotiates"); | ||
exports.InsufficientStorage = createErrorResponse(507, "InsufficientStorage"); | ||
exports.LoopDetected = createErrorResponse(508, "LoopDetected"); | ||
exports.BandwidthLimitExceeded = createErrorResponse(509, "BandwidthLimitExceeded"); | ||
exports.NotExtended = createErrorResponse(510, "NotExtended"); | ||
exports.NetworkAuthenticationRequired = createErrorResponse(511, "NetworkAuthenticationRequired"); | ||
// export shenanegans | ||
exports.default = R_; | ||
module.exports = exports; |
{ | ||
"name": "response-objects", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "simple value objects representing HTTP responses", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
259
src/index.ts
import status = require("statuses"); | ||
// in the compilation to JS, `export const x = 1` becomes `exports.x = 1`, so we | ||
// need to make sure `exports` is pointing to the right object here, before assignments | ||
// start | ||
function R_ (code: number, body?: any, headers?: any): ResponseObject { | ||
return (code >= 400 ? | ||
createErrorResponse(code) : | ||
createResponse(code) | ||
)(body, headers); | ||
} | ||
exports = R_; | ||
export default R_; | ||
@@ -31,3 +35,4 @@ export interface R<Body> extends ResponseObject { | ||
const proto: ResponseObject = { toJSON, toString, status: 0, statusCode: 0, headers: {}, [MARKER]: true }; | ||
function createResponse (code: number, name: string): RConstructor { | ||
function createResponse (code: number): RConstructor { | ||
const name = getName(code) | ||
return _setName(function Response (body?: any, headers?: object) { | ||
@@ -40,3 +45,4 @@ if (body && body[MARKER]) throw new Error(`Object is already a response: ${JSON.stringify(body)}`); | ||
const errProto: ResponseObject = Object.assign(Object.create(Error.prototype), proto); | ||
function createErrorResponse (code: number, name: string): RErrorConstructor { | ||
function createErrorResponse (code: number): RErrorConstructor { | ||
const name = getName(code) | ||
return _setName(function ErrorResponse (body?: any, headers?: object) { | ||
@@ -51,2 +57,157 @@ if (body && body[MARKER]) throw new Error(`Object is already a response: ${JSON.stringify(body)}`); | ||
export type RConstructor = (body?: any, headers?: object) => ResponseObject | ||
export type RErrorConstructor = (body?: any, headers?: object) => ErrorResponseObject | ||
/* | ||
Named module exports | ||
*/ | ||
// 1xx | ||
export const Continue = createResponse(100); | ||
export const SwitchingProtocols = createResponse(101); | ||
export const Processing = createResponse(102); | ||
// 2xx | ||
export const OK = createResponse(200) | ||
export const Ok = OK | ||
export const Created = createResponse(201) | ||
export const Accepted = createResponse(202) | ||
export const NonAuthoritativeInformation = createResponse(203) | ||
export const NoContent = createResponse(204) | ||
export const ResetContent = createResponse(205) | ||
export const PartialContent = createResponse(206) | ||
export const MultiStatus = createResponse(207) | ||
export const AlreadyReported = createResponse(208) | ||
export const IMUsed = createResponse(226) | ||
// 3xx | ||
export const MultipleChoices = createResponse(300) | ||
export const MovedPermanently = createResponse(301) | ||
export const Found = createResponse(302) | ||
export const SeeOther = createResponse(303) | ||
export const NotModified = createResponse(304) | ||
export const UseProxy = createResponse(305) | ||
export const TemporaryRedirect = createResponse(307) | ||
export const PermanentRedirect = createResponse(308) | ||
// 4xx | ||
export const BadRequest = createErrorResponse(400) | ||
export const Unauthorized = createErrorResponse(401) | ||
export const PaymentRequired = createErrorResponse(402) | ||
export const Forbidden = createErrorResponse(403) | ||
export const NotFound = createErrorResponse(404) | ||
export const MethodNotAllowed = createErrorResponse(405) | ||
export const NotAcceptable = createErrorResponse(406) | ||
export const ProxyAuthenticationRequired = createErrorResponse(407) | ||
export const RequestTimeout = createErrorResponse(408) | ||
export const Conflict = createErrorResponse(409) | ||
export const Gone = createErrorResponse(410) | ||
export const LengthRequired = createErrorResponse(411) | ||
export const PreconditionFailed = createErrorResponse(412) | ||
export const PayloadTooLarge = createErrorResponse(413) | ||
export const URITooLong = createErrorResponse(414) | ||
export const UnsupportedMediaType = createErrorResponse(415) | ||
export const RangeNotSatisfiable = createErrorResponse(416) | ||
export const ExpectationFailed = createErrorResponse(417) | ||
export const MisdirectedRequest = createErrorResponse(421) | ||
export const UnprocessableEntity = createErrorResponse(422) | ||
export const Locked = createErrorResponse(423) | ||
export const FailedDependency = createErrorResponse(424) | ||
export const UnorderedCollection = createErrorResponse(425) | ||
export const UpgradeRequired = createErrorResponse(426) | ||
export const PreconditionRequired = createErrorResponse(428) | ||
export const TooManyRequests = createErrorResponse(429) | ||
export const RequestHeaderFieldsTooLarge = createErrorResponse(431) | ||
export const UnavailableForLegalReasons = createErrorResponse(451) | ||
// 5xx | ||
export const InternalServerError = createErrorResponse(500) | ||
export const NotImplemented = createErrorResponse(501) | ||
export const BadGateway = createErrorResponse(502) | ||
export const ServiceUnavailable = createErrorResponse(503) | ||
export const GatewayTimeout = createErrorResponse(504) | ||
export const HTTPVersionNotSupported = createErrorResponse(505) | ||
export const VariantAlsoNegotiates = createErrorResponse(506) | ||
export const InsufficientStorage = createErrorResponse(507) | ||
export const LoopDetected = createErrorResponse(508) | ||
export const BandwidthLimitExceeded = createErrorResponse(509) | ||
export const NotExtended = createErrorResponse(510) | ||
export const NetworkAuthenticationRequired = createErrorResponse(511) | ||
/* | ||
Attach all exports to R_ as well | ||
*/ | ||
namespace R_ { | ||
export const Continue = createResponse(100); | ||
export const SwitchingProtocols = createResponse(101); | ||
export const Processing = createResponse(102); | ||
// 2xx | ||
export const OK = createResponse(200); | ||
export const Ok = OK; | ||
export const Created = createResponse(201); | ||
export const Accepted = createResponse(202); | ||
export const NonAuthoritativeInformation = createResponse(203); | ||
export const NoContent = createResponse(204); | ||
export const ResetContent = createResponse(205); | ||
export const PartialContent = createResponse(206); | ||
export const MultiStatus = createResponse(207); | ||
export const AlreadyReported = createResponse(208); | ||
export const IMUsed = createResponse(226); | ||
// 3xx | ||
export const MultipleChoices = createResponse(300); | ||
export const MovedPermanently = createResponse(301); | ||
export const Found = createResponse(302); | ||
export const SeeOther = createResponse(303); | ||
export const NotModified = createResponse(304); | ||
export const UseProxy = createResponse(305); | ||
export const TemporaryRedirect = createResponse(307); | ||
export const PermanentRedirect = createResponse(308); | ||
// 4xx | ||
export const BadRequest = createErrorResponse(400) | ||
export const Unauthorized = createErrorResponse(401) | ||
export const PaymentRequired = createErrorResponse(402) | ||
export const Forbidden = createErrorResponse(403) | ||
export const NotFound = createErrorResponse(404) | ||
export const MethodNotAllowed = createErrorResponse(405) | ||
export const NotAcceptable = createErrorResponse(406) | ||
export const ProxyAuthenticationRequired = createErrorResponse(407) | ||
export const RequestTimeout = createErrorResponse(408) | ||
export const Conflict = createErrorResponse(409) | ||
export const Gone = createErrorResponse(410) | ||
export const LengthRequired = createErrorResponse(411) | ||
export const PreconditionFailed = createErrorResponse(412) | ||
export const PayloadTooLarge = createErrorResponse(413) | ||
export const URITooLong = createErrorResponse(414) | ||
export const UnsupportedMediaType = createErrorResponse(415) | ||
export const RangeNotSatisfiable = createErrorResponse(416) | ||
export const ExpectationFailed = createErrorResponse(417) | ||
export const MisdirectedRequest = createErrorResponse(421) | ||
export const UnprocessableEntity = createErrorResponse(422) | ||
export const Locked = createErrorResponse(423) | ||
export const FailedDependency = createErrorResponse(424) | ||
export const UnorderedCollection = createErrorResponse(425) | ||
export const UpgradeRequired = createErrorResponse(426) | ||
export const PreconditionRequired = createErrorResponse(428) | ||
export const TooManyRequests = createErrorResponse(429) | ||
export const RequestHeaderFieldsTooLarge = createErrorResponse(431) | ||
export const UnavailableForLegalReasons = createErrorResponse(451) | ||
// 5xx | ||
export const InternalServerError = createErrorResponse(500) | ||
export const NotImplemented = createErrorResponse(501) | ||
export const BadGateway = createErrorResponse(502) | ||
export const ServiceUnavailable = createErrorResponse(503) | ||
export const GatewayTimeout = createErrorResponse(504) | ||
export const HTTPVersionNotSupported = createErrorResponse(505) | ||
export const VariantAlsoNegotiates = createErrorResponse(506) | ||
export const InsufficientStorage = createErrorResponse(507) | ||
export const LoopDetected = createErrorResponse(508) | ||
export const BandwidthLimitExceeded = createErrorResponse(509) | ||
export const NotExtended = createErrorResponse(510) | ||
export const NetworkAuthenticationRequired = createErrorResponse(511) | ||
} | ||
function _decorate (resp: ResponseObject, code: number, body?: any, headers?: object) { | ||
@@ -65,6 +226,2 @@ resp.status = resp.statusCode = code; | ||
} | ||
type RConstructor = (body?: any, headers?: object) => ResponseObject | ||
type RErrorConstructor = (body?: any, headers?: object) => ErrorResponseObject | ||
function toJSON (this: ResponseObject) { | ||
@@ -86,84 +243,2 @@ return { body: this.body, status: this.status, headers: this.headers }; | ||
function R_ (code: number, body?: any, headers?: any): ResponseObject { | ||
const name = getName(code); | ||
const responseCtor = code >= 400 ? | ||
createErrorResponse(code, name) : | ||
createResponse(code, name); | ||
return responseCtor(body, headers); | ||
} | ||
// 1xx | ||
export const Continue = createResponse(100, "Continue"); | ||
export const SwitchingProtocols = createResponse(101, "SwitchingProtocols"); | ||
export const Processing = createResponse(102, "Processing"); | ||
// 2xx | ||
export const OK = createResponse(200, "OK"); | ||
export const Ok = OK; | ||
export const Created = createResponse(201, "Created"); | ||
export const Accepted = createResponse(202, "Accepted"); | ||
export const NonAuthoritativeInformation = createResponse(203, "NonAuthoritativeInformation"); | ||
export const NoContent = createResponse(204, "NoContent"); | ||
export const ResetContent = createResponse(205, "ResetContent"); | ||
export const PartialContent = createResponse(206, "PartialContent"); | ||
export const MultiStatus = createResponse(207, "MultiStatus"); | ||
export const AlreadyReported = createResponse(208, "AlreadyReported"); | ||
export const IMUsed = createResponse(226, "IMUsed"); | ||
// 3xx | ||
export const MultipleChoices = createResponse(300, "MultipleChoices"); | ||
export const MovedPermanently = createResponse(301, "MovedPermanently"); | ||
export const Found = createResponse(302, "Found"); | ||
export const SeeOther = createResponse(303, "SeeOther"); | ||
export const NotModified = createResponse(304, "NotModified"); | ||
export const UseProxy = createResponse(305, "UseProxy"); | ||
export const TemporaryRedirect = createResponse(307, "TemporaryRedirect"); | ||
export const PermanentRedirect = createResponse(308, "PermanentRedirect"); | ||
// 4xx | ||
export const BadRequest = createErrorResponse(400, "BadRequest") | ||
export const Unauthorized = createErrorResponse(401, "Unauthorized") | ||
export const PaymentRequired = createErrorResponse(402, "PaymentRequired") | ||
export const Forbidden = createErrorResponse(403, "Forbidden") | ||
export const NotFound = createErrorResponse(404, "NotFound") | ||
export const MethodNotAllowed = createErrorResponse(405, "MethodNotAllowed") | ||
export const NotAcceptable = createErrorResponse(406, "NotAcceptable") | ||
export const ProxyAuthenticationRequired = createErrorResponse(407, "ProxyAuthenticationRequired") | ||
export const RequestTimeout = createErrorResponse(408, "RequestTimeout") | ||
export const Conflict = createErrorResponse(409, "Conflict") | ||
export const Gone = createErrorResponse(410, "Gone") | ||
export const LengthRequired = createErrorResponse(411, "LengthRequired") | ||
export const PreconditionFailed = createErrorResponse(412, "PreconditionFailed") | ||
export const PayloadTooLarge = createErrorResponse(413, "PayloadTooLarge") | ||
export const URITooLong = createErrorResponse(414, "URITooLong") | ||
export const UnsupportedMediaType = createErrorResponse(415, "UnsupportedMediaType") | ||
export const RangeNotSatisfiable = createErrorResponse(416, "RangeNotSatisfiable") | ||
export const ExpectationFailed = createErrorResponse(417, "ExpectationFailed") | ||
export const MisdirectedRequest = createErrorResponse(421, "MisdirectedRequest") | ||
export const UnprocessableEntity = createErrorResponse(422, "UnprocessableEntity") | ||
export const Locked = createErrorResponse(423, "Locked") | ||
export const FailedDependency = createErrorResponse(424, "FailedDependency") | ||
export const UnorderedCollection = createErrorResponse(425, "UnorderedCollection") | ||
export const UpgradeRequired = createErrorResponse(426, "UpgradeRequired") | ||
export const PreconditionRequired = createErrorResponse(428, "PreconditionRequired") | ||
export const TooManyRequests = createErrorResponse(429, "TooManyRequests") | ||
export const RequestHeaderFieldsTooLarge = createErrorResponse(431, "RequestHeaderFieldsTooLarge") | ||
export const UnavailableForLegalReasons = createErrorResponse(451, "UnavailableForLegalReasons") | ||
// 5xx | ||
export const InternalServerError = createErrorResponse(500, "InternalServerError") | ||
export const NotImplemented = createErrorResponse(501, "NotImplemented") | ||
export const BadGateway = createErrorResponse(502, "BadGateway") | ||
export const ServiceUnavailable = createErrorResponse(503, "ServiceUnavailable") | ||
export const GatewayTimeout = createErrorResponse(504, "GatewayTimeout") | ||
export const HTTPVersionNotSupported = createErrorResponse(505, "HTTPVersionNotSupported") | ||
export const VariantAlsoNegotiates = createErrorResponse(506, "VariantAlsoNegotiates") | ||
export const InsufficientStorage = createErrorResponse(507, "InsufficientStorage") | ||
export const LoopDetected = createErrorResponse(508, "LoopDetected") | ||
export const BandwidthLimitExceeded = createErrorResponse(509, "BandwidthLimitExceeded") | ||
export const NotExtended = createErrorResponse(510, "NotExtended") | ||
export const NetworkAuthenticationRequired = createErrorResponse(511, "NetworkAuthenticationRequired") | ||
// export shenanegans | ||
export default R_; | ||
module.exports = exports | ||
module.exports = exports; |
@@ -29,5 +29,10 @@ const expect = require("expect"); | ||
it(`R(${statusCode}) works`, function () { | ||
const r2 = R(statusCode, "body"); | ||
expect(resp).toEqual(r2); | ||
}); | ||
// NOTE: having R(status, body, headers) means R[status](body, headers) isn't needed any more | ||
// it(`Responses[${statusCode}] === Responses.${Name}`, function () { | ||
// it(`Responses[${status}] === Responses.${Name}`, function () { | ||
// expect(R[statusCode]).toBe(R[Name]); | ||
@@ -94,2 +99,6 @@ // }); | ||
describe("miscellaneous", function () { | ||
it("R is a function", function () { | ||
expect(R).toBeA("function"); | ||
}); | ||
it("R.MARKER is a symbol", function () { | ||
@@ -119,19 +128,4 @@ expect(R.MARKER).toBeA("symbol"); | ||
// rough measurements: | ||
// 1.5s with Object.create() constructors | ||
// 2.2s with `new` constructors | ||
// 4.8s with `new` constructors + failed `this instanceof` checks | ||
xdescribe("create 1e7 instances", function () { | ||
it("is fast enough", function () { | ||
const body = {}, headers = {}; | ||
let i = 0; | ||
while (++i < 1e7) { | ||
R.Ok(body, headers); | ||
} | ||
}); | ||
}); | ||
function resetBodyCreator () { | ||
R.setBodyCreator((code, body) => body != null ? body : status[code]); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
69719
687