@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(
|
|
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};
|
package/dist/rpc-client.d.ts
CHANGED
|
@@ -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 };
|
package/dist/rpc-client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
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