hono-ban
Version:
HTTP-friendly error objects for Hono, inspired by Boom
2 lines (1 loc) • 4.79 kB
JavaScript
var N={400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Too Early",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",510:"Not Extended",511:"Network Authentication Required"};import{z as B}from"@hono/zod-openapi";var J=B.object({name:B.string().openapi({example:"username",description:"The field name that failed validation"}),reason:B.string().openapi({example:"String must contain at least 3 character(s)",description:"The reason for validation failure"})}),Q=B.object({name:B.string().openapi({example:"email",description:"The field name that violated a constraint"}),reason:B.string().openapi({example:"Email already exists",description:"The reason for the constraint violation"}),resource:B.string().openapi({example:"user",description:"The resource type that contains the constraint"}),constraint:B.string().openapi({example:"unique",description:"The type of constraint that was violated"})}),$=B.object({"invalid-params":B.array(J).optional(),violations:B.array(Q).optional()}),K=B.object({type:B.string().url().openapi({example:"https://api.example.com/problems/validation-error",description:"A URI reference that identifies the problem type"}),title:B.string().openapi({example:"Validation Failed",description:"A short, human-readable summary of the problem type"}),status:B.number().int().min(400).max(599).openapi({example:400,description:"The HTTP status code"}),detail:B.string().optional().openapi({example:"The request contains invalid fields",description:"A human-readable explanation specific to this occurrence of the problem"}),instance:B.string().url().optional().openapi({example:"urn:uuid:6b56944d-5e89-4b4d-9ca7-c1be3d1f0e3f",description:"A URI reference that identifies the specific occurrence of the problem"}),timestamp:B.string().datetime().optional().openapi({example:"2025-02-26T12:34:56.789Z",description:"When the error occurred"})}).merge($);function M(x={}){let j=x.baseUrl||"https://api.example.com/problems";return{contentType:"application/problem+json",format(H){let G={type:`${j}/${H.status}`,title:N[H.status]||"Unknown Error",status:H.status,detail:H.message,instance:`urn:uuid:${crypto.randomUUID()}`,timestamp:new Date().toISOString()};if(H.data?.["invalid-params"])return K.parse({...G,"invalid-params":H.data["invalid-params"]});if(H.data?.violations)return K.parse({...G,violations:H.data.violations});return K.parse(G)}}}function V(x){return{"invalid-params":J.array().parse(x)}}function Y(x){return{"invalid-params":J.array().parse(x.errors.map((j)=>({name:j.path.join("."),reason:j.message})))}}function Z(x,j,H,G="unique"){return{violations:Q.array().parse([{name:x,reason:j,resource:H,constraint:G}])}}import{ZodError as q}from"zod";function k(x){if(typeof x!=="number"||isNaN(x))return 500;if(x<400||x>=600)return 500;return x}function X(x){let j=k(x.statusCode??500),H={status:j,message:x.message??N[j],isBan:!0};if(x.data!==void 0)H.data=x.data;if(x.headers)H.headers={...x.headers};if(x.allow)H.allow=Array.isArray(x.allow)?[...x.allow]:[x.allow];if(x.cause){if(H.cause=x.cause,x.cause instanceof Error)H.causeStack=x.cause.stack}if(x.formatter)H.formatter=x.formatter;if(x.sanitize)H.sanitize=[...x.sanitize];if(x.includeStackTrace!==void 0)H.includeStackTrace=x.includeStackTrace;return Error.captureStackTrace(H,X),H}function _(x,j,H){if(typeof j==="string")return X({...H,statusCode:x,message:j});return X({...j,...H,statusCode:x})}function I(x,j){return _(400,x,j)}function L(x){return(j,H)=>{if(!j.success&&"error"in j&&j.error instanceof q){let G=Y(j.error);throw I({message:x?.message||"Validation Error",data:G,formatter:x?.formatter,headers:x?.headers,sanitize:x?.sanitize,includeStackTrace:x?.includeStackTrace})}}}var y=L();export{y as rfc7807Hook,Y as createRFC7807ZodValidationError,V as createRFC7807ValidationError,L as createRFC7807Hook,M as createRFC7807Formatter,Z as createRFC7807ConstraintViolation,J as RFC7807ValidationParamSchema,$ as RFC7807ErrorDataSchema,K as RFC7807DetailsSchema,Q as RFC7807ConstraintViolationSchema};