Skip to content

Commit b30eaee

Browse files
committed
feat: update get signature
1 parent 831b693 commit b30eaee

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

src/main.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { version as nodeVersion } from 'process'
33
import semver from 'semver'
44
import { describe, test, expect, beforeAll } from 'vitest'
55

6+
import { streamToString } from '../test/util.js'
7+
68
import { Blobs } from './main.js'
79

810
beforeAll(async () => {
@@ -60,6 +62,40 @@ describe('get', () => {
6062
expect(val).toBe(value)
6163
})
6264

65+
test('Returns a stream', async () => {
66+
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
67+
const [url, options] = args
68+
const headers = options?.headers as Record<string, string>
69+
70+
expect(options?.method).toBe('get')
71+
72+
if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) {
73+
const data = JSON.stringify({ url: signedURL })
74+
75+
expect(headers.authorization).toBe(`Bearer ${apiToken}`)
76+
77+
return new Response(data)
78+
}
79+
80+
if (url === signedURL) {
81+
return new Response(value)
82+
}
83+
84+
throw new Error(`Unexpected fetch call: ${url}`)
85+
}
86+
87+
const blobs = new Blobs({
88+
authentication: {
89+
token: apiToken,
90+
},
91+
fetcher,
92+
siteID,
93+
})
94+
const val = await blobs.get(key, { type: 'stream' })
95+
96+
expect(await streamToString(val as unknown as NodeJS.ReadableStream)).toBe(value)
97+
})
98+
6399
test('Returns `null` when the pre-signed URL returns a 404', async () => {
64100
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
65101
const [url, options] = args

src/main.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ enum HTTPMethod {
2121
Put = 'put',
2222
}
2323

24-
enum ResponseType {
25-
ArrayBuffer = 'arrayBuffer',
26-
Blob = 'blob',
27-
JSON = 'json',
28-
Stream = 'stream',
29-
Text = 'text',
30-
}
31-
3224
interface SetOptions {
3325
ttl?: Date | number
3426
}
@@ -153,13 +145,15 @@ export class Blobs {
153145
}
154146

155147
async get(key: string): Promise<string>
156-
async get(key: string, { type }: { type: ResponseType.ArrayBuffer }): Promise<ArrayBuffer>
157-
async get(key: string, { type }: { type: ResponseType.Blob }): Promise<Blob>
158-
async get(key: string, { type }: { type: ResponseType.Stream }): Promise<ReadableStream | null>
159-
async get(key: string, { type }: { type: ResponseType.Text }): Promise<string>
148+
async get(key: string, { type }: { type: 'arrayBuffer' }): Promise<ArrayBuffer>
149+
async get(key: string, { type }: { type: 'blob' }): Promise<Blob>
150+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
151+
async get(key: string, { type }: { type: 'json' }): Promise<any>
152+
async get(key: string, { type }: { type: 'stream' }): Promise<ReadableStream | null>
153+
async get(key: string, { type }: { type: 'text' }): Promise<string>
160154
async get(
161155
key: string,
162-
options?: { type: ResponseType },
156+
options?: { type: 'arrayBuffer' | 'blob' | 'json' | 'stream' | 'text' },
163157
): Promise<ArrayBuffer | Blob | ReadableStream | string | null> {
164158
const { type } = options ?? {}
165159
const res = await this.makeStoreRequest(key, HTTPMethod.Get)
@@ -177,23 +171,23 @@ export class Blobs {
177171
return res
178172
}
179173

180-
if (type === undefined || type === ResponseType.Text) {
174+
if (type === undefined || type === 'text') {
181175
return res.text()
182176
}
183177

184-
if (type === ResponseType.ArrayBuffer) {
178+
if (type === 'arrayBuffer') {
185179
return res.arrayBuffer()
186180
}
187181

188-
if (type === ResponseType.Blob) {
182+
if (type === 'blob') {
189183
return res.blob()
190184
}
191185

192-
if (type === ResponseType.JSON) {
186+
if (type === 'json') {
193187
return res.json()
194188
}
195189

196-
if (type === ResponseType.Stream) {
190+
if (type === 'stream') {
197191
return res.body
198192
}
199193

test/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Buffer } from 'node:buffer'
2+
3+
export const streamToString = async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
const chunks: Array<any> = []
6+
7+
for await (const chunk of stream) {
8+
chunks.push(chunk)
9+
}
10+
11+
const buffer = Buffer.concat(chunks)
12+
13+
return buffer.toString('utf-8')
14+
}

0 commit comments

Comments
 (0)