validation client and server
This commit is contained in:
@@ -1,107 +1,167 @@
|
||||
class Network {
|
||||
getEnvelope(envelopeKey) {
|
||||
return fetch(
|
||||
`/api/envelope/${envelopeKey}`,
|
||||
this.withCSRFToken({ credentials: 'include' })
|
||||
).then(this.wrapJsonResponse.bind(this))
|
||||
}
|
||||
|
||||
getDocument(envelopeKey, documentId) {
|
||||
return fetch(
|
||||
`/api/document/${envelopeKey}?index=${documentId}`,
|
||||
this.withCSRFToken({ credentials: 'include' })
|
||||
).then(this.wrapBinaryResponse.bind(this))
|
||||
}
|
||||
|
||||
postEnvelope(envelopeKey, documentId, jsonString) {
|
||||
const url = `/api/envelope/${envelopeKey}?index=${documentId}`
|
||||
const options = {
|
||||
credentials: 'include',
|
||||
method: 'POST',
|
||||
body: jsonString,
|
||||
/**
|
||||
* Load envelope json data
|
||||
* @param {any} envelopeKey
|
||||
*/
|
||||
async getEnvelope(envelopeKey) {
|
||||
console.log("getEnvelope")
|
||||
return this.getRequest(`/api/envelope/${envelopeKey}`)
|
||||
.then(this.wrapJsonResponse.bind(this))
|
||||
}
|
||||
|
||||
console.debug('PostEnvelope/Calling url: ' + url)
|
||||
return fetch(url, this.withCSRFToken(options))
|
||||
.then(this.handleResponse)
|
||||
.then((res) => {
|
||||
if (!res.ok) {
|
||||
return false
|
||||
/**
|
||||
* Save signature data to server
|
||||
* @param {any} envelopeKey
|
||||
* @param {any} documentId
|
||||
* @param {any} json
|
||||
*/
|
||||
async postEnvelope(envelopeKey, documentId, json) {
|
||||
console.log("postEnvelope")
|
||||
return this.postRequest(`/api/envelope/${envelopeKey}?index=${documentId}`, json)
|
||||
.then(this.wrapJsonResponse.bind(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Load document binary data
|
||||
* @param {any} envelopeKey
|
||||
* @param {any} documentId
|
||||
*/
|
||||
async getDocument(envelopeKey, documentId) {
|
||||
console.log("getDocument")
|
||||
return this.getRequest(`/api/document/${envelopeKey}?index=${documentId}`)
|
||||
.then(this.wrapBinaryResponse.bind(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell the server that document has been seen
|
||||
* @param {any} envelopeKey
|
||||
*/
|
||||
async openDocument(envelopeKey) {
|
||||
console.log("openDocument")
|
||||
return this.postRequest(`/api/document/${envelopeKey}`, {})
|
||||
.then(this.wrapJsonResponse.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 true
|
||||
})
|
||||
}
|
||||
|
||||
openDocument(envelopeKey) {
|
||||
const url = `/api/document/${envelopeKey}`
|
||||
|
||||
const options = {
|
||||
credentials: 'include',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
return options
|
||||
}
|
||||
|
||||
console.debug('OpenDocument/Calling url: ' + url)
|
||||
return fetch(url, this.withCSRFToken(options))
|
||||
.then(this.handleResponse)
|
||||
.then((res) => {
|
||||
if (!res.ok) {
|
||||
return false
|
||||
/**
|
||||
* 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 true
|
||||
})
|
||||
}
|
||||
|
||||
withCSRFToken(options) {
|
||||
const token = document.getElementsByName('__RequestVerificationToken')[0]
|
||||
.value
|
||||
let headers = options.headers
|
||||
options.headers = { ...headers, 'X-XSRF-TOKEN': token }
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
async wrapJsonResponse(response) {
|
||||
return await this.wrapResponse(response, async (res) => await res.json())
|
||||
}
|
||||
|
||||
async wrapBinaryResponse(response) {
|
||||
return await this.wrapResponse(
|
||||
response,
|
||||
async (res) => await res.arrayBuffer()
|
||||
)
|
||||
}
|
||||
|
||||
async wrapResponse(response, responseHandler) {
|
||||
let wrappedResponse
|
||||
|
||||
if (response.ok) {
|
||||
const data = await responseHandler(response)
|
||||
wrappedResponse = new WrappedResponse(data, null)
|
||||
} else {
|
||||
const error = await response.json()
|
||||
wrappedResponse = new WrappedResponse(null, error)
|
||||
return fetch(url, options)
|
||||
}
|
||||
|
||||
return wrappedResponse
|
||||
}
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
|
||||
handleResponse(res) {
|
||||
if (!res.ok) {
|
||||
console.log(`Request failed with status ${res.status}`)
|
||||
return res
|
||||
} else {
|
||||
return res
|
||||
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
|
||||
|
||||
console.log("Handling response from", response.url)
|
||||
console.log("Status", response.status)
|
||||
console.log(response)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
console.log("Wrapped response", wrappedResponse)
|
||||
|
||||
return wrappedResponse
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WrappedResponse {
|
||||
constructor(data, error) {
|
||||
this.data = data
|
||||
this.error = error
|
||||
}
|
||||
constructor(data, error) {
|
||||
this.data = data
|
||||
this.error = error
|
||||
this.fatal = (data === null && error === null)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user