Skip to content

Commit 7ddac33

Browse files
committed
fix: useSurrealFetch and useSurrealRpc types
1 parent 297b5b3 commit 7ddac33

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

Diff for: src/runtime/composables/surreal-db.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function useSurrealDB(overrides?: Overrides) {
7878
thing: MROGParam<T, 'create', 0>,
7979
data?: MROGParam<T, 'create', 1>,
8080
options?: UseSurrealRpcOptions<T>,
81-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
81+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
8282
const { database, immediate, key, token, watch, ...opts } = options || {}
8383

8484
const params = computed<RpcRequest<T, 'create'>['params']>(() => ([toValue(thing), toValue(data)]))
@@ -107,7 +107,7 @@ export function useSurrealDB(overrides?: Overrides) {
107107
}
108108
async function info<T = any>(
109109
options?: UseSurrealRpcOptions<T>,
110-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
110+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
111111
const { database, key, token, ...opts } = options || {}
112112

113113
const _key = key ?? 'Sur_' + hash(['surreal', 'info'])
@@ -137,7 +137,7 @@ export function useSurrealDB(overrides?: Overrides) {
137137
thing: MROGParam<T, 'insert', 0>,
138138
data?: MROGParam<T, 'insert', 1>,
139139
options?: UseSurrealRpcOptions<T>,
140-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
140+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
141141
const { database, immediate, key, token, watch, ...opts } = options || {}
142142

143143
const params = computed<RpcRequest<T, 'insert'>['params']>(() => ([toValue(thing), toValue(data)]))
@@ -198,7 +198,7 @@ export function useSurrealDB(overrides?: Overrides) {
198198
thing: MROGParam<T, 'merge', 0>,
199199
data: MROGParam<T, 'merge', 1>,
200200
options?: UseSurrealRpcOptions<T>,
201-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
201+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
202202
const { database, immediate, key, token, watch, ...opts } = options || {}
203203

204204
const params = computed<RpcRequest<T, 'merge'>['params']>(() => ([toValue(thing), toValue(data)]))
@@ -233,7 +233,7 @@ export function useSurrealDB(overrides?: Overrides) {
233233
patches: MROGParam<T, 'patch', 1>,
234234
diff?: MROGParam<T, 'patch', 2>,
235235
options?: UseSurrealRpcOptions<T>,
236-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
236+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
237237
const { database, immediate, key, token, watch, ...opts } = options || {}
238238

239239
const params = computed<RpcRequest<T, 'patch'>['params']>(() => ([toValue(thing), toValue(patches), toValue(diff)]))
@@ -262,17 +262,17 @@ export function useSurrealDB(overrides?: Overrides) {
262262
token: options?.token || overrides?.token,
263263
})
264264
}
265-
async function query<T = any, R = QueryRpcResponse<T>>(
265+
async function query<T = any[]>(
266266
sql: MROGParam<any, 'query', 0>,
267267
vars?: MROGParam<any, 'query', 1>,
268-
options?: UseSurrealRpcOptions<R>,
269-
): Promise<AsyncData<PickFrom<R, KeysOf<R>> | null, FetchError<any> | RpcResponseError | null>> {
268+
options?: UseSurrealRpcOptions<T>,
269+
) {
270270
const { database, key, token, watch, ...opts } = options || {}
271271

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

275-
return useSurrealRPC<R>({ method: 'query', params }, {
275+
return useSurrealRPC<T>({ method: 'query', params }, {
276276
...opts,
277277
database: database || overrides?.database,
278278
token: token || overrides?.token,
@@ -296,7 +296,7 @@ export function useSurrealDB(overrides?: Overrides) {
296296
async function remove<T = any>(
297297
thing: MROGParam<any, 'delete', 0>,
298298
options?: UseSurrealRpcOptions<T>,
299-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
299+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
300300
const { database, key, immediate, token, watch, ...opts } = options || {}
301301

302302
const params = computed<RpcRequest<any, 'delete'>['params']>(() => ([toValue(thing)]))
@@ -327,7 +327,7 @@ export function useSurrealDB(overrides?: Overrides) {
327327
async function select<T = any>(
328328
thing: MROGParam<T, 'select', 0>,
329329
options?: UseSurrealRpcOptions<T>,
330-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
330+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
331331
const { database, key, token, watch, ...opts } = options || {}
332332

333333
const params = computed<RpcRequest<T, 'select'>['params']>(() => ([toValue(thing)]))
@@ -367,7 +367,7 @@ export function useSurrealDB(overrides?: Overrides) {
367367
async function signin(
368368
auth: MROGParam<any, 'signin', 0>,
369369
options?: UseSurrealRpcOptions<string>,
370-
): Promise<AsyncData<string | null, RpcResponseError | FetchError<any> | null>> {
370+
): Promise<AsyncData<string | undefined, RpcResponseError | FetchError<any> | null>> {
371371
const { NS, DB, SC } = toValue(auth)
372372
if (!SC && !toValue(auth).user && !toValue(auth).pass) throw createError({ statusCode: 400, message: 'Wrong admin credentials' })
373373
const { database, immediate, key, token, watch, ...opts } = options || {}
@@ -420,7 +420,7 @@ export function useSurrealDB(overrides?: Overrides) {
420420
options?: Omit<UseSurrealRpcOptions<string>, 'database'> & {
421421
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string }
422422
},
423-
): Promise<AsyncData<string | null, RpcResponseError | FetchError<any> | null>> {
423+
): Promise<AsyncData<string | undefined, RpcResponseError | FetchError<any> | null>> {
424424
const { NS, DB, SC } = toValue(auth)
425425
if (!NS) throw createError({ statusCode: 400, message: 'Missing NS param' })
426426
if (!DB) throw createError({ statusCode: 400, message: 'Missing DB param' })
@@ -463,7 +463,7 @@ export function useSurrealDB(overrides?: Overrides) {
463463
thing: MROGParam<T, 'update', 0>,
464464
data?: MROGParam<T, 'update', 1>,
465465
options?: UseSurrealRpcOptions<T>,
466-
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
466+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | undefined, FetchError<any> | RpcResponseError | null>> {
467467
const { database, immediate, key, token, watch, ...opts } = options || {}
468468

469469
const params = computed<RpcRequest<T, 'update'>['params']>(() => ([toValue(thing), toValue(data)]))
@@ -487,7 +487,7 @@ export function useSurrealDB(overrides?: Overrides) {
487487
}),
488488
})
489489
}
490-
async function version(options?: Overrides): Promise<AsyncData<string | null, FetchError<any> | null>> {
490+
async function version(options?: Overrides): Promise<AsyncData<string | undefined, FetchError<any> | null>> {
491491
return useSurrealFetch<string>('version', {
492492
...$surrealFetchOptionsOverride({
493493
database: options?.database || overrides?.database,

Diff for: src/runtime/composables/surreal-fetch.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
UseSurrealRpcOptions,
1010
RpcRequest,
1111
RpcResponseError,
12+
HttpResponseError,
1213
} from '../types'
1314
import type {
1415
ComputedRef,
@@ -22,10 +23,16 @@ import {
2223
useNuxtApp,
2324
} from '#imports'
2425

25-
export function useSurrealFetch<DataT = any, ErrorT = any>(
26+
export function useSurrealFetch<
27+
ResT,
28+
ErrorT = HttpResponseError,
29+
DataT = ResT,
30+
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
31+
DefaultT = undefined,
32+
>(
2633
endpoint: MaybeRefOrGetter<string>,
27-
options: UseSurrealFetchOptions<DataT> = {},
28-
): AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, ErrorT | FetchError<any> | null> {
34+
options: UseSurrealFetchOptions<ResT, DataT, PickKeys, DefaultT> = {},
35+
): AsyncData<DefaultT | PickFrom<DataT, PickKeys>, FetchError<ErrorT> | null> {
2936
const {
3037
database,
3138
token,
@@ -46,19 +53,25 @@ export function useSurrealFetch<DataT = any, ErrorT = any>(
4653
})
4754
}
4855

49-
export function useSurrealRPC<DataT = any>(
56+
export function useSurrealRPC<
57+
ResT,
58+
ErrorT = RpcResponseError,
59+
DataT = ResT,
60+
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
61+
DefaultT = undefined,
62+
>(
5063
req: {
5164
method: MaybeRefOrGetter<RpcRequest<DataT>['method']>
5265
params?: MaybeRefOrGetter<RpcRequest<DataT>['params']> | ComputedRef<RpcRequest<DataT>['params']>
5366
},
54-
options?: UseSurrealRpcOptions<DataT>,
55-
): AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, FetchError<any> | RpcResponseError | null> {
67+
options?: UseSurrealRpcOptions<ResT, DataT, PickKeys, DefaultT>,
68+
) {
5669
const id = ref(0)
5770
const { key, ...opts } = options || {}
5871

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

61-
return useSurrealFetch<DataT, RpcResponseError>('rpc', {
74+
return useSurrealFetch<ResT, ErrorT, DataT, PickKeys, DefaultT>('rpc', {
6275
...opts,
6376
onResponse({ response }) {
6477
if (response.status === 200 && response._data.error) {

Diff for: src/runtime/types/index.d.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,18 @@ export type SurrealFetchOptions<
3737
> = Omit<FetchOptions<T>, 'method'> & {
3838
method?: Uppercase<SurrealMethods> | SurrealMethods
3939
}
40-
export type UseSurrealFetchOptions<T, R = T> = UseFetchOptions<T, R> & Overrides
41-
export type UseSurrealRpcOptions<T, R = T> = Omit<UseSurrealFetchOptions<T, R>, 'method' | 'body' | 'onResponse'>
40+
export type UseSurrealFetchOptions<
41+
ResT,
42+
DataT = ResT,
43+
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
44+
DefaultT = undefined,
45+
> = UseFetchOptions<ResT, DataT, PickKeys, DefaultT> & Overrides
46+
export type UseSurrealRpcOptions<
47+
ResT,
48+
DataT = ResT,
49+
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
50+
DefaultT = undefined,
51+
> = Omit<UseSurrealFetchOptions<ResT, DataT, PickKeys, DefaultT>, 'method' | 'body' | 'onResponse'>
4252

4353
/* Utils */
4454

0 commit comments

Comments
 (0)