import { Multi , RedisClient } from 'redis' ; import * as IORedis from 'ioredis' ; export const errorMessage = 'Supplied redis client does not have the correct methods.' ; export function get ( client , key ) { return new Promise < string | null > ( ( resolve , reject ) => { if ( ! client . get ) { return reject ( new Error ( errorMessage ) ) ; } client . get ( key , ( err , value ) => { if ( err ) { reject ( err ) ; } else { resolve ( value && String ( value ) ) ; } } ) ; } ) ; } export function exists ( client , key ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . exists ) { return reject ( new Error ( errorMessage ) ) ; } client . exists ( key , ( err , reply ) => { if ( err ) { reject ( err ) ; } else { resolve ( reply ) ; } } ) ; } ) ; } export function del ( client , key ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! client . del ) { return reject ( new Error ( errorMessage ) ) ; } client . del ( key , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } export function set ( client , key , value ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! client . set ) { return reject ( new Error ( errorMessage ) ) ; } client . set ( key , value , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } export function mset ( client , keyValueArrayOrString , ... keyValuePairs ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! client . mset ) { return reject ( new Error ( errorMessage ) ) ; } client . mset . apply ( client , [ keyValueArrayOrString , ... keyValuePairs , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } , ] ) ; } ) ; } export function setnx ( client , key , value ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . setnx ) { return reject ( new Error ( errorMessage ) ) ; } client . setnx ( key , value , ( err , reply ) => { if ( err ) { reject ( err ) ; } else { resolve ( reply ) ; } } ) ; } ) ; } export function smembers ( client , key ) { return new Promise < Array < string > > ( ( resolve , reject ) => { if ( ! client . smembers ) { return reject ( new Error ( errorMessage ) ) ; } client . smembers ( key , ( err , values ) => { if ( err ) { reject ( err ) ; } else { resolve ( values ) ; } } ) ; } ) ; } export function scard ( client , key ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . scard ) { return reject ( new Error ( errorMessage ) ) ; } client . scard ( key , ( err , value ) => { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; } export function sismember ( client , key , value ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . sismember ) { return reject ( new Error ( errorMessage ) ) ; } client . sismember ( key , value , ( err , numFound ) => { if ( err ) { reject ( err ) ; } else { resolve ( numFound ) ; } } ) ; } ) ; } export function sadd ( client , key , value ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . sadd ) { return reject ( new Error ( errorMessage ) ) ; } client . sadd ( key , value , ( err , numInserted ) => { if ( err ) { reject ( err ) ; } else { resolve ( numInserted ) ; } } ) ; } ) ; } export function sinter ( client , keyArrayOrString , ... intersectKeys ) { return new Promise < Array < string > > ( ( resolve , reject ) => { if ( ! client . sinter ) { return reject ( new Error ( errorMessage ) ) ; } client . sinter . apply ( client , [ keyArrayOrString , ... intersectKeys , ( err , values ) => { if ( err ) { reject ( err ) ; } else { resolve ( values ) ; } } , ] ) ; } ) ; } export function hgetall ( client , key ) { return new Promise < { [ key ] : string ; } > ( ( resolve , reject ) => { if ( ! client . hgetall ) { return reject ( new Error ( errorMessage ) ) ; } client . hgetall ( key , ( err , values ) => { if ( err ) { reject ( err ) ; } else { resolve ( values ) ; } } ) ; } ) ; } export function exec < T > ( client ) { return new Promise < Array < T > > ( ( resolve , reject ) => { if ( ! client . exec ) { return reject ( new Error ( errorMessage ) ) ; } client . exec ( ( err , results ) => { if ( err ) { return reject ( err ) ; } else { if ( Array . isArray ( results [ 0 ] ) && ( results [ 0 ] [ 0 ] === null || results [ 0 ] [ 0 ] instanceof ( IORedis as any ) . ReplyError ) ) { results = results . map ( ( result ) => { const error = result [ 0 ] ; if ( error instanceof ( IORedis as any ) . ReplyError ) { return error . message ; } return result [ 1 ] ; } ) ; } resolve ( results ) ; } } ) ; } ) ; } export function psubscribe ( client , patternOrPatternArray , ... patterns ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! client . psubscribe ) { return reject ( new Error ( errorMessage ) ) ; } client . psubscribe . apply ( client , [ patternOrPatternArray , ... patterns , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } , ] ) ; } ) ; } export function punsubscribe ( client , patternOrPatternArray , ... patterns ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! client . punsubscribe ) { return reject ( new Error ( errorMessage ) ) ; } client . punsubscribe . apply ( client , [ patternOrPatternArray , ... patterns , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } , ] ) ; } ) ; } export function keys ( client , searchString ) { return new Promise < Array < string > > ( ( resolve , reject ) => { if ( ! client . keys ) { return reject ( new Error ( errorMessage ) ) ; } client . keys ( searchString , ( err , value ) => { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; } export function zscore ( client , key , member ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . zscore ) { return reject ( new Error ( errorMessage ) ) ; } client . zscore ( key , member , ( err , value ) => { if ( err ) { reject ( err ) ; } else { resolve ( parseFloat ( value ) ) ; } } ) ; } ) ; } export function hset ( client , key , field , value ) { return new Promise < number > ( ( resolve , reject ) => { if ( ! client . hset ) { return reject ( new Error ( errorMessage ) ) ; } client . hset ( key , field , value , ( err , numAdded ) => { if ( err ) { reject ( err ) ; } else { resolve ( numAdded ) ; } } ) ; } ) ; } export function hget ( client , key , field ) { return new Promise < string > ( ( resolve , reject ) => { if ( ! client . hget ) { return reject ( new Error ( errorMessage ) ) ; } client . hget ( key , field , ( err , value ) => { if ( err ) { reject ( err ) ; } else { resolve ( value ) ; } } ) ; } ) ; }