Skip to content

Allow remote function argument to be optional #14500

@j4w8n

Description

@j4w8n

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.

Image

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,

Image

but still, correctly, shows a type error if a passed-in arg doesn't match the schema

Image

Rinse and repeat for any other remote function types this should apply to.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions