metaapi.cloud-sdk
Version:
SDK for MetaApi, a professional cloud forex API which includes MetaTrader REST API and MetaTrader websocket API. Supports both MetaTrader 5 (MT5) and MetaTrader 4 (MT4). CopyFactory copy trading API included. (https://metaapi.cloud)
145 lines (144 loc) • 6.09 kB
TypeScript
import { PromiseOrNot } from '../types/util';
/** An extended promise with additional properties and methods */
export interface HandlePromise<T> extends Promise<T> {
/** Whether the promise is resolved or rejected */
completed: boolean;
/** Whether the promise is resolved */
resolved?: boolean;
/** Whether the promise is rejected */
rejected?: boolean;
/** Result value the promise resolved with */
result?: T;
/** Error the promise rejected with */
error?: Error;
/**
* Resolves the promise with specified value
* @param result Value to resolve the promise with
*/
resolve(result?: T): void;
/**
* Rejects the promise with specified error
* @param err Error to reject the promise with
*/
reject(err: Error): void;
/**
* Adds a timeout to reject the promise with `TimeoutError`
* @param milliseconds timeout in milliseconds
* @param errorMessage error message
* @returns self
*/
timeout(milliseconds: number, errorMessage: string): HandlePromise<T>;
}
/**
* Creates a promise that can be used as a handle. It will not raise errors when rejected until it is explicitly
* awaited or catch is set
* @returns modified handle promise
*/
export declare function createHandlePromise<T>(): HandlePromise<T>;
/**
* Wraps a promise into a handle promise
* @param promise native promise
* @returns handle promise
*/
export declare function wrapHandlePromise<T>(promise: Promise<T>): HandlePromise<T>;
/**
* This function ensures that a promise is returned
* @param call call
* @returns promise
*/
export declare function ensurePromise<T>(call: () => PromiseOrNot<T>): Promise<T>;
/** Additional delay options */
export type DelayOptions = {
/** Whether to delay real time, if a stubbed frozen `sinon` clock is used */
ignoreSinonClock?: boolean;
};
/**
* Waits specified delay
* @param ms Milliseconds to wait
* @param options Additional options
* @return promise resolving when the delay has ended
*/
export declare function delay(ms: number, options?: DelayOptions): DelayPromise;
/**
* Delay promise
*/
export interface DelayPromise extends Promise<void> {
/**
* Returns whether the promise is canceled
* @returns whether canceled
*/
get canceled(): boolean;
/**
* Cancels waiting and resolves the promise immediately
*/
cancel(): void;
}
/**
* Assembles log4js config from logging level map
* @param {Object} [config] log4js config
* @param {String} [config.defaultLevel = 'INFO'] Default logging level
* @param {Object} [config.levels] Logging levels
* @return {Object} Log4js config
*/
export declare function assembleLog4jsConfig(config?: any): {
appenders: {
console: {
type: string;
};
};
categories: {
default: {
appenders: string[];
level: any;
};
};
};
/** Options for `wait*` functions */
export type WaitOptions = {
/** Wait timeout in milliseconds. Defaults to `30000` */
timeoutInMs?: number;
};
/**
* Waits untill specified callable will pass successfully and return true. Uses log4js logger named `helpers.wait`
* @param {() => boolean|Promise<boolean>} callable Callable to call until it returns true
* @param {Number} [intervalInMs = 25] Interval in milliseconds between the checks
* @param {WaitOptions & DelayOptions} [options] Additional wait options
* @return {Promise} Promise resolving with callable return value when waited
* @throws {Error|TimeoutError} Error from the callable or timeout error when timed out
*/
export declare function wait(callable: any, intervalInMs?: number, options?: WaitOptions & DelayOptions): Promise<boolean>;
/**
* Waits untill specified callable will pass successfully and return true. Uses log4js logger named `helpers.wait`
* @param {() => boolean|Promise<boolean>} callable Callable to call until it returns true
* @param {Number} [intervalInMs = 25] Interval in milliseconds between the checks
* @param {WaitOptions & DelayOptions} [options] Additional wait options
* @return {Promise} Promise resolving with callable return value when waited
* @throws {Error|TimeoutError} Error from the callable or timeout error when timed out
*/
export declare function waitTrue(callable: any, intervalInMs?: number, options?: WaitOptions & DelayOptions): Promise<boolean>;
/**
* Waits untill specified callable will pass successfully. Uses log4js logger named `helpers.wait`
* @param {() => boolean|Promise<boolean>} callable Callable to call
* @param {Number} [intervalInMs = 25] Interval in milliseconds between the checks
* @param {WaitOptions & DelayOptions} [options] Additional wait options
* @return {Promise} Promise resolving with callable return value when waited
* @throws {Error|TimeoutError} Error from the callable or timeout error when timed out
*/
export declare function waitPass<T = void>(callable: () => PromiseOrNot<T>, intervalInMs?: number, options?: WaitOptions & DelayOptions): Promise<T>;
/**
* Waits untill specified callable successfully returns any non-undefined value. Uses log4js logger named `helpers.wait`
* @param callable Callable to call
* @param intervalInMs Interval in milliseconds between the checks
* @param options Additional wait options
* @return Promise resolving with callable return value when waited
* @throws {Error|TimeoutError} Error from the callable or timeout error when timed out
*/
export declare function waitAny<T>(callable: () => PromiseOrNot<T>, intervalInMs?: number, options?: WaitOptions): Promise<T>;
/**
* Calculates exponential backoff delay. At the initial iteration, there is no delay. At the next iteration, the delay
* is `startDelay`. Further, at the every next iteration the previous delay multiplies to 2
* @param iteration current iteration, where 0 is initial iteration without delaying
* @param startDelay start delay
* @param maxDelay maximum delay
*/
export declare function expBackoffDelay(iteration: number, startDelay: number, maxDelay: number): number;