Skip to content

Commit afde9eb

Browse files
author
Lucas Treffenstädt
committed
document new functionality
1 parent f806fa5 commit afde9eb

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,49 @@ try {
9999
}
100100
```
101101

102+
### Binary Data
103+
104+
It is possible to handle some content types as binary data.
105+
You can specify these as a string, regular expression, list of regular expressions and/or strings, or a custom discriminator function
106+
If one of the strings or regular expressions you provided matches the Content-Type header returned by the endpoint,
107+
or your discriminator function, called with the contents of the Content-Type header, returns `true`, the response body will be returned as a [binary blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
108+
109+
```ts
110+
111+
fetcher.configure({
112+
baseUrl: 'https://example.com/api',
113+
asBlob: 'application/octet-stream'
114+
})
115+
116+
// or
117+
118+
fetcher.configure({
119+
baseUrl: 'https://example.com/api',
120+
asBlob: /^application\/(?!json)/
121+
})
122+
123+
// or
124+
125+
fetcher.configure({
126+
baseUrl: 'https://example.com/api',
127+
asBlob: ['application/pdf', 'audio/vorbis']
128+
})
129+
130+
// or
131+
132+
fetcher.configure({
133+
baseUrl: 'https://example.com/api',
134+
asBlob: (contentType: string) => contentType.startsWith('application/o')
135+
})
136+
137+
// data is going to be a Blob
138+
const { data } = await fetcher.path('/binary/data').method('get').create()(
139+
{},
140+
{ headers: { Accept: 'application/octet-stream' } },
141+
)
142+
143+
```
144+
102145
### Middleware
103146

104147
Middlewares can be used to pre and post process fetch operations (log api calls, add auth headers etc)
@@ -190,4 +233,4 @@ const body = arrayRequestBody([{ item: 1}], { param: 2})
190233
// body type is { item: number }[] & { param: number }
191234
```
192235

193-
Happy fetching! 👍
236+
Happy fetching! 👍

0 commit comments

Comments
 (0)