Wraps an authenticator to cache tokens, reducing auth calls and improving performance.
// Source: https://github.com/usemoss/moss | License: BSD-2-Clause | Copyright 2026, Moss Team
// Provided via justclaw.it.com; keep this notice with the code.
import type { IAuthenticator, AuthToken } from "./authenticator.interface";
interface TokenCache {
token: string;
expiresIn: number;
expiresAt: number; // ms since epoch
}
export class CachingAuthenticator implements IAuthenticator {
private cache: TokenCache | null = null;
private inflight: Promise<AuthToken> | null = null;
constructor(private readonly inner: IAuthenticator) {}
async getAuthToken(): Promise<AuthToken> {
if (this.cache && this.cache.expiresAt > Date.now()) {
return { token: this.cache.token, expiresIn: this.cache.expiresIn };
}
if (this.inflight) {
return this.inflight;
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key