Files
bee-dashboard/src/utils/net.ts
T
Vojtech Simetka 87b0b71cc6 feat: add bee-desktop settings capabilities (#323)
* refactor: make the config readonly and extract endpoint calls to hook (+2 squashed commits)
Squashed commits:
[91ffe45] feat: add swap-endpoint
[e1d0c3a] feat: add bee-desktop settings capabilities

* feat: use the request mechanism that uses the bee-desktop API key

* fix: properly reset the error or on error set the config to null
2022-04-29 09:30:46 +02:00

34 lines
783 B
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import axios from 'axios'
export function getJson<T extends Record<string, any>>(url: string): Promise<T> {
return sendRequest(url, 'GET') as Promise<T>
}
export function postJson(url: string, data?: Record<string, any>): Promise<Record<string, unknown>> {
return sendRequest(url, 'POST', data)
}
async function sendRequest(
url: string,
method: 'GET' | 'POST',
data?: Record<string, unknown>,
): Promise<Record<string, any>> {
const authorization = localStorage.getItem('apiKey')
if (!authorization) {
throw Error('API key not found in local storage')
}
const headers = {
authorization,
}
const response = await axios(url, {
method,
headers,
data,
})
return response.data
}