From cd7446f76d5a5a088a1cb84adb73b495b345e81b Mon Sep 17 00:00:00 2001 From: Sandros94 Date: Fri, 28 Jun 2024 20:09:37 +0200 Subject: [PATCH] fix: return types closes #7 --- src/runtime/composables/surreal-db.ts | 70 +++++++++++------------ src/runtime/plugin.ts | 9 +-- src/runtime/server/utils/surreal-db.ts | 2 +- src/runtime/server/utils/surreal-fetch.ts | 9 +-- src/runtime/types/index.d.ts | 2 +- 5 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/runtime/composables/surreal-db.ts b/src/runtime/composables/surreal-db.ts index b495a41..2f73750 100644 --- a/src/runtime/composables/surreal-db.ts +++ b/src/runtime/composables/surreal-db.ts @@ -33,7 +33,7 @@ export function useSurrealDB(overrides?: Overrides) { async function $authenticate( token: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'authenticate', params: [toValue(token)], }, { @@ -44,7 +44,7 @@ export function useSurrealDB(overrides?: Overrides) { async function authenticate( token: MROGParam, options?: UseSurrealRpcOptions, - ): Promise | null>> { + ): Promise | null>> { const { database, key, immediate, token: tokenOvr, watch, ...opts } = options || {} const params = computed['params']>(() => ([toValue(token)])) @@ -65,7 +65,7 @@ export function useSurrealDB(overrides?: Overrides) { thing: MROGParam, data?: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'create', params: [toValue(thing), toValue(data)], }, { @@ -96,7 +96,7 @@ export function useSurrealDB(overrides?: Overrides) { // info async function $info( options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'info', }, { @@ -124,7 +124,7 @@ export function useSurrealDB(overrides?: Overrides) { thing: MROGParam, data?: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'insert', params: [toValue(thing), toValue(data)], }, { @@ -155,7 +155,7 @@ export function useSurrealDB(overrides?: Overrides) { // invalidate async function $invalidate( options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'invalidate', }, { @@ -165,7 +165,7 @@ export function useSurrealDB(overrides?: Overrides) { } async function invalidate( options?: UseSurrealRpcOptions, - ): Promise | RpcResponseError | null>> { + ): Promise | RpcResponseError | null>> { const { database, immediate, key, token, watch, ...opts } = options || {} const _key = key ?? 'Sur_' + hash(['surreal', 'invalidate']) @@ -185,7 +185,7 @@ export function useSurrealDB(overrides?: Overrides) { thing: MROGParam, data: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'merge', params: [toValue(thing), toValue(data)], }, { @@ -219,7 +219,7 @@ export function useSurrealDB(overrides?: Overrides) { patches: MROGParam, diff?: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'patch', params: [toValue(thing), toValue(patches), toValue(diff)], }, { @@ -253,7 +253,7 @@ export function useSurrealDB(overrides?: Overrides) { sql: MROGParam, vars?: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'query', params: [toValue(sql), toValue(vars)], }, { @@ -281,27 +281,27 @@ export function useSurrealDB(overrides?: Overrides) { } // remove [ thing ] (`delete` is a reserved word in JS) - async function $remove( + async function $remove( thing: MROGParam, options?: Overrides, - ) { - return $surrealRPC({ + ): Promise { + return $surrealRPC({ method: 'delete', params: [toValue(thing)], }, { database: options?.database || overrides?.database, token: options?.token || overrides?.token, }) } - async function remove( + async function remove( thing: MROGParam, - options?: UseSurrealRpcOptions, - ): Promise | RpcResponseError | null>> { + options?: UseSurrealRpcOptions, + ): Promise> | null, FetchError | RpcResponseError | null>> { const { database, key, immediate, token, watch, ...opts } = options || {} const params = computed['params']>(() => ([toValue(thing)])) const _key = key ?? 'Sur_' + hash(['surreal', 'delete', toValue(params)]) - return useSurrealRPC({ method: 'delete', params }, { + return useSurrealRPC({ method: 'delete', params }, { ...opts, database: database || overrides?.database, token: token || overrides?.token, @@ -315,7 +315,7 @@ export function useSurrealDB(overrides?: Overrides) { async function $select( thing: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'select', params: [toValue(thing)], }, { @@ -347,11 +347,11 @@ export function useSurrealDB(overrides?: Overrides) { options?: { database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string } }, - ) { + ): Promise { const { NS, DB, SC } = toValue(auth) if (!SC && !toValue(auth).user && !toValue(auth).pass) throw createError({ statusCode: 400, message: 'Wrong admin credentials' }) const { baseURL } = $surrealFetchOptionsOverride(options || overrides) - return $surrealRPC({ + return $surrealRPC({ method: 'signin', params: [toValue(auth)], }, { database: { @@ -365,8 +365,8 @@ export function useSurrealDB(overrides?: Overrides) { } async function signin( auth: MROGParam, - options?: UseSurrealRpcOptions, - ): Promise> | null, FetchError | RpcResponseError | null>> { + options?: UseSurrealRpcOptions, + ): Promise | null>> { const { NS, DB, SC } = toValue(auth) if (!SC && !toValue(auth).user && !toValue(auth).pass) throw createError({ statusCode: 400, message: 'Wrong admin credentials' }) const { database, immediate, key, token, watch, ...opts } = options || {} @@ -375,7 +375,7 @@ export function useSurrealDB(overrides?: Overrides) { const params = computed['params']>(() => ([toValue(auth)])) const _key = key ?? 'Sur_' + hash(['surreal', 'signin', toValue(params)]) - return useSurrealRPC({ method: 'signin', params }, { + return useSurrealRPC({ method: 'signin', params }, { ...opts, database: { host: baseURL, @@ -391,18 +391,18 @@ export function useSurrealDB(overrides?: Overrides) { } // signup [ NS, DB, SC, ... ] - async function $signup( + async function $signup( auth: MROGParam, options?: { database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string } }, - ) { + ): Promise { const { NS, DB, SC } = toValue(auth) if (!NS) throw createError({ statusCode: 400, message: 'Missing NS param' }) if (!DB) throw createError({ statusCode: 400, message: 'Missing DB param' }) if (!SC) throw createError({ statusCode: 400, message: 'Missing SC param' }) const { baseURL } = $surrealFetchOptionsOverride(options || overrides) - return $surrealRPC({ + return $surrealRPC({ method: 'signup', params: [toValue(auth)], }, { database: { @@ -414,12 +414,12 @@ export function useSurrealDB(overrides?: Overrides) { }, }) } - async function signup( + async function signup( auth: MROGParam, - options?: Omit, 'database'> & { + options?: Omit, 'database'> & { database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string } }, - ): Promise> | null, FetchError | RpcResponseError | null>> { + ): Promise | null>> { const { NS, DB, SC } = toValue(auth) if (!NS) throw createError({ statusCode: 400, message: 'Missing NS param' }) if (!DB) throw createError({ statusCode: 400, message: 'Missing DB param' }) @@ -430,7 +430,7 @@ export function useSurrealDB(overrides?: Overrides) { const params = computed['params']>(() => ([toValue(auth)])) const _key = key ?? 'Sur_' + hash(['surreal', 'signup', toValue(params)]) - return useSurrealRPC({ method: 'signup', params }, { + return useSurrealRPC({ method: 'signup', params }, { ...opts, database: { host: baseURL, @@ -450,7 +450,7 @@ export function useSurrealDB(overrides?: Overrides) { thing: MROGParam, data?: MROGParam, options?: Overrides, - ) { + ): Promise { return $surrealRPC({ method: 'update', params: [toValue(thing), toValue(data)], }, { @@ -478,16 +478,16 @@ export function useSurrealDB(overrides?: Overrides) { }) } - async function $version(options?: Overrides) { - return $surrealFetch('version', { + async function $version(options?: Overrides): Promise { + return $surrealFetch('version', { ...$surrealFetchOptionsOverride({ database: options?.database || overrides?.database, token: options?.token || overrides?.token, }), }) } - async function version(options?: Overrides): Promise | null>> { - return useSurrealFetch('version', { + async function version(options?: Overrides): Promise | null>> { + return useSurrealFetch('version', { ...$surrealFetchOptionsOverride({ database: options?.database || overrides?.database, token: options?.token || overrides?.token, diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index d01e5d5..2223396 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -2,9 +2,10 @@ import type {} from 'nitropack' import type { PublicRuntimeConfig } from 'nuxt/schema' import type { FetchOptions, ResponseType } from 'ofetch' +import { ofetch } from 'ofetch' import { textToBase64 } from 'undio' -import type { DatabasePreset, Overrides, RpcRequest, SurrealFetchOptions } from './types' +import type { DatabasePreset, Overrides, RpcRequest } from './types' import { createError, defineNuxtPlugin, useSurrealAuth } from '#imports' export default defineNuxtPlugin(async ({ $config }) => { @@ -28,7 +29,7 @@ export default defineNuxtPlugin(async ({ $config }) => { } } - const surrealFetch = $fetch.create({ + const surrealFetch = ofetch.create({ baseURL: defaultDB.host, onRequest({ options }) { options.headers = options.headers || {} @@ -125,10 +126,10 @@ export default defineNuxtPlugin(async ({ $config }) => { } } - function surrealRPC(req: RpcRequest, ovr?: Overrides) { + function surrealRPC(req: RpcRequest, ovr?: Overrides): Promise { let id = 0 - return surrealFetch('rpc', { + return surrealFetch('rpc', { ...surrealFetchOptionsOverride(ovr), onResponse({ response }) { if (response.status === 200 && response._data.error) { diff --git a/src/runtime/server/utils/surreal-db.ts b/src/runtime/server/utils/surreal-db.ts index 5d02be4..5f22138 100644 --- a/src/runtime/server/utils/surreal-db.ts +++ b/src/runtime/server/utils/surreal-db.ts @@ -9,7 +9,7 @@ export function useSurrealDB(event: H3Event, overrides?: ServerOverrides) { sql: RpcParams[0], vars?: RpcParams[1], options?: ServerOverrides, - ) { + ): Promise { return useSurrealRPC(event, { method: 'query', params: [sql, vars], }, { diff --git a/src/runtime/server/utils/surreal-fetch.ts b/src/runtime/server/utils/surreal-fetch.ts index 3bca3c8..5ecb276 100644 --- a/src/runtime/server/utils/surreal-fetch.ts +++ b/src/runtime/server/utils/surreal-fetch.ts @@ -4,10 +4,11 @@ import type { PublicRuntimeConfig, RuntimeConfig } from 'nuxt/schema' import type { FetchOptions, ResponseType } from 'ofetch' import { textToBase64 } from 'undio' import type { H3Event } from 'h3' +import { ofetch } from 'ofetch' import { getCookie } from 'h3' import { defu } from 'defu' -import type { DatabasePreset, Overrides, RpcRequest, RpcResponse, SurrealFetchOptions } from '../../types' +import type { DatabasePreset, Overrides, RpcRequest, SurrealFetchOptions } from '../../types' import { createError, useRuntimeConfig } from '#imports' export type SurrealDatabasesKeys = keyof ReturnType @@ -55,13 +56,13 @@ export function useSurrealFetch< event: H3Event, req: R, options: SurrealFetchOptions, -) { +): Promise { const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event) const defaultDB = useSurrealDatabases(event)[defaultDatabase as SurrealDatabasesKeys] const authToken = authTokenFn(defaultDB.auth) const userAuth = getCookie(event, cookieName) - const surrealFetch = $fetch.create({ + const surrealFetch = ofetch.create({ baseURL: defaultDB.host, onRequest({ options }) { options.headers = options.headers || {} @@ -169,7 +170,7 @@ export function useSurrealFetchOptionsOverride< export function useSurrealRPC(event: H3Event, req: RpcRequest, ovr?: ServerOverrides) { let id = 0 - return useSurrealFetch>(event, 'rpc', { + return useSurrealFetch(event, 'rpc', { ...useSurrealFetchOptionsOverride(event, ovr), onResponse({ response }) { if (response.status === 200 && response._data.error) { diff --git a/src/runtime/types/index.d.ts b/src/runtime/types/index.d.ts index 0de1596..07ffab5 100644 --- a/src/runtime/types/index.d.ts +++ b/src/runtime/types/index.d.ts @@ -33,7 +33,7 @@ export type SurrealAsyncDataOptions = AsyncDataOptions & Overrides & { key?: string } export type SurrealFetchOptions< - T extends ResponseType = ResponseType, + T extends ResponseType = 'json', > = Omit, 'method'> & { method?: Uppercase | SurrealMethods }