-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13ff57d
commit 271fa4f
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# Specify files that shouldn't be modified by Fern | ||
src/wrapper/ | ||
src/core/auth/OAuthTokenProvider.ts | ||
src/index.ts |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as core from "../../core"; | ||
import { Auth } from "../../api/resources/auth/client/Client"; | ||
|
||
/** | ||
* The OAuthTokenProvider retrieves an OAuth access token, refreshing it as needed. | ||
* The access token is then used as the bearer token in every authenticated request. | ||
*/ | ||
export class OAuthTokenProvider { | ||
private readonly BUFFER_IN_MINUTES = 2; | ||
private readonly _clientId: core.Supplier<string>; | ||
private readonly _clientSecret: core.Supplier<string>; | ||
private readonly _authClient: Auth; | ||
private _accessToken: string | undefined; | ||
private _expiresAt: Date; | ||
|
||
constructor({ | ||
clientId, | ||
clientSecret, | ||
authClient, | ||
}: { | ||
clientId: core.Supplier<string>; | ||
clientSecret: core.Supplier<string>; | ||
authClient: Auth; | ||
}) { | ||
this._clientId = clientId; | ||
this._clientSecret = clientSecret; | ||
this._authClient = authClient; | ||
this._expiresAt = new Date(); | ||
} | ||
|
||
public async getToken(): Promise<string> { | ||
if (this._accessToken && this._expiresAt > new Date()) { | ||
return this._accessToken; | ||
} | ||
return this.refresh(); | ||
} | ||
|
||
private async refresh(): Promise<string> { | ||
const tokenResponse = await this._authClient.getToken({ | ||
clientId: await core.Supplier.get(this._clientId), | ||
clientSecret: await core.Supplier.get(this._clientSecret), | ||
}); | ||
|
||
this._accessToken = tokenResponse.accessToken; | ||
this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, this.BUFFER_IN_MINUTES); | ||
return this._accessToken; | ||
} | ||
|
||
private getExpiresAt(expiresInSeconds: number, bufferInMinutes: number): Date { | ||
const now = new Date(); | ||
return new Date(now.getTime() + expiresInSeconds * 1000 - bufferInMinutes * 60 * 1000); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * as Chariot from "./api"; | ||
export { ChariotClient } from "./Client"; | ||
export { ChariotEnvironment } from "./environments"; | ||
export { ChariotError, ChariotTimeoutError } from "./errors"; | ||
export { ChariotClient } from "./wrapper/ChariotClient"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Auth } from "api/resources/auth/client/Client"; | ||
import { ChariotClient as FernClient } from "../Client"; | ||
import * as core from "../core"; | ||
import * as environments from "../environments"; | ||
import { OAuthTokenProvider } from "core/auth/OAuthTokenProvider"; | ||
|
||
export declare namespace ChariotClient { | ||
|
||
interface Options { | ||
environment?: core.Supplier<environments.ChariotEnvironment | string>; | ||
clientId: core.Supplier<string>; | ||
clientSecret: core.Supplier<string>; | ||
} | ||
} | ||
|
||
export class ChariotClient extends FernClient { | ||
constructor(protected readonly _options: ChariotClient.Options) { | ||
const _oauthTokenProvider = new OAuthTokenProvider({ | ||
clientId: _options.clientId, | ||
clientSecret: _options.clientSecret, | ||
authClient: new Auth({ | ||
environment: _options.environment, | ||
}), | ||
}); | ||
super({ | ||
..._options, | ||
token: async () => await _oauthTokenProvider.getToken(), | ||
}) | ||
} | ||
} |