-
-
Notifications
You must be signed in to change notification settings - Fork 128
HTTP Hooks
As of v3.19.0, all HTTP requests initiated by node-steamcommunity
(and some other modules) go through a unified interface which fires pre- and post-hooks.
Due to the nature of the pre-hook, you can have up to exactly one pre-request hook. To register the pre-hook, assign a function to the onPreHttpRequest
property. It should have the following arguments:
-
requestID
- A unique numeric ID which is assigned to this request (will be passed to the post hook as well) -
source
- A string describing the source of this request ("steamcommunity" if it originated inside ofnode-steamcommunity
, or optionally something else if it originated inside of another module). Might be empty string. -
options
- An object containing the request's options (including itsuri
andmethod
). This is passed to therequest
module. -
continueRequest
- A function which you should call to either allow or block the request (see below).
If you want to defer or block this request, return true
inside of the pre-hook function. Return false
, something falsy, or nothing (undefined) to allow it to continue as normal.
To defer the request, return true
and call continueRequest
with no arguments when you want the request to continue. To block it, call continueRequest
with an Error
object as the first argument (this Error
will be passed to the callback of whatever made the request).
You can also edit any of the properties in options
.
A post-hook is a bit simpler. After an HTTP request completes, node-steamcommunity
will emit a postHttpRequest
method with the following arguments:
-
requestID
- The unique numeric request ID which was passed to the pre-hook -
source
- A string describing the source of this request ("steamcommunity" if it originated inside ofnode-steamcommunity
, or optionally something else if it originated inside of another module). Might be empty string. -
options
- An object containing the request's options (including itsuri
andmethod
). This was passed to therequest
module. -
err
- If an error occurred (e.g. network error, HTTP error, Steam error, trade error) then this is anError
object.null
otherwise. -
response
- Theresponse
object from therequest
module. -
body
- The response body. Ifoptions.json
is true, then this is an object. Otherwise it's a string. -
details
- An object containing metadata about the response-
hasCallback
-true
if whatever initiated this request provided a callback,false
if not -
httpError
- If an HTTP error occurred, this is anError
object -
communityError
- If a Steam Community error occurred, this is anError
object -
tradeError
- If a trading error occurred, this is anError
object
-
If you have a module which integrates with node-steamcommunity
and you want to go through this unified HTTP interface (e.g. node-steam-tradeoffer-manager
does this), then this is all you need to know:
-
uri
- Optional. A string containing the request URI -
options
- Optional. An object containing request options (this object will be passed to therequest
module) -
callback
- Optional. Called when the request completes. -
source
- Optional (but highly encouraged). A string which is passed to hooks as thesource
value.
All arguments are optional, but you need one of uri
and options
. If uri
isn't provided, then either options.uri
or options.url
must be defined with the request URI. The options
object will be passed to the request
module (if provided). If you don't specify a method
in options
, then it will default to GET.
Convenience method which performs a GET request.
Convenience method which performs a POST request.