vector-framework 0.9.7 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- var fi=Object.defineProperty;var ai=(i,l)=>{for(var f in l)fi(i,f,{get:l[f],enumerable:!0,configurable:!0,set:(a)=>l[f]=()=>a})};var R=(i,l)=>()=>(i&&(l=i(i=0)),l);var b=(i="text/plain; charset=utf-8",l)=>(f,a={})=>{if(f===void 0||f instanceof Response)return f;let E=new Response(l?.(f)??f,a.url?void 0:a);return E.headers.set("content-type",i),E},bi,hi,Si,xi,Gi,Fi,k=async(i)=>{i.content=i.body?await i.clone().json().catch(()=>i.clone().formData()).catch(()=>i.text()):void 0},u=(i)=>{i.cookies=(i.headers.get("Cookie")||"").split(/;\s*/).map((l)=>l.split(/=(.+)/)).reduce((l,[f,a])=>a?(l[f]=a,l):l,{})},F=(i={})=>{let{origin:l="*",credentials:f=!1,allowMethods:a="*",allowHeaders:E,exposeHeaders:A,maxAge:d}=i,_=(I)=>{let O=I?.headers.get("origin");return l===!0?O:l instanceof RegExp?l.test(O)?O:void 0:Array.isArray(l)?l.includes(O)?O:void 0:l instanceof Function?l(O):l=="*"&&f?O:l},N=(I,O)=>{for(let[w,L]of Object.entries(O))L&&I.headers.append(w,L);return I};return{corsify:(I,O)=>I?.headers?.get("access-control-allow-origin")||I.status==101?I:N(I.clone(),{"access-control-allow-origin":_(O),"access-control-allow-credentials":f}),preflight:(I)=>{if(I.method=="OPTIONS"){let O=new Response(null,{status:204});return N(O,{"access-control-allow-origin":_(I),"access-control-allow-methods":a?.join?.(",")??a,"access-control-expose-headers":A?.join?.(",")??A,"access-control-allow-headers":E?.join?.(",")??E??I.headers.get("access-control-request-headers"),"access-control-max-age":d,"access-control-allow-credentials":f})}}}};var Y=R(()=>{bi=b("application/json; charset=utf-8",JSON.stringify),hi=b("text/plain; charset=utf-8",String),Si=b("text/html"),xi=b("image/jpeg"),Gi=b("image/png"),Fi=b("image/webp")});var C,Q,j;var V=R(()=>{C={OK:200,CREATED:201,ACCEPTED:202,NON_AUTHORITATIVE_INFORMATION:203,NO_CONTENT:204,RESET_CONTENT:205,PARTIAL_CONTENT:206,MULTI_STATUS:207,ALREADY_REPORTED:208,IM_USED:226,MULTIPLE_CHOICES:300,MOVED_PERMANENTLY:301,FOUND:302,SEE_OTHER:303,NOT_MODIFIED:304,USE_PROXY:305,TEMPORARY_REDIRECT:307,PERMANENT_REDIRECT:308,BAD_REQUEST:400,UNAUTHORIZED:401,PAYMENT_REQUIRED:402,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_ALLOWED:405,NOT_ACCEPTABLE:406,PROXY_AUTHENTICATION_REQUIRED:407,REQUEST_TIMEOUT:408,CONFLICT:409,GONE:410,LENGTH_REQUIRED:411,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,URI_TOO_LONG:414,UNSUPPORTED_MEDIA_TYPE:415,RANGE_NOT_SATISFIABLE:416,EXPECTATION_FAILED:417,IM_A_TEAPOT:418,MISDIRECTED_REQUEST:421,UNPROCESSABLE_ENTITY:422,LOCKED:423,FAILED_DEPENDENCY:424,TOO_EARLY:425,UPGRADE_REQUIRED:426,PRECONDITION_REQUIRED:428,TOO_MANY_REQUESTS:429,REQUEST_HEADER_FIELDS_TOO_LARGE:431,UNAVAILABLE_FOR_LEGAL_REASONS:451,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504,HTTP_VERSION_NOT_SUPPORTED:505,VARIANT_ALSO_NEGOTIATES:506,INSUFFICIENT_STORAGE:507,LOOP_DETECTED:508,NOT_EXTENDED:510,NETWORK_AUTHENTICATION_REQUIRED:511},Q={PORT:3000,HOSTNAME:"localhost",ROUTES_DIR:"./routes",CACHE_TTL:0,CORS_MAX_AGE:86400},j={JSON:"application/json",TEXT:"text/plain",HTML:"text/html",FORM_URLENCODED:"application/x-www-form-urlencoded",MULTIPART:"multipart/form-data"}});class X{protectedHandler=null;setProtectedHandler(i){this.protectedHandler=i}async authenticate(i){if(!this.protectedHandler)throw new Error("Protected handler not configured. Use vector.protected() to set authentication handler.");try{let l=await this.protectedHandler(i);return i.authUser=l,l}catch(l){throw new Error(`Authentication failed: ${l instanceof Error?l.message:String(l)}`)}}isAuthenticated(i){return!!i.authUser}getUser(i){return i.authUser||null}}class J{cacheHandler=null;memoryCache=new Map;cleanupInterval=null;setCacheHandler(i){this.cacheHandler=i}async get(i,l,f=Q.CACHE_TTL){if(f<=0)return l();if(this.cacheHandler)return this.cacheHandler(i,l,f);return this.getFromMemoryCache(i,l,f)}async getFromMemoryCache(i,l,f){let a=Date.now(),E=this.memoryCache.get(i);if(this.isCacheValid(E,a))return E.value;let A=await l();return this.setInMemoryCache(i,A,f),A}isCacheValid(i,l){return i!==void 0&&i.expires>l}setInMemoryCache(i,l,f){let a=Date.now()+f*1000;this.memoryCache.set(i,{value:l,expires:a}),this.scheduleCleanup()}scheduleCleanup(){if(this.cleanupInterval)return;this.cleanupInterval=setInterval(()=>{this.cleanupExpired()},60000)}cleanupExpired(){let i=Date.now();for(let[l,f]of this.memoryCache.entries())if(f.expires<=i)this.memoryCache.delete(l);if(this.memoryCache.size===0&&this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}clear(){if(this.memoryCache.clear(),this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}async set(i,l,f=Q.CACHE_TTL){if(f<=0)return;if(this.cacheHandler){await this.cacheHandler(i,async()=>l,f);return}this.setInMemoryCache(i,l,f)}delete(i){return this.memoryCache.delete(i)}has(i){let l=this.memoryCache.get(i);if(!l)return!1;if(l.expires<=Date.now())return this.memoryCache.delete(i),!1;return!0}generateKey(i,l){let f=new URL(i.url);return[i.method,f.pathname,f.search,l?.authUser?.id||"anonymous"].join(":")}}var m=R(()=>{V()});function U(i){if(typeof i!=="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(i))}function n(i,l){var f="",a=0,E=-1,A=0,d;for(var _=0;_<=i.length;++_){if(_<i.length)d=i.charCodeAt(_);else if(d===47)break;else d=47;if(d===47){if(E===_-1||A===1);else if(E!==_-1&&A===2){if(f.length<2||a!==2||f.charCodeAt(f.length-1)!==46||f.charCodeAt(f.length-2)!==46){if(f.length>2){var N=f.lastIndexOf("/");if(N!==f.length-1){if(N===-1)f="",a=0;else f=f.slice(0,N),a=f.length-1-f.lastIndexOf("/");E=_,A=0;continue}}else if(f.length===2||f.length===1){f="",a=0,E=_,A=0;continue}}if(l){if(f.length>0)f+="/..";else f="..";a=2}}else{if(f.length>0)f+="/"+i.slice(E+1,_);else f=i.slice(E+1,_);a=_-E-1}E=_,A=0}else if(d===46&&A!==-1)++A;else A=-1}return f}function Ai(i,l){var f=l.dir||l.root,a=l.base||(l.name||"")+(l.ext||"");if(!f)return a;if(f===l.root)return f+a;return f+i+a}function x(){var i="",l=!1,f;for(var a=arguments.length-1;a>=-1&&!l;a--){var E;if(a>=0)E=arguments[a];else{if(f===void 0)f=process.cwd();E=f}if(U(E),E.length===0)continue;i=E+"/"+i,l=E.charCodeAt(0)===47}if(i=n(i,!l),l)if(i.length>0)return"/"+i;else return"/";else if(i.length>0)return i;else return"."}function s(i){if(U(i),i.length===0)return".";var l=i.charCodeAt(0)===47,f=i.charCodeAt(i.length-1)===47;if(i=n(i,!l),i.length===0&&!l)i=".";if(i.length>0&&f)i+="/";if(l)return"/"+i;return i}function Ei(i){return U(i),i.length>0&&i.charCodeAt(0)===47}function W(){if(arguments.length===0)return".";var i;for(var l=0;l<arguments.length;++l){var f=arguments[l];if(U(f),f.length>0)if(i===void 0)i=f;else i+="/"+f}if(i===void 0)return".";return s(i)}function G(i,l){if(U(i),U(l),i===l)return"";if(i=x(i),l=x(l),i===l)return"";var f=1;for(;f<i.length;++f)if(i.charCodeAt(f)!==47)break;var a=i.length,E=a-f,A=1;for(;A<l.length;++A)if(l.charCodeAt(A)!==47)break;var d=l.length,_=d-A,N=E<_?E:_,I=-1,O=0;for(;O<=N;++O){if(O===N){if(_>N){if(l.charCodeAt(A+O)===47)return l.slice(A+O+1);else if(O===0)return l.slice(A+O)}else if(E>N){if(i.charCodeAt(f+O)===47)I=O;else if(O===0)I=0}break}var w=i.charCodeAt(f+O),L=l.charCodeAt(A+O);if(w!==L)break;else if(w===47)I=O}var P="";for(O=f+I+1;O<=a;++O)if(O===a||i.charCodeAt(O)===47)if(P.length===0)P+="..";else P+="/..";if(P.length>0)return P+l.slice(A+I);else{if(A+=I,l.charCodeAt(A)===47)++A;return l.slice(A)}}function _i(i){return i}function M(i){if(U(i),i.length===0)return".";var l=i.charCodeAt(0),f=l===47,a=-1,E=!0;for(var A=i.length-1;A>=1;--A)if(l=i.charCodeAt(A),l===47){if(!E){a=A;break}}else E=!1;if(a===-1)return f?"/":".";if(f&&a===1)return"//";return i.slice(0,a)}function di(i,l){if(l!==void 0&&typeof l!=="string")throw new TypeError('"ext" argument must be a string');U(i);var f=0,a=-1,E=!0,A;if(l!==void 0&&l.length>0&&l.length<=i.length){if(l.length===i.length&&l===i)return"";var d=l.length-1,_=-1;for(A=i.length-1;A>=0;--A){var N=i.charCodeAt(A);if(N===47){if(!E){f=A+1;break}}else{if(_===-1)E=!1,_=A+1;if(d>=0)if(N===l.charCodeAt(d)){if(--d===-1)a=A}else d=-1,a=_}}if(f===a)a=_;else if(a===-1)a=i.length;return i.slice(f,a)}else{for(A=i.length-1;A>=0;--A)if(i.charCodeAt(A)===47){if(!E){f=A+1;break}}else if(a===-1)E=!1,a=A+1;if(a===-1)return"";return i.slice(f,a)}}function Oi(i){U(i);var l=-1,f=0,a=-1,E=!0,A=0;for(var d=i.length-1;d>=0;--d){var _=i.charCodeAt(d);if(_===47){if(!E){f=d+1;break}continue}if(a===-1)E=!1,a=d+1;if(_===46){if(l===-1)l=d;else if(A!==1)A=1}else if(l!==-1)A=-1}if(l===-1||a===-1||A===0||A===1&&l===a-1&&l===f+1)return"";return i.slice(l,a)}function Ni(i){if(i===null||typeof i!=="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof i);return Ai("/",i)}function Di(i){U(i);var l={root:"",dir:"",base:"",ext:"",name:""};if(i.length===0)return l;var f=i.charCodeAt(0),a=f===47,E;if(a)l.root="/",E=1;else E=0;var A=-1,d=0,_=-1,N=!0,I=i.length-1,O=0;for(;I>=E;--I){if(f=i.charCodeAt(I),f===47){if(!N){d=I+1;break}continue}if(_===-1)N=!1,_=I+1;if(f===46){if(A===-1)A=I;else if(O!==1)O=1}else if(A!==-1)O=-1}if(A===-1||_===-1||O===0||O===1&&A===_-1&&A===d+1){if(_!==-1)if(d===0&&a)l.base=l.name=i.slice(1,_);else l.base=l.name=i.slice(d,_)}else{if(d===0&&a)l.name=i.slice(1,A),l.base=i.slice(1,_);else l.name=i.slice(d,A),l.base=i.slice(d,_);l.ext=i.slice(A,_)}if(d>0)l.dir=i.slice(0,d-1);else if(a)l.dir="/";return l}var z="/",Ii=":",Qi;var K=R(()=>{Qi=((i)=>(i.posix=i,i))({resolve:x,normalize:s,isAbsolute:Ei,join:W,relative:G,_makeLong:_i,dirname:M,basename:di,extname:Oi,format:Ni,parse:Di,sep:z,delimiter:Ii,win32:null,posix:null})});var{promises:r}=(()=>({}));class Z{outputPath;constructor(i="./.vector/routes.generated.ts"){this.outputPath=i}async generate(i){let l=M(this.outputPath);await r.mkdir(l,{recursive:!0});let f=[],a=new Map;for(let _ of i){if(!a.has(_.path))a.set(_.path,[]);a.get(_.path).push(_)}let E=0,A=[];for(let[_,N]of a){let I=G(M(this.outputPath),_).replace(/\\/g,"/").replace(/\.(ts|js)$/,""),O=`route_${E++}`,w=N.filter((L)=>L.name!=="default").map((L)=>L.name);if(N.some((L)=>L.name==="default"))if(w.length>0)f.push(`import ${O}, { ${w.join(", ")} } from '${I}';`);else f.push(`import ${O} from '${I}';`);else if(w.length>0)f.push(`import { ${w.join(", ")} } from '${I}';`);for(let L of N){let P=L.name==="default"?O:L.name;A.push(` ${P},`)}}let d=`// This file is auto-generated. Do not edit manually.
1
+ var H=(i="text/plain; charset=utf-8",f)=>(l,a={})=>{if(l===void 0||l instanceof Response)return l;let E=new Response(f?.(l)??l,a.url?void 0:a);return E.headers.set("content-type",i),E},Ai=H("application/json; charset=utf-8",JSON.stringify);var Ei=H("text/plain; charset=utf-8",String),_i=H("text/html"),di=H("image/jpeg"),Oi=H("image/png"),Ni=H("image/webp"),W=async(i)=>{i.content=i.body?await i.clone().json().catch(()=>i.clone().formData()).catch(()=>i.text()):void 0},v=(i)=>{i.cookies=(i.headers.get("Cookie")||"").split(/;\s*/).map((f)=>f.split(/=(.+)/)).reduce((f,[l,a])=>a?(f[l]=a,f):f,{})},x=(i={})=>{let{origin:f="*",credentials:l=!1,allowMethods:a="*",allowHeaders:E,exposeHeaders:A,maxAge:d}=i,_=(O)=>{let N=O?.headers.get("origin");return f===!0?N:f instanceof RegExp?f.test(N)?N:void 0:Array.isArray(f)?f.includes(N)?N:void 0:f instanceof Function?f(N):f=="*"&&l?N:f},I=(O,N)=>{for(let[w,L]of Object.entries(N))L&&O.headers.append(w,L);return O};return{corsify:(O,N)=>O?.headers?.get("access-control-allow-origin")||O.status==101?O:I(O.clone(),{"access-control-allow-origin":_(N),"access-control-allow-credentials":l}),preflight:(O)=>{if(O.method=="OPTIONS"){let N=new Response(null,{status:204});return I(N,{"access-control-allow-origin":_(O),"access-control-allow-methods":a?.join?.(",")??a,"access-control-expose-headers":A?.join?.(",")??A,"access-control-allow-headers":E?.join?.(",")??E??O.headers.get("access-control-request-headers"),"access-control-max-age":d,"access-control-allow-credentials":l})}}}};var R={OK:200,CREATED:201,ACCEPTED:202,NON_AUTHORITATIVE_INFORMATION:203,NO_CONTENT:204,RESET_CONTENT:205,PARTIAL_CONTENT:206,MULTI_STATUS:207,ALREADY_REPORTED:208,IM_USED:226,MULTIPLE_CHOICES:300,MOVED_PERMANENTLY:301,FOUND:302,SEE_OTHER:303,NOT_MODIFIED:304,USE_PROXY:305,TEMPORARY_REDIRECT:307,PERMANENT_REDIRECT:308,BAD_REQUEST:400,UNAUTHORIZED:401,PAYMENT_REQUIRED:402,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_ALLOWED:405,NOT_ACCEPTABLE:406,PROXY_AUTHENTICATION_REQUIRED:407,REQUEST_TIMEOUT:408,CONFLICT:409,GONE:410,LENGTH_REQUIRED:411,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,URI_TOO_LONG:414,UNSUPPORTED_MEDIA_TYPE:415,RANGE_NOT_SATISFIABLE:416,EXPECTATION_FAILED:417,IM_A_TEAPOT:418,MISDIRECTED_REQUEST:421,UNPROCESSABLE_ENTITY:422,LOCKED:423,FAILED_DEPENDENCY:424,TOO_EARLY:425,UPGRADE_REQUIRED:426,PRECONDITION_REQUIRED:428,TOO_MANY_REQUESTS:429,REQUEST_HEADER_FIELDS_TOO_LARGE:431,UNAVAILABLE_FOR_LEGAL_REASONS:451,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504,HTTP_VERSION_NOT_SUPPORTED:505,VARIANT_ALSO_NEGOTIATES:506,INSUFFICIENT_STORAGE:507,LOOP_DETECTED:508,NOT_EXTENDED:510,NETWORK_AUTHENTICATION_REQUIRED:511},M={PORT:3000,HOSTNAME:"localhost",ROUTES_DIR:"./routes",CACHE_TTL:0,CORS_MAX_AGE:86400},G={JSON:"application/json",TEXT:"text/plain",HTML:"text/html",FORM_URLENCODED:"application/x-www-form-urlencoded",MULTIPART:"multipart/form-data"};class T{protectedHandler=null;setProtectedHandler(i){this.protectedHandler=i}async authenticate(i){if(!this.protectedHandler)throw new Error("Protected handler not configured. Use vector.protected() to set authentication handler.");try{let f=await this.protectedHandler(i);return i.authUser=f,f}catch(f){throw new Error(`Authentication failed: ${f instanceof Error?f.message:String(f)}`)}}isAuthenticated(i){return!!i.authUser}getUser(i){return i.authUser||null}}class B{cacheHandler=null;memoryCache=new Map;cleanupInterval=null;setCacheHandler(i){this.cacheHandler=i}async get(i,f,l=M.CACHE_TTL){if(l<=0)return f();if(this.cacheHandler)return this.cacheHandler(i,f,l);return this.getFromMemoryCache(i,f,l)}async getFromMemoryCache(i,f,l){let a=Date.now(),E=this.memoryCache.get(i);if(this.isCacheValid(E,a))return E.value;let A=await f();return this.setInMemoryCache(i,A,l),A}isCacheValid(i,f){return i!==void 0&&i.expires>f}setInMemoryCache(i,f,l){let a=Date.now()+l*1000;this.memoryCache.set(i,{value:f,expires:a}),this.scheduleCleanup()}scheduleCleanup(){if(this.cleanupInterval)return;this.cleanupInterval=setInterval(()=>{this.cleanupExpired()},60000)}cleanupExpired(){let i=Date.now();for(let[f,l]of this.memoryCache.entries())if(l.expires<=i)this.memoryCache.delete(f);if(this.memoryCache.size===0&&this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}clear(){if(this.memoryCache.clear(),this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}async set(i,f,l=M.CACHE_TTL){if(l<=0)return;if(this.cacheHandler){await this.cacheHandler(i,async()=>f,l);return}this.setInMemoryCache(i,f,l)}delete(i){return this.memoryCache.delete(i)}has(i){let f=this.memoryCache.get(i);if(!f)return!1;if(f.expires<=Date.now())return this.memoryCache.delete(i),!1;return!0}generateKey(i,f){let l=new URL(i.url);return[i.method,l.pathname,l.search,f?.authUser?.id||"anonymous"].join(":")}}var{promises:c}=(()=>({}));function C(i){if(typeof i!=="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(i))}function K(i,f){var l="",a=0,E=-1,A=0,d;for(var _=0;_<=i.length;++_){if(_<i.length)d=i.charCodeAt(_);else if(d===47)break;else d=47;if(d===47){if(E===_-1||A===1);else if(E!==_-1&&A===2){if(l.length<2||a!==2||l.charCodeAt(l.length-1)!==46||l.charCodeAt(l.length-2)!==46){if(l.length>2){var I=l.lastIndexOf("/");if(I!==l.length-1){if(I===-1)l="",a=0;else l=l.slice(0,I),a=l.length-1-l.lastIndexOf("/");E=_,A=0;continue}}else if(l.length===2||l.length===1){l="",a=0,E=_,A=0;continue}}if(f){if(l.length>0)l+="/..";else l="..";a=2}}else{if(l.length>0)l+="/"+i.slice(E+1,_);else l=i.slice(E+1,_);a=_-E-1}E=_,A=0}else if(d===46&&A!==-1)++A;else A=-1}return l}function u(i,f){var l=f.dir||f.root,a=f.base||(f.name||"")+(f.ext||"");if(!l)return a;if(l===f.root)return l+a;return l+i+a}function S(){var i="",f=!1,l;for(var a=arguments.length-1;a>=-1&&!f;a--){var E;if(a>=0)E=arguments[a];else{if(l===void 0)l=process.cwd();E=l}if(C(E),E.length===0)continue;i=E+"/"+i,f=E.charCodeAt(0)===47}if(i=K(i,!f),f)if(i.length>0)return"/"+i;else return"/";else if(i.length>0)return i;else return"."}function Z(i){if(C(i),i.length===0)return".";var f=i.charCodeAt(0)===47,l=i.charCodeAt(i.length-1)===47;if(i=K(i,!f),i.length===0&&!f)i=".";if(i.length>0&&l)i+="/";if(f)return"/"+i;return i}function n(i){return C(i),i.length>0&&i.charCodeAt(0)===47}function Y(){if(arguments.length===0)return".";var i;for(var f=0;f<arguments.length;++f){var l=arguments[f];if(C(l),l.length>0)if(i===void 0)i=l;else i+="/"+l}if(i===void 0)return".";return Z(i)}function h(i,f){if(C(i),C(f),i===f)return"";if(i=S(i),f=S(f),i===f)return"";var l=1;for(;l<i.length;++l)if(i.charCodeAt(l)!==47)break;var a=i.length,E=a-l,A=1;for(;A<f.length;++A)if(f.charCodeAt(A)!==47)break;var d=f.length,_=d-A,I=E<_?E:_,O=-1,N=0;for(;N<=I;++N){if(N===I){if(_>I){if(f.charCodeAt(A+N)===47)return f.slice(A+N+1);else if(N===0)return f.slice(A+N)}else if(E>I){if(i.charCodeAt(l+N)===47)O=N;else if(N===0)O=0}break}var w=i.charCodeAt(l+N),L=f.charCodeAt(A+N);if(w!==L)break;else if(w===47)O=N}var $="";for(N=l+O+1;N<=a;++N)if(N===a||i.charCodeAt(N)===47)if($.length===0)$+="..";else $+="/..";if($.length>0)return $+f.slice(A+O);else{if(A+=O,f.charCodeAt(A)===47)++A;return f.slice(A)}}function y(i){return i}function j(i){if(C(i),i.length===0)return".";var f=i.charCodeAt(0),l=f===47,a=-1,E=!0;for(var A=i.length-1;A>=1;--A)if(f=i.charCodeAt(A),f===47){if(!E){a=A;break}}else E=!1;if(a===-1)return l?"/":".";if(l&&a===1)return"//";return i.slice(0,a)}function s(i,f){if(f!==void 0&&typeof f!=="string")throw new TypeError('"ext" argument must be a string');C(i);var l=0,a=-1,E=!0,A;if(f!==void 0&&f.length>0&&f.length<=i.length){if(f.length===i.length&&f===i)return"";var d=f.length-1,_=-1;for(A=i.length-1;A>=0;--A){var I=i.charCodeAt(A);if(I===47){if(!E){l=A+1;break}}else{if(_===-1)E=!1,_=A+1;if(d>=0)if(I===f.charCodeAt(d)){if(--d===-1)a=A}else d=-1,a=_}}if(l===a)a=_;else if(a===-1)a=i.length;return i.slice(l,a)}else{for(A=i.length-1;A>=0;--A)if(i.charCodeAt(A)===47){if(!E){l=A+1;break}}else if(a===-1)E=!1,a=A+1;if(a===-1)return"";return i.slice(l,a)}}function q(i){C(i);var f=-1,l=0,a=-1,E=!0,A=0;for(var d=i.length-1;d>=0;--d){var _=i.charCodeAt(d);if(_===47){if(!E){l=d+1;break}continue}if(a===-1)E=!1,a=d+1;if(_===46){if(f===-1)f=d;else if(A!==1)A=1}else if(f!==-1)A=-1}if(f===-1||a===-1||A===0||A===1&&f===a-1&&f===l+1)return"";return i.slice(f,a)}function r(i){if(i===null||typeof i!=="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof i);return u("/",i)}function t(i){C(i);var f={root:"",dir:"",base:"",ext:"",name:""};if(i.length===0)return f;var l=i.charCodeAt(0),a=l===47,E;if(a)f.root="/",E=1;else E=0;var A=-1,d=0,_=-1,I=!0,O=i.length-1,N=0;for(;O>=E;--O){if(l=i.charCodeAt(O),l===47){if(!I){d=O+1;break}continue}if(_===-1)I=!1,_=O+1;if(l===46){if(A===-1)A=O;else if(N!==1)N=1}else if(A!==-1)N=-1}if(A===-1||_===-1||N===0||N===1&&A===_-1&&A===d+1){if(_!==-1)if(d===0&&a)f.base=f.name=i.slice(1,_);else f.base=f.name=i.slice(d,_)}else{if(d===0&&a)f.name=i.slice(1,A),f.base=i.slice(1,_);else f.name=i.slice(d,A),f.base=i.slice(d,_);f.ext=i.slice(A,_)}if(d>0)f.dir=i.slice(0,d-1);else if(a)f.dir="/";return f}var Q="/",o=":",Ci=((i)=>(i.posix=i,i))({resolve:S,normalize:Z,isAbsolute:n,join:Y,relative:h,_makeLong:y,dirname:j,basename:s,extname:q,format:r,parse:t,sep:Q,delimiter:o,win32:null,posix:null});class V{outputPath;constructor(i="./.vector/routes.generated.ts"){this.outputPath=i}async generate(i){let f=j(this.outputPath);await c.mkdir(f,{recursive:!0});let l=[],a=new Map;for(let _ of i){if(!a.has(_.path))a.set(_.path,[]);a.get(_.path).push(_)}let E=0,A=[];for(let[_,I]of a){let O=h(j(this.outputPath),_).replace(/\\/g,"/").replace(/\.(ts|js)$/,""),N=`route_${E++}`,w=I.filter((L)=>L.name!=="default").map((L)=>L.name);if(I.some((L)=>L.name==="default"))if(w.length>0)l.push(`import ${N}, { ${w.join(", ")} } from '${O}';`);else l.push(`import ${N} from '${O}';`);else if(w.length>0)l.push(`import { ${w.join(", ")} } from '${O}';`);for(let L of I){let $=L.name==="default"?N:L.name;A.push(` ${$},`)}}let d=`// This file is auto-generated. Do not edit manually.
2
2
  // Generated at: ${new Date().toISOString()}
3
3
 
4
- ${f.join(`
4
+ ${l.join(`
5
5
  `)}
6
6
 
7
7
  export const routes = [
@@ -10,12 +10,12 @@ ${A.join(`
10
10
  ];
11
11
 
12
12
  export default routes;
13
- `;await r.writeFile(this.outputPath,d,"utf-8")}async generateDynamic(i){let l=[];for(let f of i){let a=JSON.stringify({method:f.method,path:f.options.path,options:f.options});l.push(` await import('${f.path}').then(m => ({
13
+ `;await c.writeFile(this.outputPath,d,"utf-8")}async generateDynamic(i){let f=[];for(let l of i){let a=JSON.stringify({method:l.method,path:l.options.path,options:l.options});f.push(` await import('${l.path}').then(m => ({
14
14
  ...${a},
15
- handler: m.${f.name==="default"?"default":f.name}
15
+ handler: m.${l.name==="default"?"default":l.name}
16
16
  }))`)}return`export const loadRoutes = async () => {
17
17
  return Promise.all([
18
- ${l.join(`,
18
+ ${f.join(`,
19
19
  `)}
20
20
  ]);
21
- };`}}var y=R(()=>{K()});var{existsSync:wi,promises:q}=(()=>({}));class c{routesDir;constructor(i="./routes"){this.routesDir=x(process.cwd(),i)}async scan(){let i=[];if(!wi(this.routesDir))return[];try{await this.scanDirectory(this.routesDir,i)}catch(l){if(l.code==="ENOENT")return console.warn(` ✗ Routes directory not accessible: ${this.routesDir}`),[];throw l}return i}async scanDirectory(i,l,f=""){let a=await q.readdir(i);for(let E of a){let A=W(i,E);if((await q.stat(A)).isDirectory()){let _=f?`${f}/${E}`:E;await this.scanDirectory(A,l,_)}else if(E.endsWith(".ts")||E.endsWith(".js")){let _=G(this.routesDir,A).replace(/\.(ts|js)$/,"").split(z).join("/");try{let I=await import(process.platform==="win32"?`file:///${A.replace(/\\/g,"/")}`:A);if(I.default&&typeof I.default==="function")l.push({name:"default",path:A,method:"GET",options:{method:"GET",path:`/${_}`,expose:!0}});for(let[O,w]of Object.entries(I)){if(O==="default")continue;if(w&&typeof w==="object"&&"entry"in w&&"options"in w&&"handler"in w){let L=w;l.push({name:O,path:A,method:L.options.method,options:L.options})}else if(Array.isArray(w)&&w.length>=4){let[L,,,P]=w;l.push({name:O,path:A,method:L,options:{method:L,path:P,expose:!0}})}}}catch(N){console.error(`Failed to load route from ${A}:`,N)}}}}enableWatch(i){if(typeof Bun!=="undefined"&&Bun.env.NODE_ENV==="development")console.log(`Watching for route changes in ${this.routesDir}`),setInterval(async()=>{await i()},1000)}}var t=R(()=>{K()});class T{beforeHandlers=[];finallyHandlers=[];addBefore(...i){this.beforeHandlers.push(...i)}addFinally(...i){this.finallyHandlers.push(...i)}async executeBefore(i){let l=i;for(let f of this.beforeHandlers){let a=await f(l);if(a instanceof Response)return a;l=a}return l}async executeFinally(i,l){let f=i;for(let a of this.finallyHandlers)f=await a(f,l);return f}clone(){let i=new T;return i.beforeHandlers=[...this.beforeHandlers],i.finallyHandlers=[...this.finallyHandlers],i}clear(){this.beforeHandlers=[],this.finallyHandlers=[]}}function o(i){return process.platform==="win32"?`file:///${i.replace(/\\/g,"/")}`:i}class v{middlewareManager;authManager;cacheManager;routes=[];constructor(i,l,f){this.middlewareManager=i,this.authManager=l,this.cacheManager=f}getRouteSpecificity(i){let A=0,d=i.split("/").filter(Boolean);for(let _ of d)if(this.isStaticSegment(_))A+=1000;else if(this.isParamSegment(_))A+=10;else if(this.isWildcardSegment(_))A+=1;if(A+=i.length,this.isExactPath(i))A+=1e4;return A}isStaticSegment(i){return!i.startsWith(":")&&!i.includes("*")}isParamSegment(i){return i.startsWith(":")}isWildcardSegment(i){return i.includes("*")}isExactPath(i){return!i.includes(":")&&!i.includes("*")}sortRoutes(){this.routes.sort((i,l)=>{let f=this.extractPath(i),a=this.extractPath(l),E=this.getRouteSpecificity(f);return this.getRouteSpecificity(a)-E})}extractPath(i){return i[3]||""}route(i,l){let f=this.wrapHandler(i,l),a=[i.method.toUpperCase(),this.createRouteRegex(i.path),[f],i.path];return this.routes.push(a),this.sortRoutes(),a}createRouteRegex(i){return RegExp(`^${i.replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`)}wrapHandler(i,l){return async(f)=>{let a=f;if(!a.context)a.context={};if(!a.query&&a.url){let E=new URL(a.url),A={};for(let[d,_]of E.searchParams)A[d]=A[d]?[].concat(A[d],_):_;a.query=A}if(i.metadata)a.metadata=i.metadata;f=a;try{if(i.expose===!1)return $.forbidden("Forbidden");let E=await this.middlewareManager.executeBefore(f);if(E instanceof Response)return E;if(f=E,i.auth)try{await this.authManager.authenticate(f)}catch(N){return $.unauthorized(N instanceof Error?N.message:"Authentication failed",i.responseContentType)}if(!i.rawRequest&&f.method!=="GET"&&f.method!=="HEAD")try{let N=f.headers.get("content-type");if(N?.includes("application/json"))f.content=await f.json();else if(N?.includes("application/x-www-form-urlencoded"))f.content=Object.fromEntries(await f.formData());else if(N?.includes("multipart/form-data"))f.content=await f.formData();else f.content=await f.text()}catch{f.content=null}let A,d=i.cache;if(d&&typeof d==="number"&&d>0){let N=this.cacheManager.generateKey(f,{authUser:f.authUser});A=await this.cacheManager.get(N,()=>l(f),d)}else if(d&&typeof d==="object"&&d.ttl){let N=d.key||this.cacheManager.generateKey(f,{authUser:f.authUser});A=await this.cacheManager.get(N,()=>l(f),d.ttl)}else A=await l(f);let _;if(i.rawResponse||A instanceof Response)_=A instanceof Response?A:new Response(A);else _=h(200,A,i.responseContentType);return _=await this.middlewareManager.executeFinally(_,f),_}catch(E){if(E instanceof Response)return E;return console.error("Route handler error:",E),$.internalServerError(E instanceof Error?E.message:String(E),i.responseContentType)}}}addRoute(i){this.routes.push(i),this.sortRoutes()}getRoutes(){return this.routes}async handle(i){let f=new URL(i.url).pathname;for(let[a,E,A]of this.routes)if(i.method==="OPTIONS"||i.method===a){let d=f.match(E);if(d){let _=i;if(!_.context)_.context={};_.params=d.groups||{};for(let N of A){let I=await N(_);if(I)return I}}}return $.notFound("Route not found")}clearRoutes(){this.routes=[]}}var p=R(()=>{B()});class g{server=null;router;config;corsHandler;constructor(i,l){if(this.router=i,this.config=l,l.cors){let{preflight:f,corsify:a}=F(this.normalizeCorsOptions(l.cors));this.corsHandler={preflight:f,corsify:a}}}normalizeCorsOptions(i){return{origin:i.origin||"*",credentials:i.credentials!==!1,allowHeaders:Array.isArray(i.allowHeaders)?i.allowHeaders.join(", "):i.allowHeaders||"Content-Type, Authorization",allowMethods:Array.isArray(i.allowMethods)?i.allowMethods.join(", "):i.allowMethods||"GET, POST, PUT, PATCH, DELETE, OPTIONS",exposeHeaders:Array.isArray(i.exposeHeaders)?i.exposeHeaders.join(", "):i.exposeHeaders||"Authorization",maxAge:i.maxAge||86400}}async start(){let i=this.config.port||3000,l=this.config.hostname||"localhost",f=async(a)=>{try{if(this.corsHandler&&a.method==="OPTIONS")return this.corsHandler.preflight(a);let E=await this.router.handle(a);if(this.corsHandler)E=this.corsHandler.corsify(E,a);return E}catch(E){return console.error("Server error:",E),new Response("Internal Server Error",{status:500})}};try{if(this.server=Bun.serve({port:i,hostname:l,reusePort:this.config.reusePort!==!1,fetch:f,idleTimeout:this.config.idleTimeout||60,error:(a)=>{return console.error("[ERROR] Server error:",a),new Response("Internal Server Error",{status:500})}}),!this.server||!this.server.port)throw new Error(`Failed to start server on ${l}:${i} - server object is invalid`);return console.log(`→ Vector server running at http://${l}:${i}`),this.server}catch(a){if(a.code==="EADDRINUSE"||a.message?.includes("address already in use"))a.message=`Port ${i} is already in use`,a.port=i;else if(a.code==="EACCES"||a.message?.includes("permission denied"))a.message=`Permission denied to bind to port ${i}`,a.port=i;else if(a.message?.includes("EADDRNOTAVAIL"))a.message=`Cannot bind to hostname ${l}`,a.hostname=l;throw a}}stop(){if(this.server)this.server.stop(),this.server=null,console.log("Server stopped")}getServer(){return this.server}getPort(){return this.server?.port||this.config.port||3000}getHostname(){return this.server?.hostname||this.config.hostname||"localhost"}getUrl(){let i=this.getPort();return`http://${this.getHostname()}:${i}`}}var e=R(()=>{Y()});var ii={};ai(ii,{getVectorInstance:()=>Li,Vector:()=>H});class H{static instance;router;server=null;middlewareManager;authManager;cacheManager;config={};routeScanner=null;routeGenerator=null;_protectedHandler=null;_cacheHandler=null;constructor(){this.middlewareManager=new T,this.authManager=new X,this.cacheManager=new J,this.router=new v(this.middlewareManager,this.authManager,this.cacheManager)}static getInstance(){if(!H.instance)H.instance=new H;return H.instance}setProtectedHandler(i){this._protectedHandler=i,this.authManager.setProtectedHandler(i)}getProtectedHandler(){return this._protectedHandler}setCacheHandler(i){this._cacheHandler=i,this.cacheManager.setCacheHandler(i)}getCacheHandler(){return this._cacheHandler}addRoute(i,l){return this.router.route(i,l)}async startServer(i){if(this.config={...this.config,...i},this.middlewareManager.clear(),i?.before)this.middlewareManager.addBefore(...i.before);if(i?.finally)this.middlewareManager.addFinally(...i.finally);if(this.config.autoDiscover!==!1)await this.discoverRoutes();return this.server=new g(this.router,this.config),await this.server.start()}async discoverRoutes(){let i=this.config.routesDir||"./routes";if(this.routeScanner=new c(i),!this.routeGenerator)this.routeGenerator=new Z;try{let l=await this.routeScanner.scan();if(l.length>0){if(this.config.development)await this.routeGenerator.generate(l);for(let f of l)try{let E=await import(o(f.path)),A=f.name==="default"?E.default:E[f.name];if(A){if(this.isRouteDefinition(A)){let d=A;this.router.route(d.options,d.handler),this.logRouteLoaded(d.options)}else if(this.isRouteEntry(A))this.router.addRoute(A),this.logRouteLoaded(A);else if(typeof A==="function")this.router.route(f.options,A),this.logRouteLoaded(f.options)}}catch(a){console.error(`Failed to load route ${f.name} from ${f.path}:`,a)}this.router.sortRoutes()}}catch(l){if(l.code!=="ENOENT"&&l.code!=="ENOTDIR")console.error("Failed to discover routes:",l)}}async loadRoute(i){if(typeof i==="function"){let l=i();if(Array.isArray(l))this.router.addRoute(l)}else if(i&&typeof i==="object"){for(let[,l]of Object.entries(i))if(typeof l==="function"){let f=l();if(Array.isArray(f))this.router.addRoute(f)}}}isRouteEntry(i){return Array.isArray(i)&&i.length>=3}isRouteDefinition(i){return i&&typeof i==="object"&&"entry"in i&&"options"in i&&"handler"in i}logRouteLoaded(i){}stop(){if(this.server)this.server.stop(),this.server=null;this.router.clearRoutes()}getServer(){return this.server}getRouter(){return this.router}getCacheManager(){return this.cacheManager}getAuthManager(){return this.authManager}static resetInstance(){H.instance=null}}var Li;var li=R(()=>{m();y();t();p();e();Li=H.getInstance});function Ci(i,l){let f=Pi(i,l);return{entry:[i.method.toUpperCase(),RegExp(`^${i.path.replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),[f],i.path],options:i,handler:l}}function Ri(i){return JSON.stringify(i??null,(l,f)=>typeof f==="bigint"?f.toString():f)}function D(i,l,f){let a={error:!0,message:l,statusCode:i,timestamp:new Date().toISOString()};return h(i,a,f)}function h(i,l,f=j.JSON){let a=f===j.JSON?Ri(l):l;return new Response(a,{status:i,headers:{"content-type":f}})}function Pi(i,l){let{auth:f=!1,expose:a=!1,rawRequest:E=!1,rawResponse:A=!1,responseContentType:d=j.JSON}=i;return async(_)=>{if(!a)return $.forbidden("Forbidden");try{if(f)await $i(_,d);if(!E)await k(_);u(_);let N=await l(_);return A?N:Ui.success(N,d)}catch(N){if(N instanceof Response)return N;return $.internalServerError(String(N),d)}}}var ll,fl,Ui,$,$i=async(i,l)=>{let{getVectorInstance:f}=await Promise.resolve().then(() => (li(),ii)),E=f().getProtectedHandler();if(!E)throw $.unauthorized("Authentication not configured",l);try{let A=await E(i);i.authUser=A}catch(A){throw $.unauthorized(A instanceof Error?A.message:"Authentication failed",l)}};var B=R(()=>{Y();V();({preflight:ll,corsify:fl}=F({origin:"*",credentials:!0,allowHeaders:"Content-Type, Authorization",allowMethods:"GET, POST, PUT, PATCH, DELETE, OPTIONS",exposeHeaders:"Authorization",maxAge:86400}));Ui={success:(i,l)=>h(C.OK,i,l),created:(i,l)=>h(C.CREATED,i,l)};$={badRequest:(i="Bad Request",l)=>D(C.BAD_REQUEST,i,l),unauthorized:(i="Unauthorized",l)=>D(C.UNAUTHORIZED,i,l),paymentRequired:(i="Payment Required",l)=>D(402,i,l),forbidden:(i="Forbidden",l)=>D(C.FORBIDDEN,i,l),notFound:(i="Not Found",l)=>D(C.NOT_FOUND,i,l),methodNotAllowed:(i="Method Not Allowed",l)=>D(405,i,l),notAcceptable:(i="Not Acceptable",l)=>D(406,i,l),requestTimeout:(i="Request Timeout",l)=>D(408,i,l),conflict:(i="Conflict",l)=>D(C.CONFLICT,i,l),gone:(i="Gone",l)=>D(410,i,l),lengthRequired:(i="Length Required",l)=>D(411,i,l),preconditionFailed:(i="Precondition Failed",l)=>D(412,i,l),payloadTooLarge:(i="Payload Too Large",l)=>D(413,i,l),uriTooLong:(i="URI Too Long",l)=>D(414,i,l),unsupportedMediaType:(i="Unsupported Media Type",l)=>D(415,i,l),rangeNotSatisfiable:(i="Range Not Satisfiable",l)=>D(416,i,l),expectationFailed:(i="Expectation Failed",l)=>D(417,i,l),imATeapot:(i="I'm a teapot",l)=>D(418,i,l),misdirectedRequest:(i="Misdirected Request",l)=>D(421,i,l),unprocessableEntity:(i="Unprocessable Entity",l)=>D(C.UNPROCESSABLE_ENTITY,i,l),locked:(i="Locked",l)=>D(423,i,l),failedDependency:(i="Failed Dependency",l)=>D(424,i,l),tooEarly:(i="Too Early",l)=>D(425,i,l),upgradeRequired:(i="Upgrade Required",l)=>D(426,i,l),preconditionRequired:(i="Precondition Required",l)=>D(428,i,l),tooManyRequests:(i="Too Many Requests",l)=>D(429,i,l),requestHeaderFieldsTooLarge:(i="Request Header Fields Too Large",l)=>D(431,i,l),unavailableForLegalReasons:(i="Unavailable For Legal Reasons",l)=>D(451,i,l),internalServerError:(i="Internal Server Error",l)=>D(C.INTERNAL_SERVER_ERROR,i,l),notImplemented:(i="Not Implemented",l)=>D(501,i,l),badGateway:(i="Bad Gateway",l)=>D(502,i,l),serviceUnavailable:(i="Service Unavailable",l)=>D(503,i,l),gatewayTimeout:(i="Gateway Timeout",l)=>D(504,i,l),httpVersionNotSupported:(i="HTTP Version Not Supported",l)=>D(505,i,l),variantAlsoNegotiates:(i="Variant Also Negotiates",l)=>D(506,i,l),insufficientStorage:(i="Insufficient Storage",l)=>D(507,i,l),loopDetected:(i="Loop Detected",l)=>D(508,i,l),notExtended:(i="Not Extended",l)=>D(510,i,l),networkAuthenticationRequired:(i="Network Authentication Required",l)=>D(511,i,l),invalidArgument:(i="Invalid Argument",l)=>D(C.UNPROCESSABLE_ENTITY,i,l),rateLimitExceeded:(i="Rate Limit Exceeded",l)=>D(429,i,l),maintenance:(i="Service Under Maintenance",l)=>D(503,i,l),custom:(i,l,f)=>D(i,l,f)}});B();B();export{Ci as route,h as createResponse,$ as APIError};
21
+ };`}}var{existsSync:p,promises:g}=(()=>({}));class X{routesDir;constructor(i="./routes"){this.routesDir=S(process.cwd(),i)}async scan(){let i=[];if(!p(this.routesDir))return[];try{await this.scanDirectory(this.routesDir,i)}catch(f){if(f.code==="ENOENT")return console.warn(` ✗ Routes directory not accessible: ${this.routesDir}`),[];throw f}return i}async scanDirectory(i,f,l=""){let a=await g.readdir(i);for(let E of a){let A=Y(i,E);if((await g.stat(A)).isDirectory()){let _=l?`${l}/${E}`:E;await this.scanDirectory(A,f,_)}else if(E.endsWith(".ts")||E.endsWith(".js")){let _=h(this.routesDir,A).replace(/\.(ts|js)$/,"").split(Q).join("/");try{let O=await import(process.platform==="win32"?`file:///${A.replace(/\\/g,"/")}`:A);if(O.default&&typeof O.default==="function")f.push({name:"default",path:A,method:"GET",options:{method:"GET",path:`/${_}`,expose:!0}});for(let[N,w]of Object.entries(O)){if(N==="default")continue;if(w&&typeof w==="object"&&"entry"in w&&"options"in w&&"handler"in w){let L=w;f.push({name:N,path:A,method:L.options.method,options:L.options})}else if(Array.isArray(w)&&w.length>=4){let[L,,,$]=w;f.push({name:N,path:A,method:L,options:{method:L,path:$,expose:!0}})}}}catch(I){console.error(`Failed to load route from ${A}:`,I)}}}}enableWatch(i){if(typeof Bun!=="undefined"&&Bun.env.NODE_ENV==="development")console.log(`Watching for route changes in ${this.routesDir}`),setInterval(async()=>{await i()},1000)}}class F{beforeHandlers=[];finallyHandlers=[];addBefore(...i){this.beforeHandlers.push(...i)}addFinally(...i){this.finallyHandlers.push(...i)}async executeBefore(i){let f=i;for(let l of this.beforeHandlers){let a=await l(f);if(a instanceof Response)return a;f=a}return f}async executeFinally(i,f){let l=i;for(let a of this.finallyHandlers)l=await a(l,f);return l}clone(){let i=new F;return i.beforeHandlers=[...this.beforeHandlers],i.finallyHandlers=[...this.finallyHandlers],i}clear(){this.beforeHandlers=[],this.finallyHandlers=[]}}function k(i){return process.platform==="win32"?`file:///${i.replace(/\\/g,"/")}`:i}class z{middlewareManager;authManager;cacheManager;routes=[];constructor(i,f,l){this.middlewareManager=i,this.authManager=f,this.cacheManager=l}getRouteSpecificity(i){let A=0,d=i.split("/").filter(Boolean);for(let _ of d)if(this.isStaticSegment(_))A+=1000;else if(this.isParamSegment(_))A+=10;else if(this.isWildcardSegment(_))A+=1;if(A+=i.length,this.isExactPath(i))A+=1e4;return A}isStaticSegment(i){return!i.startsWith(":")&&!i.includes("*")}isParamSegment(i){return i.startsWith(":")}isWildcardSegment(i){return i.includes("*")}isExactPath(i){return!i.includes(":")&&!i.includes("*")}sortRoutes(){this.routes.sort((i,f)=>{let l=this.extractPath(i),a=this.extractPath(f),E=this.getRouteSpecificity(l);return this.getRouteSpecificity(a)-E})}extractPath(i){return i[3]||""}route(i,f){let l=this.wrapHandler(i,f),a=[i.method.toUpperCase(),this.createRouteRegex(i.path),[l],i.path];return this.routes.push(a),this.sortRoutes(),a}createRouteRegex(i){return RegExp(`^${i.replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`)}prepareRequest(i,f){if(!i.context)i.context={};if(f?.params!==void 0)i.params=f.params;if(f?.route!==void 0)i.route=f.route;if(f?.metadata!==void 0)i.metadata=f.metadata;if(!i.query&&i.url){let l=new URL(i.url),a={};for(let[E,A]of l.searchParams)if(E in a)if(Array.isArray(a[E]))a[E].push(A);else a[E]=[a[E],A];else a[E]=A;i.query=a}if(!i.cookies)v(i)}wrapHandler(i,f){return async(l)=>{let a=l;this.prepareRequest(a,{metadata:i.metadata}),l=a;try{if(i.expose===!1)return U.forbidden("Forbidden");let E=await this.middlewareManager.executeBefore(l);if(E instanceof Response)return E;if(l=E,i.auth)try{await this.authManager.authenticate(l)}catch(O){return U.unauthorized(O instanceof Error?O.message:"Authentication failed",i.responseContentType)}if(!i.rawRequest&&l.method!=="GET"&&l.method!=="HEAD")try{let O=l.headers.get("content-type");if(O?.includes("application/json"))l.content=await l.json();else if(O?.includes("application/x-www-form-urlencoded"))l.content=Object.fromEntries(await l.formData());else if(O?.includes("multipart/form-data"))l.content=await l.formData();else l.content=await l.text()}catch{l.content=null}let A,d=i.cache,_=async()=>{let O=await f(l);if(O instanceof Response)return{_isResponse:!0,body:await O.text(),status:O.status,headers:Object.fromEntries(O.headers.entries())};return O};if(d&&typeof d==="number"&&d>0){let O=this.cacheManager.generateKey(l,{authUser:l.authUser});A=await this.cacheManager.get(O,_,d)}else if(d&&typeof d==="object"&&d.ttl){let O=d.key||this.cacheManager.generateKey(l,{authUser:l.authUser});A=await this.cacheManager.get(O,_,d.ttl)}else A=await f(l);if(A&&typeof A==="object"&&A._isResponse===!0)A=new Response(A.body,{status:A.status,headers:A.headers});let I;if(i.rawResponse||A instanceof Response)I=A instanceof Response?A:new Response(A);else I=b(200,A,i.responseContentType);return I=await this.middlewareManager.executeFinally(I,l),I}catch(E){if(E instanceof Response)return E;return console.error("Route handler error:",E),U.internalServerError(E instanceof Error?E.message:String(E),i.responseContentType)}}}addRoute(i){this.routes.push(i),this.sortRoutes()}getRoutes(){return this.routes}async handle(i){let l=new URL(i.url).pathname;for(let[a,E,A,d]of this.routes)if(i.method==="OPTIONS"||i.method===a){let _=l.match(E);if(_){let I=i;this.prepareRequest(I,{params:_.groups||{},route:d||l});for(let O of A){let N=await O(I);if(N)return N}}}return U.notFound("Route not found")}clearRoutes(){this.routes=[]}}class J{server=null;router;config;corsHandler;constructor(i,f){if(this.router=i,this.config=f,f.cors){let{preflight:l,corsify:a}=x(this.normalizeCorsOptions(f.cors));this.corsHandler={preflight:l,corsify:a}}}normalizeCorsOptions(i){return{origin:i.origin||"*",credentials:i.credentials!==!1,allowHeaders:Array.isArray(i.allowHeaders)?i.allowHeaders.join(", "):i.allowHeaders||"Content-Type, Authorization",allowMethods:Array.isArray(i.allowMethods)?i.allowMethods.join(", "):i.allowMethods||"GET, POST, PUT, PATCH, DELETE, OPTIONS",exposeHeaders:Array.isArray(i.exposeHeaders)?i.exposeHeaders.join(", "):i.exposeHeaders||"Authorization",maxAge:i.maxAge||86400}}async start(){let i=this.config.port||3000,f=this.config.hostname||"localhost",l=async(a)=>{try{if(this.corsHandler&&a.method==="OPTIONS")return this.corsHandler.preflight(a);let E=await this.router.handle(a);if(this.corsHandler)E=this.corsHandler.corsify(E,a);return E}catch(E){return console.error("Server error:",E),new Response("Internal Server Error",{status:500})}};try{if(this.server=Bun.serve({port:i,hostname:f,reusePort:this.config.reusePort!==!1,fetch:l,idleTimeout:this.config.idleTimeout||60,error:(a)=>{return console.error("[ERROR] Server error:",a),new Response("Internal Server Error",{status:500})}}),!this.server||!this.server.port)throw new Error(`Failed to start server on ${f}:${i} - server object is invalid`);return console.log(`→ Vector server running at http://${f}:${i}`),this.server}catch(a){if(a.code==="EADDRINUSE"||a.message?.includes("address already in use"))a.message=`Port ${i} is already in use`,a.port=i;else if(a.code==="EACCES"||a.message?.includes("permission denied"))a.message=`Permission denied to bind to port ${i}`,a.port=i;else if(a.message?.includes("EADDRNOTAVAIL"))a.message=`Cannot bind to hostname ${f}`,a.hostname=f;throw a}}stop(){if(this.server)this.server.stop(),this.server=null,console.log("Server stopped")}getServer(){return this.server}getPort(){return this.server?.port||this.config.port||3000}getHostname(){return this.server?.hostname||this.config.hostname||"localhost"}getUrl(){let i=this.getPort();return`http://${this.getHostname()}:${i}`}}class P{static instance;router;server=null;middlewareManager;authManager;cacheManager;config={};routeScanner=null;routeGenerator=null;_protectedHandler=null;_cacheHandler=null;constructor(){this.middlewareManager=new F,this.authManager=new T,this.cacheManager=new B,this.router=new z(this.middlewareManager,this.authManager,this.cacheManager)}static getInstance(){if(!P.instance)P.instance=new P;return P.instance}setProtectedHandler(i){this._protectedHandler=i,this.authManager.setProtectedHandler(i)}getProtectedHandler(){return this._protectedHandler}setCacheHandler(i){this._cacheHandler=i,this.cacheManager.setCacheHandler(i)}getCacheHandler(){return this._cacheHandler}addRoute(i,f){return this.router.route(i,f)}async startServer(i){if(this.config={...this.config,...i},this.middlewareManager.clear(),i?.before)this.middlewareManager.addBefore(...i.before);if(i?.finally)this.middlewareManager.addFinally(...i.finally);if(this.config.autoDiscover!==!1)await this.discoverRoutes();return this.server=new J(this.router,this.config),await this.server.start()}async discoverRoutes(){let i=this.config.routesDir||"./routes";if(this.routeScanner=new X(i),!this.routeGenerator)this.routeGenerator=new V;try{let f=await this.routeScanner.scan();if(f.length>0){if(this.config.development)await this.routeGenerator.generate(f);for(let l of f)try{let E=await import(k(l.path)),A=l.name==="default"?E.default:E[l.name];if(A){if(this.isRouteDefinition(A)){let d=A;this.router.route(d.options,d.handler),this.logRouteLoaded(d.options)}else if(this.isRouteEntry(A))this.router.addRoute(A),this.logRouteLoaded(A);else if(typeof A==="function")this.router.route(l.options,A),this.logRouteLoaded(l.options)}}catch(a){console.error(`Failed to load route ${l.name} from ${l.path}:`,a)}this.router.sortRoutes()}}catch(f){if(f.code!=="ENOENT"&&f.code!=="ENOTDIR")console.error("Failed to discover routes:",f)}}async loadRoute(i){if(typeof i==="function"){let f=i();if(Array.isArray(f))this.router.addRoute(f)}else if(i&&typeof i==="object"){for(let[,f]of Object.entries(i))if(typeof f==="function"){let l=f();if(Array.isArray(l))this.router.addRoute(l)}}}isRouteEntry(i){return Array.isArray(i)&&i.length>=3}isRouteDefinition(i){return i&&typeof i==="object"&&"entry"in i&&"options"in i&&"handler"in i}logRouteLoaded(i){}stop(){if(this.server)this.server.stop(),this.server=null;this.router.clearRoutes()}getServer(){return this.server}getRouter(){return this.router}getCacheManager(){return this.cacheManager}getAuthManager(){return this.authManager}static resetInstance(){P.instance=null}}var m=P.getInstance;var{preflight:ki,corsify:mi}=x({origin:"*",credentials:!0,allowHeaders:"Content-Type, Authorization",allowMethods:"GET, POST, PUT, PATCH, DELETE, OPTIONS",exposeHeaders:"Authorization",maxAge:86400});function e(i,f){let l=ai(i,f);return{entry:[i.method.toUpperCase(),RegExp(`^${i.path.replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),[l],i.path],options:i,handler:f}}function ii(i){return JSON.stringify(i??null,(f,l)=>typeof l==="bigint"?l.toString():l)}var fi={success:(i,f)=>b(R.OK,i,f),created:(i,f)=>b(R.CREATED,i,f)};function D(i,f,l){let a={error:!0,message:f,statusCode:i,timestamp:new Date().toISOString()};return b(i,a,l)}var U={badRequest:(i="Bad Request",f)=>D(R.BAD_REQUEST,i,f),unauthorized:(i="Unauthorized",f)=>D(R.UNAUTHORIZED,i,f),paymentRequired:(i="Payment Required",f)=>D(402,i,f),forbidden:(i="Forbidden",f)=>D(R.FORBIDDEN,i,f),notFound:(i="Not Found",f)=>D(R.NOT_FOUND,i,f),methodNotAllowed:(i="Method Not Allowed",f)=>D(405,i,f),notAcceptable:(i="Not Acceptable",f)=>D(406,i,f),requestTimeout:(i="Request Timeout",f)=>D(408,i,f),conflict:(i="Conflict",f)=>D(R.CONFLICT,i,f),gone:(i="Gone",f)=>D(410,i,f),lengthRequired:(i="Length Required",f)=>D(411,i,f),preconditionFailed:(i="Precondition Failed",f)=>D(412,i,f),payloadTooLarge:(i="Payload Too Large",f)=>D(413,i,f),uriTooLong:(i="URI Too Long",f)=>D(414,i,f),unsupportedMediaType:(i="Unsupported Media Type",f)=>D(415,i,f),rangeNotSatisfiable:(i="Range Not Satisfiable",f)=>D(416,i,f),expectationFailed:(i="Expectation Failed",f)=>D(417,i,f),imATeapot:(i="I'm a teapot",f)=>D(418,i,f),misdirectedRequest:(i="Misdirected Request",f)=>D(421,i,f),unprocessableEntity:(i="Unprocessable Entity",f)=>D(R.UNPROCESSABLE_ENTITY,i,f),locked:(i="Locked",f)=>D(423,i,f),failedDependency:(i="Failed Dependency",f)=>D(424,i,f),tooEarly:(i="Too Early",f)=>D(425,i,f),upgradeRequired:(i="Upgrade Required",f)=>D(426,i,f),preconditionRequired:(i="Precondition Required",f)=>D(428,i,f),tooManyRequests:(i="Too Many Requests",f)=>D(429,i,f),requestHeaderFieldsTooLarge:(i="Request Header Fields Too Large",f)=>D(431,i,f),unavailableForLegalReasons:(i="Unavailable For Legal Reasons",f)=>D(451,i,f),internalServerError:(i="Internal Server Error",f)=>D(R.INTERNAL_SERVER_ERROR,i,f),notImplemented:(i="Not Implemented",f)=>D(501,i,f),badGateway:(i="Bad Gateway",f)=>D(502,i,f),serviceUnavailable:(i="Service Unavailable",f)=>D(503,i,f),gatewayTimeout:(i="Gateway Timeout",f)=>D(504,i,f),httpVersionNotSupported:(i="HTTP Version Not Supported",f)=>D(505,i,f),variantAlsoNegotiates:(i="Variant Also Negotiates",f)=>D(506,i,f),insufficientStorage:(i="Insufficient Storage",f)=>D(507,i,f),loopDetected:(i="Loop Detected",f)=>D(508,i,f),notExtended:(i="Not Extended",f)=>D(510,i,f),networkAuthenticationRequired:(i="Network Authentication Required",f)=>D(511,i,f),invalidArgument:(i="Invalid Argument",f)=>D(R.UNPROCESSABLE_ENTITY,i,f),rateLimitExceeded:(i="Rate Limit Exceeded",f)=>D(429,i,f),maintenance:(i="Service Under Maintenance",f)=>D(503,i,f),custom:(i,f,l)=>D(i,f,l)};function b(i,f,l=G.JSON){let a=l===G.JSON?ii(f):f;return new Response(a,{status:i,headers:{"content-type":l}})}var li=async(i,f)=>{let a=m().getProtectedHandler();if(!a)throw U.unauthorized("Authentication not configured",f);try{let E=await a(i);i.authUser=E}catch(E){throw U.unauthorized(E instanceof Error?E.message:"Authentication failed",f)}};function ai(i,f){let{auth:l=!1,expose:a=!1,rawRequest:E=!1,rawResponse:A=!1,responseContentType:d=G.JSON}=i;return async(_)=>{if(!a)return U.forbidden("Forbidden");try{if(l)await li(_,d);if(!E)await W(_);let I=await f(_);return A?I:fi.success(I,d)}catch(I){if(I instanceof Response)return I;return U.internalServerError(String(I),d)}}}export{e as route,b as createResponse,U as APIError};
@@ -29,6 +29,11 @@ export interface VectorRequest<TTypes extends VectorTypes = DefaultVectorTypes>
29
29
  metadata?: GetMetadataType<TTypes>;
30
30
  content?: any;
31
31
  params?: Record<string, string>;
32
+ query: {
33
+ [key: string]: string | string[] | undefined;
34
+ };
35
+ headers: Headers;
36
+ cookies?: Record<string, string>;
32
37
  startTime?: number;
33
38
  [key: string]: any;
34
39
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAID,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,QAAQ,CAAC,EAAE,GAAG,CAAC;CAChB;AAGD,MAAM,WAAW,kBAAmB,SAAQ,WAAW;IACrD,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG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AAG,kBAAkB;IAC7E,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;CAC/B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vector-framework",
3
- "version": "0.9.7",
3
+ "version": "0.9.8",
4
4
  "author": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -76,7 +76,6 @@
76
76
  "dev": "bun run src/cli/index.ts dev",
77
77
  "start": "bun run src/cli/index.ts start",
78
78
  "build": "bun run src/cli/index.ts build",
79
- "example": "bun run examples/server.ts",
80
79
  "build:lib": "bun run build:clean && bun run build:ts && bun run build:bundle && bun run build:cli",
81
80
  "build:clean": "rm -rf dist",
82
81
  "build:ts": "tsc",
@@ -1,4 +1,5 @@
1
1
  import type { RouteEntry } from 'itty-router';
2
+ import { withCookies } from 'itty-router';
2
3
  import type { AuthManager } from '../auth/protected';
3
4
  import type { CacheManager } from '../cache/manager';
4
5
  import { APIError, createResponse } from '../http';
@@ -113,30 +114,63 @@ export class VectorRouter<TTypes extends VectorTypes = DefaultVectorTypes> {
113
114
  );
114
115
  }
115
116
 
116
- private wrapHandler(options: RouteOptions<TTypes>, handler: RouteHandler<TTypes>) {
117
- return async (request: any) => {
118
- // Ensure request has required properties
119
- const vectorRequest = request as VectorRequest<TTypes>;
117
+ private prepareRequest(
118
+ request: VectorRequest<TTypes>,
119
+ options?: {
120
+ params?: Record<string, string>;
121
+ route?: string;
122
+ metadata?: any;
123
+ }
124
+ ): void {
125
+ // Initialize context if not present
126
+ if (!request.context) {
127
+ request.context = {} as any;
128
+ }
120
129
 
121
- // Initialize context if not present
122
- if (!vectorRequest.context) {
123
- vectorRequest.context = {} as any;
124
- }
130
+ // Set params and route if provided
131
+ if (options?.params !== undefined) {
132
+ request.params = options.params;
133
+ }
134
+ if (options?.route !== undefined) {
135
+ request.route = options.route;
136
+ }
137
+ if (options?.metadata !== undefined) {
138
+ request.metadata = options.metadata;
139
+ }
125
140
 
126
- // Parse query parameters from URL (handles duplicate params as arrays)
127
- if (!vectorRequest.query && vectorRequest.url) {
128
- const url = new URL(vectorRequest.url);
129
- const query: Record<string, string | string[]> = {};
130
- for (let [k, v] of url.searchParams) {
131
- query[k] = query[k] ? ([] as string[]).concat(query[k], v) : v;
141
+ // Parse query parameters from URL if not already parsed
142
+ if (!request.query && request.url) {
143
+ const url = new URL(request.url);
144
+ const query: Record<string, string | string[]> = {};
145
+ for (const [key, value] of url.searchParams) {
146
+ if (key in query) {
147
+ if (Array.isArray(query[key])) {
148
+ (query[key] as string[]).push(value);
149
+ } else {
150
+ query[key] = [query[key] as string, value];
151
+ }
152
+ } else {
153
+ query[key] = value;
132
154
  }
133
- vectorRequest.query = query;
134
155
  }
156
+ request.query = query;
157
+ }
135
158
 
136
- // Add metadata to request if provided
137
- if (options.metadata) {
138
- vectorRequest.metadata = options.metadata;
139
- }
159
+ // Parse cookies if not already parsed
160
+ if (!request.cookies) {
161
+ withCookies(request as any);
162
+ }
163
+ }
164
+
165
+ private wrapHandler(options: RouteOptions<TTypes>, handler: RouteHandler<TTypes>) {
166
+ return async (request: any) => {
167
+ // Ensure request has required properties
168
+ const vectorRequest = request as VectorRequest<TTypes>;
169
+
170
+ // Prepare the request with common logic
171
+ this.prepareRequest(vectorRequest, {
172
+ metadata: options.metadata
173
+ });
140
174
 
141
175
  request = vectorRequest;
142
176
  try {
@@ -182,22 +216,45 @@ export class VectorRouter<TTypes extends VectorTypes = DefaultVectorTypes> {
182
216
  let result;
183
217
  const cacheOptions = options.cache;
184
218
 
219
+ // Create cache factory that handles Response objects
220
+ const cacheFactory = async () => {
221
+ const res = await handler(request);
222
+ // If Response, extract data for caching
223
+ if (res instanceof Response) {
224
+ return {
225
+ _isResponse: true,
226
+ body: await res.text(),
227
+ status: res.status,
228
+ headers: Object.fromEntries(res.headers.entries())
229
+ };
230
+ }
231
+ return res;
232
+ };
233
+
185
234
  if (cacheOptions && typeof cacheOptions === 'number' && cacheOptions > 0) {
186
235
  const cacheKey = this.cacheManager.generateKey(request as any, {
187
236
  authUser: request.authUser,
188
237
  });
189
- result = await this.cacheManager.get(cacheKey, () => handler(request), cacheOptions);
238
+ result = await this.cacheManager.get(cacheKey, cacheFactory, cacheOptions);
190
239
  } else if (cacheOptions && typeof cacheOptions === 'object' && cacheOptions.ttl) {
191
240
  const cacheKey =
192
241
  cacheOptions.key ||
193
242
  this.cacheManager.generateKey(request as any, {
194
243
  authUser: request.authUser,
195
244
  });
196
- result = await this.cacheManager.get(cacheKey, () => handler(request), cacheOptions.ttl);
245
+ result = await this.cacheManager.get(cacheKey, cacheFactory, cacheOptions.ttl);
197
246
  } else {
198
247
  result = await handler(request);
199
248
  }
200
249
 
250
+ // Reconstruct Response if it was cached
251
+ if (result && typeof result === 'object' && result._isResponse === true) {
252
+ result = new Response(result.body, {
253
+ status: result.status,
254
+ headers: result.headers
255
+ });
256
+ }
257
+
201
258
  let response: Response;
202
259
  if (options.rawResponse || result instanceof Response) {
203
260
  response = result instanceof Response ? result : new Response(result);
@@ -235,16 +292,17 @@ export class VectorRouter<TTypes extends VectorTypes = DefaultVectorTypes> {
235
292
  const url = new URL(request.url);
236
293
  const pathname = url.pathname;
237
294
 
238
- for (const [method, regex, handlers] of this.routes) {
295
+ for (const [method, regex, handlers, path] of this.routes) {
239
296
  if (request.method === 'OPTIONS' || request.method === method) {
240
297
  const match = pathname.match(regex);
241
298
  if (match) {
242
299
  const req = request as any as VectorRequest<TTypes>;
243
- // Initialize context for new request
244
- if (!req.context) {
245
- req.context = {} as any;
246
- }
247
- req.params = match.groups || {};
300
+
301
+ // Prepare the request with common logic
302
+ this.prepareRequest(req, {
303
+ params: match.groups || {},
304
+ route: path || pathname
305
+ });
248
306
 
249
307
  for (const handler of handlers) {
250
308
  const response = await handler(req as any);
package/src/http.ts CHANGED
@@ -1,17 +1,13 @@
1
- import {
2
- cors,
3
- type IRequest,
4
- type RouteEntry,
5
- withContent,
6
- withCookies,
7
- } from "itty-router";
1
+ import { cors, type IRequest, type RouteEntry, withContent } from "itty-router";
8
2
  import { CONTENT_TYPES, HTTP_STATUS } from "./constants";
9
3
  import type {
4
+ CacheOptions,
10
5
  DefaultVectorTypes,
11
6
  GetAuthType,
12
7
  VectorRequest,
13
8
  VectorTypes,
14
9
  } from "./types";
10
+ import { getVectorInstance } from "./core/vector";
15
11
 
16
12
  export interface ProtectedRequest<
17
13
  TTypes extends VectorTypes = DefaultVectorTypes
@@ -33,7 +29,9 @@ interface ExtendedApiOptions extends ApiOptions {
33
29
  path: string;
34
30
  }
35
31
 
36
- export interface RouteDefinition<TTypes extends VectorTypes = DefaultVectorTypes> {
32
+ export interface RouteDefinition<
33
+ TTypes extends VectorTypes = DefaultVectorTypes
34
+ > {
37
35
  entry: RouteEntry;
38
36
  options: ExtendedApiOptions;
39
37
  handler: (req: VectorRequest<TTypes>) => Promise<unknown>;
@@ -64,7 +62,7 @@ export function route<TTypes extends VectorTypes = DefaultVectorTypes>(
64
62
  return {
65
63
  entry,
66
64
  options,
67
- handler: fn
65
+ handler: fn,
68
66
  };
69
67
  }
70
68
 
@@ -263,7 +261,6 @@ export const protectedRoute = async <
263
261
  responseContentType?: string
264
262
  ) => {
265
263
  // Get the Vector instance to access the protected handler
266
- const { getVectorInstance } = await import("./core/vector");
267
264
  const vector = getVectorInstance();
268
265
 
269
266
  const protectedHandler = vector.getProtectedHandler();
@@ -290,7 +287,7 @@ export interface ApiOptions {
290
287
  expose?: boolean;
291
288
  rawRequest?: boolean;
292
289
  rawResponse?: boolean;
293
- cache?: number | null;
290
+ cache?: CacheOptions | number | null;
294
291
  responseContentType?: string;
295
292
  }
296
293
 
@@ -325,8 +322,6 @@ export function api<TTypes extends VectorTypes = DefaultVectorTypes>(
325
322
  await withContent(request);
326
323
  }
327
324
 
328
- withCookies(request);
329
-
330
325
  // Cache handling is now done in the router
331
326
  const result = await fn(request as any as VectorRequest<TTypes>);
332
327
 
@@ -51,6 +51,9 @@ export interface VectorRequest<TTypes extends VectorTypes = DefaultVectorTypes>
51
51
  metadata?: GetMetadataType<TTypes>;
52
52
  content?: any;
53
53
  params?: Record<string, string>;
54
+ query: { [key: string]: string | string[] | undefined };
55
+ headers: Headers;
56
+ cookies?: Record<string, string>;
54
57
  startTime?: number;
55
58
  [key: string]: any;
56
59
  }