{"id":20652505,"url":"https://github.com/pyncz/trpc-refresh-token-link","last_synced_at":"2026-03-17T08:37:49.633Z","repository":{"id":175721972,"uuid":"654345211","full_name":"pyncz/trpc-refresh-token-link","owner":"pyncz","description":"🔃 Refresh token link for tRPC client","archived":false,"fork":false,"pushed_at":"2023-12-28T23:59:34.000Z","size":68,"stargazers_count":16,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T19:39:15.736Z","etag":null,"topics":["helpers","jwt","trpc","typescript","utils"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@pyncz/trpc-refresh-token-link","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyncz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-15T23:44:37.000Z","updated_at":"2025-08-03T16:47:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"4ec04490-18a9-4d31-8864-193ed3f25d1d","html_url":"https://github.com/pyncz/trpc-refresh-token-link","commit_stats":null,"previous_names":["pyncz/trpc-refresh-token-link"],"tags_count":3,"template":false,"template_full_name":"pyncz/template.ts-lib","purl":"pkg:github/pyncz/trpc-refresh-token-link","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyncz%2Ftrpc-refresh-token-link","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyncz%2Ftrpc-refresh-token-link/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyncz%2Ftrpc-refresh-token-link/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyncz%2Ftrpc-refresh-token-link/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyncz","download_url":"https://codeload.github.com/pyncz/trpc-refresh-token-link/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyncz%2Ftrpc-refresh-token-link/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30619218,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-17T08:10:05.930Z","status":"ssl_error","status_checked_at":"2026-03-17T08:10:04.972Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["helpers","jwt","trpc","typescript","utils"],"created_at":"2024-11-16T17:35:20.922Z","updated_at":"2026-03-17T08:37:49.583Z","avatar_url":"https://github.com/pyncz.png","language":"TypeScript","readme":"# @pyncz/trpc-refresh-token-link\n\n🔃 Refresh token link for [tRPC](https://trpc.io/) client.\n\nInspired by the tRPC's built-in [`retryLink`](https://github.com/trpc/trpc/blob/1f4a2ead34ba0d1054dd47bfd66d082dc57a04bd/packages/client/src/links/retryLink.ts).\n\n## Install\nJust install `@pyncz/trpc-refresh-token-link` with your favorite package manager.\n```bash\npnpm install @pyncz/trpc-refresh-token-link\n```\n\n## Usage\nWith the setup below your `trpc` client will try to get a new JWT pair (access and refresh tokens) from your API should it get `401 UNAUTHORIZED`.\n\n```ts\nimport { createTRPCProxyClient, httpBatchLink } from '@trpc/client'\nimport { refreshTokenLink } from '@pyncz/trpc-refresh-token-link'\n\n/**\n * A helper client to use in `refreshTokenLink` to fetch a new jwt pair\n * Or you can use a regular http `fetch` if your API supports it\n */\nconst trpcClient = createTRPCProxyClient\u003cYourAppRouter\u003e({\n  transformer,\n  links: [\n    httpBatchLink({ /* ... */ }),\n  ],\n})\n\n/**\n * Your actual tRPC client\n */\nexport const trpc = createTRPCProxyClient\u003cYourAppRouter\u003e({\n  transformer: yourTransformer,\n  links: [\n    refreshTokenLink({\n      // Get locally stored refresh token\n      getRefreshToken: () =\u003e {\n        return storage.get('jwt')?.refresh\n      },\n\n      // Fetch a new JWT pair by refresh token from your API\n      fetchJwtPairByRefreshToken: (refreshToken) =\u003e {\n        return trpcClient.auth.refreshToken.query({ refreshToken })\n      },\n\n      // Callback on JWT pair is successfully fetched with `fetchJwtPairByRefreshToken`\n      onJwtPairFetched: (payload) =\u003e {\n        storage.set('jwt', payload)\n      },\n\n      // optional: Callback on JWT refresh request is failed\n      onRefreshFailed: () =\u003e {\n        // Probably you would like to remove stored jwt and log out the user here\n        storage.remove('jwt')\n      },\n\n      // optional: Callback on a request is failed with UNAUTHORIZED code,\n      // before the refresh flow is started\n      onUnauthorized: () =\u003e {\n        // Uh oh, just got 401!\n      },\n    }),\n\n    httpBatchLink({ /* ... */ }),\n  ],\n})\n```\n\n\u003e **Note**\n\u003e While the refresh flow is running, the outgoing requests will be paused until we get a new jwt pair (or get an error).\n\u003e \n\u003e We can't hold back *only procedures that require authentication* because we don't know which procedures are protected **beforehand** without additional meta.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyncz%2Ftrpc-refresh-token-link","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyncz%2Ftrpc-refresh-token-link","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyncz%2Ftrpc-refresh-token-link/lists"}