Skip to content

Commit

Permalink
feat(authenticate): new useSurrealDB function
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 31, 2024
1 parent 9e38976 commit 6bd0c09
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/runtime/composables/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,29 @@ type MROGParam<T, M extends keyof RpcMethods<T>, N extends number> = MaybeRefOrG
export function useSurrealDB(overrides?: Overrides) {
const { $surrealFetch, $surrealFetchOptionsOverride, $surrealRPC } = useNuxtApp()

// TODO: authenticate [ token ]
// authenticate [ token ]
async function $authenticate<T = any>(
token: MROGParam<T, 'authenticate', 0>,
overrides?: Overrides,
) {
return $surrealRPC<T>({ method: 'authenticate', params: [toValue(token)] }, overrides)
}
async function authenticate<T = any>(
token: MROGParam<T, 'authenticate', 0>,
options?: SurrealRpcOptions<T>,
) {
const { key, immediate, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'authenticate'>['params']>(() => ([toValue(token)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'authenticate', toValue(params)])

return useSurrealRPC<T>({ method: 'authenticate', params }, {
...opts,
key: _key,
immediate: immediate === undefined ? false : immediate,
watch: false,
})
}

// create [ thing, data ]
async function $create<T = any>(
Expand Down Expand Up @@ -243,6 +265,8 @@ export function useSurrealDB(overrides?: Overrides) {
}

return {
$authenticate,
authenticate,
$create,
create,
$insert,
Expand Down

0 comments on commit 6bd0c09

Please sign in to comment.