All files / src/models AuthResponse.ts

71.42% Statements 5/7
100% Branches 0/0
66.66% Functions 2/3
71.42% Lines 5/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                          1x             1x 1x 1x                 1x                        
/**
 * Represents the structure of an authentication response from the API.
 * 
 * @property access_token - The access token used to authenticate subsequent API calls.
 * @property token_type - The type of token (e.g., "Bearer").
 * @property expires_in - The token expiry time in seconds.
 */
export type AuthResponseType = {
    access_token: string;
    token_type: string;
    expires_in: string;
};
 
export class AuthResponse {
    /**
     * @param accessToken - The access token used to authenticate subsequent API calls.
     * @param tokenType - The type of token (e.g., Bearer).
     * @param expiresIn - Token expiry time in seconds.
     */
    constructor(
        public accessToken: string,
        public tokenType: string,
        public expiresIn: number
    ) { }
 
    /**
     * Factory method to create an AuthResponse object from raw API response.
     * @param data - The raw response body from the authentication API.
     * @returns An instance of `AuthResponse` instance.
     */
    static fromApiResponse(data: { access_token: string; token_type: string; expires_in: string }): AuthResponse {
        return new AuthResponse(data.access_token, data.token_type, parseInt(data.expires_in, 10));
    }
 
    /**
     * Calculates the exact timestamp when the token will expire.
     * @returns Date object representing the token expiry time.
     */
    getExpiryTime(): Date {
        const expiryTimeInMillis = Date.now() + this.expiresIn * 1000;
        return new Date(expiryTimeInMillis);
    }
}