@@ -366,6 +366,86 @@ describe('set', () => {
366
366
} )
367
367
} )
368
368
369
+ describe ( 'setJSON' , ( ) => {
370
+ test ( 'Writes to the blob store using API credentials' , async ( ) => {
371
+ expect . assertions ( 5 )
372
+
373
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
374
+ const [ url , options ] = args
375
+ const headers = options ?. headers as Record < string , string >
376
+
377
+ expect ( options ?. method ) . toBe ( 'put' )
378
+
379
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
380
+ const data = JSON . stringify ( { url : signedURL } )
381
+
382
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
383
+
384
+ return new Response ( data )
385
+ }
386
+
387
+ if ( url === signedURL ) {
388
+ expect ( options ?. body ) . toBe ( JSON . stringify ( { value } ) )
389
+ expect ( headers [ 'cache-control' ] ) . toBe ( 'max-age=0, stale-while-revalidate=60' )
390
+
391
+ return new Response ( value )
392
+ }
393
+
394
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
395
+ }
396
+
397
+ const blobs = new Blobs ( {
398
+ authentication : {
399
+ token : apiToken ,
400
+ } ,
401
+ fetcher,
402
+ siteID,
403
+ } )
404
+
405
+ await blobs . setJSON ( key , { value } )
406
+ } )
407
+
408
+ test ( 'Accepts a TTL parameter' , async ( ) => {
409
+ expect . assertions ( 6 )
410
+
411
+ const ttl = new Date ( Date . now ( ) + 15_000 )
412
+ const fetcher = async ( ...args : Parameters < typeof globalThis . fetch > ) => {
413
+ const [ url , options ] = args
414
+ const headers = options ?. headers as Record < string , string >
415
+
416
+ expect ( options ?. method ) . toBe ( 'put' )
417
+
418
+ if ( url === `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=production` ) {
419
+ const data = JSON . stringify ( { url : signedURL } )
420
+
421
+ expect ( headers . authorization ) . toBe ( `Bearer ${ apiToken } ` )
422
+
423
+ return new Response ( data )
424
+ }
425
+
426
+ if ( url === signedURL ) {
427
+ expect ( options ?. body ) . toBe ( JSON . stringify ( { value } ) )
428
+ expect ( headers [ 'cache-control' ] ) . toBe ( 'max-age=0, stale-while-revalidate=60' )
429
+ expect ( headers [ 'x-nf-expires-at' ] ) . toBe ( ttl . getTime ( ) . toString ( ) )
430
+
431
+ return new Response ( value )
432
+ }
433
+
434
+ throw new Error ( `Unexpected fetch call: ${ url } ` )
435
+ }
436
+
437
+ const blobs = new Blobs ( {
438
+ authentication : {
439
+ token : apiToken ,
440
+ } ,
441
+ fetcher,
442
+ siteID,
443
+ } )
444
+
445
+ await blobs . setJSON ( key , { value } , { ttl } )
446
+ } )
447
+ } )
448
+
369
449
describe ( 'delete' , ( ) => {
370
450
test ( 'Deletes from the blob store using API credentials' , async ( ) => {
371
451
expect . assertions ( 4 )
0 commit comments