Skip to content

Commit

Permalink
feat: default types for query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 28, 2024
1 parent 04a99d3 commit f7beaa0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
11 changes: 7 additions & 4 deletions playground/pages/fetch-examples.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,23 @@
</div>
</div>
<hr>
<div v-if="forcedError">
<div v-if="!queryData && forcedError">
This is a forced error, cought by <b>
useSurrealRPC:
</b>
<div style="color: crimson;">
{{ forcedError }}
</div>
</div>
<div v-else>
<pre>
{{ queryData?.[0].result }}
</pre>
</div>
</div>
</template>

<script setup lang="ts">
import type { RpcResponse } from '#surrealdb/types'
interface Product {
id: string
brand: string
Expand All @@ -110,7 +113,7 @@ const { data: dataCreate, execute: executeCreate } = await create<Product>('prod
const removeProduct = ref('')
const { execute: executeRemove } = await remove(removeProduct)

const { error: forcedError } = await sql<RpcResponse<Product[]>[]>(
const { data: queryData, error: forcedError } = await sql<[Product[]]>(
'SELECT * ROM products;',
undefined,
{ server: false },
Expand Down
25 changes: 13 additions & 12 deletions src/runtime/composables/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
KeysOf,
PickFrom,
Overrides,
QueryRpcResponse,
RpcMethods,
RpcParams,
RpcRequest,
Expand Down Expand Up @@ -249,29 +250,29 @@ export function useSurrealDB(overrides?: Overrides) {
}

// query [ sql, vars? ]
async function $query<T = any>(
sql: MROGParam<T, 'query', 0>,
vars?: MROGParam<T, 'query', 1>,
async function $query<T = any, R = QueryRpcResponse<T>>(
sql: MROGParam<any, 'query', 0>,
vars?: MROGParam<any, 'query', 1>,
options?: Overrides,
): Promise<T> {
return $surrealRPC<T>({
): Promise<R> {
return $surrealRPC<R>({
method: 'query', params: [toValue(sql), toValue(vars)],
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token,
})
}
async function query<T = any>(
sql: MROGParam<T, 'query', 0>,
vars?: MROGParam<T, 'query', 1>,
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
async function query<T = any, R = QueryRpcResponse<T>>(
sql: MROGParam<any, 'query', 0>,
vars?: MROGParam<any, 'query', 1>,
options?: UseSurrealRpcOptions<R>,
): Promise<AsyncData<PickFrom<R, KeysOf<R>> | null, FetchError<any> | RpcResponseError | null>> {
const { database, key, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<T, 'query'>['params']>(() => ([toValue(sql), toValue(vars)]))
const params = computed<RpcRequest<R, 'query'>['params']>(() => ([toValue(sql), toValue(vars)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'query', toValue(params)])

return useSurrealRPC<T>({ method: 'query', params }, {
return useSurrealRPC<R>({ method: 'query', params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/server/utils/surreal-db.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { H3Event } from 'h3'

import type { RpcParams } from '../../types'
import type { QueryRpcResponse, RpcParams } from '../../types'
import { type ServerOverrides, useSurrealRPC } from './surreal-fetch'

export function useSurrealDB(event: H3Event, overrides?: ServerOverrides) {
// query [ sql, vars ]
async function query<T = any>(
sql: RpcParams<T, 'query'>[0],
vars?: RpcParams<T, 'query'>[1],
async function query<T = any, R = QueryRpcResponse<T>>(
sql: RpcParams<any, 'query'>[0],
vars?: RpcParams<any, 'query'>[1],
options?: ServerOverrides,
): Promise<T> {
return useSurrealRPC<T>(event, {
): Promise<R> {
return useSurrealRPC<R>(event, {
method: 'query', params: [sql, vars],
}, {
database: options?.database || overrides?.database,
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export interface DatabasePreset {

/* useAsyncData and useFetch custom options */

export type SurrealAsyncDataOptions<T> = AsyncDataOptions<T> & Overrides & {
export type SurrealAsyncDataOptions<T, R = T> = AsyncDataOptions<T, R> & Overrides & {
key?: string
}
export type SurrealFetchOptions<
T extends ResponseType = 'json',
> = Omit<FetchOptions<T>, 'method'> & {
method?: Uppercase<SurrealMethods> | SurrealMethods
}
export type UseSurrealFetchOptions<T> = UseFetchOptions<T> & Overrides
export type UseSurrealRpcOptions<T> = Omit<UseSurrealFetchOptions<T>, 'method' | 'body' | 'onResponse'>
export type UseSurrealFetchOptions<T, R = T> = UseFetchOptions<T, R> & Overrides
export type UseSurrealRpcOptions<T, R = T> = Omit<UseSurrealFetchOptions<T, R>, 'method' | 'body' | 'onResponse'>

/* Utils */

Expand Down Expand Up @@ -229,3 +229,8 @@ export interface HttpResponseError {

export type HttpResponse<R> = Array<(HttpResponseOk<R> | HttpResponseError)>
export type HttpRes<R> = HttpResponse<R>

/* Utility Types */
export type QueryRpcResponse<T> = T extends [infer First, ...infer Rest]
? [RpcResponse<First>, ...QueryRpcResponse<Rest>]
: []

0 comments on commit f7beaa0

Please sign in to comment.