@@ -4,38 +4,32 @@ import Async from '../request/async'
44import Request from '../request'
55
66export function put ( key , value , timeToLive , asyncHandler ) {
7- if ( ! Utils . isString ( key ) ) {
8- throw new Error ( 'You can use only String as key to put into Cache ' )
7+ if ( ! key || ! Utils . isString ( key ) ) {
8+ throw new Error ( 'Cache Key must be non empty String ' )
99 }
1010
11- if ( ! ( timeToLive instanceof Async ) ) {
12- if ( typeof timeToLive === 'object' && ! arguments [ 3 ] ) {
13- asyncHandler = timeToLive
14- timeToLive = null
15- } else if ( typeof timeToLive !== ( 'number' || 'string' ) && timeToLive != null ) {
16- throw new Error ( 'You can use only String as timeToLive attribute to put into Cache' )
17- }
18- } else {
11+ if ( timeToLive instanceof Async ) {
1912 asyncHandler = timeToLive
20- timeToLive = null
13+ timeToLive = undefined
14+ }
15+
16+ if ( timeToLive && ! Utils . isNumber ( key ) ) {
17+ throw new Error ( 'Cache timeToLive must be Number' )
2118 }
2219
2320 if ( Utils . isObject ( value ) && value . constructor !== Object ) {
2421 value . ___class = value . ___class || Utils . getClassName ( value )
2522 }
2623
27- let responder = Utils . extractResponder ( [ asyncHandler ] )
28- const isAsync = ! ! responder
29-
30- if ( responder ) {
31- responder = Utils . wrapAsync ( responder )
24+ if ( asyncHandler ) {
25+ asyncHandler = Utils . wrapAsync ( asyncHandler )
3226 }
3327
3428 return Request . put ( {
3529 url : Urls . cacheItem ( key ) + ( ( timeToLive ) ? '?timeout=' + timeToLive : '' ) ,
3630 headers : { 'Content-Type' : 'application/json' } ,
3731 data : JSON . stringify ( value ) ,
38- isAsync : isAsync ,
39- asyncHandler : responder
32+ isAsync : ! ! asyncHandler ,
33+ asyncHandler : asyncHandler
4034 } )
4135}
0 commit comments