@bobtail.software/b-ssr 1.0.5 → 1.0.7

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.
@@ -1 +1 @@
1
- import"@fastify/multipart";import v from"fastify-plugin";import F from"path";var y=async(t,c)=>{if(!t.isMultipart())return c.status(415).send({statusCode:415,error:"Unsupported Media Type",message:"Multipart/form-data expected"});let h=t.parts(),l={};for await(let m of h)if(m.type==="field")l[m.fieldname]=m.value;else{let e=Symbol.for("file-stream");t[e]=m}t.body=l},T=async(t,c)=>{let h=e=>{if(c.errorHandler){let r=c.errorHandler(e);if(r)return r}return{message:"Internal Server Error",statusCode:500}};if(t.hasDecorator("viteInitDone")&&t.viteInitDone)return;if(!t.hasDecorator("multipartErrors"))try{let e=await import("@fastify/multipart");await t.register(e.default)}catch(e){if(e.code!=="FST_ERR_DEC_ALREADY_PRESENT")throw e}let l=process.env.NODE_ENV==="production";if(!l){if(!t.hasDecorator("use"))try{let r=await import("@fastify/middie");await t.register(r.default,{hook:"onRequest"})}catch(r){if(r.code!=="FST_ERR_DEC_ALREADY_PRESENT")throw r}let e=await(await import("vite")).createServer({root:c.root,server:{middlewareMode:!0,hmr:{port:24678}},appType:"custom",...c.viteConfig});t.use(e.middlewares),t.hasDecorator("viteServer")||t.decorate("viteServer",e),console.log("\u{1F680} [Fastify-SSR] Vite Dev Server Ready")}t.decorate("viteInitDone",!0);let m=async(e,r,f)=>{try{let o=e.raw.url;if(F.extname(o)!==""){r.status(404).send(`File not found: ${o}`);return}let a="";!l&&t.viteServer&&(a=await t.viteServer.transformIndexHtml(o,"<html><head></head><body></body></html>"),a=a.substring(a.indexOf("<head>")+6,a.indexOf("</head>")));let u=l?await import(c.prodEntryFile):await t.viteServer.ssrLoadModule(c.devEntryFile),n=u.render||u.default;if(typeof n!="function")throw new Error(`Entry file ${c.devEntryFile} must export a 'render' function.`);await n({req:e,reply:r,head:a,data:f})}catch(o){t.viteServer?.ssrFixStacktrace(o),console.error("[SSR Error]:",o),r.sent||r.status(500).send("Internal Server Error")}};t.decorate("addRpcRoute",function(e,r){let{handler:f,schema:o,...a}=r,u=`/rpc${e}`,n={...a,schema:o};if(o?.consumes?.includes("multipart/form-data")){let d=n.preValidation;n.preValidation=d?Array.isArray(d)?[y,...d]:[y,d]:y}this.route({method:"POST",url:u,...n,handler:async(d,i)=>{try{let p=await f.call(this,d,i);return i.sent?void 0:p}catch(p){if(console.error(`[RPC Error] ${u}:`,p),!i.sent){let{statusCode:R,message:S}=h(p);i.status(R).send({error:{message:S}})}}}})}),t.decorate("addRenderRoute",function(e,r){let{handler:f,schema:o,...a}=r||{},u=async(n,s,d)=>{try{let i=await f?.call(this,n,s);return d?i:m(n,s,i)}catch(i){if(console.error(`[Render Error] ${e}:`,i),!s.sent){let{statusCode:p,message:R}=h(i);if(d)s.status(p).send({error:{message:R}});else return m(n,s,{ssrError:{statusCode:500,message:"Internal Error"}})}}};this.route({method:"GET",url:e,schema:o,...a,handler:(n,s)=>u(n,s,!1)}),e!=="*"&&e!=="/*"&&this.route({method:"GET",url:`/loader${e}`,schema:o,...a,handler:(n,s)=>u(n,s,!0)})})},x=v(T,{name:"fastify-b-ssr"});export{x as default};
1
+ import"@fastify/multipart";import v from"fastify-plugin";import F from"path";var y=async(e,c)=>{if(!e.isMultipart())return c.status(415).send({statusCode:415,error:"Unsupported Media Type",message:"Multipart/form-data expected"});let h=e.parts(),p={};for await(let l of h)if(l.type==="field")p[l.fieldname]=l.value;else{let t=Symbol.for("file-stream");e[t]=l}e.body=p},T=async(e,c)=>{let h=t=>{if(c.errorHandler){let n=c.errorHandler(t);if(n)return n}return{message:"Internal Server Error",statusCode:500}};if(e.hasDecorator("viteInitDone")&&e.viteInitDone)return;if(!e.hasDecorator("multipartErrors"))try{let t=await import("@fastify/multipart");await e.register(t.default)}catch(t){if(t.code!=="FST_ERR_DEC_ALREADY_PRESENT")throw t}let p=process.env.NODE_ENV==="production";if(!p){if(!e.hasDecorator("use"))try{let r=await import("@fastify/middie");await e.register(r.default,{hook:"onRequest"})}catch(r){if(r.code!=="FST_ERR_DEC_ALREADY_PRESENT")throw r}let t=await import("vite"),n=c.viteConfig||{},u=await t.createServer({root:c.root,appType:"custom",...n,server:{hmr:{port:24678},...n.server,middlewareMode:!0}});e.use(u.middlewares),e.hasDecorator("viteServer")||e.decorate("viteServer",u),console.log("\u{1F680} [Fastify-SSR] Vite Dev Server Ready")}e.decorate("viteInitDone",!0);let l=async(t,n,u)=>{try{let r=t.raw.url;if(F.extname(r)!==""){n.status(404).send(`File not found: ${r}`);return}let a="";!p&&e.viteServer&&(a=await e.viteServer.transformIndexHtml(r,"<html><head></head><body></body></html>"),a=a.substring(a.indexOf("<head>")+6,a.indexOf("</head>")));let m=p?await import(c.prodEntryFile):await e.viteServer.ssrLoadModule(c.devEntryFile),o=m.render||m.default;if(typeof o!="function")throw new Error(`Entry file ${c.devEntryFile} must export a 'render' function.`);await o({req:t,reply:n,head:a,data:u})}catch(r){e.viteServer?.ssrFixStacktrace(r),console.error("[SSR Error]:",r),n.sent||n.status(500).send("Internal Server Error")}};e.decorate("addRpcRoute",function(t,n){let{handler:u,schema:r,...a}=n,m=`/rpc${t}`,o={...a,schema:r};if(r?.consumes?.includes("multipart/form-data")){let d=o.preValidation;o.preValidation=d?Array.isArray(d)?[y,...d]:[y,d]:y}this.route({method:"POST",url:m,...o,handler:async(d,i)=>{try{let f=await u.call(this,d,i);return i.sent?void 0:f}catch(f){if(console.error(`[RPC Error] ${m}:`,f),!i.sent){let{statusCode:R,message:S}=h(f);i.status(R).send({error:{message:S}})}}}})}),e.decorate("addRenderRoute",function(t,n){let{handler:u,schema:r,...a}=n||{},m=async(o,s,d)=>{try{let i=await u?.call(this,o,s);return d?i:l(o,s,i)}catch(i){if(console.error(`[Render Error] ${t}:`,i),!s.sent){let{statusCode:f,message:R}=h(i);if(d)s.status(f).send({error:{message:R}});else return l(o,s,{ssrError:{statusCode:500,message:"Internal Error"}})}}};this.route({method:"GET",url:t,schema:r,...a,handler:(o,s)=>m(o,s,!1)}),t!=="*"&&t!=="/*"&&this.route({method:"GET",url:`/loader${t}`,schema:r,...a,handler:(o,s)=>m(o,s,!0)})})},x=v(T,{name:"fastify-b-ssr"});export{x as default};
@@ -1,3 +1,9 @@
1
+ declare class ApiClientError extends Error {
2
+ readonly status: number;
3
+ readonly statusText: string;
4
+ readonly body: unknown;
5
+ constructor(message: string, status: number, statusText: string, body: unknown);
6
+ }
1
7
  type ClientArgs<TArgs extends {
2
8
  params?: unknown;
3
9
  query?: unknown;
@@ -17,4 +23,4 @@ declare function createClientRpc<TArgs extends {
17
23
  body?: unknown;
18
24
  }, TReturn>({ method, url, isMultipart }: RpcClientOptions): (args: ClientArgs<TArgs>) => Promise<TReturn>;
19
25
 
20
- export { type RpcClientOptions, createClientRpc };
26
+ export { ApiClientError, type RpcClientOptions, createClientRpc };
@@ -1 +1 @@
1
- function y(r){return r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function w(r,s){let t=r;for(let i in s)if(Object.prototype.hasOwnProperty.call(s,i)){let a=s[i];if(a==null)continue;let c=encodeURIComponent(String(a)),g=new RegExp(`:${y(i)}(\\??)`,"g");t=t.replace(g,c)}t=t.replace(/\/:[a-zA-Z0-9_]+\?/g,"");let p=t.match(/:[a-zA-Z0-9_]+/g);if(p)throw new Error(`Error al compilar la URL. Faltan par\xE1metros requeridos: ${p.join(", ")}`);return t}function h({method:r,url:s,isMultipart:t}){return async p=>{let{params:i={},query:a={},body:c}=p,g=w(s,i),d=new URL(g,window.location.origin);a&&Object.entries(a).forEach(([n,e])=>{e!=null&&d.searchParams.append(n,String(e))});let l={method:r,headers:{},credentials:"include"};if(r==="POST")if(t){let n=new FormData,e=c?{...c}:{};"file"in e&&e.file instanceof File&&(n.append("file",e.file),delete e.file),Object.entries(e).forEach(([u,f])=>{n.append(u,String(f))}),l.body=n}else l.headers["Content-Type"]="application/json",l.headers.Accept="application/json",l.body=JSON.stringify(c||{});let o=await fetch(d.toString(),l);if(!o.ok){let n;try{n=await o.json()}catch{}throw new Error(n?.error?.message||n?.message||`La solicitud fall\xF3 con estado ${o.status}`)}if(!(o.headers.get("Content-Length")==="0"||o.status===204))return o.json()}}export{h as createClientRpc};
1
+ var f=class extends Error{status;statusText;body;constructor(o,e,c,i){super(o),this.name="ApiClientError",this.status=e,this.statusText=c,this.body=i}};function w(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function h(s,o){let e=s;for(let i in o)if(Object.prototype.hasOwnProperty.call(o,i)){let l=o[i];if(l==null)continue;let u=encodeURIComponent(String(l)),d=new RegExp(`:${w(i)}(\\??)`,"g");e=e.replace(d,u)}e=e.replace(/\/:[a-zA-Z0-9_]+\?/g,"");let c=e.match(/:[a-zA-Z0-9_]+/g);if(c)throw new Error(`Error al compilar la URL. Faltan par\xE1metros requeridos: ${c.join(", ")}`);return e}function m({method:s,url:o,isMultipart:e}){return async c=>{let{params:i={},query:l={},body:u}=c,d=h(o,i),y=new URL(d,window.location.origin);l&&Object.entries(l).forEach(([r,n])=>{n!=null&&y.searchParams.append(r,String(n))});let p={method:s,headers:{},credentials:"include"};if(s==="POST")if(e){let r=new FormData,n=u?{...u}:{};"file"in n&&n.file instanceof File&&(r.append("file",n.file),delete n.file),Object.entries(n).forEach(([a,g])=>{r.append(a,String(g))}),p.body=r}else p.headers["Content-Type"]="application/json",p.headers.Accept="application/json",p.body=JSON.stringify(u||{});let t=await fetch(y.toString(),p);if(!t.ok){let r=`Request failed with status ${t.status} (${t.statusText})`,n=null,a="";try{n=await t.json(),a=n?.error?.message||n?.message}catch{try{a=await t.text(),n=a}catch{}}let g=a?`${r}: ${a}`:r;throw new f(g,t.status,t.statusText,n)}if(!(t.headers.get("Content-Length")==="0"||t.status===204))return t.json()}}export{f as ApiClientError,m as createClientRpc};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobtail.software/b-ssr",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Fastify + Vite SSR Plugin wrapper with RPC",
5
5
  "author": "Victor Moreno <info@bobtail.software> (https://bobtail.software)",
6
6
  "license": "GPL-3.0",