aretherecookies-mobile/js/apis/ImagesApi.js
2019-09-21 15:45:07 +00:00

42 lines
821 B
JavaScript

// @flow
import type { ImageRaw } from "../records/ImageRecord";
import { fetchRequest, fetchRequestBinary } from "./FetchApi";
export const getImages = (foodItemId: string): Promise<Array<ImageRaw>> => {
return fetchRequest({
endpoint: `/images/${foodItemId}`,
method: "GET"
});
};
export const addImage = async ({
foodItemId,
username,
imageUri
}: {
foodItemId: 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/${foodItemId}`,
body
});
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
};