moloch
moloch
Explore posts from servers
TTCTheo's Typesafe Cult
Created by moloch on 10/5/2023 in #questions
Has anyone managed to generate a static tailwind config using something like next-plugin-preval?
Hey y'all!
next-plugin-preval seems to be a pretty outdated plugin that doesn't play nice with config.transpilePackages. However, it seems wild to me that I have to include a bunch of unneeded tailwind javascript code in my client bundle just to import and use things like my theme colours and breakpoints. Has anyone found a solution to this?
1 replies
TtRPC
Created by moloch on 10/4/2023 in #❓-help
Missing keys in TRPCError when returning as JSON
Hey all! I am returning a TRPCError from a custom TRPC server but only the code and name keys seems to be serialized and returned to the client. It's quite strange. If I directly return a key such as error.message I get the string no problem, but if I return the entire error object I only recieve error.code and error.name . Any idea why this is? Here's a little code snippet:
catch (error) {
if (error instanceof TRPCError) {
console.log(Object.keys(error)) // [ 'code', 'name' ]
console.log(error.cause) // The actual cause of the error
console.log(error.message) // The error message!!!
// An error from tRPC occured
const httpCode = getHTTPStatusCodeFromError(error);
return res.status(httpCode).json({ ...error });
}
// Another error occured
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
catch (error) {
if (error instanceof TRPCError) {
console.log(Object.keys(error)) // [ 'code', 'name' ]
console.log(error.cause) // The actual cause of the error
console.log(error.message) // The error message!!!
// An error from tRPC occured
const httpCode = getHTTPStatusCodeFromError(error);
return res.status(httpCode).json({ ...error });
}
// Another error occured
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
1 replies
TTCTheo's Typesafe Cult
Created by moloch on 4/6/2023 in #questions
Build for production with custom server
Hey everyone! I've been struggling to create a T3 setup that includes a custom server on production. I've looked at a few Next.js custom sever example projects but the env.mjs seems to through a wrench in the build process. Does anybody have a working project example that builds a custom server using create-t3-app ?
1 replies
TtRPC
Created by moloch on 3/30/2023 in #❓-help
Is there a way to define the Websocket protocol when using wsLink()
I am attempting to use tRPC with Azure's Pub/Sub Websockets service it appears to require custom protocols to be defined as explained here: https://learn.microsoft.com/en-us/azure/azure-web-pubsub/reference-json-webpubsub-subprotocol They give the following code example:
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.webpubsub.azure.v1');
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.webpubsub.azure.v1');
How might this be done using:
const client = createWSClient({
url: `wss://test.webpubsub.azure.com/client/hubs/hub1`,
});
const client = createWSClient({
url: `wss://test.webpubsub.azure.com/client/hubs/hub1`,
});
1 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
Ripping my hair out here trying to get Websockets working with tRPC and Next. Everything seems to be working and the frontend client is connecting to my websocket server, however triggering the addFact mutation does not not successfully emit the new catFact despite there not being any errors. I've looked at various repos that achieve similar functionality and haven't been able to spot the problem, does anyone have any guidance?
export const catFactsRouter = createTRPCRouter({
fact: publicProcedure.query(({ ctx }) => {

const fact = ctx.prisma.catFact.findFirst({
orderBy: {
createdAt: "desc",
},
});

return fact;
}),
addFact: protectedProcedure
.input(
z.object({
token: z.string().min(1),
fact: z.string().min(1)
})
)
.mutation(async ({ input, ctx }) => {
console.log(`protectedProcedure.mutation()`);
const fact = await ctx.prisma.catFact.create({
data: {
fact: input.fact,
}
});
console.log(`ctx.ee.emit(Events.NEW_CAT_FACT, fact)`);
ctx.ee.emit(Events.NEW_CAT_FACT, fact);
console.log(`ee.emit(Events.NEW_CAT_FACT, fact);`);
return fact;
}),
onFact: publicProcedure.subscription(({ ctx }) => {
console.log("publicProcedure.subscription")

// `resolve()` is triggered for each client when they start subscribing `onFact`
// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
const onCatFact = (data: CatFact) => {
console.log(`Emitting fact to client:`, data);
// emit data to client
emit.next(data);
};
console.log("ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);")

ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);

return () => {
ctx.ee.off(Events.NEW_CAT_FACT, onCatFact);
};
});
}),
});
export const catFactsRouter = createTRPCRouter({
fact: publicProcedure.query(({ ctx }) => {

const fact = ctx.prisma.catFact.findFirst({
orderBy: {
createdAt: "desc",
},
});

return fact;
}),
addFact: protectedProcedure
.input(
z.object({
token: z.string().min(1),
fact: z.string().min(1)
})
)
.mutation(async ({ input, ctx }) => {
console.log(`protectedProcedure.mutation()`);
const fact = await ctx.prisma.catFact.create({
data: {
fact: input.fact,
}
});
console.log(`ctx.ee.emit(Events.NEW_CAT_FACT, fact)`);
ctx.ee.emit(Events.NEW_CAT_FACT, fact);
console.log(`ee.emit(Events.NEW_CAT_FACT, fact);`);
return fact;
}),
onFact: publicProcedure.subscription(({ ctx }) => {
console.log("publicProcedure.subscription")

// `resolve()` is triggered for each client when they start subscribing `onFact`
// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
const onCatFact = (data: CatFact) => {
console.log(`Emitting fact to client:`, data);
// emit data to client
emit.next(data);
};
console.log("ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);")

ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);

return () => {
ctx.ee.off(Events.NEW_CAT_FACT, onCatFact);
};
});
}),
});
8 replies