Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 740c4a2

Browse files
committed
type for response
1 parent 2ce518c commit 740c4a2

File tree

7 files changed

+23
-36
lines changed

7 files changed

+23
-36
lines changed

cli/tokens.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/google/uuid"
109
"github.com/spf13/cobra"
1110
"golang.org/x/exp/slices"
1211
"golang.org/x/xerrors"
@@ -99,16 +98,14 @@ type tokenListRow struct {
9998
Owner string `json:"-" table:"owner"`
10099
}
101100

102-
func tokenListRowFromToken(token codersdk.APIKey, usersByID map[uuid.UUID]codersdk.User) tokenListRow {
103-
user := usersByID[token.UserID]
104-
101+
func tokenListRowFromToken(token codersdk.ConvertedAPIKey) tokenListRow {
105102
return tokenListRow{
106-
APIKey: token,
103+
APIKey: token.APIKey,
107104
ID: token.ID,
108105
LastUsed: token.LastUsed,
109106
ExpiresAt: token.ExpiresAt,
110107
CreatedAt: token.CreatedAt,
111-
Owner: user.Username,
108+
Owner: token.Username,
112109
}
113110
}
114111

@@ -150,20 +147,10 @@ func listTokens() *cobra.Command {
150147
))
151148
}
152149

153-
userRes, err := client.Users(cmd.Context(), codersdk.UsersRequest{})
154-
if err != nil {
155-
return err
156-
}
157-
158-
usersByID := map[uuid.UUID]codersdk.User{}
159-
for _, user := range userRes.Users {
160-
usersByID[user.ID] = user
161-
}
162-
163150
displayTokens = make([]tokenListRow, len(tokens))
164151

165152
for i, token := range tokens {
166-
displayTokens[i] = tokenListRowFromToken(token, usersByID)
153+
displayTokens[i] = tokenListRowFromToken(token)
167154
}
168155

169156
out, err := formatter.Format(cmd.Context(), displayTokens)

coderd/apikey.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,6 @@ func (api *API) apiKey(rw http.ResponseWriter, r *http.Request) {
167167
httpapi.Write(ctx, rw, http.StatusOK, convertAPIKey(key))
168168
}
169169

170-
type ConvertedAPIKey struct {
171-
codersdk.APIKey
172-
Username string `json:"username"`
173-
}
174-
175170
// @Summary Get user tokens
176171
// @ID get-user-tokens
177172
// @Security CoderSessionToken
@@ -221,16 +216,16 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) {
221216
return
222217
}
223218

224-
var apiKeys []ConvertedAPIKey
219+
var apiKeys []codersdk.ConvertedAPIKey
225220
for _, key := range keys {
226221
user, err := api.Database.GetUserByID(ctx, key.UserID)
227222
if err != nil {
228-
apiKeys = append(apiKeys, ConvertedAPIKey{
223+
apiKeys = append(apiKeys, codersdk.ConvertedAPIKey{
229224
APIKey: convertAPIKey(key),
230225
Username: "",
231226
})
232227
} else {
233-
apiKeys = append(apiKeys, ConvertedAPIKey{
228+
apiKeys = append(apiKeys, codersdk.ConvertedAPIKey{
234229
APIKey: convertAPIKey(key),
235230
Username: user.Username,
236231
})

codersdk/apikey.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ type TokensFilter struct {
9090
IncludeAll bool `json:"include_all"`
9191
}
9292

93+
type ConvertedAPIKey struct {
94+
APIKey
95+
Username string `json:"username"`
96+
}
97+
9398
// asRequestOption returns a function that can be used in (*Client).Request.
9499
// It modifies the request query parameters.
95100
func (f TokensFilter) asRequestOption() RequestOption {
@@ -101,7 +106,7 @@ func (f TokensFilter) asRequestOption() RequestOption {
101106
}
102107

103108
// Tokens list machine API keys.
104-
func (c *Client) Tokens(ctx context.Context, userID string, filter TokensFilter) ([]APIKey, error) {
109+
func (c *Client) Tokens(ctx context.Context, userID string, filter TokensFilter) ([]ConvertedAPIKey, error) {
105110
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/tokens", userID), nil, filter.asRequestOption())
106111
if err != nil {
107112
return nil, err
@@ -110,7 +115,7 @@ func (c *Client) Tokens(ctx context.Context, userID string, filter TokensFilter)
110115
if res.StatusCode > http.StatusOK {
111116
return nil, ReadBodyAsError(res)
112117
}
113-
apiKey := []APIKey{}
118+
apiKey := []ConvertedAPIKey{}
114119
return apiKey, json.NewDecoder(res.Body).Decode(&apiKey)
115120
}
116121

site/src/api/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ export const getApiKey = async (): Promise<TypesGen.GenerateAPIKeyResponse> => {
142142

143143
export const getTokens = async (
144144
params: TypesGen.TokensFilter,
145-
): Promise<TypesGen.APIKey[]> => {
146-
const response = await axios.get<TypesGen.APIKey[]>(
145+
): Promise<TypesGen.ConvertedAPIKey[]> => {
146+
const response = await axios.get<TypesGen.ConvertedAPIKey[]>(
147147
`/api/v2/users/me/keys/tokens`,
148148
{
149149
params,

site/src/api/typesGenerated.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ export interface ComputedParameter extends Parameter {
135135
readonly default_source_value: boolean
136136
}
137137

138+
// From codersdk/apikey.go
139+
export interface ConvertedAPIKey extends APIKey {
140+
readonly username: string
141+
}
142+
138143
// From codersdk/users.go
139144
export interface CreateFirstUserRequest {
140145
readonly email: string

site/src/pages/UserSettingsPage/TokensPage/TokensPage.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import makeStyles from "@material-ui/core/styles/makeStyles"
55
import { useTranslation, Trans } from "react-i18next"
66
import { useTokensData, useCheckTokenPermissions } from "./hooks"
77
import { TokensSwitch, ConfirmDeleteDialog } from "./components"
8-
import { APIKey } from "api/typesGenerated"
9-
10-
export interface ConvertedAPIKey extends APIKey {
11-
username: string
12-
}
138

149
export const TokensPage: FC<PropsWithChildren<unknown>> = () => {
1510
const styles = useStyles()
@@ -53,7 +48,7 @@ export const TokensPage: FC<PropsWithChildren<unknown>> = () => {
5348
setViewAllTokens={setViewAllTokens}
5449
/>
5550
<TokensPageView
56-
tokens={tokens as ConvertedAPIKey[]}
51+
tokens={tokens}
5752
viewAllTokens={viewAllTokens}
5853
isLoading={isFetching}
5954
hasLoaded={isFetched}

site/src/pages/UserSettingsPage/TokensPage/TokensPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { FC } from "react"
1515
import { AlertBanner } from "components/AlertBanner/AlertBanner"
1616
import IconButton from "@material-ui/core/IconButton/IconButton"
1717
import { useTranslation } from "react-i18next"
18-
import { ConvertedAPIKey } from "./TokensPage"
18+
import { ConvertedAPIKey } from "api/typesGenerated"
1919

2020
const lastUsedOrNever = (lastUsed: string) => {
2121
const t = dayjs(lastUsed)

0 commit comments

Comments
 (0)