Skip to content

Commit

Permalink
Completed Map Store the default store, vitest added (Fixed #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
MananGandhi1810 committed Jul 27, 2024
1 parent a960be3 commit 8065aae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
36 changes: 36 additions & 0 deletions otp_store/map_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import OTPStore from "./otp_store";

class MapStore extends OTPStore {
private _otpMap: Map<string, [number, Date]>;

constructor() {
super()
this._otpMap = new Map<string, [number, Date]>();
}

async get(key: string): Promise<number> {
return (this._otpMap.get(key) ?? [0])[0];
}

async set(key: string, value: number, ttl?: number): Promise<string | null> {
try {
this._otpMap.set(key, [value, new Date(Date.now() + (ttl ?? 0))]);
return "OK"
}
catch (e) {
return String(e);
}
}

async del(key: string): Promise<number | null> {
try {
this._otpMap.delete(key);
return 1;
}
catch (e) {
return -1
}
}
}

export default MapStore;
10 changes: 10 additions & 0 deletions tests/otp_store/map_store.test.ts
Original file line number Diff line number Diff line change
@@ -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) });
})
2 changes: 1 addition & 1 deletion tests/otp_store/redis_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand Down

0 comments on commit 8065aae

Please sign in to comment.