Skip to content

Commit

Permalink
refactor pw response retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Sep 9, 2024
1 parent f2a8105 commit 7564f3e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/CacheRouteHandler/PwApiResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
*/
import path from 'node:path';

const pwCoreRoot = resolvePackageRoot('playwright-core');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pwClientModule = require(`${pwCoreRoot}/lib/client/api`);
// can't make it lazy, because it's used in the class extends
export const PwApiResponse = getPlaywrightClientApi().APIResponse;

export const PwApiResponse = pwClientModule.APIResponse;
function getPlaywrightClientApi() {
const pwCoreRoot = getPlaywrightCoreRoot();
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(`${pwCoreRoot}/lib/client/api`);
}

function getPlaywrightCoreRoot() {
const pwCoreRoot = resolvePackageRoot('playwright-core');
if (!pwCoreRoot) {
throw new Error('Cannot find playwright-core package. Please install @playwright/test');
}
return pwCoreRoot;
}

function resolvePackageRoot(packageName: string) {
const packageJsonPath = require.resolve(`${packageName}/package.json`);
Expand Down

0 comments on commit 7564f3e

Please sign in to comment.