Skip to main content

Class: OAuthClient

Defined in: src/management/oauth-client.ts:71

OAuth2 client_credentials token manager. Caches tokens, refreshes before expiry, and deduplicates concurrent requests. Backs ManagementClient auth; can also be constructed standalone. A single 30-second deadline bounds token headers and JSON-body reads, including transports that ignore cancellation. A timed-out refresh cannot cache a late token; subsequent calls may start a fresh refresh. Successful requests dispose their timer.

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';

const oauth = new OAuthClient({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
tsgId: '1234567890',
onTokenRefresh: (info) => console.log('refreshed, expiresInMs=', info.expiresInMs),
});

const token = await oauth.getToken();
// token => 'eyJhbGciOi...' (bearer access token)

Constructors​

Constructor​

new OAuthClient(opts): OAuthClient;

Defined in: src/management/oauth-client.ts:83

Parameters​

ParameterType
optsOAuthClientOptions

Returns​

OAuthClient

Properties​

tokenEndpoint​

readonly tokenEndpoint: string;

Defined in: src/management/oauth-client.ts:72

Methods​

getToken()​

getToken(): Promise<string>;

Defined in: src/management/oauth-client.ts:104

Get a valid access token, refreshing if needed.

Returns​

Promise<string>

Bearer access token string.

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';
const oauth = new OAuthClient({ clientId: 'cid', clientSecret: 'secret', tsgId: '1234567890' });

const token = await oauth.getToken();
// token => 'eyJhbGciOi...' (cached until ~30s before expiry, then auto-refreshed)

clearToken()​

clearToken(): void;

Defined in: src/management/oauth-client.ts:131

Clear the cached token, forcing a fresh fetch on next call.

Returns​

void

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';
const oauth = new OAuthClient({ clientId: 'cid', clientSecret: 'secret', tsgId: '1234567890' });

oauth.clearToken();
oauth.getTokenInfo().hasToken; // => false; next getToken() triggers a fresh fetch

isTokenExpired()​

isTokenExpired(): boolean;

Defined in: src/management/oauth-client.ts:149

Check if the current token has passed its expiry time. Returns true if no token exists.

Returns​

boolean

Whether the token is expired.

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';
const oauth = new OAuthClient({ clientId: 'cid', clientSecret: 'secret', tsgId: '1234567890' });

oauth.isTokenExpired(); // => true (no token fetched yet)
await oauth.getToken();
oauth.isTokenExpired(); // => false

isTokenExpiringSoon()​

isTokenExpiringSoon(bufferMs?): boolean;

Defined in: src/management/oauth-client.ts:169

Check if the token is within the pre-expiry buffer window. Returns true if no token exists.

Parameters​

ParameterTypeDescription
bufferMs?numberCustom buffer in ms. Defaults to the configured tokenBufferMs.

Returns​

boolean

Whether the token is expiring soon.

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';
const oauth = new OAuthClient({ clientId: 'cid', clientSecret: 'secret', tsgId: '1234567890' });
await oauth.getToken();

oauth.isTokenExpiringSoon(); // => false (just fetched)
oauth.isTokenExpiringSoon(3_600_000); // => true (1h buffer larger than remaining TTL)

getTokenInfo()​

getTokenInfo(): TokenInfo;

Defined in: src/management/oauth-client.ts:190

Get a snapshot of the current token state without exposing the actual token value.

Returns​

TokenInfo

Current TokenInfo.

Example​

import { OAuthClient } from '@cdot65/prisma-airs-sdk';
const oauth = new OAuthClient({ clientId: 'cid', clientSecret: 'secret', tsgId: '1234567890' });
await oauth.getToken();

const info = oauth.getTokenInfo();
// info =>
// { hasToken: true, isValid: true, isExpired: false, isExpiringSoon: false,
// expiresInMs: 86370000, expiresAt: 1717000000000 }