type Key = Parameters < KVNamespace [ "get" ] > [ 0 ] ; type Value = Parameters < KVNamespace [ "put" ] > [ 1 ] ; type Type = "text" | "json" | "arrayBuffer" | "stream" ; type KVOptions = Parameters < KVNamespace [ "put" ] > [ 2 ] ; interface MemoizationOptions < K extends ( ... args : any [ ] ) => any > { keyGenerator ? : ( ... args : Parameters < K > ) => Key ; resultTransformer ? : ( result : any ) => Value ; type ? : Type ; } const makeMemoizer = ( namespace ) => < K extends ( ... args : any [ ] ) => any > ( func , { keyGenerator , resultTransformer , type } = { } , kvOptions ? ) => { keyGenerator = keyGenerator || ( ( ... args ) => `${ func . name }:${ args . join ( "," ) }` ) ; resultTransformer = resultTransformer || JSON . stringify ; type = type || "json" ; return async ( ... args ) => { const key = keyGenerator ( ... args ) ; const cachedResult = await namespace . get ( key , type as any ) ; if ( cachedResult ) return cachedResult ; const result = await func ( ... args ) ; await namespace . put ( key , resultTransformer ( result ) , kvOptions ) ; return result ; } ; } ; export { makeMemoizer } ;