This commit is contained in:
Jonathan Jenne
2023-11-27 16:36:23 +01:00
parent e52eca809e
commit 38577f66e0
8 changed files with 99 additions and 53 deletions

View File

@@ -1,16 +1,13 @@
class Network {
getEnvelope(envelopeKey) {
return fetch(
`/api/envelope/${envelopeKey}`,
this.withCSRFToken({ credentials: 'include' })
).then((res) => res.json())
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((res) => res.arrayBuffer())
return fetch(`/api/document/${envelopeKey}?index=${documentId}`, this.withCSRFToken({ credentials: 'include' }))
.then(this.wrapBinaryResponse.bind(this))
}
postEnvelope(envelopeKey, documentId, jsonString) {
@@ -39,8 +36,6 @@
actionType: actionType,
}
console.log(data)
const options = {
credentials: 'include',
method: 'POST',
@@ -71,6 +66,28 @@
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 wrappedResponse
}
handleResponse(res) {
if (!res.ok) {
console.log(`Request failed with status ${res.status}`)
@@ -80,3 +97,10 @@
}
}
}
class WrappedResponse {
constructor(data, error) {
this.data = data
this.error = error
}
}