Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autoParseDates option to RedisAdapter #231

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading