fix: add backend upload proxy endpoint
This commit is contained in:
@@ -13,6 +13,17 @@ const toBackendUrl = (path) => {
|
||||
return `${backendUrl}${path.startsWith('/') ? path : `/${path}`}`;
|
||||
};
|
||||
|
||||
const parseResponseBody = async (response) => {
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
|
||||
if (contentType.includes('application/json')) {
|
||||
return response.json().catch(() => ({}));
|
||||
}
|
||||
|
||||
const text = await response.text().catch(() => '');
|
||||
return text ? { message: text } : {};
|
||||
};
|
||||
|
||||
const request = async (path, options = {}) => {
|
||||
const { deviceToken } = getAppState();
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
@@ -38,6 +49,36 @@ const request = async (path, options = {}) => {
|
||||
return data;
|
||||
};
|
||||
|
||||
const uploadBinary = async (path, body, options = {}) => {
|
||||
const { deviceToken } = getAppState();
|
||||
const headers = {};
|
||||
|
||||
if (deviceToken) {
|
||||
headers.Authorization = `Bearer ${deviceToken}`;
|
||||
}
|
||||
|
||||
if (options.contentType) {
|
||||
headers['Content-Type'] = options.contentType;
|
||||
}
|
||||
|
||||
const response = await fetch(toBackendUrl(path), {
|
||||
method: options.method || 'PUT',
|
||||
body,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
...headers,
|
||||
...(options.headers || {})
|
||||
}
|
||||
});
|
||||
|
||||
const data = await parseResponseBody(response);
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || data.error || response.statusText || 'Request failed');
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getBackendUrl = () => backendUrl;
|
||||
|
||||
export const api = {
|
||||
@@ -83,5 +124,9 @@ export const api = {
|
||||
},
|
||||
pushNotifications: {
|
||||
markRead: (notificationId) => request(`/push-notifications/${notificationId}/read`, { method: 'POST', body: JSON.stringify({}) })
|
||||
},
|
||||
uploads: {
|
||||
uploadRecordingBlob: (recordingId, blob, contentType = 'application/octet-stream') =>
|
||||
uploadBinary(`/videos/upload/${recordingId}`, blob, { method: 'PUT', contentType })
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user