Skip to content

Commit

Permalink
feat(useSurrealRPC): type improvements and key defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 31, 2024
1 parent 60b11fe commit 6f95e1e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AsyncData } from 'nuxt/app'
import type { FetchError } from 'ofetch'
import { hash } from 'ohash'
import {
useFetch,
useNuxtApp,
Expand All @@ -14,10 +15,10 @@ import type {
RpcResponse,
} from '../types'
import type {
MaybeRefOrGetter,
ComputedRef,
MaybeRefOrGetter,
} from '#imports'
import { ref } from '#imports'
import { ref, toValue } from '#imports'

export function useSurrealFetch<T = any>(
endpoint: MaybeRefOrGetter<string>,
Expand Down Expand Up @@ -45,19 +46,24 @@ export function useSurrealFetch<T = any>(

export function useSurrealRPC<T = any>(
req: {
method: RpcRequest<T>['method']
params: RpcRequest<T>['params'] | ComputedRef<RpcRequest<T>['params']>
method: MaybeRefOrGetter<RpcRequest<T>['method']>
params: MaybeRefOrGetter<RpcRequest<T>['params']> | ComputedRef<RpcRequest<T>['params']>
},
options?: SurrealRpcOptions<T>,
): AsyncData<RpcResponse<T> | null, FetchError<any> | null> {
const id = ref(0)
const { key, ...opts } = options || {}

const _key = key ?? 'Sur_' + hash(['surreal', 'rpc', toValue(req.method), toValue(req.params)])

return useSurrealFetch<RpcResponse<T>>('rpc', {
...options,
...opts,
method: 'POST',
body: {
id: id.value++,
...req,
method: toValue(req.method),
params: toValue(req.params),
},
key: _key,
})
}

0 comments on commit 6f95e1e

Please sign in to comment.