Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Sep 11, 2024
1 parent 6834102 commit a31960c
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 310 deletions.
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ export default createConfigForNuxt({
max: 3,
},
}],

// Project Specific
'@typescript-eslint/no-empty-object-type': 'off',
},
})
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"changelogen": "^0.5.5",
"eslint": "^9.10.0",
"nuxt": "^3.13.1",
"typescript": "5.5",
"vite": "^5.4.4",
"vitepress": "^1.3.4",
"vitest": "^2.0.5",
Expand Down
535 changes: 253 additions & 282 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PublicRuntimeConfig, RuntimeConfig } from 'nuxt/schema'
import type { PublicRuntimeConfig, RuntimeConfig } from '@nuxt/schema'
import { defineNuxtModule, addPlugin, addImportsDir, addServerImportsDir, createResolver } from '@nuxt/kit'
import { defu } from 'defu'

Expand Down Expand Up @@ -57,6 +57,14 @@ export default defineNuxtModule<ModuleOptions>({
},
},
server: {
databases: {
default: {
host: '',
ws: '',
NS: '',
DB: '',
},
},
defaultDatabase: 'default',
},
},
Expand All @@ -71,7 +79,7 @@ export default defineNuxtModule<ModuleOptions>({

// Public RuntimeConfig
nuxt.options.runtimeConfig.public.surrealdb = defu<
Omit<ModuleOptions, 'server'>,
PublicRuntimeConfig['surrealdb'],
Omit<ModuleOptions, 'server'>[]
>(
nuxt.options.runtimeConfig.public.surrealdb,
Expand Down Expand Up @@ -99,13 +107,13 @@ export default defineNuxtModule<ModuleOptions>({
},
})

interface SurrealServerOptions {
surrealdb?: ModuleOptions['server']
}
interface SurrealOptions {
surrealdb?: Omit<ModuleOptions, 'server'>
}
declare module '@nuxt/schema' {
interface NuxtOptions {
runtimeConfig: {
surrealdb: ModuleOptions['server']
public: {
surrealdb: Omit<ModuleOptions, 'server'>
}
}
}
interface RuntimeConfig extends SurrealServerOptions {}
interface PublicRuntimeConfig extends SurrealOptions {}
}
27 changes: 11 additions & 16 deletions src/runtime/server/utils/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// The following nitropack import is from https://github.com/nuxt/module-builder/issues/141#issuecomment-2078248248
import type {} from 'nitropack'
import type { PublicRuntimeConfig, RuntimeConfig } from '@nuxt/schema'
import type { FetchOptions, ResponseType } from 'ofetch'
import { textToBase64 } from 'undio'
import type { H3Event } from 'h3'
Expand All @@ -9,16 +10,13 @@ import { createDefu, defu } from 'defu'

import type {
DatabasePreset,
DatabasePresetKeys,
DatabasePresetServerKeys,
RpcRequest,
ServerOverrides,
SurrealFetchOptions,
} from '../../types/index'
import { createError, useRuntimeConfig } from '#imports'

type DPresets = Record<DatabasePresetKeys, DatabasePreset>
type DPresetsServer = Record<DatabasePresetServerKeys, DatabasePreset>
type DatabasePresetKeys = keyof PublicRuntimeConfig['surrealdb']['databases'] | keyof RuntimeConfig['surrealdb']['databases']

function authTokenFn(dbAuth: DatabasePreset['auth']) {
if (!dbAuth) return undefined
Expand All @@ -31,9 +29,8 @@ function authTokenFn(dbAuth: DatabasePreset['auth']) {
}

export function useSurrealDatabases(event?: H3Event): {
[key in DatabasePresetServerKeys]: DatabasePreset
[key in DatabasePresetKeys]: DatabasePreset
} {
// TODO: properly type this
const {
surrealdb: {
databases: privateDatabases,
Expand All @@ -45,9 +42,9 @@ export function useSurrealDatabases(event?: H3Event): {
defaultDatabase: defaultPublicDatabase,
},
},
} = useRuntimeConfig(event) as any
const defaultPrivateDB = privateDatabases[defaultPrivateDatabase] as DPresets
const defaultPublicDB = _publicDatabases[defaultPublicDatabase] as DPresetsServer
} = useRuntimeConfig(event)
const defaultPrivateDB = privateDatabases[defaultPrivateDatabase as keyof RuntimeConfig['surrealdb']['databases']]
const defaultPublicDB = _publicDatabases[defaultPublicDatabase as keyof PublicRuntimeConfig['surrealdb']['databases']]

const defuPublicDatabases = createDefu((obj, key, value) => {
obj[key] = defu(value, obj[key], defaultPublicDB)
Expand All @@ -61,7 +58,7 @@ export function useSurrealDatabases(event?: H3Event): {
return true
})

const databases = defuDatabases(privateDatabases, publicDatabases) as DPresetsServer
const databases = defuDatabases(privateDatabases, publicDatabases)

return databases
}
Expand All @@ -74,9 +71,8 @@ export function useSurrealFetch<
req: R,
options: SurrealFetchOptions & ServerOverrides,
): Promise<T> {
// TODO: properly type this
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event) as any
const defaultDB = useSurrealDatabases(event)[defaultDatabase as DatabasePresetServerKeys]
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event)
const defaultDB = useSurrealDatabases(event)[defaultDatabase as keyof RuntimeConfig['surrealdb']['databases']]
const authToken = authTokenFn(defaultDB.auth)
const userAuth = getCookie(event, cookieName)
const { database, token, ...opts } = options
Expand Down Expand Up @@ -129,14 +125,13 @@ export function useSurrealFetchOptionsOverride<
overrides: ServerOverrides = {},
defaults?: Pick<FetchOptions<R>, 'headers'>,
) {
// TODO: properly type this
const {
database,
token,
} = overrides
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event) as any
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event)
const databases = useSurrealDatabases(event)
const authToken = authTokenFn(databases[defaultDatabase as DatabasePresetServerKeys].auth)
const authToken = authTokenFn(databases[defaultDatabase as keyof RuntimeConfig['surrealdb']['databases']].auth)
const userAuth = getCookie(event, cookieName)

const headers = defaults?.headers as Record<string, string> || {}
Expand Down
1 change: 0 additions & 1 deletion src/runtime/types/auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ComputedRef, Ref } from 'vue'

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface User {}

export interface UserSession {
Expand Down

0 comments on commit a31960c

Please sign in to comment.