|
99 | 99 | }
|
100 | 100 | ```
|
101 | 101 |
|
| 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 | + |
102 | 145 | ### Middleware
|
103 | 146 |
|
104 | 147 | 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})
|
190 | 233 | // body type is { item: number }[] & { param: number }
|
191 | 234 | ```
|
192 | 235 |
|
193 |
| -Happy fetching! 👍 |
| 236 | +Happy fetching! 👍 |
0 commit comments