🧹 Moved Sb1 API to separate workspace and created common workspace
- Created common workspace - Create Sparebank1Api workspace - Moved logger to common - Moved SB1 types to types.ts - Logger will avoid duplicating first line when capturing console.logs - Updated imports and added type keyword - Added nonUniqueId type
This commit is contained in:
30
packages/sparebank1Api/oauth.ts
Normal file
30
packages/sparebank1Api/oauth.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { OAuthTokenResponse, Result } from "./types"
|
||||
import * as querystring from "node:querystring"
|
||||
import { baseUrl, failure, success } from "./common"
|
||||
import logger from "@common/logger"
|
||||
|
||||
export async function refreshToken(
|
||||
clientId: string,
|
||||
clientSecret: string,
|
||||
refreshToken: string,
|
||||
): Promise<Result<OAuthTokenResponse, string>> {
|
||||
const queries = querystring.stringify({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
refresh_token: refreshToken,
|
||||
grant_type: "refresh_token",
|
||||
})
|
||||
const url = `${baseUrl}/oauth/token?${queries}`
|
||||
logger.debug(`Sending POST request to url: '${url}'`)
|
||||
const response = await fetch(url, {
|
||||
method: "post",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
})
|
||||
logger.debug(`Received response with status '${response.status}'`)
|
||||
if (!response.ok) {
|
||||
return failure(await response.text())
|
||||
}
|
||||
return success(await response.json())
|
||||
}
|
Reference in New Issue
Block a user