source: main/trunk/model-interfaces-dev/atea/macron-restoration/src/js/MacronRestorationModule.js@ 35772

Last change on this file since 35772 was 35772, checked in by cstephen, 2 years ago

Upgrade to support the new macroniser backend.
Fix chunk/asset resolution

File size: 4.1 KB
Line 
1/**
2 * @file Defines components that are used to interact with the transcription proxy servlet.
3 * @author Carl Stephens
4 * @module
5 */
6
7import { log } from "./Util"
8
9export class MacronRestorationError extends Error {
10 /**
11 * Initialises a new instance of the {@link MacronRestorationError} object.
12 *
13 * @param {Number | undefined} statusCode The HTTP status code of the error.
14 * @param {String | undefined} message The status message.
15 * @param {String | null} fileName The file on which the transcription failed.
16 */
17 constructor(message = undefined, fileName = null, statusCode = -1) {
18 super(message);
19
20 /** @type {String | null} The name of the file that the transcription error occured on. */
21 this.fileName = fileName;
22
23 /** @type {Number | undefined} The status code returned by the API when the erro was generated. */
24 this.statusCode = statusCode;
25 }
26}
27
28/**
29 * A service that performs requests to the macron restoration API.
30 */
31export default class MacronRestorationService {
32 constructor() {
33 /** @type {String} The URL to which query POST requests should be made. */
34 if (process.env.NODE_ENV !== "production") {
35 this.rootQueryUrl = "//localhost:8383/gs3-macroniser/";
36 }
37 else {
38 this.rootQueryUrl = "/gs3-macroniser/";
39 }
40 }
41
42 /**
43 * Performs a request to the macron restoration API to restore the given input text.
44 * @param {String} input The input text to macronise
45 * @returns {Promise<{ w?: String, macronised?: Boolean, linebreaks?: Number }>} The JSON response from the macron restoration API.
46 */
47 async directMacronisation(input, preserveExistingMacrons) {
48 const queryUrl = this.rootQueryUrl + "direct";
49
50 const formData = new FormData();
51 formData.append("fragment", input);
52 formData.append("preserveExistingMacrons", preserveExistingMacrons);
53
54 try {
55 const response = await fetch(
56 queryUrl,
57 {
58 method: "POST",
59 body: formData
60 }
61 );
62
63 if (!response.ok) {
64 log(`Macron Restoration API failed with status ${response.status} and message ${response.statusText}`, "error")
65 throw new MacronRestorationError(response.statusText, undefined, response.status);
66 }
67
68 return await response.json();
69 }
70 catch (e) {
71 log(`Macron restoration failed with reason ${e}`, "error");
72 throw new MacronRestorationError(undefined, "Unknown");
73 }
74 }
75
76 /**
77 * Performs a query to macronise the given file.
78 *
79 * @param {File} file The file to macronise.
80 * @param {Boolean} preserveExistingMacrons Indicates if existing macrons on the file should be preserved.
81 * @returns {Promise<{ fileName: String, filePath: String, fileType: String }>} Information about the macronised file.
82 * @throws {MacronRestorationError} When the macronisation request fails to complete.
83 */
84 async fileMacronisation(file, preserveExistingMacrons) {
85 const queryUrl = this.rootQueryUrl + "file";
86 const formData = new FormData();
87
88 formData.append("o", "json");
89 formData.append("preserveExistingMacrons", preserveExistingMacrons);
90 formData.append(file.name, file, file.name);
91
92 try {
93 const response = await fetch(
94 queryUrl,
95 {
96 method: "POST",
97 body: formData
98 }
99 );
100
101 if (!response.ok) {
102 log(`Macronisation API failed with status ${response.status} and message ${response.statusText}`, "error")
103 throw new MacronRestorationError(response.statusText, file.Name, response.status);
104 }
105
106 return await response.json();
107 }
108 catch (e) {
109 log(`Macronisation failed with reason ${e}`, "error");
110 throw new MacronRestorationError("Unknown", file.Name, "Unknown");
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.