@johntad/m-pesa
Version:
A TypeScript SDK for integrating M-Pesa mobile payment services into applications, enabling seamless money transfers and transactions.
33 lines (32 loc) • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthResponse = void 0;
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(accessToken, tokenType, expiresIn) {
this.accessToken = accessToken;
this.tokenType = tokenType;
this.expiresIn = expiresIn;
}
/**
* 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) {
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() {
const expiryTimeInMillis = Date.now() + this.expiresIn * 1000;
return new Date(expiryTimeInMillis);
}
}
exports.AuthResponse = AuthResponse;