Skip to content

@supabase/ssr should include @types/cookie as a (non-dev) dependency #53

Closed

Description

@dawaltconley

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

The server client example for NextJS setups throws errors with stricter TS/ESLint configs (specifically with the @typescript-eslint/no-unsafe-argument rule):

// from https://supabase.com/docs/guides/auth/server-side/nextjs
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'

export function createClient() {
  const cookieStore = cookies()

  return createServerClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    {
      cookies: {
        get(name: string) {
          return cookieStore.get(name)?.value
        },
        set(name: string, value: string, options: CookieOptions) {
          try {
            // ERROR: Unsafe argument of type `any` assigned to a parameter of type `[key: string, value: string, cookie?: Partial<ResponseCookie> | undefined] | [options: ResponseCookie]`
            cookieStore.set({ name, value, ...options })
          } catch (error) {
            // The `set` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
        /* ... */
      },
    }
  )
}

It's not immediately obvious when trying to debug but { name, value, ...options } evaluates to any. This is because CookieOptions is an alias for a type provided by the @types/cookie package, but this is a devDependency, so it's not being installed with @supabase/ssr:

// from dist/index.d.ts
import { CookieSerializeOptions } from 'cookie';

type CookieOptions = Partial<CookieSerializeOptions>;

As is, CookieOptions is basically an obscured any type, which only makes the type issues harder to debug.

To Reproduce

Attempt to use the server client example in any project with @typescript-eslint/no-unsafe-argument set to error.

Expected behavior

This can be fixed by adding @types/cookie to a project manually, but it would be better to include the package with @supabase/ssr.

Activity

  1. added
    bug
    on May 2, 2024
  2. transferred this issue fromsupabase/supabaseon May 3, 2024
  3. encima commented on May 3, 2024

    @encima
    Member

    Transferred to auth for the right eyes. PRs welcome as well!

  4. added
    help wanted
    on May 3, 2024
  5. rbutera commented on May 17, 2024

    @rbutera

    I've created a PR over at @supabase/auth-helpers:

    supabase/auth-helpers#781

    Thank you @dawaltconley for figuring this out as it was a bit of a head scratcher for me!

  6. ryangriffinau commented on Jun 3, 2024

    @ryangriffinau

    Had the same issue and could not work it out - thanks @dawaltconley !

  7. transferred this issue fromsupabase/authon Aug 27, 2024
  8. Simplereally commented on Aug 30, 2024

    @Simplereally

    Ran into this issue today and was banging my head against the wall for a while.

    For those who are unsure of the solution:

    1. Install cookie type as dev dependency as OP mentioned via bun add @types/cookie -d or npm i -d @types/cookie
    2. Update server.ts to include:
    import { type CookieSerializeOptions } from "cookie";
    type CookieOptions = Partial<CookieSerializeOptions>;
    
    1. Use it in the set function:
    cookiesToSet.forEach(({ name, value, options }) =>
        cookieStore.set(name, value, options as CookieOptions),
    );
    
  9. J0 commented on Sep 4, 2024

    @J0
    Contributor

    Hey everyone,

    Thanks for the feedback. I can relate to the issue of having to manually install the types package and potentially doing some head scratching if one does not install it. However, my understanding is that types generally live as dev dependencies by convention and listing it as a prod dependency instead would force users to opt in even if they do not need the types

    Leaving it as a dev dependency provides more optionality and I'm currently in favour of that. I think we can look into better documenting this on the SSR guide

    I'll also check in with the team to see what they think. In the meantime let us know if there are any questions/concerns or more arguments for/against inclusion.

    Thanks

  10. dawaltconley commented on Sep 4, 2024

    @dawaltconley
    Author

    For what it's worth, I do not think the convention on this is very clear or well-established, but I've seen more recommendations for library authors to install any exposed type dependencies as non-dev than dev (see here and here). The most contentious case is @types/node, since this introduces globals and is often installed manually.

    In my view, every Typescript project already ships types to all users via declaration files, whether they use them or not. Unless you go pure JavaScript, it's really a choice between shipping complete vs incomplete types to all users. I know it's "one more dependency," but type packages are very small and usually (as in this case) 0 deps.

    But I can't claim that's a universal view. Including @types/cookie in the installation part of the docs would probably resolve the confusion for most users (though it should probably be under optionalDependencies in that case).

  11. J0 commented on Sep 10, 2024

    @J0
    Contributor

    Hey,

    We've decided to include it. Thanks!

  12. added a commit that references this issue on Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions