11import { Router , Kv } from '@fermyon/spin-sdk'
22
3- const en = new TextEncoder ( )
4- const de = new TextDecoder ( )
3+ const encoder = new TextEncoder ( )
4+ const decoder = new TextDecoder ( )
55
66let router = new Router ( )
77
@@ -19,11 +19,11 @@ function getAll() {
1919 const keys = store . getKeys ( )
2020 let body = { }
2121 for ( let key of keys ) {
22- body [ key ] = de . decode ( store . get ( key ) )
22+ body [ key ] = decoder . decode ( store . get ( key ) )
2323 }
2424 return {
2525 status : 200 ,
26- body : en . encode ( JSON . stringify ( body ) )
26+ body : encoder . encode ( JSON . stringify ( body ) )
2727 }
2828}
2929
@@ -40,29 +40,29 @@ function getJson(key) {
4040 catch ( e ) {
4141 return {
4242 status : 400 ,
43- body : en . encode ( "Key holds a non-JSON value" )
43+ body : encoder . encode ( "Key holds a non-JSON value" )
4444 }
4545 }
4646 return {
4747 status : 200 ,
4848 headers : {
4949 "Content-Type" : "application/json"
5050 } ,
51- body : en . encode ( JSON . stringify ( model ) )
51+ body : encoder . encode ( JSON . stringify ( model ) )
5252 }
5353}
5454
5555/// sets a JSON value in the store
5656function setJson ( key , body ) {
5757 const store = Kv . openDefault ( )
58- const model = JSON . parse ( de . decode ( body ) )
58+ const model = JSON . parse ( decoder . decode ( body ) )
5959 store . setJson ( key , model )
6060 return {
6161 status : 201 ,
6262 headers : {
6363 "Location" : `/${ key } `
6464 } ,
65- body : de . decode ( JSON . stringify ( {
65+ body : decoder . decode ( JSON . stringify ( {
6666 key,
6767 value : model
6868 } ) )
@@ -78,19 +78,19 @@ function getByKey(key) {
7878 const value = store . get ( key ) ;
7979 return {
8080 status : 200 ,
81- body : de . decode ( value )
81+ body : decoder . decode ( value )
8282 }
8383}
8484
8585/// adds a value to the store for a given key
8686function set ( key , body ) {
8787 const store = Kv . openDefault ( )
88- const model = JSON . parse ( de . decode ( body ) )
88+ const model = JSON . parse ( decoder . decode ( body ) )
8989
9090 if ( ! model . hasOwnProperty ( "value" ) ) {
9191 return {
9292 status : 400 ,
93- body : en . encode ( "Value is required" )
93+ body : encoder . encode ( "Value is required" )
9494 }
9595 }
9696 store . set ( key , model . value )
@@ -99,7 +99,7 @@ function set(key, body) {
9999 headers : {
100100 "Location" : `/${ key } `
101101 } ,
102- body : de . decode ( JSON . stringify ( {
102+ body : decoder . decode ( JSON . stringify ( {
103103 key,
104104 value : model . value
105105 } ) )
@@ -123,7 +123,7 @@ function deleteByKey(key) {
123123function notFound ( ) {
124124 return {
125125 status : 404 ,
126- body : en . encode ( "Not Found" )
126+ body : encoder . encode ( "Not Found" )
127127 }
128128}
129129
0 commit comments