aretherecookies-mobile/js/apis/ImagesApi.js
2020-04-17 22:50:23 -05:00

42 lines
821 B
JavaScript

// @flow
import type { ImageRaw } from '../records/ImageRecord';
import { fetchRequest, fetchRequestBinary } from './FetchApi';
export const getImages = (productId: string): Promise<Array<ImageRaw>> => {
return fetchRequest({
endpoint: `/images/${productId}`,
method: 'GET',
});
};
export const addImage = async ({
productId,
username,
imageUri,
}: {
productId: string,
username: string,
imageUri: string,
}) => {
const body = new FormData();
// $FlowFixMe - react-native does different stuff with FormData
body.append('photo', {
uri: imageUri,
type: 'image/jpeg',
name: 'photo.jpg',
});
body.append('username', username);
const res = await fetchRequestBinary({
endpoint: `/images/${productId}`,
body,
});
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
};