From 8065aae8f457292c8c2dabaf543bf2adda55730c Mon Sep 17 00:00:00 2001 From: Manan Gandhi Date: Sat, 27 Jul 2024 21:16:58 +0530 Subject: [PATCH] Completed Map Store the default store, vitest added (Fixed #7) --- otp_store/map_store.ts | 36 +++++++++++++++++++++++++++++ tests/otp_store/map_store.test.ts | 10 ++++++++ tests/otp_store/redis_store.test.ts | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 otp_store/map_store.ts create mode 100644 tests/otp_store/map_store.test.ts diff --git a/otp_store/map_store.ts b/otp_store/map_store.ts new file mode 100644 index 0000000..de88b5c --- /dev/null +++ b/otp_store/map_store.ts @@ -0,0 +1,36 @@ +import OTPStore from "./otp_store"; + +class MapStore extends OTPStore { + private _otpMap: Map; + + constructor() { + super() + this._otpMap = new Map(); + } + + async get(key: string): Promise { + return (this._otpMap.get(key) ?? [0])[0]; + } + + async set(key: string, value: number, ttl?: number): Promise { + try { + this._otpMap.set(key, [value, new Date(Date.now() + (ttl ?? 0))]); + return "OK" + } + catch (e) { + return String(e); + } + } + + async del(key: string): Promise { + try { + this._otpMap.delete(key); + return 1; + } + catch (e) { + return -1 + } + } +} + +export default MapStore; \ No newline at end of file diff --git a/tests/otp_store/map_store.test.ts b/tests/otp_store/map_store.test.ts new file mode 100644 index 0000000..a30b736 --- /dev/null +++ b/tests/otp_store/map_store.test.ts @@ -0,0 +1,10 @@ +import { expect, test } from 'vitest' +import RedisStore from '../../otp_store/redis_store' +import MapStore from '../../otp_store/map_store' + +test('Test Map Store', async () => { + const mapStore: MapStore = new MapStore(); + + mapStore.set('test', 123, 60).then(() => { }); + mapStore.get('test').then(value => { expect(value).toBe(123) }); +}) \ No newline at end of file diff --git a/tests/otp_store/redis_store.test.ts b/tests/otp_store/redis_store.test.ts index f4e6bf7..32b54a6 100644 --- a/tests/otp_store/redis_store.test.ts +++ b/tests/otp_store/redis_store.test.ts @@ -2,7 +2,7 @@ import { expect, test } from 'vitest' import RedisStore from '../../otp_store/redis_store' import { createClient } from '@redis/client' -test('Test OTP Manager with default Map store', async () => { +test('Test Redis Store', async () => { const redisClient = createClient({ url: 'redis://:1234567890@localhost:6379' });