Skip to content

Commit

Permalink
Fix: Typedefs for clientClient functions (#556)
Browse files Browse the repository at this point in the history
* add explicit return types for createClient functions

* add deprecated functions for App Router createClient functions

* add changeset
  • Loading branch information
dijonmusters authored May 23, 2023
1 parent c184ab2 commit a128b9e
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-stingrays-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-nextjs': patch
---

Fix typedefs and add deprecated functions for App Router createClient functions
16 changes: 11 additions & 5 deletions packages/nextjs/src/clientComponentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import {
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { SupabaseClient } from '@supabase/supabase-js';

let supabase: SupabaseClient<any, string> | undefined;
import type { SupabaseClient } from '@supabase/supabase-js';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';

// can't type this properly as `Database`, `SchemaName` and `Schema` are only available within `createClientComponentClient` function
let supabase: any;

export function createClientComponentClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
Expand All @@ -23,7 +29,7 @@ export function createClientComponentClient<
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}) {
} = {}): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
Expand All @@ -32,7 +38,7 @@ export function createClientComponentClient<

const _supabase =
supabase ??
createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand Down
154 changes: 143 additions & 11 deletions packages/nextjs/src/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import {
SupabaseClientOptionsWithoutAuth,
CookieOptionsWithName
} from '@supabase/auth-helpers-shared';
import { NextResponse } from 'next/server';
import { createPagesBrowserClient } from './pagesBrowserClient';
import { createPagesServerClient } from './pagesServerClient';
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import { NextRequest, NextResponse } from 'next/server';
import { createMiddlewareClient } from './middlewareClient';
import { createClientComponentClient } from './clientComponentClient';
import { createServerComponentClient } from './serverComponentClient';
import { createRouteHandlerClient } from './routeHandlerClient';
import { createServerActionClient } from './serverActionClient';

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 All @@ -15,7 +24,10 @@ export function createBrowserSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
Expand All @@ -28,9 +40,9 @@ export function createBrowserSupabaseClient<
cookieOptions?: CookieOptionsWithName;
} = {}) {
console.warn(
'Please utilize the `createPagesBrowserClient` function instead of the deprecated `createBrowserSupabaseClient` function.'
'Please utilize the `createPagesBrowserClient` function instead of the deprecated `createBrowserSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages'
);
return createPagesBrowserClient<Database, SchemaName>({
return createPagesBrowserClient<Database, SchemaName, Schema>({
supabaseUrl,
supabaseKey,
options,
Expand All @@ -45,7 +57,10 @@ export function createServerSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: GetServerSidePropsContext | { req: NextApiRequest; res: NextApiResponse },
{
Expand All @@ -61,9 +76,9 @@ export function createServerSupabaseClient<
} = {}
) {
console.warn(
'Please utilize the `createPagesServerClient` function instead of the deprecated `createServerSupabaseClient` function.'
'Please utilize the `createPagesServerClient` function instead of the deprecated `createServerSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages'
);
return createPagesServerClient<Database, SchemaName>(context, {
return createPagesServerClient<Database, SchemaName, Schema>(context, {
supabaseUrl,
supabaseKey,
options,
Expand All @@ -78,7 +93,10 @@ export function createMiddlewareSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { req: NextRequest; res: NextResponse },
{
Expand All @@ -94,13 +112,127 @@ export function createMiddlewareSupabaseClient<
} = {}
) {
console.warn(
'Please utilize the `createMiddlewareClient function instead of the deprecated `createMiddlewareSupabaseClient` function.'
'Please utilize the `createMiddlewareClient` function instead of the deprecated `createMiddlewareSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#middleware'
);

return createMiddlewareClient<Database, SchemaName, Schema>(context, {
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}

/**
* @deprecated utilize the `createClientComponentClient` function instead
*/
export function createClientComponentSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}) {
console.warn(
'Please utilize the `createClientComponentClient` function instead of the deprecated `createClientComponentSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#client-component'
);

return createMiddlewareClient<Database, SchemaName>(context, {
return createClientComponentClient<Database, SchemaName, Schema>({
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}

/**
* @deprecated utilize the `createServerComponentClient` function instead
*/
export function createServerComponentSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { headers: () => ReadonlyHeaders; cookies: () => ReadonlyRequestCookies },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createServerComponentClient` function instead of the deprecated `createServerComponentSupabaseClient` function. Additionally, this function no longer requires the `headers` function as a parameter. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#server-component'
);

return createServerComponentClient<Database, SchemaName, Schema>(
{ cookies: context.cookies },
{
supabaseUrl,
supabaseKey,
options,
cookieOptions
}
);
}

/**
* @deprecated utilize the `createRouteHandlerClient` function instead
*/
export function createRouteHandlerSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { headers: () => ReadonlyHeaders; cookies: () => ReadonlyRequestCookies },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createRouteHandlerClient` function instead of the deprecated `createRouteHandlerSupabaseClient` function. Additionally, this function no longer requires the `headers` function as a parameter. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#route-handler'
);

return createRouteHandlerClient<Database, SchemaName, Schema>(
{ cookies: context.cookies },
{
supabaseUrl,
supabaseKey,
options,
cookieOptions
}
);
}
15 changes: 11 additions & 4 deletions packages/nextjs/src/middlewareClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {
serializeCookie,
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { NextRequest, NextResponse } from 'next/server';
import { NextResponse } from 'next/server';
import { splitCookiesString } from 'set-cookie-parser';

import type { NextRequest } from 'next/server';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';

class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: { req: NextRequest; res: NextResponse },
Expand Down Expand Up @@ -60,7 +64,10 @@ export function createMiddlewareClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { req: NextRequest; res: NextResponse },
{
Expand All @@ -74,14 +81,14 @@ export function createMiddlewareClient<
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}

return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand Down
12 changes: 9 additions & 3 deletions packages/nextjs/src/pagesServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import { splitCookiesString } from 'set-cookie-parser';

import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';

class NextServerAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context:
Expand Down Expand Up @@ -57,7 +60,10 @@ export function createPagesServerClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: GetServerSidePropsContext | { req: NextApiRequest; res: NextApiResponse },
{
Expand All @@ -71,14 +77,14 @@ export function createPagesServerClient<
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}

return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand Down
11 changes: 8 additions & 3 deletions packages/nextjs/src/routeHandlerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from '@supabase/auth-helpers-shared';

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(
Expand Down Expand Up @@ -39,7 +41,10 @@ export function createRouteHandlerClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: {
cookies: () => ReadonlyRequestCookies;
Expand All @@ -55,14 +60,14 @@ export function createRouteHandlerClient<
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}

return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand Down
Loading

0 comments on commit a128b9e

Please sign in to comment.