Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | /** * Utilities for SDK Development * This file contains common utilities for input validation, * date/time formatting, string manipulation and more */ /** * Input Validation Utility */ export class Validator { /** * Validates that a required field is present and non-empty. * @param value - The value to validate. * @param fieldName - The name of the field for error messages. */ static validateRequired(value: unknown, fieldName: string): void { Iif (value === undefined || value === null || value === '') { throw new Error(`${fieldName} is required.`); } } /** * Validates that a value is a positive number. * @param value - The value to validate. * @param fieldName - The name of the field for error messages. */ static validatePositiveNumber(value: unknown, fieldName: string): void { Iif (typeof value !== 'number' || value <= 0) { throw new Error(`${fieldName} must be a positive number.`); } } /** * Validates that a URL is properly formatted. * @param url - The URL to validate. * @param fieldName - The name of the field for error messages. */ static validateURL(url: string, fieldName: string): void { const urlRegex = /^(https?:\/\/)?([\w.-]+)+(\.[a-z]{2,})+(:[0-9]{1,5})?(\/[\w#!:.?+=&%@!-]*)?$/i; Iif (!urlRegex.test(url)) { throw new Error(`${fieldName} must be a valid URL.`); } } } /** * Date/Time Utility */ export class DateTimeUtil { /** * Formats a Date object into the required API format (YYYYMMDDHHmmss). * @param date - The date to format. * @returns The formatted date string. */ static formatToApiTimestamp(date: Date): string { const pad = (n: number) => n.toString().padStart(2, '0'); return ( date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate()) + pad(date.getHours()) + pad(date.getMinutes()) + pad(date.getSeconds()) ); } /** * Calculates the expiration time given a number of seconds. * @param expiresInSeconds - The expiration duration in seconds. * @returns The expiration timestamp as a Date object. */ static calculateExpiryTime(expiresInSeconds: number): Date { return new Date(Date.now() + expiresInSeconds * 1000); } } /** * String Utility */ export class StringUtil { /** * Encodes a string to Base64. * @param value - The string to encode. * @returns The Base64-encoded string. */ static toBase64(value: string): string { return Buffer.from(value).toString('base64'); } /** * Truncates a string to the specified length, adding "..." if truncated. * @param value - The string to truncate. * @param maxLength - The maximum allowed length. * @returns The truncated string. */ static truncate(value: string, maxLength: number): string { return value.length > maxLength ? value.substring(0, maxLength) + '...' : value; } } /** * Exponential Backoff Utility */ export class RetryUtil { /** * Calculates the delay for a retry attempt using exponential backoff. * @param attempt - The current retry attempt (0-indexed). * @param baseDelay - The base delay in milliseconds (default: 100ms). * @returns The calculated delay in milliseconds. */ static calculateBackoffDelay(attempt: number, baseDelay: number = 100): number { return Math.pow(2, attempt) * baseDelay; } } export const Utils = { Validator, DateTimeUtil, StringUtil, RetryUtil, }; |