(
refresh_token: string,
opts: RefreshOptions = {},
)
| 171 | } |
| 172 | |
| 173 | export async function refreshTokens( |
| 174 | refresh_token: string, |
| 175 | opts: RefreshOptions = {}, |
| 176 | ): Promise<OAuthTokens> { |
| 177 | const clientId = resolveClientId(); |
| 178 | const fetchImpl = opts.fetchImpl ?? fetch; |
| 179 | const body = new URLSearchParams({ |
| 180 | grant_type: "refresh_token", |
| 181 | refresh_token, |
| 182 | client_id: clientId, |
| 183 | }); |
| 184 | |
| 185 | const res = await fetchImpl(tokenEndpoint(), { |
| 186 | method: "POST", |
| 187 | headers: { |
| 188 | "content-type": "application/x-www-form-urlencoded", |
| 189 | accept: "application/json", |
| 190 | }, |
| 191 | body: body.toString(), |
| 192 | }); |
| 193 | |
| 194 | if (res.status === 400 || res.status === 401) { |
| 195 | throw ErrRefreshFailed(await safeText(res)); |
| 196 | } |
| 197 | if (!res.ok) { |
| 198 | throw ErrApi(res.status, (await safeText(res)) || res.statusText); |
| 199 | } |
| 200 | |
| 201 | const payload = await readJsonOrThrow(res); |
| 202 | const tokens = parseTokenResponse(payload); |
| 203 | // Refresh grant → preserve a refresh_token the server didn't rotate. |
| 204 | await persistOAuth(tokens, { preserveMissing: true }); |
| 205 | return tokens; |
| 206 | } |
| 207 | |
| 208 | export interface RevokeOptions extends RefreshOptions { |
| 209 | /** Hint to the server about which token we're sending (RFC 7009). */ |
no test coverage detected