UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

92 lines 2.65 kB
// Utility functions for cross-platform compatibility /** * Check if the code is running in a browser environment */ export const isBrowser = typeof globalThis !== 'undefined' && typeof globalThis.window !== 'undefined' && typeof globalThis.document !== 'undefined'; /** * Check if the code is running in a Node.js environment */ export const isNode = typeof globalThis !== 'undefined' && typeof globalThis.process !== 'undefined' && globalThis.process.versions && globalThis.process.versions.node; /** * Create a Buffer-like object that works in both browser and Node.js */ export function createBuffer(data) { if (isNode) { // In Node.js, use Buffer if (data instanceof ArrayBuffer) { return Buffer.from(data); } if (typeof data === 'string') { return Buffer.from(data, 'utf8'); } return data; } else { // In browser, use Uint8Array if (data instanceof ArrayBuffer) { return new Uint8Array(data); } if (typeof data === 'string') { return new TextEncoder().encode(data); } return new Uint8Array(data); } } /** * Convert Buffer/Uint8Array to string */ export function bufferToString(buffer) { if (isNode) { return buffer.toString('utf8'); } else { return new TextDecoder().decode(buffer); } } /** * Create a File object that works in both environments */ export function createFile(data, name, type) { if (isBrowser) { // In browser, use File constructor if (typeof data === 'string') { return new File([data], name, { type }); } return new File([data], name, { type }); } else { // In Node.js, use Blob (or you might want to use a different approach) if (typeof data === 'string') { return new Blob([data], { type }); } return new Blob([data], { type }); } } /** * Create FormData that works in both environments */ export function createFormData() { if (isBrowser) { return new FormData(); } else { // In Node.js, you might need to use a polyfill or different approach // For now, we'll assume FormData is available (it might be polyfilled) return new FormData(); } } /** * Check if a value is a cross-platform buffer */ export function isCrossPlatformBuffer(value) { if (typeof value !== 'object' || value === null) { return false; } return value instanceof Uint8Array || value instanceof ArrayBuffer; } //# sourceMappingURL=compatibility.js.map