@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
59 lines (58 loc) • 3.09 kB
TypeScript
import type { Class, UnixTimestamp } from '../types.js';
import type { BackendErrorResponseObject, ErrorData, ErrorObject } from './error.model.js';
/**
* Evaluates the `condition` (casts it to Boolean).
* Expects it to be truthy, otherwise throws AppError.
*
* Should be used NOT for "expected" / user-facing errors, but
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
*
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
*
* API is similar to Node's assert(), except:
* 1. Throws js-lib's AppError
* 2. Has a default message, if not provided
*
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
*/
export declare function _assert(condition: any, // will be evaluated as Boolean
message?: string, errorData?: ErrorData): asserts condition;
/**
* Like _assert(), but prints more helpful error message.
* API is similar to Node's assert.equals().
*
* Does SHALLOW, but strict equality (===), use _assertDeepEquals() for deep equality.
*/
export declare function _assertEquals<T>(actual: any, expected: T, message?: string, errorData?: ErrorData): asserts actual is T;
/**
* Like _assert(), but prints more helpful error message.
* API is similar to Node's assert.deepEquals().
*
* Does DEEP equality via _deepEquals()
*/
export declare function _assertDeepEquals<T>(actual: any, expected: T, message?: string, errorData?: ErrorData): asserts actual is T;
export declare function _assertIsError<ERR extends Error = Error>(err: any, errorClass?: Class<ERR>): asserts err is ERR;
/**
* Asserts that passed object is indeed an Error of defined ErrorClass.
* If yes - returns peacefully (with TypeScript assertion).
* In not - throws (re-throws) that error up.
*/
export declare function _assertErrorClassOrRethrow<ERR extends Error>(err: any, errorClass: Class<ERR>): asserts err is ERR;
export declare function _assertIsErrorObject<DATA_TYPE extends ErrorData = ErrorData>(obj: any): asserts obj is ErrorObject<DATA_TYPE>;
export declare function _assertIsBackendErrorResponseObject<DATA_TYPE extends ErrorData = ErrorData>(obj: any): asserts obj is BackendErrorResponseObject<DATA_TYPE>;
export declare function _assertIsString(v: any, message?: string): asserts v is string;
export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
/**
* Casts an arbitrary number as UnixTimestamp.
* Right now does not perform any validation (unlike `asUnixTimestamp2000`),
* but only type casting.
*/
export declare function asUnixTimestamp(n: number): UnixTimestamp;
/**
* Casts an arbitrary number as UnixTimestamp2000.
* Throws if the number is not inside 2000-01-01 and 2500-01-01 time interval,
* which would indicate a bug.
*/
export declare function asUnixTimestamp2000(n: number): UnixTimestamp;