Skip to content

Commit

Permalink
fix: return types closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 28, 2024
1 parent 8917ca8 commit cd7446f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
70 changes: 35 additions & 35 deletions src/runtime/composables/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function useSurrealDB(overrides?: Overrides) {
async function $authenticate(
token: MROGParam<any, 'authenticate', 0>,
options?: Overrides,
) {
): Promise<void> {
return $surrealRPC<any>({
method: 'authenticate', params: [toValue(token)],
}, {
Expand All @@ -44,7 +44,7 @@ export function useSurrealDB(overrides?: Overrides) {
async function authenticate(
token: MROGParam<any, 'authenticate', 0>,
options?: UseSurrealRpcOptions<any>,
): Promise<AsyncData<any | null, RpcResponseError | FetchError<any> | null>> {
): Promise<AsyncData<void, RpcResponseError | FetchError<any> | null>> {
const { database, key, immediate, token: tokenOvr, watch, ...opts } = options || {}

const params = computed<RpcRequest<any, 'authenticate'>['params']>(() => ([toValue(token)]))
Expand All @@ -65,7 +65,7 @@ export function useSurrealDB(overrides?: Overrides) {
thing: MROGParam<T, 'create', 0>,
data?: MROGParam<T, 'create', 1>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'create', params: [toValue(thing), toValue(data)],
}, {
Expand Down Expand Up @@ -96,7 +96,7 @@ export function useSurrealDB(overrides?: Overrides) {
// info
async function $info<T = any>(
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'info',
}, {
Expand Down Expand Up @@ -124,7 +124,7 @@ export function useSurrealDB(overrides?: Overrides) {
thing: MROGParam<T, 'insert', 0>,
data?: MROGParam<T, 'insert', 1>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'insert', params: [toValue(thing), toValue(data)],
}, {
Expand Down Expand Up @@ -155,7 +155,7 @@ export function useSurrealDB(overrides?: Overrides) {
// invalidate
async function $invalidate(
options?: Overrides,
) {
): Promise<void> {
return $surrealRPC<any>({
method: 'invalidate',
}, {
Expand All @@ -165,7 +165,7 @@ export function useSurrealDB(overrides?: Overrides) {
}
async function invalidate(
options?: UseSurrealRpcOptions<any>,
): Promise<AsyncData<any, FetchError<any> | RpcResponseError | null>> {
): Promise<AsyncData<void, FetchError<any> | RpcResponseError | null>> {
const { database, immediate, key, token, watch, ...opts } = options || {}

const _key = key ?? 'Sur_' + hash(['surreal', 'invalidate'])
Expand All @@ -185,7 +185,7 @@ export function useSurrealDB(overrides?: Overrides) {
thing: MROGParam<T, 'merge', 0>,
data: MROGParam<T, 'merge', 1>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'merge', params: [toValue(thing), toValue(data)],
}, {
Expand Down Expand Up @@ -219,7 +219,7 @@ export function useSurrealDB(overrides?: Overrides) {
patches: MROGParam<T, 'patch', 1>,
diff?: MROGParam<T, 'patch', 2>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'patch', params: [toValue(thing), toValue(patches), toValue(diff)],
}, {
Expand Down Expand Up @@ -253,7 +253,7 @@ export function useSurrealDB(overrides?: Overrides) {
sql: MROGParam<T, 'query', 0>,
vars?: MROGParam<T, 'query', 1>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'query', params: [toValue(sql), toValue(vars)],
}, {
Expand Down Expand Up @@ -281,27 +281,27 @@ export function useSurrealDB(overrides?: Overrides) {
}

// remove [ thing ] (`delete` is a reserved word in JS)
async function $remove(
async function $remove<T = any>(
thing: MROGParam<any, 'delete', 0>,
options?: Overrides,
) {
return $surrealRPC<any>({
): Promise<T> {
return $surrealRPC<T>({
method: 'delete', params: [toValue(thing)],
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token,
})
}
async function remove(
async function remove<T = any>(
thing: MROGParam<any, 'delete', 0>,
options?: UseSurrealRpcOptions<any>,
): Promise<AsyncData<any, FetchError<any> | RpcResponseError | null>> {
options?: UseSurrealRpcOptions<T>,
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
const { database, key, immediate, token, watch, ...opts } = options || {}

const params = computed<RpcRequest<any, 'delete'>['params']>(() => ([toValue(thing)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'delete', toValue(params)])

return useSurrealRPC<any>({ method: 'delete', params }, {
return useSurrealRPC<T>({ method: 'delete', params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
Expand All @@ -315,7 +315,7 @@ export function useSurrealDB(overrides?: Overrides) {
async function $select<T = any>(
thing: MROGParam<T, 'select', 0>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'select', params: [toValue(thing)],
}, {
Expand Down Expand Up @@ -347,11 +347,11 @@ export function useSurrealDB(overrides?: Overrides) {
options?: {
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string }
},
) {
): Promise<string> {
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<string | null>({
return $surrealRPC<string>({
method: 'signin', params: [toValue(auth)],
}, {
database: {
Expand All @@ -365,8 +365,8 @@ export function useSurrealDB(overrides?: Overrides) {
}
async function signin(
auth: MROGParam<any, 'signin', 0>,
options?: UseSurrealRpcOptions<string | null>,
): Promise<AsyncData<PickFrom<string | null, KeysOf<string | null>> | null, FetchError<any> | RpcResponseError | null>> {
options?: UseSurrealRpcOptions<string>,
): Promise<AsyncData<string | null, RpcResponseError | FetchError<any> | 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 || {}
Expand All @@ -375,7 +375,7 @@ export function useSurrealDB(overrides?: Overrides) {
const params = computed<RpcRequest<any, 'signin'>['params']>(() => ([toValue(auth)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'signin', toValue(params)])

return useSurrealRPC<string | null>({ method: 'signin', params }, {
return useSurrealRPC<string>({ method: 'signin', params }, {
...opts,
database: {
host: baseURL,
Expand All @@ -391,18 +391,18 @@ export function useSurrealDB(overrides?: Overrides) {
}

// signup [ NS, DB, SC, ... ]
async function $signup<T = any>(
async function $signup(
auth: MROGParam<any, 'signup', 0>,
options?: {
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string }
},
) {
): Promise<string> {
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<T>({
return $surrealRPC<string>({
method: 'signup', params: [toValue(auth)],
}, {
database: {
Expand All @@ -414,12 +414,12 @@ export function useSurrealDB(overrides?: Overrides) {
},
})
}
async function signup<T = any>(
async function signup(
auth: MROGParam<any, 'signup', 0>,
options?: Omit<UseSurrealRpcOptions<T>, 'database'> & {
options?: Omit<UseSurrealRpcOptions<string>, 'database'> & {
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | { host?: string }
},
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | RpcResponseError | null>> {
): Promise<AsyncData<string | null, RpcResponseError | FetchError<any> | 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' })
Expand All @@ -430,7 +430,7 @@ export function useSurrealDB(overrides?: Overrides) {
const params = computed<RpcRequest<any, 'signup'>['params']>(() => ([toValue(auth)]))
const _key = key ?? 'Sur_' + hash(['surreal', 'signup', toValue(params)])

return useSurrealRPC<T>({ method: 'signup', params }, {
return useSurrealRPC<string>({ method: 'signup', params }, {
...opts,
database: {
host: baseURL,
Expand All @@ -450,7 +450,7 @@ export function useSurrealDB(overrides?: Overrides) {
thing: MROGParam<T, 'update', 0>,
data?: MROGParam<T, 'update', 1>,
options?: Overrides,
) {
): Promise<T> {
return $surrealRPC<T>({
method: 'update', params: [toValue(thing), toValue(data)],
}, {
Expand Down Expand Up @@ -478,16 +478,16 @@ export function useSurrealDB(overrides?: Overrides) {
})
}

async function $version(options?: Overrides) {
return $surrealFetch('version', {
async function $version(options?: Overrides): Promise<string> {
return $surrealFetch<string>('version', {
...$surrealFetchOptionsOverride({
database: options?.database || overrides?.database,
token: options?.token || overrides?.token,
}),
})
}
async function version(options?: Overrides): Promise<AsyncData<any, FetchError<any> | null>> {
return useSurrealFetch('version', {
async function version(options?: Overrides): Promise<AsyncData<string | null, FetchError<any> | null>> {
return useSurrealFetch<string>('version', {
...$surrealFetchOptionsOverride({
database: options?.database || overrides?.database,
token: options?.token || overrides?.token,
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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 || {}
Expand Down Expand Up @@ -125,10 +126,10 @@ export default defineNuxtPlugin(async ({ $config }) => {
}
}

function surrealRPC<T = any>(req: RpcRequest<T>, ovr?: Overrides) {
function surrealRPC<T = any>(req: RpcRequest<T>, ovr?: Overrides): Promise<T> {
let id = 0

return surrealFetch<T, string, SurrealFetchOptions>('rpc', {
return surrealFetch<T>('rpc', {
...surrealFetchOptionsOverride(ovr),
onResponse({ response }) {
if (response.status === 200 && response._data.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/utils/surreal-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function useSurrealDB(event: H3Event, overrides?: ServerOverrides) {
sql: RpcParams<T, 'query'>[0],
vars?: RpcParams<T, 'query'>[1],
options?: ServerOverrides,
) {
): Promise<T> {
return useSurrealRPC<T>(event, {
method: 'query', params: [sql, vars],
}, {
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/server/utils/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof useSurrealDatabases>
Expand Down Expand Up @@ -55,13 +56,13 @@ export function useSurrealFetch<
event: H3Event,
req: R,
options: SurrealFetchOptions,
) {
): Promise<T> {
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 || {}
Expand Down Expand Up @@ -169,7 +170,7 @@ export function useSurrealFetchOptionsOverride<
export function useSurrealRPC<T = any>(event: H3Event, req: RpcRequest<T>, ovr?: ServerOverrides) {
let id = 0

return useSurrealFetch<RpcResponse<T>>(event, 'rpc', {
return useSurrealFetch<T>(event, 'rpc', {
...useSurrealFetchOptionsOverride(event, ovr),
onResponse({ response }) {
if (response.status === 200 && response._data.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type SurrealAsyncDataOptions<T> = AsyncDataOptions<T> & Overrides & {
key?: string
}
export type SurrealFetchOptions<
T extends ResponseType = ResponseType,
T extends ResponseType = 'json',
> = Omit<FetchOptions<T>, 'method'> & {
method?: Uppercase<SurrealMethods> | SurrealMethods
}
Expand Down

0 comments on commit cd7446f

Please sign in to comment.