UNPKG

@creamapi/cream

Version:

Concise REST API Maker - An extension library for express to create REST APIs faster

103 lines (102 loc) 3.73 kB
"use strict"; /* * Copyright 2025 Raul Radu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CookieTimeFrame = void 0; /** * Represents a time frame for managing cookie expiration. * Provides methods to initialize, set expiration, and retrieve time details. */ class CookieTimeFrame { /** * Stores the initialization timestamp. * `undefined` until initialized with `fromNow()`. */ _now = undefined; /** * Stores the initialization timestamp. * `undefined` until initialized with `fromNow()`. */ _final = undefined; /** * Initializes the timeframe with the current timestamp. * * @returns The updated `CookieTimeFrame` instance. */ fromNow() { if (this._now != undefined) return this; this._now = Date.now(); return this; } /** * Sets the expiration time in milliseconds, relative to initialization. * * @param milliseconds - The duration from the initialization point. * @returns The updated `CookieTimeFrame` instance. * @throws Error if the timeframe has not been initialized or if milliseconds are negative */ willEndIn(milliseconds) { if (this._now == undefined) throw Error('Timeframe has never been initialized, use CookieTimeFrame.fromNow to initialize the timeframe.'); if (milliseconds < 0) { throw Error('The method allows only for forwarding time frames, the end time cannot be in the past!'); } this._final = this._now + milliseconds; return this; } /** * Ensures that both initialization and expiration values are set. * * @throws Error if the timeframe has not been properly initialized or finalized. */ checkIfParamsAreSet() { if (this._now == undefined) throw Error('Timeframe has never been initialized, use CookieTimeFrame.fromNow to initialize the timeframe.'); if (this._final == undefined) throw Error('Timeframe has no defined end, use CookieTimeFrame.willEndIn to finish the timeframe.'); } /** * Retrieves the expiration date as a `Date` object. * * @returns A `Date` object representing the expiration time. * @throws Error if the timeframe has not been properly initialized or finalized. */ getEndDate() { this.checkIfParamsAreSet(); return new Date(this._final); } /** * Calculates the remaining time in milliseconds until expiration. * * @returns The number of milliseconds until expiration. * @throws Error if the timeframe has not been properly initialized or finalized. */ getDeltaTime() { this.checkIfParamsAreSet(); return this._final - this._now; } /** * This method is used to get the starting point of the time frame; * @returns */ getStartingPoint() { if (this._now == undefined) throw Error('Timeframe has never been initialized, use CookieTimeFrame.fromNow to initialize the timeframe.'); return this._now; } } exports.CookieTimeFrame = CookieTimeFrame;