From 00b05c0a5ec02d0673ac1cb7684d8343f5e0aaea Mon Sep 17 00:00:00 2001 From: Volodymyr Momot <86200333+mmtv-qa@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:57:38 +0200 Subject: [PATCH 1/3] feat: add generic to json method added the ability to specify the json type during parsing --- packages/playwright-core/src/client/fetch.ts | 4 ++-- packages/playwright-core/src/client/network.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index 58928532ac9e4..2cfbbff14bcf1 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -350,9 +350,9 @@ export class APIResponse implements api.APIResponse { return content.toString('utf8'); } - async json(): Promise { + async json(): Promise { const content = await this.text(); - return JSON.parse(content); + return JSON.parse(content) as T; } async [Symbol.asyncDispose]() { diff --git a/packages/playwright-core/src/client/network.ts b/packages/playwright-core/src/client/network.ts index cb18681ccf4e3..25007db199baf 100644 --- a/packages/playwright-core/src/client/network.ts +++ b/packages/playwright-core/src/client/network.ts @@ -718,9 +718,9 @@ export class Response extends ChannelOwner implements return content.toString('utf8'); } - async json(): Promise { + async json(): Promise { const content = await this.text(); - return JSON.parse(content); + return JSON.parse(content) as T; } request(): Request { From 3693c4ac1345294f804c26f8dd0ad1884ab61068 Mon Sep 17 00:00:00 2001 From: Volodymyr Momot <86200333+mmtv-qa@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:05:16 +0200 Subject: [PATCH 2/3] Update types.d.ts --- packages/playwright-core/types/types.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index ba83204e7e596..1445e54d7af54 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -18433,7 +18433,7 @@ export interface APIResponse { * * This method will throw if the response body is not parsable via `JSON.parse`. */ - json(): Promise; + json(): Promise; /** * Contains a boolean stating whether the response was successful (status in the range 200-299) or not. @@ -20571,7 +20571,7 @@ export interface Response { * * This method will throw if the response body is not parsable via `JSON.parse`. */ - json(): Promise; + json(): Promise; /** * Contains a boolean stating whether the response was successful (status in the range 200-299) or not. From e4b1a689c521f7370f0dda2dfad7a0d7dc6fc034 Mon Sep 17 00:00:00 2001 From: Volodymyr Momot <86200333+mmtv-qa@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:17:39 +0200 Subject: [PATCH 3/3] Update types.d.ts --- packages/playwright-core/types/types.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index 1445e54d7af54..ba83204e7e596 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -18433,7 +18433,7 @@ export interface APIResponse { * * This method will throw if the response body is not parsable via `JSON.parse`. */ - json(): Promise; + json(): Promise; /** * Contains a boolean stating whether the response was successful (status in the range 200-299) or not. @@ -20571,7 +20571,7 @@ export interface Response { * * This method will throw if the response body is not parsable via `JSON.parse`. */ - json(): Promise; + json(): Promise; /** * Contains a boolean stating whether the response was successful (status in the range 200-299) or not.