11// @ts -check
22
3+ const process = require ( 'process' )
4+
35const { default : fetch } = require ( 'node-fetch' )
46
57const { STORE_ENDPOINT } = require ( './consts' )
@@ -14,12 +16,13 @@ const isValidKey = (key) => key && typeof key === 'string'
1416 */
1517module . exports . getStore = function getStore ( context ) {
1618 const headers = { authorization : `Bearer ${ context . clientContext . blobstore . token } ` }
19+ const endpoint = process . env . STORE_ENDPOINT || STORE_ENDPOINT
1720 return {
1821 async get ( key ) {
1922 if ( ! isValidKey ( key ) ) {
2023 throw new Error ( 'Invalid key' )
2124 }
22- const response = await fetch ( `${ STORE_ENDPOINT } /item/${ encodeURIComponent ( key ) } ` , { headers } )
25+ const response = await fetch ( `${ endpoint } /item/${ encodeURIComponent ( key ) } ` , { headers } )
2326 if ( response . status === 404 ) {
2427 return
2528 }
@@ -43,7 +46,7 @@ module.exports.getStore = function getStore(context) {
4346 } catch ( error ) {
4447 throw new Error ( `Could not serialize value for key ${ key } . Item must be JSON-serializable` )
4548 }
46- const response = await fetch ( `${ STORE_ENDPOINT } /item/${ encodeURIComponent ( key ) } ` , {
49+ const response = await fetch ( `${ endpoint } /item/${ encodeURIComponent ( key ) } ` , {
4750 method : 'PUT' ,
4851 headers,
4952 body,
@@ -59,7 +62,7 @@ module.exports.getStore = function getStore(context) {
5962 throw new Error ( 'Invalid key' )
6063 }
6164
62- const response = await fetch ( `${ STORE_ENDPOINT } /item/${ encodeURIComponent ( key ) } ` , { method : 'DELETE' , headers } )
65+ const response = await fetch ( `${ endpoint } /item/${ encodeURIComponent ( key ) } ` , { method : 'DELETE' , headers } )
6366
6467 if ( response . status === 404 ) {
6568 return false
@@ -75,7 +78,7 @@ module.exports.getStore = function getStore(context) {
7578 if ( ! isValidKey ( prefix ) ) {
7679 throw new Error ( 'Invalid key' )
7780 }
78- const response = await fetch ( `${ STORE_ENDPOINT } /list/${ encodeURIComponent ( prefix ) } ` , { headers } )
81+ const response = await fetch ( `${ endpoint } /list/${ encodeURIComponent ( prefix ) } ` , { headers } )
7982 if ( response . status === 404 ) {
8083 return { count : 0 , objects : [ ] }
8184 }
0 commit comments