Skip to content

Commit 0059b84

Browse files
codebienpkalsi97inancgumus
authored
🤖 Merge PR DefinitelyTyped#74444 [@types/k6] Update types for the new v1.6.0 version by @codebien
Co-authored-by: Pranjul Kalsi <pranjulkalsi@gmail.com> Co-authored-by: İnanç Gümüş <inanc.gumus@grafana.com>
1 parent 95c727a commit 0059b84

File tree

6 files changed

+183
-21
lines changed

6 files changed

+183
-21
lines changed

‎types/k6/browser/index.d.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,6 +4891,39 @@ export interface Page {
48914891
*/
48924892
on(event: "response", listener: (response: Response) => void): void;
48934893

4894+
/**
4895+
* Registers a handler function to listen for network requests that
4896+
* fail to reach the server (DNS errors, connection refused, timeouts, etc.).
4897+
* The handler will receive an instance of {@link Request}, which includes
4898+
* information about the failed request.
4899+
*
4900+
* **Usage**
4901+
*
4902+
* ```js
4903+
* page.on('requestfailed', request => {
4904+
* const failure = request.failure();
4905+
* console.log(`Request failed: ${request.url()}`);
4906+
* console.log(` Error: ${failure ? failure.errorText : 'unknown'}`);
4907+
* });
4908+
* ```
4909+
*/
4910+
on(event: "requestfailed", listener: (request: Request) => void): void;
4911+
4912+
/**
4913+
* Registers a handler function to listen for network requests that
4914+
* successfully complete (receive a response). The handler will receive an
4915+
* instance of {@link Request}, which includes information about the request.
4916+
*
4917+
* **Usage**
4918+
*
4919+
* ```js
4920+
* page.on('requestfinished', request => {
4921+
* console.log(`Request finished: ${request.method()} ${request.url()}`);
4922+
* });
4923+
* ```
4924+
*/
4925+
on(event: "requestfinished", listener: (request: Request) => void): void;
4926+
48944927
/**
48954928
* Returns the page that opened the current page. The first page that is
48964929
* navigated to will have a null opener.
@@ -4990,6 +5023,37 @@ export interface Page {
49905023
waitUntil?: "load" | "domcontentloaded" | "networkidle";
49915024
}): Promise<Response | null>;
49925025

5026+
/**
5027+
* Goes back to the previous page in the history.
5028+
*
5029+
* @example
5030+
* ```js
5031+
* await page.goto('https://example.com');
5032+
* await page.goto('https://example.com/page2');
5033+
* await page.goBack(); // Will navigate back to the first page
5034+
* ```
5035+
*
5036+
* @param options Navigation options.
5037+
* @returns A promise that resolves to the response of the requested navigation when it happens.
5038+
* Returns null if there is no previous entry in the history.
5039+
*/
5040+
goBack(options?: NavigationOptions): Promise<Response | null>;
5041+
5042+
/**
5043+
* Goes forward to the next page in the history.
5044+
*
5045+
* @example
5046+
* ```js
5047+
* await page.goBack(); // Navigate back first
5048+
* await page.goForward(); // Then navigate forward
5049+
* ```
5050+
*
5051+
* @param options Navigation options.
5052+
* @returns A promise that resolves to the response of the requested navigation when it happens.
5053+
* Returns null if there is no next entry in the history.
5054+
*/
5055+
goForward(options?: NavigationOptions): Promise<Response | null>;
5056+
49935057
/**
49945058
* Adds a route to the page to modify network requests made by that page.
49955059
*
@@ -6003,6 +6067,25 @@ export interface Request {
60036067
* @returns request URL
60046068
*/
60056069
url(): string;
6070+
6071+
/**
6072+
* Returns the failure info for a failed request, or null if the request succeeded.
6073+
* This method returns information about network failures such as DNS errors,
6074+
* connection refused, timeouts, etc. It does not return information for HTTP
6075+
* 4xx/5xx responses, which are successful network requests.
6076+
* @returns The failure information or null if the request succeeded.
6077+
*/
6078+
failure(): RequestFailure | null;
6079+
}
6080+
6081+
/**
6082+
* RequestFailure contains information about a failed request.
6083+
*/
6084+
export interface RequestFailure {
6085+
/**
6086+
* The error text describing why the request failed.
6087+
*/
6088+
errorText: string;
60066089
}
60076090

