mirror of
https://github.com/logos-storage/logos-storage-js.git
synced 2026-03-23 04:03:19 +00:00
35 lines
613 B
TypeScript
35 lines
613 B
TypeScript
|
|
import { SafeValue } from "../values/values"
|
||
|
|
|
||
|
|
export const Fetch = {
|
||
|
|
async safe<T extends Object>(url: string, init: RequestInit): Promise<SafeValue<T>> {
|
||
|
|
const res = await fetch(url, init)
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
const message = await res.text()
|
||
|
|
|
||
|
|
return {
|
||
|
|
error: true,
|
||
|
|
data: {
|
||
|
|
type: "api",
|
||
|
|
message,
|
||
|
|
status: res.status
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const json = await res.json()
|
||
|
|
|
||
|
|
return { error: false, data: json }
|
||
|
|
} catch (e) {
|
||
|
|
return {
|
||
|
|
error: true,
|
||
|
|
data: {
|
||
|
|
type: "error",
|
||
|
|
message: e instanceof Error ? e.message : "JSON parsing error :" + e
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|