async increment(key: string): Promise<ClientRateLimitInfo> {
const keyWithPrefix = this.prefixKey(key);
const defaultPayload = {
totalHits: 1,
resetTime: new Date(Date.now() + this.windowMs),
}
// @ts-ignore
const record: Required<ClientRateLimitInfo> | null = await this.namespace.get<Required<ClientRateLimitInfo>>(
keyWithPrefix,
"json",
);
const payload = { ...defaultPayload, ...(record ? { totalHits: record.totalHits + 1, resetTime: new Date(record.resetTime) } : {}) }
console.log("Payload - ", payload, " Expire - ", payload.resetTime.toLocaleString(), " Current - ", new Date().toLocaleString())
await this.namespace.put(keyWithPrefix, JSON.stringify(payload), {
expirationTtl: !record ? payload.resetTime.getTime() / 1000 : undefined,
});
return payload;
}
async increment(key: string): Promise<ClientRateLimitInfo> {
const keyWithPrefix = this.prefixKey(key);
const defaultPayload = {
totalHits: 1,
resetTime: new Date(Date.now() + this.windowMs),
}
// @ts-ignore
const record: Required<ClientRateLimitInfo> | null = await this.namespace.get<Required<ClientRateLimitInfo>>(
keyWithPrefix,
"json",
);
const payload = { ...defaultPayload, ...(record ? { totalHits: record.totalHits + 1, resetTime: new Date(record.resetTime) } : {}) }
console.log("Payload - ", payload, " Expire - ", payload.resetTime.toLocaleString(), " Current - ", new Date().toLocaleString())
await this.namespace.put(keyWithPrefix, JSON.stringify(payload), {
expirationTtl: !record ? payload.resetTime.getTime() / 1000 : undefined,
});
return payload;
}