valync
Version:
**A lightweight, framework-agnostic async data handling library for React & Vue, inspired by Riverpod’s AsyncValue pattern and powered by ts-results-es.**
34 lines • 1 kB
JavaScript
export const DefaultValyncClient = (url, init) => fetch(url, init).then(async (resp) => {
let json;
try {
json = await resp.json();
}
catch {
return {
status: "failed",
error: {
name: "ParseError",
message: "Invalid JSON",
},
};
}
// DEV-ONLY: Validate ApiResponse format
if (process.env.NODE_ENV !== "production" &&
(typeof json !== "object" ||
!("status" in json) ||
(json.status !== "success" && json.status !== "failed"))) {
console.warn(`[Valync] Expected ApiResponse<T> format missing. Got:`, json);
}
if (!resp.ok || json.status === "failed") {
return {
status: "failed",
error: json?.error ?? {
name: "HttpError",
message: resp.statusText,
code: resp.status,
},
};
}
return json;
});
//# sourceMappingURL=util.js.map