@bobtail.software/b-ssr 1.0.13 → 1.0.15
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.
|
@@ -14,6 +14,11 @@ type AddRenderRouteOptions<TSchema extends FastifySchema, TRpcSchema extends Fas
|
|
|
14
14
|
handler?: (this: FastifyInstance, req: FastifyRequest<RouteGenericForSchema<TSchema>>, reply: FastifyReply<RouteGenericForSchema<TSchema>>) => Promise<unknown> | unknown;
|
|
15
15
|
prefix?: string;
|
|
16
16
|
};
|
|
17
|
+
type AddLoaderRouteOptions<TSchema extends FastifySchema> = BaseRouteOptions<RouteGenericForSchema<TSchema>> & {
|
|
18
|
+
handler: (this: FastifyInstance, req: FastifyRequest<RouteGenericForSchema<TSchema>>, reply: FastifyReply<RouteGenericForSchema<TSchema>>) => Promise<unknown> | unknown;
|
|
19
|
+
schema?: TSchema;
|
|
20
|
+
prefix?: string;
|
|
21
|
+
};
|
|
17
22
|
interface RouteGenericForSchema<TSchema extends FastifySchema> extends RouteGenericInterface {
|
|
18
23
|
Body: InferZod<TSchema['body']>;
|
|
19
24
|
Querystring: InferZod<TSchema['querystring']>;
|
|
@@ -43,8 +48,9 @@ declare module 'fastify' {
|
|
|
43
48
|
schema?: TSchema;
|
|
44
49
|
}) => void;
|
|
45
50
|
addRpcRoute: <TRpcSchema extends FastifySchema>(url: string, options: AddRpcRouteOptions<TRpcSchema>) => void;
|
|
51
|
+
addLoaderRoute: <TSchema extends FastifySchema>(url: string, options: AddLoaderRouteOptions<TSchema>) => void;
|
|
46
52
|
}
|
|
47
53
|
}
|
|
48
54
|
declare const _default: FastifyPluginAsyncZod<FastifyReactSsrOptions>;
|
|
49
55
|
|
|
50
|
-
export { type AddRenderRouteOptions, type AddRpcRouteOptions, type BaseRouteOptions, type FastifyReactSsrOptions, type RouteGenericForSchema, _default as default };
|
|
56
|
+
export { type AddLoaderRouteOptions, type AddRenderRouteOptions, type AddRpcRouteOptions, type BaseRouteOptions, type FastifyReactSsrOptions, type RouteGenericForSchema, _default as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import"@fastify/multipart";import
|
|
1
|
+
import"@fastify/multipart";import F from"fastify-plugin";import v from"path";var S=async(t,m)=>{if(!t.isMultipart())return m.status(415).send({statusCode:415,error:"Unsupported Media Type",message:"Multipart/form-data expected"});let f=t.parts(),p={};for await(let l of f)if(l.type==="field")p[l.fieldname]=l.value;else{let e=Symbol.for("file-stream");t[e]=l}t.body=p},T=async(t,m)=>{let f=e=>{if(m.errorHandler){let o=m.errorHandler(e);if(o)return o}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 p=process.env.NODE_ENV==="production";if(!p){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 import("vite"),o=m.viteConfig||{},u=await e.createServer({root:m.root,appType:"custom",...o,server:{hmr:{port:24678},...o.server,middlewareMode:!0}});t.use(u.middlewares),t.hasDecorator("viteServer")||t.decorate("viteServer",u),console.log("\u{1F680} [Fastify-SSR] Vite Dev Server Ready")}t.decorate("viteInitDone",!0);let l=async(e,o,u)=>{try{let r=e.raw.url;if(v.extname(r)!==""){o.status(404).send(`File not found: ${r}`);return}let i="";!p&&t.viteServer&&(i=await t.viteServer.transformIndexHtml(r,"<html><head></head><body></body></html>"),i=i.substring(i.indexOf("<head>")+6,i.indexOf("</head>")));let d=p?await import(m.prodEntryFile):await t.viteServer.ssrLoadModule(m.devEntryFile),n=d.render||d.default;if(typeof n!="function")throw new Error(`Entry file ${m.devEntryFile} must export a 'render' function.`);await n({req:e,reply:o,head:i,data:u})}catch(r){t.viteServer?.ssrFixStacktrace(r),console.error("[SSR Error]:",r),o.sent||o.status(500).send("Internal Server Error")}};t.decorate("addRpcRoute",function(e,o){let{handler:u,schema:r,...i}=o,d=`/rpc${e}`,n={...i,schema:r};if(r?.consumes?.includes("multipart/form-data")){let s=n.preValidation;n.preValidation=s?Array.isArray(s)?[S,...s]:[S,s]:S}this.route({method:"POST",url:d,...n,handler:async(s,c)=>{try{let h=await u.call(this,s,c);return c.sent?void 0:h}catch(h){if(console.error(`[RPC Error] ${d}:`,h),!c.sent){let{statusCode:R,message:y}=f(h);c.status(R).send({error:{message:y}})}}}})}),t.decorate("addRenderRoute",function(e,o){let{handler:u,schema:r,...i}=o||{},d=async(n,a,s)=>{try{let c=await u?.call(this,n,a);return s?c:l(n,a,c)}catch(c){if(console.error(`[Render Error] ${e}:`,c),!a.sent){let{statusCode:h,message:R}=f(c);if(s)a.status(h).send({error:{message:R}});else return l(n,a,{ssrError:{statusCode:500,message:"Internal Error"}})}}};this.route({method:"GET",url:e,schema:r,...i,handler:(n,a)=>d(n,a,!1)}),e!=="*"&&e!=="/*"&&this.route({method:"GET",url:`/loader${e}`,schema:r,...i,handler:(n,a)=>d(n,a,!0)})}),t.decorate("addLoaderRoute",function(e,o){let{handler:u,schema:r,...i}=o,d=`/api${e}`;this.route({method:"GET",url:d,schema:r,...i,handler:async(n,a)=>{try{let s=await u.call(this,n,a);return a.sent?void 0:s}catch(s){if(console.error(`[Loader API Error] ${d}:`,s),!a.sent){let{statusCode:c,message:h}=f(s);a.status(c).send({error:{message:h}})}}}})})},x=F(T,{name:"fastify-b-ssr"});export{x as default};
|