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.

133 lines
3.6 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 = 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. fileDiscard: this.baseUrl + '/api/file/',
  12. searchAndDisplace: this.baseUrl + '/search-and-displace',
  13. };
  14. constructor() {
  15. }
  16. /**
  17. * Upload a file to the server and return its response.
  18. * Throws an error if the response wasn't successful
  19. *
  20. * TODO: Annotate the return type correctly
  21. *
  22. * @param {File} file The file we want to upload
  23. *
  24. * @returns {Promise<FileUploadResponse>} The response from the server
  25. */
  26. public async uploadFile(file: File): Promise<FileUploadResponse> {
  27. let formData = new FormData();
  28. formData.append('file', file);
  29. try {
  30. let response = await axios.post<FileUploadResponse>(
  31. this.apiRoutes.file,
  32. formData,
  33. {
  34. headers: {
  35. 'Content-Type': 'multipart/form-data'
  36. }
  37. }
  38. )
  39. return response.data;
  40. } catch (err) {
  41. throw err;
  42. }
  43. }
  44. /**
  45. * Get data for a file from the server.
  46. * Throws an error if the response wasn't successful
  47. *
  48. * @param {string} fileId The id of the file we want to query
  49. *
  50. * @returns {Promise<FileStatusResponse>} The response from the server
  51. *
  52. * @throws
  53. */
  54. public async getFileData(fileId: string): Promise<FileStatusResponse> {
  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. try {
  70. let response = await axios.post(
  71. this.apiRoutes.searchAndDisplace,
  72. {
  73. 'content': content,
  74. 'searchers': searchers
  75. }
  76. );
  77. return response.data;
  78. } catch (err) {
  79. throw err;
  80. }
  81. }
  82. /**
  83. * Convert a file from MD to ODT
  84. *
  85. * @param {string} content
  86. * @param {string} fileId
  87. *
  88. * @returns
  89. */
  90. public async convertFile(content: string, fileId: string) {
  91. try {
  92. let response = await axios.post(
  93. this.apiRoutes.fileDownload,
  94. {
  95. 'file_id': fileId,
  96. 'content': content
  97. }
  98. );
  99. return response.data;
  100. } catch (err) {
  101. throw err;
  102. }
  103. }
  104. /**
  105. * Discard a file in progress
  106. *
  107. * @param {string} fileId
  108. */
  109. public async discardFile(fileId: string) {
  110. try {
  111. let response = await axios.delete(
  112. this.apiRoutes.fileDiscard + fileId
  113. );
  114. return response.data;
  115. } catch (err) {
  116. throw err;
  117. }
  118. }
  119. }