Skip to content

Commit

Permalink
Fix: types for next cookies and headers functions (#614)
Browse files Browse the repository at this point in the history
* use return types for Next.js cookies and headers functions

* add changeset

* fix formating
  • Loading branch information
dijonmusters authored Jul 29, 2023
1 parent 9715901 commit 8c77550
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-cats-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-nextjs': patch
---

Fix type for next.js cookies and headers functions
11 changes: 6 additions & 5 deletions packages/nextjs/src/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import { createMiddlewareClient } from './middlewareClient';
import { createClientComponentClient } from './clientComponentClient';
import { createServerComponentClient } from './serverComponentClient';
import { createRouteHandlerClient } from './routeHandlerClient';
import { createServerActionClient } from './serverActionClient';
import { headers, cookies } from 'next/headers';

import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import type { NextRequest } from 'next/server';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { ReadonlyHeaders } from 'next/dist/server/web/spec-extension/adapters/headers';
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';

/**
* @deprecated utilize the `createPagesBrowserClient` function instead
Expand Down Expand Up @@ -169,7 +167,10 @@ export function createServerComponentSupabaseClient<
? Database[SchemaName]
: any
>(
context: { headers: () => ReadonlyHeaders; cookies: () => ReadonlyRequestCookies },
context: {
headers: () => ReturnType<typeof headers>;
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
Expand Down Expand Up @@ -209,7 +210,7 @@ export function createRouteHandlerSupabaseClient<
? Database[SchemaName]
: any
>(
context: { headers: () => ReadonlyHeaders; cookies: () => ReadonlyRequestCookies },
context: { headers: () => ReturnType<typeof headers>; cookies: () => ReturnType<typeof cookies> },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
Expand Down
6 changes: 3 additions & 3 deletions packages/nextjs/src/routeHandlerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { cookies } from 'next/headers';

import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';

class NextRouteHandlerAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: {
cookies: () => ReadonlyRequestCookies;
cookies: () => ReturnType<typeof cookies>;
},
cookieOptions?: CookieOptions
) {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function createRouteHandlerClient<
: any
>(
context: {
cookies: () => ReadonlyRequestCookies;
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
Expand Down
6 changes: 3 additions & 3 deletions packages/nextjs/src/serverComponentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { cookies } from 'next/headers';

import type { SupabaseClient } from '@supabase/supabase-js';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';

class NextServerComponentAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: {
cookies: () => ReadonlyRequestCookies;
cookies: () => ReturnType<typeof cookies>;
},
cookieOptions?: CookieOptions
) {
Expand Down Expand Up @@ -44,7 +44,7 @@ export function createServerComponentClient<
: any
>(
context: {
cookies: () => ReadonlyRequestCookies;
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/SessionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export const SessionContextProvider = ({

useEffect(() => {
if (!session && initialSession) {
setSession(initialSession)
setSession(initialSession);
}
}, [session, initialSession]);

useEffect(() => {
let mounted = true;

Expand Down

0 comments on commit 8c77550

Please sign in to comment.