Initializes a new instance of the ApiRequest class.
Parses the provided URL, extracts its origin and pathname, and initializes the URL property. If the URL contains query parameters, they are parsed and added to the Parameters object using AddParameter. Optionally accepts a custom URI encoding method.
The request URL to initialize the ApiRequest with.
Optional
uriEncodeMethod: TUriEncodeFunctionOptional. A custom method for encoding URI components.
Static
Readonly
SignatureStatic
Readonly
SignatureStatic
Readonly
TimestampGets the URI encoding method to be used for requests.
If a custom encoding method (encodeURIMethod
) is defined on the instance, it returns that method.
Otherwise, it defaults to using the EncodeByRFC3986
method.
The function used to encode URIs.
Gets the URL query string constructed from the current Parameters
object,
excluding any signature-related parameters. The query string is built by
sorting the parameter keys, encoding both keys and values, and concatenating
them in the standard URL format. If a parameter value is undefined
, only
the key is included without a value assignment.
The constructed URL query string (starting with '?' or '&' as appropriate), with all keys and values URI-encoded.
Adds a parameter to the request.
The name of the parameter to add.
Optional
val: stringThe value of the parameter. Optional.
The current instance for method chaining.
Adds a randomly generated signature nonce parameter to the API request.
This method generates a random string of 6 characters and adds it as the value
for the SignatureNonce
parameter. Useful for ensuring request uniqueness and
preventing replay attacks.
The current instance of the API request for method chaining.
Adds the current timestamp as a parameter to the API request.
The current instance of the API request for method chaining.
Constructs a signed API request URL by generating a signature using the provided secret and signature method.
The secret key used to sign the request.
A function that generates a signature from the string to sign and the access secret.
A promise that resolves to the fully constructed and signed request URL as a string.
Static
VerifyVerifies the authenticity and validity of an API request based on its signature and timestamp.
The secret key used for signature verification.
The full URL containing the query parameters to verify.
A function that generates a signature from the URL and secret.
(Optional) Function to encode URI components, defaults to EncodeByRFC3986.
Encodes a string according to RFC 3986 specifications.
This function first applies encodeURIComponent
to the input string,
then further encodes the characters !
, *
, (
, )
, and '
to ensure
full compliance with RFC 3986.
The input string to encode.
The RFC 3986-compliant encoded string.
(Optional) Function to decode URI components, defaults to DecodeByRFC3986.
Decodes a URI component string according to RFC 3986 specifications.
This function first replaces specific percent-encoded characters (%21
, %2A
, %28
, %29
, %27
)
with their corresponding literal characters (!
, *
, (
, )
, '
) before applying decodeURIComponent
.
This ensures compatibility with RFC 3986, which allows these characters to remain unencoded in URI components.
The percent-encoded URI component string to decode.
The decoded string, with RFC 3986 reserved characters properly handled.
A promise that resolves to true
if verification succeeds, or an ApiRequestVerifyError
if verification fails.
Represents an API request builder that supports parameter management, signature generation, and verification for secure API communication.
This class provides methods to construct API request URLs with parameters, generate cryptographic signatures, and verify signed requests. It is designed to be compatible with signature-based authentication schemes.
Remarks
Timestamp
andSignatureNonce
parameters when making requests.Example
Since
1.1.6
Version
2021-12-09