zomars
zomarsβ€’14mo ago

Is there a way to extract a procedure signature?

I'm looking into extracting certain handlers into functions to keep certain procedures more manageable. Is there a way to define a function that expects the same signature as a procedure? Here's an example of what I'm trying to achieve.
No description
4 Replies
Nick
Nickβ€’14mo ago
Best to keep it simple and request exactly what’s needed rather than coupling it to trpc. The code is more reusable that way
Alex / KATT 🐱
Alex / KATT πŸ±β€’14mo ago
i'd probably make it independent of trpc too - perhaps even extracting the zod schema of the input like
const schema = z.object({ foo: z.string() })

function myFn(opts: {
input: typeof schema['_output']
user: { id: string }
}) {
/// ..
}

export const adminRouter = router({
toggleFeatureFlag: authedAdminProcedure.input(schema).mutation(async opts => { })
const schema = z.object({ foo: z.string() })

function myFn(opts: {
input: typeof schema['_output']
user: { id: string }
}) {
/// ..
}

export const adminRouter = router({
toggleFeatureFlag: authedAdminProcedure.input(schema).mutation(async opts => { })
another useful pattern in cases like this where you want to pass data deeply is to use async local storage in node i use it for logging and making sure that all logs, no matter how deep, have correlation ids example:
import { AsyncLocalStorage } from 'node:async_hooks';

interface LogStore {
correlationId: string;
user: {
id: string | null;
email: string | null;
} | null;
}
export const logStorage = new AsyncLocalStorage<LogStore>();
import { AsyncLocalStorage } from 'node:async_hooks';

interface LogStore {
correlationId: string;
user: {
id: string | null;
email: string | null;
} | null;
}
export const logStorage = new AsyncLocalStorage<LogStore>();
and i have a HOC around my API requests to attach data to the storage you could potentially do an AdminStore, call adminStorge.run() in the authedAdminProcedure and then those things will just magically exist anywhere down the stack of those functions
zomars
zomarsOPβ€’14mo ago
Some real wizardry in here Thanks πŸ™
Alex / KATT 🐱
Alex / KATT πŸ±β€’14mo ago
Don't hesitate to reach out if you wanna pair on anything You got my cal ☺