homebridge-plugin-utils
Version:
Opinionated utilities to provide common capabilities and create rich configuration webUI experiences for Homebridge plugins.
68 lines (67 loc) • 2.98 kB
TypeScript
/**
* @internal
*
* A utility type that recursively makes all properties of an object, including nested objects, optional. This should only be used on JSON objects only. Otherwise,
* you're going to end up with class methods marked as optional as well. Credit for this belongs to: https://github.com/joonhocho/tsdef.
*
* @template T - The type to make recursively partial.
*/
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer I> ? Array<DeepPartial<I>> : DeepPartial<T[P]>;
};
/**
* @internal
*
* A utility type that recursively makes all properties of an object, including nested objects, optional. This should only be used on JSON objects only. Otherwise,
* you're going to end up with class methods marked as optional as well. Credit for this belongs to: https://github.com/joonhocho/tsdef.
*
* @template T - The type to make recursively partial.
*/
export type DeepReadonly<T> = {
readonly [P in keyof T]: T[P] extends Array<infer I> ? Array<DeepReadonly<I>> : DeepReadonly<T[P]>;
};
/**
* @internal
*
* A utility type that makes a given type assignable to it's type or null.
*/
export type Nullable<T> = T | null;
export interface HomebridgePluginLogging {
debug: (message: string, ...parameters: unknown[]) => void;
error: (message: string, ...parameters: unknown[]) => void;
info: (message: string, ...parameters: unknown[]) => void;
warn: (message: string, ...parameters: unknown[]) => void;
}
/**
* A utility method that formats a bitrate value into a human-readable form as kbps or Mbps.
*
* @param value - The bitrate value to convert.
*
* @returns Returns the value as a human-readable string.
*/
export declare function formatBps(value: number): string;
/**
* A utility method that retries an operation at a specific interval for up to an absolute total number of retries.
* @param operation - The operation callback to try until successful.
* @param retryInterval - Interval to retry, in milliseconds.
* @param totalRetries - Optionally, specify the total number of retries.
*
* @returns Returns `true` when the operation is successful, `false` otherwise or if the total number of retries has been exceeded.
*
* @remarks `operation` must be an asynchronous function that returns `true` when successful, and `false` otherwise.
*
* @category Utilities
*/
export declare function retry(operation: () => Promise<boolean>, retryInterval: number, totalRetries?: number): Promise<boolean>;
export declare function runWithTimeout<T>(promise: Promise<T>, timeout: number): Promise<Nullable<T>>;
export declare function sleep(sleepTimer: number): Promise<NodeJS.Timeout>;
/**
* A utility method that camel case's a string.
* @param string - The string to camel case.
*
* @returns Returns the camel cased string.
*
* @category Utilities
*/
export declare function toCamelCase(input: string): string;
export declare function validateName(name: string): string;