refactor(network): Netzwerkklasse entfernen und Methoden statisch machen.
- Unnötige Funktionen entfernen.
This commit is contained in:
parent
86eb687296
commit
369d101d7b
@ -13,9 +13,6 @@ class App {
|
|||||||
constructor(envelopeKey, envelopeReceiver, documentBytes, licenseKey, locale, container) {
|
constructor(envelopeKey, envelopeReceiver, documentBytes, licenseKey, locale, container) {
|
||||||
this.container = container ?? `#${this.constructor.name.toLowerCase()}`;
|
this.container = container ?? `#${this.constructor.name.toLowerCase()}`;
|
||||||
this.envelopeKey = envelopeKey
|
this.envelopeKey = envelopeKey
|
||||||
|
|
||||||
this.Network = new Network()
|
|
||||||
|
|
||||||
this.Instance = null
|
this.Instance = null
|
||||||
this.currentDocument = null
|
this.currentDocument = null
|
||||||
this.currentReceiver = null
|
this.currentReceiver = null
|
||||||
@ -275,7 +272,7 @@ class App {
|
|||||||
|
|
||||||
// Export annotation data and save to database
|
// Export annotation data and save to database
|
||||||
try {
|
try {
|
||||||
const res = await this.Network.postEnvelope(this.envelopeKey, await iJSON);
|
const res = await postEnvelope(this.envelopeKey, await iJSON);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
if (res.status === 403) {
|
if (res.status === 403) {
|
||||||
|
|||||||
@ -1,149 +1,30 @@
|
|||||||
class Network {
|
/**
|
||||||
|
* Fetches CSRF Token from page
|
||||||
/**
|
* @returns
|
||||||
* Load envelope json data
|
*/
|
||||||
* @param {any} envelopeKey
|
function getCSRFToken() {
|
||||||
*/
|
const token = document.getElementsByName('__RequestVerificationToken')[0].value
|
||||||
async getEnvelope(envelopeKey) {
|
return { 'X-XSRF-TOKEN': token }
|
||||||
return this.getRequest(`/api/envelope/${envelopeKey}`)
|
|
||||||
.then(this.wrapJsonResponse.bind(this))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Save signature data to server
|
|
||||||
* @param {any} envelopeKey
|
|
||||||
* @param {any} documentId
|
|
||||||
* @param {any} json
|
|
||||||
*/
|
|
||||||
async postEnvelope(envelopeKey, annotations) {
|
|
||||||
return this.postRequest(`/api/envelope/${envelopeKey}`, annotations)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load document binary data
|
|
||||||
* @param {any} envelopeKey
|
|
||||||
* @param {any} documentId
|
|
||||||
*/
|
|
||||||
async getDocument(envelopeKey, documentId) {
|
|
||||||
return this.getRequest(`/api/document/${envelopeKey}?index=${documentId}`)
|
|
||||||
.then(this.wrapBinaryResponse.bind(this))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add CSRF Token to request headers
|
|
||||||
* @param {any} options
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
withCSRFToken(options) {
|
|
||||||
const token = getCSRFToken
|
|
||||||
let headers = options.headers
|
|
||||||
|
|
||||||
options.headers = {
|
|
||||||
...headers,
|
|
||||||
...token
|
|
||||||
}
|
|
||||||
|
|
||||||
return options
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches CSRF Token from page
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
getCSRFToken() {
|
|
||||||
const token = document.getElementsByName('__RequestVerificationToken')[0].value
|
|
||||||
return { 'X-XSRF-TOKEN': token }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a GET HTTP request to `url`
|
|
||||||
* @param {any} url
|
|
||||||
*/
|
|
||||||
getRequest(url) {
|
|
||||||
const token = this.getCSRFToken()
|
|
||||||
const options = {
|
|
||||||
credentials: 'include',
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
...token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(url, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a POST HTTP request for url
|
|
||||||
* @param {any} url
|
|
||||||
* @param {any} json
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
postRequest(url, json) {
|
|
||||||
const token = this.getCSRFToken()
|
|
||||||
const options = {
|
|
||||||
credentials: 'include',
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
...token,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(json)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(url, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads and wraps a json response
|
|
||||||
* @param {any} response
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async wrapJsonResponse(response) {
|
|
||||||
return await this.wrapResponse(
|
|
||||||
response,
|
|
||||||
async (res) => await res.json())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads and wraps a binary response
|
|
||||||
* @param {any} response
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async wrapBinaryResponse(response) {
|
|
||||||
return await this.wrapResponse(
|
|
||||||
response,
|
|
||||||
async (res) => await res.arrayBuffer())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wraps a fetch response depending on status code
|
|
||||||
* @param {any} response
|
|
||||||
* @param {any} responseHandler
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async wrapResponse(response, responseHandler) {
|
|
||||||
let wrappedResponse
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const data = await responseHandler(response)
|
|
||||||
wrappedResponse = new WrappedResponse(data, null)
|
|
||||||
} else if (response.status === 403) {
|
|
||||||
const error = await response.json()
|
|
||||||
wrappedResponse = new WrappedResponse(null, error)
|
|
||||||
} else {
|
|
||||||
wrappedResponse = new WrappedResponse(null, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
return wrappedResponse
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class WrappedResponse {
|
/**
|
||||||
constructor(data, error) {
|
* Save signature data to server
|
||||||
this.data = data
|
* @param {any} envelopeKey
|
||||||
this.error = error
|
* @param {any} annotations
|
||||||
this.fatal = (data === null && error === null)
|
*/
|
||||||
|
function postEnvelope(envelopeKey, annotations) {
|
||||||
|
const token = getCSRFToken()
|
||||||
|
const options = {
|
||||||
|
credentials: 'include',
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
...token,
|
||||||
|
'Content-Type': 'application/json; charset=utf-8'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fetch(`/api/envelope/${envelopeKey}`, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setLangAsync(language, flagCode) {
|
async function setLangAsync(language, flagCode) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user