Skip to content

Commit

Permalink
Fixing build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg committed Sep 5, 2023
1 parent b5abea2 commit 63e6342
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
9 changes: 5 additions & 4 deletions packages/node/src/lib/__tests__/oauth-util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RefreshToken, OauthData, OauthSettings } from '../oauth-util'
import { TestFetchClient } from '../../__tests__/test-helpers/create-test-analytics'
import { readFileSync } from 'fs'
import { HTTPResponse } from '../http-client'

const privateKey = Buffer.from(`-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDVll7uJaH322IN
Expand Down Expand Up @@ -40,16 +41,16 @@ const createOAuthSuccess = (body?: any) => {
ok: true,
status: 200,
statusText: 'OK',
}) as unknown as Promise<Response>
}) as unknown as Promise<HTTPResponse>
}

const createOAuthError = (overrides: Partial<Response> = {}) => {
const createOAuthError = (overrides: Partial<HTTPResponse> = {}) => {
return Promise.resolve({
ok: false,
status: 400,
statusText: 'Foo',
...overrides,
}) as Promise<Response>
}) as Promise<HTTPResponse>
}

const getOauthData = () => {
Expand All @@ -73,7 +74,7 @@ test('OAuth Success', async () => {
fetcher.mockReturnValueOnce(
createOAuthSuccess({
access_token: 'token',
expires_in: 100,
expires_in: '100',
})
)

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/lib/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface HTTPFetchRequest {
* @link https://developer.mozilla.org/en-US/docs/Web/API/Response
*/
export interface HTTPResponse {
headers: Headers
headers: Record<string, any>
json(): Promise<any>
status: number
statusText: string
Expand Down
16 changes: 12 additions & 4 deletions packages/node/src/lib/oauth-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const RefreshTokenAsync = async (data: OauthData) => {
alg: 'RS256',
kid: data.settings.keyId,
'Content-Type': 'application/x-www-form-urlencoded',
}
} as Record<string, string>
const jti = Math.floor(Math.random() * 9999).toString()

const body = {
Expand Down Expand Up @@ -83,17 +83,25 @@ export const RefreshTokenAsync = async (data: OauthData) => {
const response = await data.httpClient.makeRequest(requestOptions)

if (response.status === 200) {
let access_token = ''
let expires_in = 0
const result = await (response.json() as Promise<{
access_token: string
expires_in: number
}>)
try {
access_token = result.access_token
expires_in = result.expires_in
} catch {
throw new Error('Malformed token response - ' + result)
}
data.refreshTimer = setTimeout(
RefreshToken,
(result.expires_in * 1000) / 2,
(expires_in * 1000) / 2,
data
)
data.refreshTimer.unref()
data.token = result.access_token
data.token = access_token
data.refreshPromise = undefined
return
}
Expand All @@ -104,7 +112,7 @@ export const RefreshTokenAsync = async (data: OauthData) => {
throw new Error(response.statusText)
} else if (response.status == 429) {
// Rate limit, wait until reset timestamp
const rateLimitResetTime = response.headers.get('X-RateLimit-Reset')
const rateLimitResetTime = response.headers['X-RateLimit-Reset']
let rateLimitDiff = 60
if (rateLimitResetTime) {
rateLimitDiff =
Expand Down
13 changes: 1 addition & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15578,7 +15578,7 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:7.x, semver@npm:^7.3.2, semver@npm:^7.3.4, semver@npm:^7.3.5, semver@npm:^7.3.7":
"semver@npm:7.x, semver@npm:^7.3.4, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8":
version: 7.5.4
resolution: "semver@npm:7.5.4"
dependencies:
Expand Down Expand Up @@ -15609,17 +15609,6 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.3.7, semver@npm:^7.3.8":
version: 7.5.4
resolution: "semver@npm:7.5.4"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3
languageName: node
linkType: hard

"send@npm:0.18.0":
version: 0.18.0
resolution: "send@npm:0.18.0"
Expand Down

0 comments on commit 63e6342

Please sign in to comment.