Source

SlayTheSpireAPI/TimeServerHelpers/TimeLookup.ts

import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import config from '../../../config.json';

/**
 * Contains the query to be sent to the Slay the Spire time server
 */
export class TimeLookup {
    query: AxiosRequestConfig = {
        method: 'get',
        url: config.slayTheSpire.timeServer.host,
    };

    /**
     * Construct a query to the Slay the Spire time server to later be executed
     * @param {AxiosRequestConfig?} query Optional axios request config to override default
     * functionality
     */
    public constructor(query?: AxiosRequestConfig) {
        if (query) this.query = query;
    }

    /**
     * Execute query and make HTTP/S request to the Slay the Spire time server to obtain the
     * current time, in seconds, since January 1, 1970 00:00:00
     * @returns {AxiosResponse}
     * @example
     * const timeServerQuery = new TimeLookup;
     * const currentTime = await timeServerQuery.execute();
     */
    public async execute(): Promise<AxiosResponse> {
        try {
            return await axios.request<AxiosResponse>(this.query);
        } catch (e) {
            throw new Error(e);
        }
    }
}