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.

106 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import axios from 'axios';
  2. import FileUploadResponse from '@/interfaces/responses/FileUploadResponse';
  3. import FileStatusResponse from '@/interfaces/responses/FileStatusResponse';
  4. export default class ApiService {
  5. /** @type {string} */
  6. private readonly baseUrl: string = window.location.origin;
  7. /** @type { [key:string] : string; } */
  8. private readonly apiRoutes = {
  9. file: this.baseUrl + '/api/file',
  10. fileDownload: this.baseUrl + '/api/file/convert',
  11. searchAndDisplace: this.baseUrl + '/search-and-displace'
  12. };
  13. constructor() {
  14. }
  15. /**
  16. * Upload a file to the server and return its response.
  17. * Throws an error if the response wasn't successful
  18. *
  19. * TODO: Annotate the return type correctly
  20. *
  21. * @param {File} file The file we want to upload
  22. *
  23. * @returns {Promise<FileUploadResponse>} The response from the server
  24. */
  25. public async uploadFile(file: File): Promise<FileUploadResponse> {
  26. let formData = new FormData();
  27. formData.append('file', file);
  28. try {
  29. let response = await axios.post<FileUploadResponse>(
  30. this.apiRoutes.file,
  31. formData,
  32. {
  33. headers: {
  34. 'Content-Type': 'multipart/form-data'
  35. }
  36. }
  37. )
  38. return response.data;
  39. } catch (err) {
  40. throw err;
  41. }
  42. }
  43. /**
  44. * Get data for a file from the server.
  45. * Throws an error if the response wasn't successful
  46. *
  47. * @param {string} fileId The id of the file we want to query
  48. *
  49. * @returns {Promise<FileStatusResponse>} The response from the server
  50. *
  51. * @throws
  52. */
  53. public async getFileData(fileId: string): Promise<FileStatusResponse> {
  54. try {
  55. let response = await axios.get<FileStatusResponse>(this.apiRoutes.searchAndDisplace + `/${fileId}`);
  56. return response.data;
  57. } catch (err) {
  58. throw err;
  59. }
  60. }
  61. /**
  62. * Perform a search and displace operation on a document
  63. *
  64. * @param {string} content The content of the document
  65. * @param {Array} searchers The list of searchers and their replace values
  66. */
  67. public async filterDocument(content: string, searchers: Array<{ key: string; replace_with: string; }>) {
  68. try {
  69. let response = await axios.post(
  70. this.apiRoutes.searchAndDisplace,
  71. {
  72. 'content': content,
  73. 'searchers': searchers
  74. }
  75. );
  76. return response.data;
  77. } catch (err) {
  78. throw err;
  79. }
  80. }
  81. public async convertFile(content: string) {
  82. try {
  83. let response = await axios.post(
  84. this.apiRoutes.fileDownload,
  85. {
  86. 'content': content
  87. }
  88. );
  89. return response.data;
  90. } catch (err) {
  91. throw err;
  92. }
  93. }
  94. }