Repository navigation
Conversation
Extends the tokens namespace on TinybirdClient with list, get, revoke (delete), refresh, and updateScopes, plus a getValue helper that returns a token's raw secret value (the SDK equivalent of `tb token copy`, minus the OS clipboard interaction, which doesn't make sense in an SDK context). - src/api/api.ts: add listTokens/getToken/deleteToken/refreshToken/ updateTokenScopes to the low-level TinybirdApi class, mirroring the existing createToken method. - src/api/tokens.ts: add corresponding wrapper functions following the exact style of the existing createJWT function (unchanged), including TokenApiError handling with context-aware messages. - src/client/tokens.ts: add TokensNamespace methods list/get/getValue/ revoke/refresh/updateScopes with JSDoc usage examples matching the existing createJWT docs. - Export new types/functions from src/index.ts. - Extend src/api/tokens.test.ts and src/client/tokens.test.ts with tests for every new method, following their existing style. Resolves PROTM-2272. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends the
tokensnamespace onTinybirdClientso it can manage tokens it didn't create, not just mint JWTs. Addslist(),get(nameOrId),revoke(nameOrId),refresh(nameOrId),updateScopes(nameOrId, scopes), andgetValue(nameOrId), backed by newGET /v0/tokens,GET /v0/tokens/{name_or_id},DELETE /v0/tokens/{name_or_id},POST /v0/tokens/{name_or_id}/refresh, andPUT /v0/tokens/{name_or_id}calls. This brings SDK token support roughly to parity withtb token ls/rm/refresh/scopes/copy.Linear issue: https://linear.app/tinybird/issue/PROTM-2272/typescript-sdk-add-token-lifecycle-management-refreshrevokescopescopy
The existing
createJWTmethod/function is untouched.Method names
client.tokens.list()—GET /v0/tokensclient.tokens.get(nameOrId)—GET /v0/tokens/{name_or_id}, result includesscopes(covers "scope inspection" — no separate scopes-only method was added since the get result already surfaces this)client.tokens.revoke(nameOrId)—DELETE /v0/tokens/{name_or_id}client.tokens.refresh(nameOrId)—POST /v0/tokens/{name_or_id}/refreshclient.tokens.updateScopes(nameOrId, scopes)—PUT /v0/tokens/{name_or_id}client.tokens.getValue(nameOrId)— the "copy" equivalent (see below)On "copy"
In the Forward CLI,
tb token copycallsclient.token_get(token_id)(i.e. the sameGET /v0/tokens/{id}used forget/scope-inspection) and then doespyperclip.copy(token["token"])— copying the raw secret value to the OS clipboard. Clipboard access isn't a meaningful SDK operation (no terminal/OS context, and headless/server environments have no clipboard), so this PR doesn't touch the OS clipboard at all. Instead,TokenInfo.value(mapped from the API'stokenfield) is already populated wheneverget()/list()/refresh()/updateScopes()expose it, andgetValue(nameOrId)is a small convenience wrapper that just returns that string so callers can do whatever they need with it (store it, hand it to another service, etc.). It throws aTokenApiErrorif the token doesn't expose a raw value (e.g. the admin token).Design notes
TinybirdApiinsrc/api/api.ts(listTokens,getToken,deleteToken,refreshToken,updateTokenScopes), mirroring the existingcreateTokenmethod, and wrapped insrc/api/tokens.tswith the sameTokenApiConfig/TokenApiErrorpattern and contextual error messages ascreateJWT.updateScopessendsPUT /v0/tokens/{name_or_id}with a JSON body{ scopes: [...] }, reusing the same{ type, resource, filter }scope shape theget/listresponses already return. (The legacy Python CLI client builds this as repeatedscope=TYPE:resource:filterquery params for old-style static tokens — a JSON body was chosen here for consistency with the JWT scope shape and because it's the more modern/idiomatic representation for a TS SDK.)Test plan
pnpm typecheckpassespnpm test:runpasses (1088 tests, including new tests insrc/api/tokens.test.tsandsrc/client/tokens.test.tscovering list/get/getValue/revoke/refresh/updateScopes, success and error paths)pnpm buildsucceedsCHANGELOG.mdandpackage.jsonversion untouched, per instructions🤖 Generated with Claude Code