@@ -82,7 +82,7 @@ export interface NamespaceKeyInfo {
82
82
export async function listNamespaceKeys (
83
83
accountId : string ,
84
84
namespaceId : string ,
85
- prefix ?: string
85
+ prefix = ""
86
86
) {
87
87
return await fetchListResult < NamespaceKeyInfo > (
88
88
`/accounts/${ accountId } /storage/kv/namespaces/${ namespaceId } /keys` ,
@@ -98,15 +98,20 @@ export async function putKeyValue(
98
98
value : string ,
99
99
args ?: { expiration ?: number ; expiration_ttl ?: number }
100
100
) {
101
+ let searchParams : URLSearchParams | undefined ;
102
+ if ( args ) {
103
+ searchParams = new URLSearchParams ( ) ;
104
+ if ( args . expiration ) {
105
+ searchParams . set ( "expiration" , `${ args . expiration } ` ) ;
106
+ }
107
+ if ( args . expiration_ttl ) {
108
+ searchParams . set ( "expiration_ttl" , `${ args . expiration_ttl } ` ) ;
109
+ }
110
+ }
101
111
return await fetchResult (
102
112
`/accounts/${ accountId } /storage/kv/namespaces/${ namespaceId } /values/${ key } ` ,
103
113
{ method : "PUT" , body : value } ,
104
- args
105
- ? new URLSearchParams ( {
106
- expiration : args . expiration ?. toString ( ) ,
107
- expiration_ttl : args . expiration_ttl ?. toString ( ) ,
108
- } )
109
- : undefined
114
+ searchParams
110
115
) ;
111
116
}
112
117
@@ -262,6 +267,10 @@ export function getNamespaceId({
262
267
/**
263
268
* KV namespace binding names must be valid JS identifiers.
264
269
*/
265
- export function isValidNamespaceBinding ( binding : string ) : boolean {
266
- return / ^ [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * $ / . test ( binding ) ;
270
+ export function isValidNamespaceBinding (
271
+ binding : string | undefined
272
+ ) : binding is string {
273
+ return (
274
+ typeof binding === "string" && / ^ [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * $ / . test ( binding )
275
+ ) ;
267
276
}
0 commit comments