// resources/js/composables/useUrlEncoder.js // Utility for encoding/decoding hashkeys and payloads in URLs /** * Encode a hashkey value to URL format: h:HASHKEY */ export function encodeHash(hashkey) { if (!hashkey) return null; // Base64 encode the hashkey for URL safety try { const encoded = btoa(encodeURIComponent(hashkey)); return `h:${encoded}`; } catch (e) { console.error('[encodeHash] Error encoding hash:', e); return null; } } /** * Encode a payload object to URL format: e:BASE64_JSON */ export function encodePayload(payload) { if (!payload) return null; try { const json = JSON.stringify(payload); const encoded = btoa(encodeURIComponent(json)); return `e:${encoded}`; } catch (e) { console.error('[encodePayload] Error encoding payload:', e); return null; } } /** * Decode a hashkey from URL format */ export function decodeHash(encodedValue) { if (!encodedValue || !encodedValue.startsWith('h:')) return null; const parts = encodedValue.split(':'); if (parts.length < 2) return null; try { // Remove the 'h:' prefix and decode const base64 = encodedValue.substring(2); // First decode from base64, then decode URI components const decoded = atob(base64); return decodeURIComponent(decoded); } catch (e) { console.error('[decodeHash] Error decoding hash:', e); return null; } } /** * Decode a payload from URL format */ export function decodePayload(encodedValue) { if (!encodedValue || !encodedValue.startsWith('e:')) return null; const parts = encodedValue.split(':'); if (parts.length < 2) return null; try { // Remove the 'e:' prefix and decode const base64 = encodedValue.substring(2); // First decode from base64, then parse JSON const decodedJson = atob(base64); return JSON.parse(decodedJson); } catch (e) { console.error('[decodePayload] Error decoding payload:', e); return null; } } /** * Check if a value is a hashkey encoding */ export function isHashKeyFormat(value) { return typeof value === 'string' && value.startsWith('h:'); } /** * Check if a value is a payload encoding */ export function isPayloadFormat(value) { return typeof value === 'string' && value.startsWith('e:'); }