Skip to content

Commit

Permalink
chore(nextjs): Remove cyclic module dependency (#4689)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Dec 2, 2024
1 parent 8ee5d84 commit 666953d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .changeset/moody-chicken-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
35 changes: 2 additions & 33 deletions packages/nextjs/src/server/clerkClient.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import { createClerkClient } from '@clerk/backend';
import { constants } from '@clerk/backend/internal';

import { buildRequestLike, isPrerenderingBailout } from '../app-router/server/utils';
import { clerkMiddlewareRequestDataStorage } from './clerkMiddleware';
import {
API_URL,
API_VERSION,
DOMAIN,
IS_SATELLITE,
PROXY_URL,
PUBLISHABLE_KEY,
SDK_METADATA,
SECRET_KEY,
TELEMETRY_DEBUG,
TELEMETRY_DISABLED,
} from './constants';
import { createClerkClientWithOptions } from './createClerkClient';
import { clerkMiddlewareRequestDataStorage } from './middleware-storage';
import { decryptClerkRequestData, getHeader } from './utils';

const clerkClientDefaultOptions = {
secretKey: SECRET_KEY,
publishableKey: PUBLISHABLE_KEY,
apiUrl: API_URL,
apiVersion: API_VERSION,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
proxyUrl: PROXY_URL,
domain: DOMAIN,
isSatellite: IS_SATELLITE,
sdkMetadata: SDK_METADATA,
telemetry: {
disabled: TELEMETRY_DISABLED,
debug: TELEMETRY_DEBUG,
},
};

const createClerkClientWithOptions: typeof createClerkClient = options =>
createClerkClient({ ...clerkClientDefaultOptions, ...options });

/**
* Constructs a BAPI client that accesses request data within the runtime.
* Necessary if middleware dynamic keys are used.
Expand Down
6 changes: 1 addition & 5 deletions packages/nextjs/src/server/clerkMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AsyncLocalStorage } from 'node:async_hooks';

import type { AuthObject, ClerkClient } from '@clerk/backend';
import type { AuthenticateRequestOptions, ClerkRequest, RedirectFun, RequestState } from '@clerk/backend/internal';
import { AuthStatus, constants, createClerkRequest, createRedirect } from '@clerk/backend/internal';
Expand All @@ -12,6 +10,7 @@ import { withLogger } from '../utils/debugLogger';
import { clerkClient } from './clerkClient';
import { PUBLISHABLE_KEY, SECRET_KEY, SIGN_IN_URL, SIGN_UP_URL } from './constants';
import { errorThrower } from './errorThrower';
import { clerkMiddlewareRequestDataStorage, clerkMiddlewareRequestDataStore } from './middleware-storage';
import {
isNextjsNotFoundError,
isNextjsRedirectError,
Expand Down Expand Up @@ -80,9 +79,6 @@ interface ClerkMiddleware {
(request: NextMiddlewareRequestParam, event: NextMiddlewareEvtParam): NextMiddlewareReturn;
}

const clerkMiddlewareRequestDataStore = new Map<'requestData', AuthenticateRequestOptions>();
export const clerkMiddlewareRequestDataStorage = new AsyncLocalStorage<typeof clerkMiddlewareRequestDataStore>();

// @ts-expect-error TS is not happy here. Will dig into it
export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]) => {
const [request, event] = parseRequestAndEvent(args);
Expand Down
33 changes: 33 additions & 0 deletions packages/nextjs/src/server/createClerkClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createClerkClient } from '@clerk/backend';

import {
API_URL,
API_VERSION,
DOMAIN,
IS_SATELLITE,
PROXY_URL,
PUBLISHABLE_KEY,
SDK_METADATA,
SECRET_KEY,
TELEMETRY_DEBUG,
TELEMETRY_DISABLED,
} from './constants';

const clerkClientDefaultOptions = {
secretKey: SECRET_KEY,
publishableKey: PUBLISHABLE_KEY,
apiUrl: API_URL,
apiVersion: API_VERSION,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
proxyUrl: PROXY_URL,
domain: DOMAIN,
isSatellite: IS_SATELLITE,
sdkMetadata: SDK_METADATA,
telemetry: {
disabled: TELEMETRY_DISABLED,
debug: TELEMETRY_DEBUG,
},
};

export const createClerkClientWithOptions: typeof createClerkClient = options =>
createClerkClient({ ...clerkClientDefaultOptions, ...options });
6 changes: 6 additions & 0 deletions packages/nextjs/src/server/middleware-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AsyncLocalStorage } from 'node:async_hooks';

import type { AuthenticateRequestOptions } from '@clerk/backend/internal';

export const clerkMiddlewareRequestDataStore = new Map<'requestData', AuthenticateRequestOptions>();
export const clerkMiddlewareRequestDataStorage = new AsyncLocalStorage<typeof clerkMiddlewareRequestDataStore>();

0 comments on commit 666953d

Please sign in to comment.