1
1
// @ts -check
2
2
3
+ const process = require ( 'process' )
4
+
3
5
const { default : fetch } = require ( 'node-fetch' )
4
6
5
7
const { STORE_ENDPOINT } = require ( './consts' )
@@ -14,12 +16,13 @@ const isValidKey = (key) => key && typeof key === 'string'
14
16
*/
15
17
module . exports . getStore = function getStore ( context ) {
16
18
const headers = { authorization : `Bearer ${ context . clientContext . blobstore . token } ` }
19
+ const endpoint = process . env . STORE_ENDPOINT || STORE_ENDPOINT
17
20
return {
18
21
async get ( key ) {
19
22
if ( ! isValidKey ( key ) ) {
20
23
throw new Error ( 'Invalid key' )
21
24
}
22
- const response = await fetch ( `${ STORE_ENDPOINT } /item/${ encodeURIComponent ( key ) } ` , { headers } )
25
+ const response = await fetch ( `${ endpoint } /item/${ encodeURIComponent ( key ) } ` , { headers } )
23
26
if ( response . status === 404 ) {
24
27
return
25
28
}
@@ -43,7 +46,7 @@ module.exports.getStore = function getStore(context) {
43
46
} catch ( error ) {
44
47
throw new Error ( `Could not serialize value for key ${ key } . Item must be JSON-serializable` )
45
48
}
46
- const response = await fetch ( `${ STORE_ENDPOINT } /item/${ encodeURIComponent ( key ) } ` , {
49
+ const response = await fetch ( `${ endpoint } /item/${ encodeURIComponent ( key ) } ` , {
47
50
method : 'PUT' ,
48
51
headers,
49
52
body,
@@ -59,7 +62,7 @@ module.exports.getStore = function getStore(context) {
59
62
throw new Error ( 'Invalid key' )
60
63
}
61
64
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 } )
63
66
64
67
if ( response . status === 404 ) {
65
68
return false
@@ -75,7 +78,7 @@ module.exports.getStore = function getStore(context) {
75
78
if ( ! isValidKey ( prefix ) ) {
76
79
throw new Error ( 'Invalid key' )
77
80
}
78
- const response = await fetch ( `${ STORE_ENDPOINT } /list/${ encodeURIComponent ( prefix ) } ` , { headers } )
81
+ const response = await fetch ( `${ endpoint } /list/${ encodeURIComponent ( prefix ) } ` , { headers } )
79
82
if ( response . status === 404 ) {
80
83
return { count : 0 , objects : [ ] }
81
84
}
0 commit comments