{"_id":"@alapandas03/token-refresher","_rev":"3-3b8fbe67821ea521f44295790c4572ef","name":"@alapandas03/token-refresher","dist-tags":{"latest":"1.0.4"},"versions":{"1.0.0":{"name":"@alapandas03/token-refresher","version":"1.0.0","keywords":["api","stream","service","token-refresh","axios","refreshtoken","accesstoken"],"author":"","license":"MIT","_id":"@alapandas03/token-refresher@1.0.0","maintainers":[{"name":"alapandas03","email":"alapandas333@gmail.com"}],"homepage":"https://github.com/AlapanDastoken-refresher#readme","bugs":{"url":"https://github.com/AlapanDastoken-refresher/issues"},"dist":{"shasum":"21e1ff9364448535065316577cd4aa8d46e7ffe6","tarball":"https://registry.npmjs.org/@alapandas03/token-refresher/-/token-refresher-1.0.0.tgz","fileCount":3,"integrity":"sha512-MYWEpPnPDwlXP1Mnusu6dgK61heMl9lWF/5tAf9HSQ09QuF4oefUVUOpGVdX3dQ4XfPrIK8Nsyfh5D5geiSoOw==","signatures":[{"sig":"MEUCIQCWekZEonBb1UUFXcIJ3l90RtxiysYdDOEOfT5bIkzbFgIgVqGOZL5CfLw6Rw+7w2bwvKbMlJOdqBAtYVn6P3phiWc=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":7583},"jest":{"testEnvironment":"jsdom"},"main":"main.js","scripts":{"test":"exit 0"},"_npmUser":{"name":"alapandas03","email":"alapandas333@gmail.com"},"repository":{"url":"git+https://github.com/AlapanDastoken-refresher.git","type":"git"},"_npmVersion":"9.8.0","description":"A service for handling API requests with token refresh capabilities","directories":{},"_nodeVersion":"23.11.0","dependencies":{"axios":"^1.11.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.5","@types/jest":"^30.0.0","axios-mock-adapter":"^2.1.0","jest-environment-jsdom":"^30.0.0"},"_npmOperationalInternal":{"tmp":"tmp/token-refresher_1.0.0_1754026818151_0.25610063357588775","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@alapandas03/token-refresher","version":"1.0.1","keywords":["api","stream","service","token-refresh","axios","refreshtoken","accesstoken"],"author":"","license":"MIT","_id":"@alapandas03/token-refresher@1.0.1","maintainers":[{"name":"alapandas03","email":"alapandas333@gmail.com"}],"homepage":"https://github.com/AlapanDastoken-refresher#readme","bugs":{"url":"https://github.com/AlapanDastoken-refresher/issues"},"dist":{"shasum":"b230a90f81364acbe319c82bb09ba65e8298f197","tarball":"https://registry.npmjs.org/@alapandas03/token-refresher/-/token-refresher-1.0.1.tgz","fileCount":3,"integrity":"sha512-ppw3zY4PbuHtUahbYJJq1Isofe4+TQIF/T+8OcWZpmbWMvA7SuePRCD50KpQil7I88MojxDE1lM2LbFiqaN0eg==","signatures":[{"sig":"MEYCIQDZaruXEZ/37BotB1tBL3S3NX3cxXfbrAkLi19jJ+pFqQIhAJPIBbq5pZTAbmODQJX94OnwDqskf4934JUS57k/AtFp","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10983},"jest":{"testEnvironment":"jsdom"},"main":"main.js","scripts":{"test":"exit 0"},"_npmUser":{"name":"alapandas03","email":"alapandas333@gmail.com"},"repository":{"url":"git+https://github.com/AlapanDastoken-refresher.git","type":"git"},"_npmVersion":"9.8.0","description":"A service for handling API requests with token refresh capabilities","directories":{},"_nodeVersion":"23.11.0","dependencies":{"axios":"^1.11.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.5","@types/jest":"^30.0.0","axios-mock-adapter":"^2.1.0","jest-environment-jsdom":"^30.0.0"},"_npmOperationalInternal":{"tmp":"tmp/token-refresher_1.0.1_1754027142043_0.6277795202073873","host":"s3://npm-registry-packages-npm-production"}},"1.0.4":{"name":"@alapandas03/token-refresher","version":"1.0.4","description":"A service for handling API requests with token refresh capabilities","main":"main.js","scripts":{"test":"exit 0"},"keywords":["api","stream","service","token-refresh","axios","refreshtoken","accesstoken"],"author":"","license":"MIT","dependencies":{"axios":"^1.11.0"},"repository":{"type":"git","url":"git+https://github.com/AlapanDastoken-refresher.git"},"bugs":{"url":"https://github.com/AlapanDastoken-refresher/issues"},"homepage":"https://github.com/AlapanDastoken-refresher#readme","publishConfig":{"access":"public"},"devDependencies":{"@types/jest":"^30.0.0","axios-mock-adapter":"^2.1.0","jest":"^30.0.5","jest-environment-jsdom":"^30.0.0"},"jest":{"testEnvironment":"jsdom"},"_id":"@alapandas03/token-refresher@1.0.4","_nodeVersion":"23.11.0","_npmVersion":"9.8.0","dist":{"integrity":"sha512-6tfpxkSQlUgK0YlNe5rI404KFdHBnHp0QEdcqM7VWygeLGK61Hc4vXTJNGnOAlDfKohVLN5Sp10VScB/P+UmQw==","shasum":"bafc8cddb50aeb042df17d5fb8a7af9de4ff6ed1","tarball":"https://registry.npmjs.org/@alapandas03/token-refresher/-/token-refresher-1.0.4.tgz","fileCount":3,"unpackedSize":10958,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCohOnl4H6XkSVeb3hcm7ivEujyQXK2LrTsaIHH0BE+oAIhANGylpvIFxm6Lj3JjDTQd7fzXycnyBfAX1LHJcXUhoPP"}]},"_npmUser":{"name":"alapandas03","email":"alapandas333@gmail.com"},"directories":{},"maintainers":[{"name":"alapandas03","email":"alapandas333@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/token-refresher_1.0.4_1754027224789_0.9782381986270432"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-01T05:40:18.092Z","modified":"2025-08-01T05:47:05.114Z","1.0.0":"2025-08-01T05:40:18.312Z","1.0.1":"2025-08-01T05:45:42.196Z","1.0.4":"2025-08-01T05:47:04.968Z"},"bugs":{"url":"https://github.com/AlapanDastoken-refresher/issues"},"license":"MIT","homepage":"https://github.com/AlapanDastoken-refresher#readme","keywords":["api","stream","service","token-refresh","axios","refreshtoken","accesstoken"],"repository":{"type":"git","url":"git+https://github.com/AlapanDastoken-refresher.git"},"description":"A service for handling API requests with token refresh capabilities","maintainers":[{"name":"alapandas03","email":"alapandas333@gmail.com"}],"readme":"# @alapandas03/token-refresher\r\n\r\nA lightweight, customizable service for handling API requests with automatic token refresh capabilities. Perfect for applications requiring JWT or OAuth2 token management.\r\n\r\n## Features\r\n\r\n-  Automatic token refresh on 401 errors\r\n-  Smart request queuing during token refresh to prevent token race conditions\r\n-  Configurable token storage (localStorage, sessionStorage, or custom storage)\r\n-  Support for all HTTP methods (GET, POST, PUT, DELETE)\r\n-  Fully customizable refresh token endpoint\r\n-  Works in both browser and Node.js environments\r\n-  Built on top of Axios for reliable HTTP requests\r\n\r\n## Installation\r\n\r\n```bash\r\nnpm install @alapandas03/token-refresher\r\n```\r\n\r\n## Quick Start\r\n\r\n```javascript\r\nconst TokenRefresher = require('@alapandas03/token-refresher');\r\n\r\nconst api = new TokenRefresher({\r\n    baseURL: 'https://api.example.com',\r\n    refreshTokenEndpoint: '/auth/refresh'\r\n});\r\n\r\n// Example usage\r\nasync function fetchUserProfile() {\r\n    try {\r\n        const response = await api.get('/user/profile');\r\n        return response.data;\r\n    } catch (error) {\r\n        console.error('Error:', error);\r\n    }\r\n}\r\n```\r\n\r\n## Advanced Usage\r\n\r\n### 1. Custom Token Storage\r\n\r\nYou can customize where and how tokens are stored by providing custom functions:\r\n\r\n```javascript\r\nconst api = new TokenRefresher({\r\n    baseURL: 'https://api.example.com',\r\n    refreshTokenEndpoint: '/auth/refresh',\r\n    // Custom token management functions\r\n    getAccessToken: () => sessionStorage.getItem('my-access-token'),\r\n    getRefreshToken: () => sessionStorage.getItem('my-refresh-token'),\r\n    setAccessToken: (token) => sessionStorage.setItem('my-access-token', token),\r\n    clearTokens: () => {\r\n        sessionStorage.removeItem('my-access-token');\r\n        sessionStorage.removeItem('my-refresh-token');\r\n    }\r\n});\r\n```\r\n\r\n### 2. Handling Failed Token Refresh\r\n\r\nThe service automatically handles token refresh failures:\r\n- Clears existing tokens\r\n- Rejects all queued requests\r\n- Throws an error that you can catch to redirect to login\r\n\r\n```javascript\r\ntry {\r\n    const response = await api.get('/protected-endpoint');\r\n    return response.data;\r\n} catch (error) {\r\n    if (error.response?.status === 401) {\r\n        // Token refresh failed, redirect to login\r\n        window.location.href = '/login';\r\n    }\r\n}\r\n```\r\n\r\n### 3. Making Authenticated Requests\r\n\r\n```javascript\r\n// GET request\r\nconst getData = await api.get('/endpoint');\r\n\r\n// POST request with data\r\nconst postData = await api.post('/endpoint', {\r\n    key: 'value'\r\n});\r\n\r\n// PUT request\r\nconst putData = await api.put('/endpoint', {\r\n    key: 'updated value'\r\n});\r\n\r\n// DELETE request\r\nconst deleteData = await api.delete('/endpoint');\r\n```\r\n\r\n### 4. Configuration Options\r\n\r\n```javascript\r\nconst api = new TokenRefresher({\r\n    // Required options\r\n    baseURL: 'https://api.example.com',\r\n    refreshTokenEndpoint: '/auth/refresh',\r\n\r\n    // Optional token management (defaults to localStorage)\r\n    getAccessToken: () => customStorage.getToken(),\r\n    getRefreshToken: () => customStorage.getRefreshToken(),\r\n    setAccessToken: (token) => customStorage.setToken(token),\r\n    clearTokens: () => customStorage.clear(),\r\n\r\n    // Additional axios config options\r\n    timeout: 5000,\r\n    headers: {\r\n        'Custom-Header': 'value'\r\n    }\r\n});\r\n```\r\n\r\n## Common Use Cases\r\n\r\n1. **Single Page Applications (SPA)**\r\n   - Automatic token refresh without interrupting user experience\r\n   - Queued requests continue automatically after token refresh\r\n\r\n2. **Mobile-First Applications**\r\n   - Efficient token management for intermittent connections\r\n   - Customizable storage for mobile-specific requirements\r\n\r\n3. **Microservices Architecture**\r\n   - Consistent token management across multiple service calls\r\n   - Centralized refresh token handling\r\n\r\n4. **OAuth2 Implementations**\r\n   - Perfect for handling OAuth2 access/refresh token flows\r\n   - Automatic token refresh on expiration\r\n\r\n## Error Handling\r\n\r\nThe service handles various scenarios:\r\n\r\n```javascript\r\ntry {\r\n    const response = await api.get('/endpoint');\r\n    // Success handling\r\n} catch (error) {\r\n    if (error.response) {\r\n        // Server responded with error status\r\n        console.error('Server Error:', error.response.status);\r\n    } else if (error.request) {\r\n        // Request was made but no response\r\n        console.error('Network Error');\r\n    } else {\r\n        // Error in request configuration\r\n        console.error('Request Error:', error.message);\r\n    }\r\n}\r\n```\r\n\r\n## Best Practices\r\n\r\n1. **Token Storage**\r\n   - Use secure storage methods (HttpOnly cookies for refresh tokens)\r\n   - Consider using sessionStorage for access tokens in browsers\r\n\r\n2. **Error Handling**\r\n   - Implement proper error boundaries\r\n   - Handle token refresh failures gracefully\r\n\r\n3. **Security**\r\n   - Never store sensitive tokens in localStorage\r\n   - Implement proper CSRF protection\r\n\r\n## License\r\n\r\nMIT\r\n\r\n## Contributing\r\n\r\nContributions welcome! Please read the contributing guidelines before making a pull request.","readmeFilename":"README.md"}