Skip to content

Commit

Permalink
feat: add autoParseDates option to RedisAdapter (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: zivn <[email protected]>
  • Loading branch information
zivni and zivn authored Sep 13, 2024
1 parent b9d2adf commit e65c9a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/redis/examples/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const redisInstance = await connect({
});

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

// Create bot and register session middleware
const bot = new Bot<MyContext>('');
Expand Down
2 changes: 1 addition & 1 deletion packages/redis/examples/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type MyContext = Context & SessionFlavor<SessionData>;
const redisInstance = new IORedis('redis://localhost:6379/0');

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

// Create bot and register session middleware
const bot = new Bot<MyContext>('');
Expand Down
22 changes: 21 additions & 1 deletion packages/redis/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@ import { Client, StorageAdapter } from './deps.deno.ts';
export class RedisAdapter<T> implements StorageAdapter<T> {
private redis: Client;
private readonly ttl?: number;
private readonly autoParseDates: boolean

/**
* @constructor
* @param {opts} Constructor options
* @param {opts.ttl} ttl - Session time to life in SECONDS.
* @param {opts.instance} instance - Instance of redis.
* @param {opts.autoParseDates} autoParseDates - set to true to convert string in the json date format to date object
*/
constructor({ instance, ttl }: { instance?: Client; ttl?: number }) {
constructor({ instance, ttl, autoParseDates }: { instance?: Client; ttl?: number, autoParseDates?: boolean }) {
if (instance) {
this.redis = instance;
} else {
throw new Error('You should pass redis instance to constructor.');
}

this.ttl = ttl;
this.autoParseDates = autoParseDates || false;
}

async read(key: string) {
const session = await this.redis.get(key);
if (session === null || session === undefined) {
return undefined;
}
if (this.autoParseDates) {
return JSON.parse(session, dateParser) as unknown as T;
}
return JSON.parse(session) as unknown as T;
}

Expand All @@ -39,3 +45,17 @@ export class RedisAdapter<T> implements StorageAdapter<T> {
await this.redis.del(key);
}
}

const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$/;
const dateParser = (key: string, value: any) => {

if (typeof value === 'string' && ISO_8601.test(value)) {
var newValue;
if (!value.endsWith("Z")) {
newValue = `${value}Z`;
}
else newValue = value
return new Date(newValue);
}
return value;
}

0 comments on commit e65c9a0

Please sign in to comment.