Skip to content

Commit e65c9a0

Browse files
zivnizivn
andauthored
feat: add autoParseDates option to RedisAdapter (#231)
Co-authored-by: zivn <zivn@yit.co.il>
1 parent b9d2adf commit e65c9a0

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/redis/examples/deno.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const redisInstance = await connect({
1414
});
1515

1616
//create storage
17-
const storage = new RedisAdapter({ instance: redisInstance, ttl: 10 });
17+
const storage = new RedisAdapter({ instance: redisInstance, ttl: 10, autoParseDates: true });
1818

1919
// Create bot and register session middleware
2020
const bot = new Bot<MyContext>('');

packages/redis/examples/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type MyContext = Context & SessionFlavor<SessionData>;
1010
const redisInstance = new IORedis('redis://localhost:6379/0');
1111

1212
//create storage
13-
const storage = new RedisAdapter({ instance: redisInstance, ttl: 10 });
13+
const storage = new RedisAdapter({ instance: redisInstance, ttl: 10, autoParseDates: true });
1414

1515
// Create bot and register session middleware
1616
const bot = new Bot<MyContext>('');

packages/redis/src/mod.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@ import { Client, StorageAdapter } from './deps.deno.ts';
33
export class RedisAdapter<T> implements StorageAdapter<T> {
44
private redis: Client;
55
private readonly ttl?: number;
6+
private readonly autoParseDates: boolean
67

78
/**
89
* @constructor
910
* @param {opts} Constructor options
1011
* @param {opts.ttl} ttl - Session time to life in SECONDS.
1112
* @param {opts.instance} instance - Instance of redis.
13+
* @param {opts.autoParseDates} autoParseDates - set to true to convert string in the json date format to date object
1214
*/
13-
constructor({ instance, ttl }: { instance?: Client; ttl?: number }) {
15+
constructor({ instance, ttl, autoParseDates }: { instance?: Client; ttl?: number, autoParseDates?: boolean }) {
1416
if (instance) {
1517
this.redis = instance;
1618
} else {
1719
throw new Error('You should pass redis instance to constructor.');
1820
}
1921

2022
this.ttl = ttl;
23+
this.autoParseDates = autoParseDates || false;
2124
}
2225

2326
async read(key: string) {
2427
const session = await this.redis.get(key);
2528
if (session === null || session === undefined) {
2629
return undefined;
2730
}
31+
if (this.autoParseDates) {
32+
return JSON.parse(session, dateParser) as unknown as T;
33+
}
2834
return JSON.parse(session) as unknown as T;
2935
}
3036

@@ -39,3 +45,17 @@ export class RedisAdapter<T> implements StorageAdapter<T> {
3945
await this.redis.del(key);
4046
}
4147
}
48+
49+
const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$/;
50+
const dateParser = (key: string, value: any) => {
51+
52+
if (typeof value === 'string' && ISO_8601.test(value)) {
53+
var newValue;
54+
if (!value.endsWith("Z")) {
55+
newValue = `${value}Z`;
56+
}
57+
else newValue = value
58+
return new Date(newValue);
59+
}
60+
return value;
61+
}

0 commit comments

Comments
 (0)