Skip to content

Commit

Permalink
feat(nextjs): Move shared NextJS SSR types to types package
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis committed Feb 28, 2022
1 parent 89bcde8 commit 78d8c7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
23 changes: 6 additions & 17 deletions packages/nextjs/src/middleware/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Session, User } from '@clerk/clerk-sdk-node';
import { GetSessionTokenOptions } from '@clerk/types';
import { ServerSideAuth } from '@clerk/types';
import { GetServerSidePropsContext } from 'next';

// TODO: Remove when we're using TS >=4.5
Expand All @@ -10,9 +10,7 @@ export type WithServerSideAuthOptions = {
loadSession?: boolean;
};

export type WithServerSideAuthCallback<Return, Options> = (
context: ContextWithAuth<Options>,
) => Return;
export type WithServerSideAuthCallback<Return, Options> = (context: ContextWithAuth<Options>) => Return;

export type WithServerSideAuthResult<CallbackReturn> = (
context: GetServerSidePropsContext,
Expand All @@ -26,16 +24,7 @@ export type AuthData = {
getToken: (...args: any) => Promise<string | null>;
};

export type ContextWithAuth<Options extends WithServerSideAuthOptions = any> =
GetServerSidePropsContext & {
auth: ServerSideAuth;
} & (Options extends { loadSession: true }
? { session: Session | null }
: {}) &
(Options extends { loadUser: true } ? { user: User | null } : {});

export type ServerSideAuth = {
sessionId: string | null;
userId: string | null;
getToken: (options?: GetSessionTokenOptions) => Promise<string | null>;
};
export type ContextWithAuth<Options extends WithServerSideAuthOptions = any> = GetServerSidePropsContext & {
auth: ServerSideAuth;
} & (Options extends { loadSession: true } ? { session: Session | null } : {}) &
(Options extends { loadUser: true } ? { user: User | null } : {});
13 changes: 9 additions & 4 deletions packages/types/src/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { SessionJSON, UserJSON } from './json';
import { SessionResource } from './session';
import { GetSessionTokenOptions } from './token';
import { UserResource } from './user';

export type ServerSideAuth = {
sessionId: string | null;
userId: string | null;
getToken: (options?: GetSessionTokenOptions) => Promise<string | null>;
};

type SsrSessionState<SessionType> =
| {
sessionId: null;
Expand All @@ -22,10 +29,8 @@ type SsrUserState<UserType> =
user: UserType | undefined;
};

export type SsrAuthData = SsrSessionState<SessionResource> &
SsrUserState<UserResource>;
export type ClerkSsrState = SsrSessionState<SessionJSON> &
SsrUserState<UserJSON>;
export type SsrAuthData = SsrSessionState<SessionResource> & SsrUserState<UserResource>;
export type ClerkSsrState = SsrSessionState<SessionJSON> & SsrUserState<UserJSON>;
export type InitialState =
| {
user: undefined;
Expand Down

0 comments on commit 78d8c7c

Please sign in to comment.