diff --git a/README.md b/README.md index 3eec14c..7f2be2d 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,12 @@ You can set `content` flag to `false` to skip loading `content` fields in the HA cy.recordHar({ content: false }); ``` +To include only requests on specific hosts, you can specify a list of hosts using `includeHosts`. + +```js +cy.recordHar({ includeHosts: [ '.*.execute-api.eu-west-1.amazonaws.com'] }); +``` + To exclude some requests, you can specify a list of paths to be excluded using `excludePaths`. ```js diff --git a/src/Plugin.ts b/src/Plugin.ts index 2e0a507..e74e7c7 100644 --- a/src/Plugin.ts +++ b/src/Plugin.ts @@ -17,6 +17,7 @@ export interface SaveOptions { export interface RecordOptions { content: boolean; excludePaths: string[]; + includeHosts: string[]; } export class Plugin { diff --git a/src/network/NetworkObserver.ts b/src/network/NetworkObserver.ts index 96ab79d..d353028 100644 --- a/src/network/NetworkObserver.ts +++ b/src/network/NetworkObserver.ts @@ -542,9 +542,19 @@ export class NetworkObserver { } private excludeRequest(request: NetworkRequest): boolean { - const { path = '/' } = request.parsedURL; + const { host, path = '/' } = request.parsedURL; + const { includeHosts, excludePaths } = this.options; + if (includeHosts?.length > 0) { + if ( + !includeHosts.some((hostPattern: string): boolean => + new RegExp(hostPattern).test(host) + ) + ) { + return true; + } + } - return !!this.options.excludePaths?.some((excludedPath: string): boolean => + return !!excludePaths?.some((excludedPath: string): boolean => new RegExp(excludedPath).test(path) ); }