@@ -12,7 +12,7 @@ import {
1212 PUT ,
1313 RouteHandler ,
1414} from "miniflare:shared" ;
15- import { KVHeaders , KVLimits , KVParams } from "./constants" ;
15+ import { KVHeaders , KVLimits , KVParams , MAX_BULK_GET_KEYS } from "./constants" ;
1616import {
1717 decodeKey ,
1818 decodeListOptions ,
@@ -22,7 +22,6 @@ import {
2222 validatePutOptions ,
2323} from "./validator.worker" ;
2424
25- const MAX_BULK_GET_KEYS = 100 ;
2625interface KVParams {
2726 key : string ;
2827}
@@ -78,8 +77,8 @@ function secondsToMillis(seconds: number): number {
7877
7978async function processKeyValue (
8079 obj : KeyValueEntry < unknown > | null ,
81- type : string = "text" ,
82- withMetadata : boolean = false
80+ type : "text" | "json" = "text" ,
81+ withMetadata = false
8382) {
8483 const decoder = new TextDecoder ( ) ;
8584 let decodedValue = "" ;
@@ -92,19 +91,18 @@ async function processKeyValue(
9291
9392 let val = null ;
9493 try {
95- val =
96- obj ?. value == null
97- ? null
98- : type === "json"
99- ? JSON . parse ( decodedValue )
100- : decodedValue ;
94+ val = ! obj ?. value
95+ ? null
96+ : type === "json"
97+ ? JSON . parse ( decodedValue )
98+ : decodedValue ;
10199 } catch ( err : any ) {
102100 throw new HttpError (
103101 400 ,
104- "At least of of the requested keys corresponds to a non-JSON value"
102+ "At least one of the requested keys corresponds to a non-JSON value"
105103 ) ;
106104 }
107- if ( val == null ) {
105+ if ( val === null ) {
108106 return null ;
109107 }
110108 if ( withMetadata ) {
@@ -137,11 +135,13 @@ export class KVNamespaceObject extends MiniflareDurableObject {
137135 const keys : string [ ] = parsedBody . keys ;
138136 const type = parsedBody ?. type ;
139137 if ( type && type !== "text" && type !== "json" ) {
140- return new Response ( "" , { status : 400 } ) ;
138+ return new Response ( `Type ${ type } is invalid` , { status : 400 } ) ;
141139 }
142140 const obj : { [ key : string ] : any } = { } ;
143141 if ( keys . length > MAX_BULK_GET_KEYS ) {
144- return new Response ( "" , { status : 400 } ) ;
142+ return new Response ( `Accepting a max of 100 keys, got ${ keys . length } ` , {
143+ status : 400 ,
144+ } ) ;
145145 }
146146 for ( const key of keys ) {
147147 validateGetOptions ( key , { cacheTtl : parsedBody ?. cacheTtl } ) ;
0 commit comments