Repo for the search and displace core module including the interface to select files and search and displace operations to run on them.
https://searchanddisplace.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
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 = window.location.origin;
/** @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<FileUploadResponse>} The response from the server */ public async uploadFile(file: File): Promise<FileUploadResponse> { let formData = new FormData(); formData.append('file', file);
try { let response = await axios.post<FileUploadResponse>( 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<FileStatusResponse>} The response from the server * * @throws */ public async getFileData(fileId: string): Promise<FileStatusResponse> { try { let response = await axios.get<FileStatusResponse>(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; } } }
|