import { Utils } from '../Utils'; import requestPromise = require('request-promise'); import { Upload } from '../server/SharedMediaTypes'; export namespace Networking { export async function FetchFromServer(relativeRoute: string) { return (await fetch(relativeRoute)).text(); } export async function PostToServer(relativeRoute: string, body?: any) { const options = { uri: Utils.prepend(relativeRoute), method: 'POST', body, json: true, }; return requestPromise.post(options); } /** * Handles uploading basic file types to server and makes the API call to "/uploadFormData" endpoint * with the mapping of GUID to filem as parameters. * * @param files the files to be uploaded to the server * @returns the response as a json from the server */ export async function UploadFilesToServer(files: File | File[]): Promise[]> { const formData = new FormData(); if (Array.isArray(files)) { if (!files.length) { return []; } files.forEach(file => formData.append(Utils.GenerateGuid(), file)); } else { formData.append(Utils.GenerateGuid(), files); } const parameters = { method: 'POST', body: formData, }; const response = await fetch('/uploadFormData', parameters); return response.json(); } export async function UploadYoutubeToServer(videoId: string): Promise[]> { const parameters = { method: 'POST', body: JSON.stringify({ videoId }), json: true, }; const response = await fetch('/uploadYoutubeVideo', parameters); return response.json(); } }