@hirosystems/api-toolkit
Version:
API development toolkit
69 lines (68 loc) • 2.66 kB
TypeScript
export declare const isDevEnv: boolean;
export declare const isTestEnv: boolean;
export declare const isProdEnv: boolean;
/**
* Digests a string value into a SHA256 hash.
* @param content - String input
* @returns Hashed value
*/
export declare function sha256(content: string): string;
/**
* Parses a boolean string using conventions from CLI arguments, URL query params, and environmental
* variables. If the input is defined but empty string then true is returned. If the input is
* undefined or null than false is returned. For example, if the input comes from a CLI arg like
* `--enable_thing` or URL query param like `?enable_thing`, then this function expects to receive a
* defined but empty string, and returns true. Otherwise, it checks or values like `true`, `1`,
* `on`, `yes` (and the inverses). Throws if an unexpected input value is provided.
*/
export declare function parseBoolean(val: string | undefined | null): boolean;
/**
* Encodes a buffer as a `0x` prefixed lower-case hex string. Returns an empty string if the buffer
* is zero length.
*/
export declare function bufferToHex(buff: Buffer, prefix?: boolean): string;
/**
* Decodes a `0x` prefixed hex string to a buffer.
* @param hex - A hex string with a `0x` prefix.
*/
export declare function hexToBuffer(hex: string): Buffer;
/**
* Decodes a hex string to a Buffer, trims the 0x-prefix if exists.
* If already a buffer, returns the input immediately.
*/
export declare function coerceToBuffer(hex: string | Buffer | ArrayBufferView): Buffer;
/**
* Converts a hex string into a UTF-8 string.
* @param hex - Hex string
* @returns UTF-8 string
*/
export declare function hexToUtf8String(hex: string): string;
/**
* Converts a number to a hex string.
* @param number - Number
* @param paddingBytes - Padding bytes
* @returns Hex string
*/
export declare function numberToHex(number: number, paddingBytes?: number): string;
/**
* Checks if a string has `0x` prefix.
* @param val - Hex string
* @returns Boolean
*/
export declare const has0xPrefix: (val: string) => boolean;
/**
* Converts a string to an enum value.
* @param enumType - The enum type
* @param value - The string value to convert
* @returns Enum item or undefined
*/
export declare function toEnumValue<T>(enm: {
[s: string]: T;
}, value: string): T | undefined;
/**
* Unwraps a value that may be undefined or null.
* @param val - The value to unwrap
* @param onNullish - Callback to throw an error if the value is null or undefined
* @returns The unwrapped value
*/
export declare function unwrap<T>(val: T | null, onNullish?: () => string): Exclude<T, undefined | null>;