1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-27 07:24:17 +00:00
pds-2021-g2-nest/nest_frontend/components/providers/GlobalServer.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

import React, { useCallback } from "react"
2021-04-29 14:58:31 +00:00
import useLocalStorageState from "../../hooks/useLocalStorageState"
import ContextServer from "../../contexts/ContextServer"
import isString from "is-string"
2021-04-26 16:36:41 +00:00
/**
* Provides {@link ContextServer} to all contained elements.
*
* Defaults to using `http://127.0.0.1:5000` as server address.
*
* @param children
* @returns {JSX.Element}
* @constructor
*/
export default function GlobalServer({ children }) {
const [server, setServer] = useLocalStorageState("server", "http://127.0.0.1:5000")
/**
* Fetch JSON data from the API.
*
* @param method - The method to use.
* @param path - The path to request data at (ex. `/api/repositories`)
* @param body - The body of the request (it will be automatically converted to JSON.
* @param init - Additional arguments to pass to the `init` parameter of {@link fetch}.
* @returns {Promise<*>}
*/
const fetchData = useCallback(async (method, path, body, init) => {
2021-04-26 16:36:41 +00:00
if(!server) {
throw new Error(`Invalid server: ${server}`)
}
if(!init) {
init = {}
}
2021-04-26 16:36:41 +00:00
if(!init["headers"]) {
init["headers"] = {}
}
init["headers"]["Content-Type"] = "application/json"
if(method.toUpperCase() === "GET" || method.toUpperCase() === "HEAD") {
let usp = new URLSearchParams()
for(const key in body) {
if(!body.hasOwnProperty(key)) {
return
}
const value = body[key]
if(!isString(value)) {
usp.set(key, value)
}
}
path += `?${usp.toString()}`
}
else {
init["body"] = JSON.stringify(body)
}
2021-04-26 16:36:41 +00:00
const response = await fetch(`${server}${path}`, {
method: method,
...init,
})
if(response.status >= 500) {
throw new Error(`Server error: ${response.status} ${response.statusText}`)
}
const json = await response.json()
if(json["result"] !== "success") {
throw new Error(json["msg"])
2021-04-26 16:36:41 +00:00
}
return json["data"]
}, [server])
2021-04-26 16:36:41 +00:00
return (
2021-05-11 14:37:15 +00:00
<ContextServer.Provider value={{ server, setServer, fetchData }}>
2021-04-26 16:36:41 +00:00
{children}
</ContextServer.Provider>
)
}