Skip to main content

Class: OAuthClient

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

OAuth2 client_credentials token manager. Caches tokens, refreshes before expiry, and deduplicates concurrent requests. Backs ManagementClient auth; can also be constructed standalone.

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:70

Parameters

ParameterType
optsOAuthClientOptions

Returns

OAuthClient

Properties

tokenEndpoint

readonly tokenEndpoint: string;

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

Methods

getToken()

getToken(): Promise<string>;

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

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:118

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:136

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:156

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:177

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 }