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.

109 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
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 = 'http://core.sandd';
  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. * Upload a file to the server and return its response.
  16. * Throws an error if the response wasn't successful
  17. *
  18. * TODO: Annotate the return type correctly
  19. *
  20. * @param {File} file The file we want to upload
  21. *
  22. * @returns {Promise<FileUploadResponse>} The response from the server
  23. */
  24. public async uploadFile(file: File): Promise<FileUploadResponse>
  25. {
  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. {
  55. try {
  56. let response = await axios.get<FileStatusResponse>(this.apiRoutes.searchAndDisplace + `/${fileId}`);
  57. return response.data;
  58. } catch (err) {
  59. throw err;
  60. }
  61. }
  62. /**
  63. * Perform a search and displace operation on a document
  64. *
  65. * @param {string} content The content of the document
  66. * @param {Array} searchers The list of searchers and their replace values
  67. */
  68. public async filterDocument(content: string, searchers: Array<{ key: string; replace_with: string; }>)
  69. {
  70. try {
  71. let response = await axios.post(
  72. this.apiRoutes.searchAndDisplace,
  73. {
  74. 'content': content,
  75. 'searchers': searchers
  76. }
  77. );
  78. return response.data;
  79. } catch (err) {
  80. throw err;
  81. }
  82. }
  83. public async convertFile(content:string)
  84. {
  85. try {
  86. let response = await axios.post(
  87. this.apiRoutes.fileDownload,
  88. {
  89. 'content': content
  90. }
  91. );
  92. return response.data;
  93. } catch (err) {
  94. throw err;
  95. }
  96. }
  97. }