60086091
/**

‎types/k6/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/k6",
4-
"version": "1.5.9999",
4+
"version": "1.6.9999",
55
"type": "module",
66
"projects": [
77
"https://grafana.com/docs/k6/latest/"
@@ -28,7 +28,7 @@
2828
"./experimental/fs": "./experimental/fs/index.d.ts",
2929
"./experimental/redis": "./experimental/redis/index.d.ts",
3030
"./experimental/streams": "./experimental/streams/index.d.ts",
31-
"./experimental/websockets": "./experimental/websockets/index.d.ts"
31+
"./websockets": "./websockets/index.d.ts"
3232
},
3333
"owners": [
3434
{

‎types/k6/test/browser.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,68 @@ async function test() {
670670
response.url();
671671
});
672672

673+
// $ExpectType void
674+
page.on("requestfailed", request => {
675+
// $ExpectType Promise<Record<string, string>>
676+
request.allHeaders();
677+
// $ExpectType Frame
678+
request.frame();
679+
// $ExpectType Record<string, string>
680+
request.headers();
681+
// $ExpectType Promise<{ name: string; value: string; }[]>
682+
request.headersArray();
683+
// $ExpectType Promise<string | null>
684+
request.headerValue("content-type");
685+
// $ExpectType boolean
686+
request.isNavigationRequest();
687+
// $ExpectType string
688+
request.method();
689+
// $ExpectType string | null
690+
request.postData();
691+
// $ExpectType ArrayBuffer | null
692+
request.postDataBuffer();
693+
// $ExpectType ResourceType
694+
request.resourceType();
695+
// $ExpectType Promise<Response | null>
696+
request.response();
697+
// $ExpectType Promise<{ body: number; headers: number; }>
698+
request.size();
699+
// $ExpectType ResourceTiming
700+
request.timing();
701+
// $ExpectType RequestFailure | null
702+
request.failure();
703+
});
704+
705+
// $ExpectType void
706+
page.on("requestfinished", request => {
707+
// $ExpectType Promise<Record<string, string>>
708+
request.allHeaders();
709+
// $ExpectType Frame
710+
request.frame();
711+
// $ExpectType Record<string, string>
712+
request.headers();
713+
// $ExpectType Promise<{ name: string; value: string; }[]>
714+
request.headersArray();
715+
// $ExpectType Promise<string | null>
716+
request.headerValue("content-type");
717+
// $ExpectType boolean
718+
request.isNavigationRequest();
719+
// $ExpectType string
720+
request.method();
721+
// $ExpectType string | null
722+
request.postData();
723+
// $ExpectType ArrayBuffer | null
724+
request.postDataBuffer();
725+
// $ExpectType ResourceType
726+
request.resourceType();
727+
// $ExpectType Promise<Response | null>
728+
request.response();
729+
// $ExpectType Promise<{ body: number; headers: number; }>
730+
request.size();
731+
// $ExpectType ResourceTiming
732+
request.timing();
733+
});
734+
673735
// $ExpectType Promise<Page | null>
674736
page.opener();
675737

@@ -782,6 +844,24 @@ async function test() {
782844
// $ExpectType Promise<Response | null>
783845
page.reload({ waitUntil: "domcontentloaded" });
784846

847+
// $ExpectType Promise<Response | null>
848+
page.goBack();
849+
// $ExpectType Promise<Response | null>
850+
page.goBack({ timeout: 10000 });
851+
// $ExpectType Promise<Response | null>
852+
page.goBack({ waitUntil: "domcontentloaded" });
853+
// $ExpectType Promise<Response | null>
854+
page.goBack({ timeout: 10000, waitUntil: "load" });
855+
856+
// $ExpectType Promise<Response | null>
857+
page.goForward();
858+
// $ExpectType Promise<Response | null>
859+
page.goForward({ timeout: 10000 });
860+
// $ExpectType Promise<Response | null>
861+
page.goForward({ waitUntil: "domcontentloaded" });
862+
// $ExpectType Promise<Response | null>
863+
page.goForward({ timeout: 10000, waitUntil: "load" });
864+
785865
// $ExpectType Promise<ArrayBuffer>
786866
page.screenshot();
787867
// $ExpectType Promise<ArrayBuffer>

‎types/k6/test/websockets.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Blob, CompressionAlgorithm, EventName, WebSocket } from "k6/experimental/websockets";
21
import { CookieJar } from "k6/http";
2+
import { Blob, CompressionAlgorithm, EventName, WebSocket } from "k6/websockets";
33

44
let str: string;
55
let ab: ArrayBuffer;

‎types/k6/tsconfig.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"experimental/csv/index.d.ts",
3737
"experimental/fs/index.d.ts",
3838
"experimental/redis/index.d.ts",
39-
"experimental/websockets/index.d.ts",
4039
"experimental/streams/index.d.ts",
40+
"websockets/index.d.ts",
4141

4242
"test/browser.ts",
4343
"test/data.ts",

types/k6/experimental/websockets/index.d.ts renamed to types/k6/websockets/index.d.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { CookieJar } from "../../http/index.js";
2-
import { ReadableStream } from "../streams/index.js";
1+
import { ReadableStream } from "../experimental/streams/index.js";
2+
import { CookieJar } from "../http/index.js";
33

44
/**
5-
* This module provides an experimental implementation of the WebSocket API
6-
* for k6.
5+
* This module provides a WebSocket API for k6.
76
*
8-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/
7+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/
98
*/
109

