Skip to content

Commit

Permalink
Chore: make cookie functions optional in server client (#654)
Browse files Browse the repository at this point in the history
* make cookie functions optional in server client

* fix cookie method types

* add changeset
  • Loading branch information
dijonmusters authored Oct 26, 2023
1 parent a6b35ca commit b9099fd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-beds-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/ssr': patch
---

Fixed types for cookie methods
8 changes: 4 additions & 4 deletions packages/ssr/src/createBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
GenericSchema,
SupabaseClientOptions
} from '@supabase/supabase-js/dist/module/lib/types';
import type { BrowserCookieMethods, CookieOptionsWithName } from './types';
import type { CookieMethods, CookieOptionsWithName } from './types';

let cachedBrowserClient: SupabaseClient<any, string> | undefined;

Expand All @@ -24,7 +24,7 @@ export function createBrowserClient<
supabaseUrl: string,
supabaseKey: string,
options?: SupabaseClientOptions<SchemaName> & {
cookies: BrowserCookieMethods;
cookies: CookieMethods;
cookieOptions?: CookieOptionsWithName;
isSingleton?: boolean;
}
Expand All @@ -35,7 +35,7 @@ export function createBrowserClient<
);
}

let cookies: BrowserCookieMethods = {};
let cookies: CookieMethods = {};
let isSingleton = true;
let cookieOptions: CookieOptionsWithName | undefined;
let userDefinedClientOptions;
Expand All @@ -58,7 +58,7 @@ export function createBrowserClient<
storage: {
getItem: async (key: string) => {
if (typeof cookies.get === 'function') {
return (await cookies.get(key)) ?? null;
return cookies.get(key);
}

if (isBrowser()) {
Expand Down
39 changes: 21 additions & 18 deletions packages/ssr/src/createServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
GenericSchema,
SupabaseClientOptions
} from '@supabase/supabase-js/dist/module/lib/types';
import type { CookieOptionsWithName, ServerCookieMethods } from './types';
import type { CookieOptionsWithName, CookieMethods } from './types';

export function createServerClient<
Database = any,
Expand All @@ -20,7 +20,7 @@ export function createServerClient<
supabaseUrl: string,
supabaseKey: string,
options: SupabaseClientOptions<SchemaName> & {
cookies: ServerCookieMethods;
cookies: CookieMethods;
cookieOptions?: CookieOptionsWithName;
}
) {
Expand All @@ -32,13 +32,6 @@ export function createServerClient<

const { cookies, cookieOptions, ...userDefinedClientOptions } = options;

if (!cookies.get || !cookies.set || !cookies.remove) {
// todo: point to helpful docs in error message, once they have been written! 😏
throw new Error(
'The Supabase client requires functions to get, set, and remove cookies in your specific framework!'
);
}

const cookieClientOptions = {
global: {
headers: {
Expand All @@ -51,15 +44,25 @@ export function createServerClient<
detectSessionInUrl: isBrowser(),
persistSession: true,
storage: {
getItem: async (key: string) => (await cookies.get(key)) ?? null,
setItem: async (key: string, value: string) =>
await cookies.set(key, value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
}),
removeItem: async (key: string) =>
await cookies.remove(key, { ...DEFAULT_COOKIE_OPTIONS, ...cookieOptions, maxAge: 0 })
getItem: async (key: string) => {
if (typeof cookies.get === 'function') {
return cookies.get(key);
}
},
setItem: async (key: string, value: string) => {
if (typeof cookies.set === 'function') {
await cookies.set(key, value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
}
},
removeItem: async (key: string) => {
if (typeof cookies.remove === 'function') {
await cookies.remove(key, { ...DEFAULT_COOKIE_OPTIONS, ...cookieOptions, maxAge: 0 });
}
}
}
}
};
Expand Down
11 changes: 3 additions & 8 deletions packages/ssr/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import type { CookieSerializeOptions } from 'cookie';

export type CookieOptions = Partial<CookieSerializeOptions>;
export type CookieOptionsWithName = { name?: string } & CookieOptions;
export type ServerCookieMethods = {
get: (key: string) => Promise<string | null | undefined> | string | null | undefined;
set: (key: string, value: string, options?: CookieOptions) => Promise<void> | void;
remove: (key: string, options?: CookieOptions) => Promise<void> | void;
};
export type BrowserCookieMethods = {
export type CookieMethods = {
get?: (key: string) => Promise<string | null | undefined> | string | null | undefined;
set?: (key: string, value: string, options?: CookieOptions) => Promise<void> | void;
remove?: (key: string, options?: CookieOptions) => Promise<void> | void;
set?: (key: string, value: string, options: CookieOptions) => Promise<void> | void;
remove?: (key: string, options: CookieOptions) => Promise<void> | void;
};

0 comments on commit b9099fd

Please sign in to comment.