2021-02-14 18:21:34 +00:00
|
|
|
import {stringifyParams} from 'query'
|
2021-02-20 18:31:18 +00:00
|
|
|
import globalStore from 'store'
|
|
|
|
import {setLogin} from 'reducers/login'
|
2021-09-14 20:02:57 +00:00
|
|
|
import configPromise from 'config'
|
2021-05-01 11:31:03 +00:00
|
|
|
import download from 'downloadjs'
|
|
|
|
|
|
|
|
function getFileNameFromContentDispostionHeader(contentDisposition: string): string | undefined {
|
|
|
|
const standardPattern = /filename=(["']?)(.+)\1/i
|
|
|
|
const wrongPattern = /filename=([^"'][^;"'\n]+)/i
|
|
|
|
|
|
|
|
if (standardPattern.test(contentDisposition)) {
|
|
|
|
return contentDisposition.match(standardPattern)[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wrongPattern.test(contentDisposition)) {
|
|
|
|
return contentDisposition.match(wrongPattern)[1]
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 18:21:34 +00:00
|
|
|
|
2021-02-23 20:52:57 +00:00
|
|
|
class RequestError extends Error {
|
|
|
|
constructor(message, errors) {
|
|
|
|
super(message)
|
|
|
|
this.errors = errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 21:28:36 +00:00
|
|
|
class API {
|
2021-02-20 18:31:18 +00:00
|
|
|
constructor(store) {
|
|
|
|
this.store = store
|
|
|
|
}
|
|
|
|
|
2021-11-04 17:13:24 +00:00
|
|
|
async loadUser() {
|
|
|
|
try {
|
|
|
|
const result = await this.get('/user')
|
|
|
|
this.store.dispatch(setLogin(result))
|
|
|
|
} catch {
|
|
|
|
this.store.dispatch(setLogin(null))
|
2021-02-20 18:31:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:24:17 +00:00
|
|
|
async logout() {
|
2021-11-04 17:13:24 +00:00
|
|
|
const config = await configPromise
|
|
|
|
const url = new URL(config.apiUrl + '/logout')
|
|
|
|
url.searchParams.append('next', window.location.href) // bring us back to the current page
|
2021-05-14 17:24:17 +00:00
|
|
|
window.location.href = url.toString()
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:26:37 +00:00
|
|
|
async makeLoginUrl() {
|
2021-09-14 20:02:57 +00:00
|
|
|
const config = await configPromise
|
2021-11-14 22:42:02 +00:00
|
|
|
const url = new URL(config.loginUrl || (config.apiUrl + '/login'))
|
2021-11-04 17:13:24 +00:00
|
|
|
url.searchParams.append('next', window.location.href) // bring us back to the current page
|
|
|
|
return url.toString()
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetch(url, options = {}) {
|
2021-09-14 20:02:57 +00:00
|
|
|
const config = await configPromise
|
2021-02-20 18:31:18 +00:00
|
|
|
|
2021-05-01 11:31:03 +00:00
|
|
|
const {returnResponse = false, ...fetchOptions} = options
|
|
|
|
|
2021-11-04 17:13:24 +00:00
|
|
|
const response = await window.fetch(config.apiUrl + url, {
|
2021-05-01 11:31:03 +00:00
|
|
|
...fetchOptions,
|
2021-11-04 17:13:24 +00:00
|
|
|
credentials: 'include',
|
2021-02-10 21:28:36 +00:00
|
|
|
})
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
if (response.status === 401) {
|
|
|
|
throw new Error('401 Unauthorized')
|
|
|
|
}
|
|
|
|
|
2021-05-01 11:31:03 +00:00
|
|
|
if (returnResponse) {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return response
|
|
|
|
} else if (response.status === 204) {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
throw new RequestError('Error code ' + response.status)
|
2021-02-23 20:52:57 +00:00
|
|
|
}
|
2021-05-01 11:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let json
|
|
|
|
try {
|
|
|
|
json = await response.json()
|
|
|
|
} catch (err) {
|
|
|
|
json = null
|
|
|
|
}
|
2021-02-23 20:52:57 +00:00
|
|
|
|
2021-02-14 18:21:34 +00:00
|
|
|
if (response.status === 200) {
|
2021-02-23 20:52:57 +00:00
|
|
|
return json
|
2021-02-26 20:57:36 +00:00
|
|
|
} else if (response.status === 204) {
|
|
|
|
return null
|
2021-02-14 18:21:34 +00:00
|
|
|
} else {
|
2021-02-23 20:52:57 +00:00
|
|
|
throw new RequestError('Error code ' + response.status, json?.errors)
|
2021-02-14 18:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async post(url, {body: body_, ...options}) {
|
2021-02-17 20:50:18 +00:00
|
|
|
let body = body_
|
|
|
|
let headers = {...(options.headers || {})}
|
|
|
|
|
|
|
|
if (!(typeof body === 'string' || body instanceof FormData)) {
|
2021-02-20 18:31:18 +00:00
|
|
|
body = JSON.stringify(body)
|
|
|
|
headers['Content-Type'] = 'application/json'
|
2021-02-17 20:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:27:16 +00:00
|
|
|
return await this.fetch(url, {
|
2021-02-23 20:52:57 +00:00
|
|
|
method: 'post',
|
2021-02-14 18:27:16 +00:00
|
|
|
...options,
|
|
|
|
body,
|
2021-02-20 18:31:18 +00:00
|
|
|
headers,
|
2021-02-14 18:21:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(url, {query, ...options} = {}) {
|
|
|
|
const queryString = query ? stringifyParams(query) : null
|
|
|
|
return await this.fetch(url + (queryString ? '?' + queryString : ''), {method: 'get', ...options})
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(url, options = {}) {
|
2021-02-14 18:27:16 +00:00
|
|
|
return await this.get(url, {...options, method: 'delete'})
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
2021-02-20 18:31:18 +00:00
|
|
|
|
2021-02-23 20:52:57 +00:00
|
|
|
async put(url, options = {}) {
|
|
|
|
return await this.post(url, {...options, method: 'put'})
|
|
|
|
}
|
|
|
|
|
2021-05-01 11:31:03 +00:00
|
|
|
async downloadFile(url, options = {}) {
|
|
|
|
const res = await this.fetch(url, {returnResponse: true, ...options})
|
|
|
|
const blob = await res.blob()
|
|
|
|
const filename = getFileNameFromContentDispostionHeader(res.headers.get('content-disposition'))
|
|
|
|
const contentType = res.headers.get('content-type')
|
|
|
|
|
|
|
|
// Apparently this workaround is needed for some browsers
|
|
|
|
const newBlob = new Blob([blob], {type: contentType})
|
|
|
|
|
|
|
|
download(newBlob, filename, contentType)
|
|
|
|
}
|
2021-02-10 21:28:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 18:31:18 +00:00
|
|
|
const api = new API(globalStore)
|
2021-02-10 21:28:36 +00:00
|
|
|
|
|
|
|
export default api
|