UNPKG

@openapi-qraft/react

Version:

OpenAPI client for React, providing type-safe requests and dynamic TanStack Query React Hooks via a modular, Proxy-based architecture.

59 lines (57 loc) 1.88 kB
class InvalidTokenError extends Error { } InvalidTokenError.prototype.name = 'InvalidTokenError'; /** * Decode a base64 string to a UTF-8 string. * Could be used in Node.js or in the browser. */ function b64DecodeUnicode(data) { if (typeof atob === 'function') return decodeURIComponent(Array.from(atob(data), byteToPercent).join('')); return Buffer.from(data, 'base64').toString('utf-8'); } function base64UrlDecode(str) { let output = str.replace(/-/g, '+').replace(/_/g, '/'); switch(output.length % 4){ case 0: break; case 2: output += '=='; break; case 3: output += '='; break; default: throw new Error('base64 string is not of the correct length'); } try { return b64DecodeUnicode(output); } catch (_err) { return atob(output); } } function jwtDecode(token, options) { if (typeof token !== 'string') { throw new InvalidTokenError('Invalid token specified: must be a string'); } options ||= {}; const pos = options.header === true ? 0 : 1; const part = token.split('.')[pos]; if (typeof part !== 'string') { throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`); } let decoded; try { decoded = base64UrlDecode(part); } catch (e) { throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`); } try { return JSON.parse(decoded); } catch (e) { throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`); } } function byteToPercent(b) { return `%${`00${b.charCodeAt(0).toString(16)}`.slice(-2)}`; } export { InvalidTokenError, b64DecodeUnicode, jwtDecode }; //# sourceMappingURL=index.js.map