-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the problem
I'd like to enhance a remote function with additional but optional behavior.
In my remote function, I'm getting the user's session. By default, this function validates the session based on local JWT information from a cookie (i.e. no additional network request). However, there may be scenarios where I still want the session data but also need to verify the user of that session against a third-party backend server (they could have been banned, signed out globally from another device, etc, but their JWT is still valid).
import { getRequestEvent, query } from "$app/server"
import type { Session } from "@supabase/supabase-js"
import { getValidatedSession } from "./shared.js"
import * as v from "valibot"
export const getSession = query(
v.optional(v.boolean()),
async (validate_user): Promise<Session | null> => {
const { locals } = getRequestEvent()
return await getValidatedSession(locals.supabase, validate_user)
}
)The code works, but throws a type error whenever I'm not passing an argument.
Describe the proposed solution
This can be resolved if we make a change to the type (used AI here):
export type RemoteQueryFunction<Input, Output> = (
- arg: Input
+ ...args: undefined extends Input ? [arg?: Input] : [arg: Input]
) => RemoteQuery<Output>;Which yields no type error when passing in no arg,
but still, correctly, shows a type error if a passed-in arg doesn't match the schema
Rinse and repeat for any other remote function types this should apply to.
Importance
would make my life easier