Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 93092a8

Browse files
committed
chore: update endpoint and token name
1 parent 120ffdb commit 93092a8

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/lib/consts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const BUILDER_FUNCTIONS_FLAG = true
22
const HTTP_STATUS_METHOD_NOT_ALLOWED = 405
33
const HTTP_STATUS_OK = 200
44
const METADATA_VERSION = 1
5-
const STORE_ENDPOINT = 'https://jsonbin.org/ascorbic'
5+
const STORE_ENDPOINT = 'https://functions-kv.netlify.app'
66

77
module.exports = {
88
BUILDER_FUNCTIONS_FLAG,

src/lib/store.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
export interface ObjectDescription {
2+
lastModified: string
3+
key: string
4+
}
5+
export interface ListResponse<T = any> {
6+
count: number
7+
objects: Array<ObjectDescription>
8+
}
19
export interface Store {
210
get<T = any>(key: string): Promise<T>
311
set<T = any>(key: string, value: T): Promise<boolean>
412
delete(key: string): Promise<boolean>
5-
list(prefix: string): Promise<Array<string>>
13+
list(prefix: string): Promise<ListResponse>
614
patch<T extends Record<string, any>>(key: string, value: Partial<T>): Promise<T>
715
}
816

src/lib/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const isValidKey = (key) => key && typeof key === 'string'
1313
* @returns Promise<Store>
1414
*/
1515
module.exports.getStore = function getStore(context) {
16-
const headers = { authorization: `Bearer ${context.clientContext.store.token}` }
16+
const headers = { authorization: `Bearer ${context.clientContext.blobstore.token}` }
1717
return {
1818
async get(key) {
1919
if (!isValidKey(key)) {
@@ -77,7 +77,7 @@ module.exports.getStore = function getStore(context) {
7777
}
7878
const response = await fetch(`${STORE_ENDPOINT}/list/${encodeURIComponent(prefix)}`, { headers })
7979
if (response.status === 404) {
80-
return []
80+
return { count: 0, objects: [] }
8181
}
8282
if (!response.ok) {
8383
throw new Error(`There was an error loading the value for prefix ${prefix}: ${response.statusText}`)

test/store.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ const { STORE_ENDPOINT } = require('../src/lib/consts')
88
// Throw on invalid domain
99
nock.disableNetConnect()
1010

11-
// eslint-disable-next-line node/no-unsupported-features/node-builtins
12-
const url = new URL(STORE_ENDPOINT)
13-
const HOST = `${url.protocol}//${url.host}`
14-
const ROOT = url.pathname
11+
const HOST = STORE_ENDPOINT
1512

1613
const context = {
17-
clientContext: { store: { token: 'atoken' } },
14+
clientContext: { blobstore: { token: 'atoken' } },
1815
}
1916
const store = getStore(context)
2017

@@ -27,38 +24,38 @@ test.afterEach(() => {
2724
})
2825

2926
test('gets a value', async (t) => {
30-
nock(HOST).get(`${ROOT}/item/${KEY}`).reply(200, response)
27+
nock(HOST).get(`/item/${KEY}`).reply(200, response)
3128
const value = await store.get(KEY)
3229
t.deepEqual(value, { hello: 'world' })
3330
})
3431

3532
test('returns undefined for missing value', async (t) => {
36-
nock(HOST).get(`${ROOT}/item/invalid`).reply(404)
33+
nock(HOST).get(`/item/invalid`).reply(404)
3734
const value = await store.get('invalid')
3835
t.is(value, undefined)
3936
})
4037

4138
test('throws on network error', async (t) => {
42-
nock(HOST).get(`${ROOT}/item/network`).replyWithError('oh no')
39+
nock(HOST).get(`/item/network`).replyWithError('oh no')
4340
await t.throwsAsync(() => store.get('network'))
4441
})
4542

4643
test('sets a value', async (t) => {
47-
nock(HOST).put(`${ROOT}/item/${KEY}`).reply(200)
44+
nock(HOST).put(`/item/${KEY}`).reply(200)
4845
const value = await store.set(KEY, { hello: 1 })
4946
t.truthy(value)
5047
})
5148

5249
test('sends the correct value', async (t) => {
5350
const data = { hello: 1 }
5451

55-
nock(HOST).put(`${ROOT}/item/${KEY}`, data).reply(200)
52+
nock(HOST).put(`/item/${KEY}`, data).reply(200)
5653
const value = await store.set(KEY, data)
5754
t.truthy(value)
5855
})
5956

6057
test('throws on invalid object', async (t) => {
61-
nock(HOST).put(`${ROOT}/item/${KEY}`).reply(200)
58+
nock(HOST).put(`/item/${KEY}`).reply(200)
6259

6360
const circular = {
6461
foo: {},
@@ -73,13 +70,13 @@ test('throws on invalid object', async (t) => {
7370
})
7471

7572
test('deletes a value', async (t) => {
76-
nock(HOST).delete(`${ROOT}/item/${KEY}`).reply(200)
73+
nock(HOST).delete(`/item/${KEY}`).reply(200)
7774
const value = await store.delete(KEY)
7875
t.truthy(value)
7976
})
8077

8178
test('returns false when deleting non-existent key', async (t) => {
82-
nock(HOST).delete(`${ROOT}/item/invalid`).reply(404)
79+
nock(HOST).delete(`/item/invalid`).reply(404)
8380
const value = await store.delete('invalid')
8481
t.falsy(value)
8582
})
@@ -101,7 +98,7 @@ test('throws when deleting an invalid key', async (t) => {
10198

10299
test('sends credentials', async (t) => {
103100
nock(HOST, { reqheaders: { authorization: `Bearer atoken` } })
104-
.get(`${ROOT}/item/creds`)
101+
.get(`/item/creds`)
105102
.reply(200, {})
106103
const value = await store.get('creds')
107104
t.truthy(value)

0 commit comments

Comments
 (0)