UNPKG

pagamio-frontend-commons-lib

Version:

Pagamio library for Frontend reusable components like the form engine and table container

125 lines (124 loc) 4.22 kB
export const createQueryConfig = (startDate, endDate, options, filters) => { return { startDate, endDate, filters, metrics: options.metrics ?? [], aggregationType: options.aggregationType ?? 'SUM', dimensions: options.dimensions, limit: options.limit, sortBy: options.sortBy, sortDirection: options.sortDirection, page: options.page, size: options.size, }; }; /** * Helper function to get nested object value by path */ export const getNestedValue = (obj, path) => { return path.split('.').reduce((acc, part) => acc?.[part], obj); }; /** * Converts a simple array to a SpringBoot response format * * @param {T[]} response - The array to convert into SpringBoot response format * @returns {SpringBootResponse<T>} A SpringBoot paginated response object with the array as content * @template T - The type of items in the array */ export function arrayToSpringBootResponse(response) { return { content: response, empty: response.length === 0, first: true, last: true, number: 0, numberOfElements: response.length, pageable: { sort: { empty: true, sorted: false, unsorted: true, }, offset: 0, pageNumber: 0, pageSize: response.length, paged: false, unpaged: true, }, size: response.length, sort: { empty: true, sorted: false, unsorted: true, }, totalElements: response.length, totalPages: 1, }; } /** * Generates a cryptographically secure random string * @param length - The length of the random string to generate (default: 6) * @returns A secure random string */ export const generateSecureRandomString = (length = 6) => { if (typeof window !== 'undefined' && window.crypto?.getRandomValues) { const array = new Uint8Array(length); window.crypto.getRandomValues(array); return Array.from(array, (byte) => byte.toString(36)) .join('') .substring(0, length); } // Node.js environment if (typeof require !== 'undefined') { try { const crypto = require('crypto'); return crypto.randomBytes(length).toString('hex').substring(0, length); } catch (error) { // If crypto module is not available, fall back to timestamp + counter console.warn('Crypto module not available, using timestamp-based fallback'); console.error('Error while generating secure random string:', error); const timestamp = Date.now().toString(36); const counter = (typeof performance !== 'undefined' ? performance.now() : Date.now()).toString(36); return (timestamp + counter).substring(0, length); } } //timestamp with counter const timestamp = Date.now().toString(36); const counter = (performance?.now() || Date.now()).toString(36); return (timestamp + counter).substring(0, length); }; /** * Uploads a file to a presigned URL using XMLHttpRequest */ export function uploadFileWithXHR(url, file) { return new Promise((resolve, reject) => { let acl = null; try { const parsed = new URL(url); acl = parsed.searchParams.get('x-amz-acl'); } catch { } const xhr = new XMLHttpRequest(); xhr.open('PUT', url, true); xhr.responseType = 'text'; xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) { resolve(); } else { reject(new Error(`Upload failed: ${xhr.status} ${xhr.response?.slice(0, 180)}`)); } }; xhr.onerror = () => { reject(new Error('Network error during upload')); }; const contentType = file.type || 'application/octet-stream'; if (contentType) xhr.setRequestHeader('Content-Type', contentType); if (acl) xhr.setRequestHeader('x-amz-acl', acl); xhr.send(file); }); }