Skip to content

Commit

Permalink
feat(useSurrealRPC): new composable
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 29, 2024
1 parent 7285c13 commit d48709f
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 36 deletions.
25 changes: 7 additions & 18 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
<pre>
{{ test ?? 'No Data' }}
</pre>
<pre>
{{ _items ?? _itemsError }}
</pre>
</div>
</div>
</template>

<script setup lang="ts">
import type { Res } from '../src/runtime/types'
import type { HttpRes } from '../src/runtime/types'
interface Product {
id: string
Expand All @@ -34,23 +31,15 @@ interface Product {
price: number
currency: string
}
const { items, sql, $sql, version } = useSurrealDB()
const { data: _items, error: _itemsError } = await items<Res<Product[]>>('products', {
transform: (data: any) => {
return data[0].status === 'OK' ? data[0].result : []
},
})
const { sql } = useSurrealDB()
const { data, error } = await sql<HttpRes<Product[]>>('SELECT * FROM products;')
const test = ref<Res<Product[]> | undefined>()
const test = ref<any>([])
async function fetchSql() {
test.value = await $sql<Res<Product[]>>('SELECT * FROM products;', {
database: 'staging',
test.value = await useSurrealRPC({
method: 'select',
params: ['products'],
})
}
const { data, error } = await sql<Res<Product[]>>('SELECT * FROM products;')
const { data: _version } = await version({
database: 'staging',
})
</script>
23 changes: 22 additions & 1 deletion src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import type { AsyncData, UseFetchOptions } from 'nuxt/app'
import { type MaybeRefOrGetter, ref } from 'vue'
import type { FetchError } from 'ofetch'

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

export function useSurrealFetch<T = any>(
endpoint: MaybeRefOrGetter<string>,
Expand Down Expand Up @@ -49,3 +56,17 @@ export function useSurrealFetch<T = any>(
$fetch: useNuxtApp().$surrealFetch,
})
}

export function useSurrealRPC<T = any>(req: RpcRequest<T>, ovr?: Overrides) {
const { $surrealFetch, $surrealFetchOptionsOverride } = useNuxtApp()
const id = ref<number>(0)

return $surrealFetch<RpcResponse<T>>('rpc', {
...$surrealFetchOptionsOverride(ovr),
method: 'POST',
body: {
id: id.value++,
...req,
},
})
}
163 changes: 146 additions & 17 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import type { PublicRuntimeConfig } from '@nuxt/schema'

export interface DatabasePreset {
host?: string
NS?: string | null
DB?: string | null
}
/* Database Overrides */

export interface Overrides {
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | DatabasePreset
token?: string
}

type OKResponse<T> = {
result: T
status: 'OK'
time: string
}

type ErrorResponse = {
result: string
status: 'ERR'
time: string
export interface DatabasePreset {
host?: string
NS?: string | null
DB?: string | null
}

export type Response<T> = Array<(OKResponse<T> | ErrorResponse)>
export type Res<T> = Response<T>
/* Module build utils */

export type PickFrom<T, K extends Array<string>> = T extends Array<any>
? T
Expand All @@ -43,3 +32,143 @@ export type KeysOf<T> = Array<
: never
: never
>

/* SurrealDB Methods and Params types */

type CreateParams<T> = [
thing: string,
data?: T,
]

type InsertParams<T> = [
thing: string,
data?: T,
]

type MergeParams<T> = [
thing: string,
data: T,
]

type PatchParams<T> = [
thing: string,
patches: Array<{ op: string, path: string, value: T }>,
diff?: boolean,
]

type QueryParams<T> = [
sql: string,
vars?: T,
]

type SignInParams<T = { [key: string]: string | undefined }> = [{
NS?: string
DB?: string
SC?: string
user?: string
pass?: string
} & T]

type SignUpParams<T = { [key: string]: string }> = [{
NS?: string
DB?: string
SC?: string
} & T]

type UpdateParams<T> = [
thing: string,
data?: T,
]

type UseParams = [
NS?: string,
DB?: string,
]

export interface RpcMethods<T> {
authenticate: [string]
create: CreateParams<T>
delete: [string]
info: never
insert: InsertParams<T>
invalidate: never
merge: MergeParams<T>
patch: PatchParams<T>
query: QueryParams<T>
select: [string]
signin: SignInParams<T>
signup: SignUpParams<T>
update: UpdateParams<T>
use: UseParams
}

export interface RpcMethodsWS<T> extends RpcMethods<T> {
kill: [string]
let: [
name: string,
value: T,
]
live: [
table: string,
diff?: T,
]
unset: [string]
}

export type RpcParams<M extends keyof RpcMethods<any>, T> = RpcMethods<T>[M]
export type RpcParamsWS<M extends keyof RpcMethodsWS<any>, T> = RpcMethodsWS<T>[M]

/* SurrealDB RPC Request and Response types */

export interface RpcRequest<
T = any,
M extends keyof RpcMethods<T> = keyof RpcMethods<T>,
P extends RpcParams<M, T> = RpcParams<M, T>,
> {
method: M
params?: P
}

export interface RpcRequestWS<
T = any,
M extends keyof RpcMethodsWS<T> = keyof RpcMethods<T>,
P extends RpcParamsWS<M, T> = RpcParamsWS<M, T>,
> {
method: M
params?: P
}

type Test = RpcRequest<any, 'create'>

export interface RpcResponseOk<R> {
result: R
error?: never
}

export interface RpcResponseError {
result?: never
error: {
code: number
message: string
}
}

export type RpcResponse<R> = RpcResponseOk<R> | RpcResponseError
export type RpcRes<R> = RpcResponse<R>

/* SurrealDB HTTP Response types */

export interface HttpResponseOk<R> {
result: R
status: 'OK'
time: string
}

export interface HttpResponseError {
result: string
status: 'ERR'
time: string
}

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

0 comments on commit d48709f

Please sign in to comment.