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.

180 lines
5.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
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. sdOnOriginalDocument: `${this.baseUrl}/search-and-displace/original-document`,
  14. };
  15. constructor() {
  16. }
  17. /**
  18. * Upload a file to the server and return its response.
  19. * Throws an error if the response wasn't successful
  20. *
  21. * TODO: Annotate the return type correctly
  22. *
  23. * @param {File} file The file we want to upload
  24. *
  25. * @returns {Promise<FileUploadResponse>} The response from the server
  26. */
  27. public async uploadFile(file: File): Promise<FileUploadResponse> {
  28. let formData = new FormData();
  29. formData.append('file', file);
  30. try {
  31. let response = await axios.post<FileUploadResponse>(
  32. this.apiRoutes.file,
  33. formData,
  34. {
  35. headers: {
  36. 'Content-Type': 'multipart/form-data'
  37. }
  38. }
  39. )
  40. return response.data;
  41. } catch (err) {
  42. throw err;
  43. }
  44. }
  45. /**
  46. *
  47. * @param document
  48. * @param searchers
  49. */
  50. public async sdOnOriginalDocument(
  51. document: File,
  52. searchers: Array<{ key: string; type: string; value: string; }>
  53. ): Promise<any> {
  54. let formData = new FormData();
  55. formData.append('document', document);
  56. formData.append('searchers', JSON.stringify(searchers));
  57. try {
  58. let response = await axios.post<any>(
  59. this.apiRoutes.sdOnOriginalDocument,
  60. formData,
  61. {
  62. headers: {
  63. 'Content-Type': 'multipart/form-data'
  64. }
  65. }
  66. )
  67. return response.data;
  68. } catch (err) {
  69. throw err;
  70. }
  71. }
  72. public async verifySdOnOriginalDocumentIsDone(id: string) {
  73. try {
  74. let response = await axios.get<any>( `${this.apiRoutes.sdOnOriginalDocument}/${id}`);
  75. return response.data;
  76. } catch (err) {
  77. throw err;
  78. }
  79. }
  80. /**
  81. * Get data for a file from the server.
  82. * Throws an error if the response wasn't successful
  83. *
  84. * @param {string} fileId The id of the file we want to query
  85. *
  86. * @returns {Promise<FileStatusResponse>} The response from the server
  87. *
  88. * @throws
  89. */
  90. public async getFileData(fileId: string): Promise<FileStatusResponse> {
  91. try {
  92. let response = await axios.get<FileStatusResponse>(this.apiRoutes.searchAndDisplace + `/${fileId}`);
  93. return response.data;
  94. } catch (err) {
  95. throw err;
  96. }
  97. }
  98. /**
  99. * Perform a search and displace operation on a document
  100. *
  101. * @param {string} content The content of the document
  102. * @param {Array} searchers The list of searchers and their replace values
  103. * @param {boolean} searchOnly Whether or not to also displace the content (default yes)
  104. */
  105. public async filterDocument(
  106. content: string,
  107. searchers: Array<{ key: string; type: string; value: string; }>,
  108. searchOnly: boolean = false
  109. ) {
  110. try {
  111. let response = await axios.post(
  112. this.apiRoutes.searchAndDisplace,
  113. {
  114. 'content': content,
  115. 'searchers': searchers,
  116. 'searchOnly': searchOnly
  117. }
  118. );
  119. return response.data;
  120. } catch (err) {
  121. throw err;
  122. }
  123. }
  124. /**
  125. * Convert a file from MD to ODT
  126. *
  127. * @param {string} content
  128. * @param {string} fileId
  129. *
  130. * @returns
  131. */
  132. public async convertFile(content: string, fileId: string) {
  133. try {
  134. let response = await axios.post(
  135. this.apiRoutes.fileDownload,
  136. {
  137. 'file_id': fileId,
  138. 'content': content
  139. }
  140. );
  141. return response.data;
  142. } catch (err) {
  143. throw err;
  144. }
  145. }
  146. /**
  147. * Discard a file in progress
  148. *
  149. * @param {string} fileId
  150. */
  151. public async discardFile(fileId: string) {
  152. try {
  153. let response = await axios.delete(
  154. this.apiRoutes.fileDiscard + fileId
  155. );
  156. return response.data;
  157. } catch (err) {
  158. throw err;
  159. }
  160. }
  161. }