@@ -3,28 +3,34 @@ import { Client, StorageAdapter } from './deps.deno.ts';
33export 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