1110
/**
@@ -43,7 +42,7 @@ export class WebSocket {
4342

4443
/**
4544
* The Websocket constructor returns a newly created WebSocket object.
46-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/
45+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/
4746
*
4847
* @param url - The URL to which to connect; this should be the URL to which the WebSocket server will respond.
4948
* @param protocols - Either a single protocol string or an array of protocol strings. The param is reserved for future use and will be presently ignored.
@@ -53,7 +52,7 @@ export class WebSocket {
5352

5453
/**
5554
* Enqueues data to be transmitted to the server over the WebSocket connection.
56-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-send/
55+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-send/
5756
*
5857
* @param data - the data to send to the server
5958
*/
@@ -62,7 +61,7 @@ export class WebSocket {
6261
/**
6362
* Bind event names to event handlers to be executed when their
6463
* respective event is received by the server.
65-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener/
64+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-addeventlistener/
6665
*
6766
* @param event - the event to listen for
6867
* @param listener - the callback to invoke when the event is emitted
@@ -71,7 +70,7 @@ export class WebSocket {
7170

7271
/**
7372
* Closes the WebSocket connection or connection attempt, if any.
74-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-close/
73+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-close/
7574
*
7675
* @param code - An integer WebSocket connection close code value indicating a reason for closure.
7776
* @param reason - A human-readable string WebSocket connection close reason. No longer than 123 bytes of UTF-8 text.
@@ -80,47 +79,47 @@ export class WebSocket {
8079

8180
/**
8281
* Sends a ping message over the WebSocket connection.
83-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-ping/
82+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-ping/
8483
*/
8584
ping(): void;
8685

8786
/**
8887
* Sets an event handler which is invoked when a message event happens.
89-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage/
88+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onmessage/
9089
*
9190
* @param event - the message event
9291
*/
9392
onmessage: (event?: MessageEvent) => void;
9493

9594
/**
9695
* Sets an event handler which is invoked when the WebSocket connection's opens.
97-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onopen/
96+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onopen/
9897
*/
9998
onopen: () => void;
10099

101100
/**
102101
* Sets an event handler which is invoked when the WebSocket connection's closes.
103-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onclose/
102+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onclose/
104103
*/
105104
onclose: () => void;
106105

107106
/**
108107
* Sets an event handler which is invoked when errors occur.
109-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onerror/
108+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onerror/
110109
*
111110
* @param event - the error event
112111
*/
113112
onerror: (event?: ErrorEvent) => void;
114113

115114
/**
116115
* Sets an event handler which is invoked when a ping message is received.
117-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onping/
116+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onping/
118117
*/
119118
onping: () => void;
120119

121120
/**
122121
* Sets an event handler which is invoked when a pong message is received.
123-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onpong/
122+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onpong/
124123
*/
125124
onpong: () => void;
126125
}
@@ -147,7 +146,7 @@ export class Blob {
147146

148147
/**
149148
* k6 specific WebSocket parameters.
150-
* https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/params/
149+
* https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/params/
151150
*/
152151
export interface Params {
153152
/** Request headers. */

0 commit comments

Comments
 (0)