UNPKG

@settlemint/sdk-utils

Version:

Shared utilities and helper functions for SettleMint SDK modules

143 lines 5.14 kB
//#region src/json.d.ts /** * Attempts to parse a JSON string into a typed value, returning a default value if parsing fails. * * @param value - The JSON string to parse * @param defaultValue - The value to return if parsing fails or results in null/undefined * @returns The parsed JSON value as type T, or the default value if parsing fails * * @example * import { tryParseJson } from "@settlemint/sdk-utils"; * * const config = tryParseJson<{ port: number }>( * '{"port": 3000}', * { port: 8080 } * ); * // Returns: { port: 3000 } * * const invalid = tryParseJson<string[]>( * 'invalid json', * [] * ); * // Returns: [] */ declare function tryParseJson<T>(value: string, defaultValue?: T | null): T | null; /** * Extracts a JSON object from a string. * * @param value - The string to extract the JSON object from * @returns The parsed JSON object, or null if no JSON object is found * @throws {Error} If the input string is too long (longer than 5000 characters) * @example * import { extractJsonObject } from "@settlemint/sdk-utils"; * * const json = extractJsonObject<{ port: number }>( * 'port info: {"port": 3000}', * ); * // Returns: { port: 3000 } */ declare function extractJsonObject<T>(value: string): T | null; /** * Converts a value to a JSON stringifiable format. * * @param value - The value to convert * @returns The JSON stringifiable value * * @example * import { makeJsonStringifiable } from "@settlemint/sdk-utils"; * * const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) }); * // Returns: '{"amount":"1000"}' */ declare function makeJsonStringifiable<T>(value: unknown): T; //#endregion //#region src/retry.d.ts /** * Retry a function when it fails. * @param fn - The function to retry. * @param maxRetries - The maximum number of retries. * @param initialSleepTime - The initial time to sleep between exponential backoff retries. * @param stopOnError - The function to stop on error. * @returns The result of the function or undefined if it fails. * @example * import { retryWhenFailed } from "@settlemint/sdk-utils"; * import { readFile } from "node:fs/promises"; * * const result = await retryWhenFailed(() => readFile("/path/to/file.txt"), 3, 1_000); */ declare function retryWhenFailed<T>(fn: () => Promise<T>, maxRetries?: number, initialSleepTime?: number, stopOnError?: (error: Error) => boolean): Promise<T>; //#endregion //#region src/string.d.ts /** * Capitalizes the first letter of a string. * * @param val - The string to capitalize * @returns The input string with its first letter capitalized * * @example * import { capitalizeFirstLetter } from "@settlemint/sdk-utils"; * * const capitalized = capitalizeFirstLetter("hello"); * // Returns: "Hello" */ declare function capitalizeFirstLetter(val: string): string; /** * Converts a camelCase string to a human-readable string. * * @param s - The camelCase string to convert * @returns The human-readable string * * @example * import { camelCaseToWords } from "@settlemint/sdk-utils"; * * const words = camelCaseToWords("camelCaseString"); * // Returns: "Camel Case String" */ declare function camelCaseToWords(s: string): string; /** * Replaces underscores and hyphens with spaces. * * @param s - The string to replace underscores and hyphens with spaces * @returns The input string with underscores and hyphens replaced with spaces * * @example * import { replaceUnderscoresAndHyphensWithSpaces } from "@settlemint/sdk-utils"; * * const result = replaceUnderscoresAndHyphensWithSpaces("Already_Spaced-Second"); * // Returns: "Already Spaced Second" */ declare function replaceUnderscoresAndHyphensWithSpaces(s: string): string; /** * Truncates a string to a maximum length and appends "..." if it is longer. * * @param value - The string to truncate * @param maxLength - The maximum length of the string * @returns The truncated string or the original string if it is shorter than the maximum length * * @example * import { truncate } from "@settlemint/sdk-utils"; * * const truncated = truncate("Hello, world!", 10); * // Returns: "Hello, wor..." */ declare function truncate(value: string, maxLength: number): string; //#endregion //#region src/url.d.ts /** * Extracts the base URL before a specific segment in a URL. * * @param baseUrl - The base URL to extract the path from * @param pathSegment - The path segment to start from * @returns The base URL before the specified segment * @example * ```typescript * import { extractBaseUrlBeforeSegment } from "@settlemint/sdk-utils/url"; * * const baseUrl = extractBaseUrlBeforeSegment("https://example.com/api/v1/subgraphs/name/my-subgraph", "/subgraphs"); * // Returns: "https://example.com/api/v1" * ``` */ declare function extractBaseUrlBeforeSegment(baseUrl: string, pathSegment: string): string; //#endregion export { camelCaseToWords, capitalizeFirstLetter, extractBaseUrlBeforeSegment, extractJsonObject, makeJsonStringifiable, replaceUnderscoresAndHyphensWithSpaces, retryWhenFailed, truncate, tryParseJson }; //# sourceMappingURL=index.d.ts.map