Here is the code - ```ts async increment(key: string): Promise<ClientRateLimitInfo> { const keyW

Here is the code -
async increment(key: string): Promise<ClientRateLimitInfo> {
    const keyWithPrefix = this.prefixKey(key);
    // @ts-ignore
    let payload: Required<ClientRateLimitInfo> = await this.namespace.get<Required<ClientRateLimitInfo>>(
      keyWithPrefix,
      "json",
    );

    const wasCreated = payload != null

    if (wasCreated) payload = {
      totalHits: payload.totalHits + 1,
      resetTime: new Date(payload.resetTime),
    };
    else
      payload = {
        totalHits: 1,
        resetTime: new Date(Date.now() + this.windowMs),
      };

    console.log("Payload - ", payload, " Expire - ", payload.resetTime.toLocaleString(), " Current - ", new Date().toLocaleString())

    await this.namespace.put(keyWithPrefix, JSON.stringify(payload), {
      expiration: !wasCreated ? Math.floor(payload.resetTime.getTime() / 1000) : undefined,
      // expirationTtl: this.windowMs
    });

    return payload;
  }
Was this page helpful?