Skip to content

Commit

Permalink
fix(testing): Use @clerk/backend package to fetch tokens (#3667)
Browse files Browse the repository at this point in the history
  • Loading branch information
anagstef authored Jul 5, 2024
1 parent aa35afd commit 5f502bf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-days-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/testing": patch
---

Use `@clerk/backend` package to fetch tokens
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"lint": "eslint src/"
},
"dependencies": {
"@clerk/backend": "1.3.0",
"@clerk/shared": "2.3.2",
"@clerk/types": "4.7.0",
"dotenv": "16.4.5"
Expand Down
2 changes: 0 additions & 2 deletions packages/testing/src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const TESTING_TOKEN_PARAM = '__clerk_testing_token';

export const TESTING_TOKEN_API_URL = 'https://api.clerk.com/v1/testing_tokens';
32 changes: 10 additions & 22 deletions packages/testing/src/common/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createClerkClient } from '@clerk/backend';
import { isProductionFromSecretKey, parsePublishableKey } from '@clerk/shared';
import dotenv from 'dotenv';

import { TESTING_TOKEN_API_URL } from './constants';
import type { ClerkSetupOptions, ClerkSetupReturn } from './types';

export const fetchEnvVars = async (options?: ClerkSetupOptions): Promise<ClerkSetupReturn> => {
Expand Down Expand Up @@ -41,29 +41,17 @@ export const fetchEnvVars = async (options?: ClerkSetupOptions): Promise<ClerkSe
);
}

const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${secretKey}`,
'Content-Type': 'application/json',
},
};

log('Fetching testing token from Clerk Backend API...');

const apiUrl = process.env.CLERK_API_URL;
const testingTokenApiUrl = apiUrl ? `${apiUrl}/v1/testing_tokens` : TESTING_TOKEN_API_URL;

await fetch(testingTokenApiUrl, options)
.then(response => {
return response.json();
})
.then(data => {
testingToken = data.token;
})
.catch(reason => {
throw new Error('Failed to fetch testing token from Clerk API. Error: ' + reason);
});
try {
const apiUrl = process.env.CLERK_API_URL;
const clerkClient = createClerkClient({ secretKey, apiUrl });
const tokenData = await clerkClient.testingTokens.createTestingToken();
testingToken = tokenData.token;
} catch (err) {
console.error('Failed to fetch testing token from Clerk API.');
throw err;
}
}

return {
Expand Down

0 comments on commit 5f502bf

Please sign in to comment.