@xeplr/ui-account 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/package.json +1 -1
- package/src/api.js +5 -1
package/README.md
CHANGED
|
@@ -164,7 +164,14 @@ const saved = await authFetch('/api/tasks', { method: 'POST', body: JSON.stringi
|
|
|
164
164
|
|
|
165
165
|
**It returns the parsed JSON body, not a `Response`** — there is no `.json()` to call. It:
|
|
166
166
|
|
|
167
|
-
- prefixes the `configure()` base URL (a URL starting with `http` is used as is) and sends `Content-Type: application/json` unless you pass your own
|
|
167
|
+
- prefixes the `configure()` base URL (a URL starting with `http` is used as is) and sends `Content-Type: application/json` unless you pass your own — **except for a `FormData` body**, which describes itself (multipart, with a boundary), so an upload can go through `authFetch` like anything else:
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
const form = new FormData()
|
|
171
|
+
form.append('file', file)
|
|
172
|
+
const saved = await authFetch('/factory/files/task_edit/brief', { method: 'POST', body: form })
|
|
173
|
+
```
|
|
174
|
+
|
|
168
175
|
- attaches `Authorization: Bearer <token>`;
|
|
169
176
|
- attaches one header per registered multi-tenant level whose active scope has an `id` (see [Multi-tenancy](#multi-tenancy));
|
|
170
177
|
- stores the token from an `X-New-Token` response header — `@xeplr/auth`'s sliding refresh, so most expiries never become a 401;
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -319,8 +319,12 @@ function absorbNewToken(res) {
|
|
|
319
319
|
export async function authFetch(endpoint, options = {}) {
|
|
320
320
|
const url = endpoint.startsWith('http') ? endpoint : `${getBaseUrl()}${endpoint}`;
|
|
321
321
|
|
|
322
|
+
// A FormData body sets its own Content-Type, boundary and all. Declaring
|
|
323
|
+
// JSON over it makes the server read the upload as an empty JSON body, so a
|
|
324
|
+
// multipart request keeps the browser's own header.
|
|
325
|
+
const isForm = typeof FormData !== 'undefined' && options.body instanceof FormData;
|
|
322
326
|
const headers = {
|
|
323
|
-
'Content-Type': 'application/json',
|
|
327
|
+
...(isForm ? {} : { 'Content-Type': 'application/json' }),
|
|
324
328
|
...options.headers,
|
|
325
329
|
};
|
|
326
330
|
|