@elusion-sdk/briq
Version:
A modern TypeScript SDK for Briq SMS API integration
59 lines • 2.01 kB
JavaScript
import { PAGINATION_DEFAULTS } from "./constants";
export function formatPhoneNumber(phoneNumber) {
let cleaned = phoneNumber.replace(/[^\d+]/g, "");
if (!cleaned.startsWith("+")) {
cleaned = `+${cleaned}`;
}
return cleaned;
}
export function formatPhoneNumbers(phoneNumbers) {
return phoneNumbers.map(formatPhoneNumber);
}
export function calculateSMSSegments(message) {
const basicLength = 160;
const unicodeLength = 70;
const hasUnicode = /[^\x00-\x7F]/.test(message);
const segmentLength = hasUnicode ? unicodeLength : basicLength;
return Math.ceil(message.length / segmentLength);
}
export function normalizePaginationParams(params) {
const page = Math.max(1, params.page || PAGINATION_DEFAULTS.PAGE);
const limit = Math.min(PAGINATION_DEFAULTS.MAX_LIMIT, Math.max(1, params.limit || PAGINATION_DEFAULTS.LIMIT));
const offset = params.offset || (page - 1) * limit;
return { page, limit, offset };
}
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function calculateBackoffDelay(attempt, baseDelay = 1000, maxDelay = 10000, factor = 2) {
const delay = baseDelay * Math.pow(factor, attempt - 1);
return Math.min(delay, maxDelay);
}
export function isRetryableError(error) {
if (error.code === "NETWORK_ERROR" || error.code === "TIMEOUT_ERROR") {
return true;
}
if (error.statusCode) {
return error.statusCode >= 500 || error.statusCode === 429;
}
return false;
}
export function deepClone(obj) {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (Array.isArray(obj)) {
return obj.map((item) => deepClone(item));
}
const cloned = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
}
//# sourceMappingURL=helpers.js.map