Skip to content

Commit a234b32

Browse files
jacekradkobrkalow
andauthored
feat(clerk-js): Add debug=skip_cache param to token requests (#7155)
Co-authored-by: Bryce Kalow <[email protected]>
1 parent 5aa9aff commit a234b32

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

.changeset/old-wombats-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Added debug query param to token requests initiated with `skipCache` option.

packages/clerk-js/src/core/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export const PRESERVED_QUERYSTRING_PARAMS = [
1212
];
1313

1414
export const CLERK_MODAL_STATE = '__clerk_modal_state';
15-
export const CLERK_SYNCED = '__clerk_synced';
16-
export const CLERK_SUFFIXED_COOKIES = 'suffixed_cookies';
1715
export const CLERK_SATELLITE_URL = '__clerk_satellite_url';
16+
export const CLERK_SUFFIXED_COOKIES = 'suffixed_cookies';
17+
export const CLERK_SYNCED = '__clerk_synced';
1818
export const ERROR_CODES = {
1919
FORM_IDENTIFIER_NOT_FOUND: 'form_identifier_not_found',
2020
FORM_PASSWORD_INCORRECT: 'form_password_incorrect',

packages/clerk-js/src/core/resources/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export class Session extends BaseResource implements SessionResource {
405405
// TODO: update template endpoint to accept organizationId
406406
const params: Record<string, string | null> = template ? {} : { organizationId };
407407

408-
const tokenResolver = Token.create(path, params);
408+
const tokenResolver = Token.create(path, params, skipCache);
409409

410410
// Cache the promise immediately to prevent concurrent calls from triggering duplicate requests
411411
SessionTokenCache.set({ tokenId, tokenResolver });

packages/clerk-js/src/core/resources/Token.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import type { JWT, TokenJSON, TokenJSONSnapshot, TokenResource } from '@clerk/shared/types';
22

3-
import { decode } from '../../utils';
4-
import { BaseResource } from './internal';
3+
import { decode } from '@/utils';
4+
5+
import { BaseResource } from './Base';
56

67
export class Token extends BaseResource implements TokenResource {
78
pathRoot = 'tokens';
89

910
jwt?: JWT;
1011

11-
static async create(path: string, body: any = {}): Promise<TokenResource> {
12+
static async create(path: string, body: any = {}, skipCache = false): Promise<TokenResource> {
13+
const search = skipCache ? `debug=skip_cache` : undefined;
14+
1215
const json = (await BaseResource._fetch<TokenJSON>({
13-
path,
14-
method: 'POST',
1516
body,
17+
method: 'POST',
18+
path,
19+
search,
1620
})) as unknown as TokenJSON;
1721

1822
return new Token(json, path);

packages/clerk-js/src/core/resources/__tests__/Token.test.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { InstanceType } from '@clerk/shared/types';
22
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from 'vitest';
33

4-
import { mockFetch, mockNetworkFailedFetch } from '@/test/core-fixtures';
4+
import { mockFetch, mockJwt, mockNetworkFailedFetch } from '@/test/core-fixtures';
55
import { debugLogger } from '@/utils/debug';
66

77
import { SUPPORTED_FAPI_VERSION } from '../../constants';
@@ -44,7 +44,7 @@ describe('Token', () => {
4444
});
4545

4646
describe('with offline browser and network failure', () => {
47-
let warnSpy;
47+
let warnSpy: ReturnType<typeof vi.spyOn>;
4848

4949
beforeEach(() => {
5050
Object.defineProperty(window.navigator, 'onLine', {
@@ -103,5 +103,54 @@ describe('Token', () => {
103103
});
104104
});
105105
});
106+
107+
it('creates token successfully with valid response', async () => {
108+
mockFetch(true, 200, { jwt: mockJwt });
109+
BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any;
110+
111+
const token = await Token.create('/path/to/tokens', { organizationId: 'org_123' });
112+
113+
expect(global.fetch).toHaveBeenCalledTimes(1);
114+
const [url, options] = (global.fetch as Mock).mock.calls[0];
115+
expect(url.toString()).toContain('https://clerk.example.com/v1/path/to/tokens');
116+
expect(url.toString()).not.toContain('debug=skip_cache');
117+
expect(options).toMatchObject({
118+
body: 'organization_id=org_123',
119+
credentials: 'include',
120+
method: 'POST',
121+
});
122+
expect(token).toBeInstanceOf(Token);
123+
expect(token.jwt).toBeDefined();
124+
});
125+
126+
it('creates token with skipCache=false by default', async () => {
127+
mockFetch(true, 200, { jwt: mockJwt });
128+
BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any;
129+
130+
await Token.create('/path/to/tokens');
131+
132+
const [url] = (global.fetch as Mock).mock.calls[0];
133+
expect(url.toString()).not.toContain('debug=skip_cache');
134+
});
135+
136+
it('creates token with skipCache=true and includes query parameter', async () => {
137+
mockFetch(true, 200, { jwt: mockJwt });
138+
BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any;
139+
140+
await Token.create('/path/to/tokens', {}, true);
141+
142+
const [url] = (global.fetch as Mock).mock.calls[0];
143+
expect(url.toString()).toContain('debug=skip_cache');
144+
});
145+
146+
it('creates token with skipCache=false explicitly and excludes query parameter', async () => {
147+
mockFetch(true, 200, { jwt: mockJwt });
148+
BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any;
149+
150+
await Token.create('/path/to/tokens', {}, false);
151+
152+
const [url] = (global.fetch as Mock).mock.calls[0];
153+
expect(url.toString()).not.toContain('debug=skip_cache');
154+
});
106155
});
107156
});

0 commit comments

Comments
 (0)