import axios from 'axios'; import FileUploadResponse from '@interfaces/responses/FileUploadResponse'; import FileStatusResponse from '@interfaces/responses/FileStatusResponse'; export default class ApiService { /** @type {string} */ private readonly baseUrl: string = 'http://core.sandd'; /** @type { [key:string] : string; } */ private readonly apiRoutes = { file: this.baseUrl + '/api/file', fileDownload: this.baseUrl + '/api/file/convert', searchAndDisplace: this.baseUrl + '/search-and-displace' }; constructor() {} /** * Upload a file to the server and return its response. * Throws an error if the response wasn't successful * * TODO: Annotate the return type correctly * * @param {File} file The file we want to upload * * @returns {Promise} The response from the server */ public async uploadFile(file: File): Promise { let formData = new FormData(); formData.append('file', file); try { let response = await axios.post( this.apiRoutes.file, formData, { headers: { 'Content-Type': 'multipart/form-data' } } ) return response.data; } catch (err) { throw err; } } /** * Get data for a file from the server. * Throws an error if the response wasn't successful * * @param {string} fileId The id of the file we want to query * * @returns {Promise} The response from the server * * @throws */ public async getFileData(fileId: string): Promise { try { let response = await axios.get(this.apiRoutes.searchAndDisplace + `/${fileId}`); return response.data; } catch (err) { throw err; } } /** * Perform a search and displace operation on a document * * @param {string} content The content of the document * @param {Array} searchers The list of searchers and their replace values */ public async filterDocument(content: string, searchers: Array<{ key: string; replace_with: string; }>) { try { let response = await axios.post( this.apiRoutes.searchAndDisplace, { 'content': content, 'searchers': searchers } ); return response.data; } catch (err) { throw err; } } public async convertFile(content:string) { try { let response = await axios.post( this.apiRoutes.fileDownload, { 'content': content } ); return response.data; } catch (err) { throw err; } } }