Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/continueRequestParameters.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const { BytesValue, Header } = require('./networkTypes')
19
20
/**
21
* Represents the parameters for a continue request command.
22
* Described in https://w3c.github.io/webdriver-bidi/#command-network-continueRequest.
23
*/
24
class ContinueRequestParameters {
25
#map = new Map()
26
27
constructor(request) {
28
this.#map.set('request', request)
29
}
30
31
/**
32
* Sets the body value for the request.
33
*
34
* @param {BytesValue} value - The value to set as the body. Must be an instance of BytesValue.
35
* @returns {ContinueRequestParameters} - The current instance of the ContinueRequestParameters for chaining.
36
* @throws {Error} - If the value is not an instance of BytesValue.
37
*/
38
body(value) {
39
if (!(value instanceof BytesValue)) {
40
throw new Error(`Value must be an instance of BytesValue. Received: '${value})'`)
41
}
42
this.#map.set('body', Object.fromEntries(value.asMap()))
43
return this
44
}
45
46
/**
47
* Sets the cookies for the request.
48
*
49
* @param {Header[]} cookieHeaders - An array of cookie headers.
50
* @returns {ContinueRequestParameters} - The current instance of the ContinueRequestParameters for chaining.
51
* @throws {Error} - If a cookie header is not an instance of Header.
52
*/
53
cookies(cookieHeaders) {
54
const cookies = []
55
cookieHeaders.forEach((header) => {
56
if (!(header instanceof Header)) {
57
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
58
}
59
cookies.push(Object.fromEntries(header.asMap()))
60
})
61
62
this.#map.set('cookies', cookies)
63
return this
64
}
65
66
/**
67
* Sets the headers for the request.
68
*
69
* @param {Header[]} headers - An array of Header objects.
70
* @returns {ContinueRequestParameters} - The current instance of the ContinueRequestParameters for chaining.
71
* @throws {Error} - If the header value is not an instance of Header.
72
*/
73
headers(headers) {
74
const headerList = []
75
headers.forEach((header) => {
76
if (!(header instanceof Header)) {
77
throw new Error(`Header value must be an instance of Header. Received:'${header}'`)
78
}
79
headerList.push(Object.fromEntries(header.asMap()))
80
})
81
82
this.#map.set('headers', headerList)
83
return this
84
}
85
86
/**
87
* Sets the HTTP method for the request.
88
*
89
* @param {string} method - The HTTP method to be set.
90
* @returns {ContinueRequestParameters} - The updated `continueRequestParameters` object.
91
* @throws {Error} - If the method parameter is not a string.
92
*/
93
method(method) {
94
if (typeof method !== 'string') {
95
throw new Error(`Http method must be a string. Received: '${method})'`)
96
}
97
this.#map.set('method', method)
98
return this
99
}
100
101
/**
102
* Sets the URL for the request.
103
*
104
* @param {string} url - The URL to set for the request.
105
* @returns {ContinueRequestParameters} - The current instance of the ContinueRequestParameters for chaining.
106
* @throws {Error} - If the url parameter is not a string.
107
*/
108
url(url) {
109
if (typeof url !== 'string') {
110
throw new Error(`Url must be a string. Received:'${url}'`)
111
}
112
113
this.#map.set('url', url)
114
return this
115
}
116
117
asMap() {
118
return this.#map
119
}
120
}
121
122
module.exports = { ContinueRequestParameters }
123
124