-
Notifications
You must be signed in to change notification settings - Fork 7
/
File.ts
79 lines (72 loc) · 2.67 KB
/
File.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { LoginCredentials } from '../../utils/soap/Client/Client.interfaces';
import soap from '../../utils/soap/soap';
/**
* File class
* @abstract
* @extends {soap.Client}
* @constructor
*/
export default abstract class File<T> extends soap.Client {
public readonly documentGu: string;
private readonly methodName: string;
public constructor(credentials: LoginCredentials, documentGu: string, methodName: string) {
super(credentials);
/**
* The DocumentGU of the file so that it can be fetched from synergy servers
* This value is important for `File.get()` method. You cannot fetch the file without it
* @public
* @readonly
* @type {string}
*/
this.documentGu = documentGu;
/**
* Synergy servers have different methods for retrieving files. For example,
*
* To retrieve a document, there is a specific method for it: `GetContentOfAttachedDoc`
*
* To retrieve a report card, there is a specific method for it: `GetReportCardDocumentData`
*
* Therefore, methodName must be defined to get retrieve the file data. See how methodName is implemented
* in `Document.ts` and `ReportCard.ts`
* @private
* @readonly
* @type {string}
*/
this.methodName = methodName;
}
/**
* Parse the XML object to translate it into an ordinary object. This method must be written for every class that extends Document (which gets the file from synergy servers using a POST fetch request)
* @param {unknown} xmlObject The XML Object passed after parsing
* @protected
* @returns {T} Returns a reformatted XML object to make it easier for code
* @description
* ```js
* const xmlObject = await super.processRequest({...}); // { "@_Attribute": [{ "@_Nested": [{...}, {...}]}]}
* parseXMLObject(xmlObject); // { attribute: { nested: [{...}, {...}] } }
*
* ```
*/
protected abstract parseXMLObject(xmlObject: unknown): T;
/**
* Retrieve the file from synergy servers. After retrieving the xmlObject, this method calls parseXMLObject which must be defined to parse the xmlObject into a readable, typesafe object.
* @public
* @returns {Promise<T>} Returns a base64 object
* @description
* ```js
* const base64 = await document.get(); // { attribute: { nested: {...} }, base64: "base64 code" }
* ```
*/
public get(): Promise<T> {
return new Promise((res, rej) => {
super
.processRequest<Record<string, unknown>>({
methodName: this.methodName,
paramStr: { childIntId: 0, DocumentGU: this.documentGu },
})
.then((base64Data) => {
res(this.parseXMLObject(base64Data));
})
.catch(rej);
});
}
}