@azure/core-util package
Interfaces
AbortOptions |
Options related to abort controller. |
CreateAbortablePromiseOptions |
Options for the createAbortablePromise function. |
DelayOptions |
Options for support abort functionality for the delay method |
Type Aliases
AbortablePromiseBuilder |
Represents a function that returns a promise that can be aborted. |
EncodingType |
The supported character encoding type |
HttpMethods |
Supported HTTP methods to use when making requests. |
UnknownObject |
A generic shape for a plain JS object. |
Functions
calculate |
Calculates the delay interval for retry attempts using exponential delay with jitter. |
cancelable |
promise.race() wrapper that aborts rest of promises as soon as the first promise settles. |
compute |
Generates a SHA-256 hash. |
compute |
Generates a SHA-256 HMAC signature. |
create |
Creates an abortable promise. |
delay(number, Delay |
A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. |
get |
Given what is thought to be an error object, return the message if possible. If the message is missing, returns a stringified version of the input. |
get |
Returns a random integer value between a lower and upper bound, inclusive of both bounds. Note that this uses Math.random and isn't secure. If you need to use this for any kind of security purpose, find a better source of random. |
is |
Helper TypeGuard that checks if something is defined or not. |
is |
Typeguard for an error object shape (has name and message) |
is |
Helper to determine when an input is a generic JS object. |
is |
Helper TypeGuard that checks if the input is an object with the specified properties. |
object |
Helper TypeGuard that checks if the input is an object with the specified property. |
randomUUID() | Generated Universally Unique Identifier |
string |
The helper that transforms string to specific character encoded bytes array. |
uint8Array |
The helper that transforms bytes with specific character encoding into string |
Function Details
calculateRetryDelay(number, { maxRetryDelayInMs: number, retryDelayInMs: number })
Calculates the delay interval for retry attempts using exponential delay with jitter.
function calculateRetryDelay(retryAttempt: number, config: { maxRetryDelayInMs: number, retryDelayInMs: number }): { retryAfterInMs: number }
Parameters
- retryAttempt
-
number
The current retry attempt number.
- config
-
{ maxRetryDelayInMs: number, retryDelayInMs: number }
The exponential retry configuration.
Returns
{ retryAfterInMs: number }
An object containing the calculated retry delay.
cancelablePromiseRace<T>(AbortablePromiseBuilder<T[number]>[], { abortSignal?: AbortSignalLike })
promise.race() wrapper that aborts rest of promises as soon as the first promise settles.
function cancelablePromiseRace<T>(abortablePromiseBuilders: AbortablePromiseBuilder<T[number]>[], options?: { abortSignal?: AbortSignalLike }): Promise<T[number]>
Parameters
- abortablePromiseBuilders
-
AbortablePromiseBuilder<T[number]>[]
- options
-
{ abortSignal?: AbortSignalLike }
Returns
Promise<T[number]>
computeSha256Hash(string, "base64" | "hex")
Generates a SHA-256 hash.
function computeSha256Hash(content: string, encoding: "base64" | "hex"): Promise<string>
Parameters
- content
-
string
The data to be included in the hash.
- encoding
-
"base64" | "hex"
The textual encoding to use for the returned hash.
Returns
Promise<string>
computeSha256Hmac(string, string, "base64" | "hex")
Generates a SHA-256 HMAC signature.
function computeSha256Hmac(key: string, stringToSign: string, encoding: "base64" | "hex"): Promise<string>
Parameters
- key
-
string
The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
- stringToSign
-
string
The data to be signed.
- encoding
-
"base64" | "hex"
The textual encoding to use for the returned HMAC digest.
Returns
Promise<string>
createAbortablePromise<T>((resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, CreateAbortablePromiseOptions)
Creates an abortable promise.
function createAbortablePromise<T>(buildPromise: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, options?: CreateAbortablePromiseOptions): Promise<T>
Parameters
- buildPromise
-
(resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void
A function that takes the resolve and reject functions as parameters.
- options
- CreateAbortablePromiseOptions
The options for the abortable promise.
Returns
Promise<T>
A promise that can be aborted.
delay(number, DelayOptions)
A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.
function delay(timeInMs: number, options?: DelayOptions): Promise<void>
Parameters
- timeInMs
-
number
The number of milliseconds to be delayed.
- options
- DelayOptions
The options for delay - currently abort options
Returns
Promise<void>
Promise that is resolved after timeInMs
getErrorMessage(unknown)
Given what is thought to be an error object, return the message if possible. If the message is missing, returns a stringified version of the input.
function getErrorMessage(e: unknown): string
Parameters
- e
-
unknown
Something thrown from a try block
Returns
string
The error message or a string of the input
getRandomIntegerInclusive(number, number)
Returns a random integer value between a lower and upper bound, inclusive of both bounds. Note that this uses Math.random and isn't secure. If you need to use this for any kind of security purpose, find a better source of random.
function getRandomIntegerInclusive(min: number, max: number): number
Parameters
- min
-
number
The smallest integer value allowed.
- max
-
number
The largest integer value allowed.
Returns
number
isDefined<T>(undefined | null | T)
Helper TypeGuard that checks if something is defined or not.
function isDefined<T>(thing: undefined | null | T): thing
Parameters
- thing
-
undefined | null | T
Anything
Returns
thing
isError(unknown)
Typeguard for an error object shape (has name and message)
function isError(e: unknown): e
Parameters
- e
-
unknown
Something caught by a catch clause.
Returns
e
isObject(unknown)
Helper to determine when an input is a generic JS object.
function isObject(input: unknown): input
Parameters
- input
-
unknown
Returns
input
true when input is an object type that is not null, Array, RegExp, or Date.
isObjectWithProperties<Thing, PropertyName>(Thing, PropertyName[])
Helper TypeGuard that checks if the input is an object with the specified properties.
function isObjectWithProperties<Thing, PropertyName>(thing: Thing, properties: PropertyName[]): thing
Parameters
- thing
-
Thing
Anything.
- properties
-
PropertyName[]
The name of the properties that should appear in the object.
Returns
thing
objectHasProperty<Thing, PropertyName>(Thing, PropertyName)
Helper TypeGuard that checks if the input is an object with the specified property.
function objectHasProperty<Thing, PropertyName>(thing: Thing, property: PropertyName): thing
Parameters
- thing
-
Thing
Any object.
- property
-
PropertyName
The name of the property that should appear in the object.
Returns
thing
randomUUID()
Generated Universally Unique Identifier
function randomUUID(): string
Returns
string
RFC4122 v4 UUID.
stringToUint8Array(string, EncodingType)
The helper that transforms string to specific character encoded bytes array.
function stringToUint8Array(value: string, format: EncodingType): Uint8Array
Parameters
- value
-
string
the string to be converted
- format
- EncodingType
the format we use to decode the value
Returns
Uint8Array
a uint8array
uint8ArrayToString(Uint8Array, EncodingType)
The helper that transforms bytes with specific character encoding into string
function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string
Parameters
- bytes
-
Uint8Array
the uint8array bytes
- format
- EncodingType
the format we use to encode the byte
Returns
string
a string of the encoded string