Skip to content

Commit 33b774c

Browse files
committed
feat: update get signature
1 parent 4cceed2 commit 33b774c

File tree

5 files changed

+244
-23
lines changed

5 files changed

+244
-23
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ await store.set('some-key', 'Hello!')
4242

4343
const item = await store.get('some-key')
4444

45-
assert.strictEqual(await item.text(), 'Hello!')
45+
assert.strictEqual(item, 'Hello!')
4646
```
4747

4848
### Authentication
@@ -81,23 +81,29 @@ Authentication with the blob storage is done in one of two ways:
8181

8282
## API
8383

84-
### `get(key: string): Promise<Response | null>`
84+
### `get(key: string, { type: string }): Promise<any>`
8585

8686
Retrieves an object with the given key.
8787

88-
If an object with the given key is found, a
89-
[standard `Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response) is returned, allowing you to use
90-
methods like `.json()`, `.text()`, or `.blob()` to read the underlying value.
88+
Depending on the most convenient format for you to access the value, you may choose to supply a `type` property as a
89+
second parameter, with one of the following values:
9190

92-
Otherwise, `null` is returned.
91+
- `arrayBuffer`: Returns the entry as an
92+
[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
93+
- `blob`: Returns the entry as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
94+
- `json`: Parses the entry as JSON and returns the resulting object
95+
- `stream`: Returns the entry as a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
96+
- `text` (default): Returns the entry as a string of plain text
97+
98+
If an object with the given key is not found, `null` is returned.
9399

94100
```javascript
95-
const entry = await blobs.get('some-key')
101+
const entry = await blobs.get('some-key', { type: 'json' })
96102

97-
console.log(await entry.text())
103+
console.log(entry)
98104
```
99105

100-
### `set(key: string, value: ReadableStream | string | ArrayBuffer | Blob): Promise<void>`
106+
### `set(key: string, value: ArrayBuffer | Blob | ReadableStream | string): Promise<void>`
101107

102108
Creates an object with the given key and value.
103109

package-lock.json

Lines changed: 172 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"@netlify/eslint-config-node": "^7.0.1",
5151
"c8": "^7.11.0",
5252
"husky": "^8.0.0",
53+
"node-fetch": "^3.3.1",
54+
"semver": "^7.5.3",
5355
"typescript": "^5.0.0",
5456
"vitest": "^0.32.2"
5557
},

src/main.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
import { describe, test, expect } from 'vitest'
1+
import { version as nodeVersion } from 'process'
2+
3+
import semver from 'semver'
4+
5+
import { describe, test, expect, beforeAll } from 'vitest'
26

37
import { Blobs } from './main.js'
48

9+
beforeAll(async () => {
10+
if (semver.lt(nodeVersion, '18.0.0')) {
11+
const nodeFetch = await import('node-fetch')
12+
13+
// @ts-expect-error
14+
globalThis.fetch = nodeFetch.default
15+
globalThis.Headers = nodeFetch.Headers
16+
// @ts-expect-error
17+
globalThis.Request = nodeFetch.Request
18+
// @ts-expect-error
19+
globalThis.Response = nodeFetch.Response
20+
}
21+
})
22+
523
describe('With API credentials', () => {
624
test('Reads a key from the blob store', async () => {
725
const siteID = '12345'
@@ -38,6 +56,6 @@ describe('With API credentials', () => {
3856
})
3957
const val = await blobs.get(key)
4058

41-
expect(await (val as Response).text()).toBe(value)
59+
expect(val).toBe(value)
4260
})
4361
})

0 commit comments

Comments
 (0)