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.
 
 
 
 
 
 

183 lines
5.2 KiB

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',
fileDiscard: this.baseUrl + '/api/file/',
searchAndDisplace: this.baseUrl + '/search-and-displace',
sdOnOriginalDocument: `${this.baseUrl}/search-and-displace/original-document`,
};
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;
}
}
/**
*
* @param document
* @param searchers
*/
public async sdOnOriginalDocument(
document: File,
searchers: Array<{ key: string; type: string; value: string; }>
): Promise<any> {
let formData = new FormData();
formData.append('document', document);
formData.append('searchers', JSON.stringify(searchers));
try {
let response = await axios.post<any>(
this.apiRoutes.sdOnOriginalDocument,
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
)
return response.data;
} catch (err) {
throw err;
}
}
public async verifySdOnOriginalDocumentIsDone(id: string) {
try {
let response = await axios.get<any>( `${this.apiRoutes.sdOnOriginalDocument}/${id}`);
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
* @param {boolean} searchOnly Whether or not to also displace the content (default yes)
*/
public async filterDocument(
file: string,
searchers: Array<{ key: string; type: string; value: string; }>,
searchOnly: boolean = false,
) {
try {
let response = await axios.post(
this.apiRoutes.searchAndDisplace,
{
'file': file,
'searchers': searchers,
'searchOnly': searchOnly
}
);
return response.data;
} catch (err) {
throw err;
}
}
/**
* Convert a file from MD to ODT
*
* @param {string} content
* @param {string} fileId
*
* @returns
*/
public async convertFile(file: {id: string, name: string}, searchers: Array<{ key: string; type: string; value: string; }>) {
try {
let response = await axios.post(
this.apiRoutes.fileDownload,
{
'file': {
'id': file.id,
'type': (file.name.substring(file.name.lastIndexOf('.') + 1, file.name.length) !== 'pdf') ? 'odt' : 'pdf'
},
'searchers': searchers
}
);
return response.data;
} catch (err) {
throw err;
}
}
/**
* Discard a file in progress
*
* @param {string} fileId
*/
public async discardFile(fileId: string) {
try {
let response = await axios.delete(
this.apiRoutes.fileDiscard + fileId
);
return response.data;
} catch (err) {
throw err;
}
}
}