Skip to content

Commit

Permalink
chore: reorganize types
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 30, 2024
1 parent a9491ef commit 200d159
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { AsyncData, AsyncDataOptions, UseFetchOptions, NuxtError } from 'nuxt/app'
import { useAsyncData, useFetch, useNuxtApp, useRuntimeConfig } from 'nuxt/app'
import { type MaybeRefOrGetter, computed, ref } from 'vue'
import type { AsyncData, NuxtError } from 'nuxt/app'
import type { FetchError } from 'ofetch'
import { textToBase64 } from 'undio'
import { hash } from 'ohash'

import type {
DatabasePreset,
KeysOf,
Overrides,
PickFrom,
SurrealAsyncDataOptions,
SurrealFetchOptions,
RpcRequest,
RpcResponse,
} from '../types'

export function useSurrealFetch<T = any>(
endpoint: MaybeRefOrGetter<string>,
options: UseFetchOptions<T> & Overrides = {},
options: SurrealFetchOptions<T> = {},
): AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | null> {
const {
database,
Expand Down Expand Up @@ -87,9 +88,7 @@ export function useSurrealFetch<T = any>(

export function useSurrealRPC<T = any>(
req: RpcRequest<T>,
options: AsyncDataOptions<RpcResponse<T>> & Overrides & {
key?: string
} = {},
options: SurrealAsyncDataOptions<RpcResponse<T>> = {},
): AsyncData<RpcResponse<T> | null, NuxtError<unknown> | null> {
const { $surrealRPC } = useNuxtApp()
const {
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PublicRuntimeConfig } from '@nuxt/schema'
import type { AsyncDataOptions, UseFetchOptions } from 'nuxt/app'

/* Database Overrides */

Expand All @@ -17,6 +18,13 @@ export interface DatabasePreset {
}
}

/* useAsyncData and useFetch custom options */

export type SurrealAsyncDataOptions<T> = AsyncDataOptions<T> & Overrides & {
key?: string
}
export type SurrealFetchOptions<T> = UseFetchOptions<T> & Overrides

/* Module build utils */

export type PickFrom<T, K extends Array<string>> = T extends Array<any>
Expand All @@ -37,7 +45,7 @@ export type KeysOf<T> = Array<
: never
>

/* SurrealDB Methods and Params types */
/* SurrealDB RPC Methods and Params types */

type CreateParams<T> = [
thing: string,
Expand Down

0 comments on commit 200d159

Please sign in to comment.