Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F6092320
MwApiHttpClient.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
MwApiHttpClient.js
View Options
/**
* Minimal MediaWiki API HTTP client.
*
*/
export
class
MwApiHttpClient
{
constructor
(
{
cookies
,
options
}
)
{
this
.
cookies
=
cookies
;
this
.
apiUrl
=
options
.
apiUrl
;
this
.
verbose
=
options
.
verbose
;
}
async
request
(
params
)
{
const
url
=
new
URL
(
this
.
apiUrl
);
url
.
searchParams
.
set
(
'format'
,
'json'
);
const
body
=
new
URLSearchParams
();
for
(
const
[
key
,
value
]
of
Object
.
entries
(
params
)
)
{
body
.
append
(
key
,
value
);
}
// TODO we probably want to take the version from the package.json
// and use it in the User Agent.
// Headers since NodeJS 18 and stable in 21.
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const
headers
=
new
Headers
(
{
'Content-Type'
:
'application/x-www-form-urlencoded'
,
'User-Agent'
:
'wdio-core-api-client/1.0 (tests)'
}
);
const
cookieHeader
=
this
.
cookies
.
toHeader
();
if
(
cookieHeader
)
{
headers
.
set
(
'Cookie'
,
cookieHeader
);
}
try
{
// AbortSignal since NodeJS 16.14.0
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const
signal
=
AbortSignal
.
timeout
(
60000
);
// fetch was introduced in 18 and stable in 21.
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const
response
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
,
body
,
signal
}
);
const
text
=
await
response
.
text
();
if
(
this
.
verbose
)
{
console
.
log
(
'[API] API Response:'
,
text
);
}
if
(
!
response
.
ok
)
{
console
.
error
(
`[API] HTTP
${
response
.
status
}
:
${
response
.
statusText
}
`
);
throw
new
Error
(
`HTTP
${
response
.
status
}
`
);
}
// getSetCookie in NodeJS since 18.16.0
this
.
cookies
.
getCookiesFromHeaders
(
response
.
headers
.
getSetCookie
()
);
try
{
return
text
?
JSON
.
parse
(
text
)
:
{};
}
catch
(
error
)
{
console
.
error
(
'[API] Failed getting response as JSON:'
,
error
.
message
,
text
);
throw
error
;
}
}
catch
(
error
)
{
if
(
error
.
name
===
'AbortError'
)
{
console
.
error
(
'[API] Network timeout:'
,
error
.
message
,
{
url
:
this
.
apiUrl
}
);
}
throw
error
;
}
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Oct 5 2026, 23:06 (6 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ec/6c/0a3791637e2909d6d64488917b12
Default Alt Text
MwApiHttpClient.js (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment