nuxt-surrealdb
Version:
A Nuxt module aimed to simplify the use of SurrealDB
32 lines (31 loc) • 796 B
JavaScript
import { hash } from "ohash";
import {
createError,
ref,
toValue,
useSurrealFetch
} from "#imports";
export function useSurrealRPC(req, options) {
const id = ref(0);
const { key, ...opts } = options || {};
const _key = key ?? "Sur_" + hash(["surreal", "rpc", toValue(req.method), toValue(req.params)]);
return useSurrealFetch("rpc", {
...opts,
onResponse({ response }) {
if (response.status === 200 && response._data.error) {
throw createError({
statusCode: response._data.error.code,
message: response._data.error.message
});
} else if (response.status === 200) {
response._data = response._data.result;
}
},
method: "POST",
body: {
id: id.value++,
...req
},
key: _key
});
}