UNPKG

workery

Version:

Modern web framework for building APIs on Cloudflare Workers.

2 lines (1 loc) 2.97 kB
import{z as e}from"zod";import{extendZodWithOpenApi as o}from"@asteasolutions/zod-to-openapi";import{isJsonCoercible as s,jsonCoerce as r}from"./helpers.js";function t(o=e.string(),t){return{location:"path",schema:o,options:{preprocessor:s(o)?r:void 0,...t}}}function n(o=e.string(),t){return{location:"query",schema:o,options:{preprocessor:s(o)?r:void 0,...t}}}function a(o=e.string(),t){return{location:"header",schema:o,options:{preprocessor:s(o)?r:void 0,...t}}}function i(o=e.string(),t){return{location:"cookie",schema:o,options:{preprocessor:s(o)?r:void 0,...t}}}function c(o=String,s){return{location:"body",schema:o instanceof e.ZodType?o:e.any(),schemaOr:o instanceof e.ZodType?void 0:o,options:{mediaType:"application/json",...s}}}function p(e){return{location:"$depends",dependency:e,options:{includeInSchema:!0}}}async function d(o,s,r){const{req:t}=s.baseArgs,{params:n,queries:a,cookies:i}=s.rawParameters??{};r=r??new WeakMap;let c=!0;const p={},u=[],m={path:(e,o)=>{const s=o;let r=(n??{})[s.options.altName??e];return s.schema.safeParse(void 0!==r&&s.options.preprocessor?s.options.preprocessor(r):r)},query:(o,s)=>{const r=s;let t=(a??{})[r.options.altName??o]??[];return r.schema instanceof e.ZodArray||(t=t[0]),r.schema.safeParse(r.options.preprocessor?r.options.preprocessor(t):t)},header:(e,o)=>{const s=o;let r=t.headers.get(s.options.altName??e.replace(/_/g,"-"))??void 0;return s.schema.safeParse(void 0!==r&&s.options.preprocessor?s.options.preprocessor(r):r)},cookie:(e,o)=>{const s=o;let r=(i??{})[s.options.altName??e];return s.schema.safeParse(void 0!==r&&s.options.preprocessor?s.options.preprocessor(r):r)},body:async(o,s)=>{const r=s;if(r.schemaOr){let e;return e=r.schemaOr===String?await t.text():r.schemaOr===Blob?await t.blob():t.body,{success:!0,data:e}}{let o;try{o=await t.json()}catch(o){return{success:!1,error:new e.ZodError([{message:"Invalid JSON.",path:[],code:e.ZodIssueCode.custom}])}}return r.schema.safeParse(o)}}};for(const[e,t]of Object.entries(o)){let o;if("$depends"==t.location){const o=t.dependency;if(o.useCache&&void 0!==r.get(o)){p[e]=r.get(o);continue}const n=await d(o.parameters,s,r);c&&=n.success,n.success?(p[e]=await o.handle({...s.baseArgs,...n.args},s.later),o.useCache&&r.set(o,p[e])):u.push(...n.errors)}else o=await m[t.location](e,t),c&&=o.success,o.success?p[e]=o.data:u.push({location:t.location,name:e,issues:o.error.issues})}return{success:c,errors:u,args:p}}function u(o=String,s){return o instanceof e.ZodType?{description:s?.description??o._def.openapi?.metadata?.description??"",headers:e.object(s?.headers??{}),content:{[s?.mediaType??"application/json"]:{schema:o}}}:{description:s?.description??"",headers:e.object(s?.headers??{}),content:{[s?.mediaType??"application/json"]:{schema:o===String?{type:"string"}:{type:"string",format:"binary"}}}}}void 0===e.string().openapi&&o(e);export{c as Body,i as Cookie,p as Depends,a as Header,t as Path,n as Query,u as Responds,d as parseArgs};