Add support for message history pagination, preserve scroll in rendering, and improve draft/message workflows.
This commit is contained in:
+16
-4
@@ -82,18 +82,19 @@ function normalizeError(error, scope, response) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function request(url, data, method = "POST", options = {}) {
|
||||
async function bufferedRequest(url, {method, body, headers}, options = {}, asBlob = false) {
|
||||
const scope = requestScope(options);
|
||||
let response;
|
||||
try {
|
||||
response = await abortable(() => fetch(url, {
|
||||
method,
|
||||
credentials: "same-origin",
|
||||
headers: {"Accept": "application/json", "Content-Type": "application/json"},
|
||||
body: data == null ? undefined : JSON.stringify(data),
|
||||
headers,
|
||||
body,
|
||||
signal: scope.signal
|
||||
}), scope.signal);
|
||||
return await readJson(response, scope.signal);
|
||||
if (!asBlob || !response.ok) return await readJson(response, scope.signal);
|
||||
return {blob: await abortable(() => response.blob(), scope.signal)};
|
||||
} catch (error) {
|
||||
return normalizeError(error, scope, response).toJSON();
|
||||
} finally {
|
||||
@@ -101,6 +102,17 @@ export async function request(url, data, method = "POST", options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
export function request(url, data, method = "POST", options = {}) {
|
||||
return bufferedRequest(url, {method, headers: {"Accept": "application/json", "Content-Type": "application/json"},
|
||||
body: data == null ? undefined : JSON.stringify(data)}, options);
|
||||
}
|
||||
|
||||
// Let fetch set the multipart boundary; share deadlines, cancellation and errors.
|
||||
export const multipart = (url, form, options = {}) => bufferedRequest(url,
|
||||
{method: "POST", headers: {"Accept": "application/json"}, body: form}, {timeoutMs: 120000, ...options});
|
||||
export const download = (url, options = {}) => bufferedRequest(url,
|
||||
{method: "GET", headers: {"Accept": "*/*"}}, {timeoutMs: 120000, ...options}, true);
|
||||
|
||||
export const get = (url, options) => request(url, null, "GET", options);
|
||||
export const post = (url, data, options) => request(url, data, "POST", options);
|
||||
export const put = (url, data, options) => request(url, data, "PUT", options);
|
||||
|
||||
Reference in New Issue
Block a user