Skip to content

Commit

Permalink
fix: server-side database preset overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jul 10, 2024
1 parent 59eb72d commit a0cff31
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
5 changes: 4 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export default defineNuxtConfig({
server: {
databases: {
staging: {
auth: '',
auth: {
user: '',
pass: '',
},
},
},
},
Expand Down
31 changes: 28 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { DatabasePreset } from './runtime/types'
export type * from './runtime/types'

type PublicDatabases = PublicRuntimeConfig['surrealdb']['databases']
type PrivateDatabases = RuntimeConfig['surrealdb']['databases']

// Module options TypeScript interface definition
export interface ModuleOptions {
Expand All @@ -23,7 +24,7 @@ export interface ModuleOptions {
[key: string]: DatabasePreset | undefined
}
server?: {
defaultDatabase?: keyof PublicDatabases | keyof RuntimeConfig['surrealdb']['databases']
defaultDatabase?: keyof PublicDatabases | keyof PrivateDatabases
databases?: {
[key: string]: DatabasePreset | undefined
}
Expand Down Expand Up @@ -54,7 +55,7 @@ export default defineNuxtModule<ModuleOptions>({
NS: '',
DB: '',
SC: '',
auth: '',
KV: '',
},
},
server: {
Expand All @@ -72,7 +73,7 @@ export default defineNuxtModule<ModuleOptions>({

// Public RuntimeConfig
nuxt.options.runtimeConfig.public.surrealdb = defu<
PublicRuntimeConfig['surrealdb'],
Omit<ModuleOptions, 'server'>,
Omit<ModuleOptions, 'server'>[]
>(
nuxt.options.runtimeConfig.public.surrealdb,
Expand All @@ -99,3 +100,27 @@ export default defineNuxtModule<ModuleOptions>({
addServerImportsDir(resolve(runtimeDir, 'server', 'utils'))
},
})

declare module '@nuxt/schema' {
interface NuxtOptions {
surrealdb?: ModuleOptions
runtimeConfig: {
surrealdb: ModuleOptions['server']
public: {
surrealdb: Omit<ModuleOptions, 'server'>
}
}
}
}

declare module 'nuxt/schema' {
interface NuxtOptions {
surrealdb?: ModuleOptions
runtimeConfig: {
surrealdb: ModuleOptions['server']
public: {
surrealdb: Omit<ModuleOptions, 'server'>
}
}
}
}
30 changes: 16 additions & 14 deletions src/runtime/server/utils/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { ofetch } from 'ofetch'
import { getCookie } from 'h3'
import { defu } from 'defu'

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

export type SurrealDatabasesKeys = keyof ReturnType<typeof useSurrealDatabases>

export type ServerOverrides = Omit<Overrides, 'database'> & {
database?: SurrealDatabasesKeys | DatabasePreset
}

function authTokenFn(dbAuth: DatabasePreset['auth']) {
if (!dbAuth) return undefined
if (typeof dbAuth === 'string') {
Expand All @@ -27,7 +27,9 @@ function authTokenFn(dbAuth: DatabasePreset['auth']) {
}
}

export function useSurrealDatabases(event?: H3Event) {
export function useSurrealDatabases(event?: H3Event): {
[key in DatabasePresetServerKeys]: DatabasePreset
} {
const {
surrealdb: {
databases: privateDatabases,
Expand All @@ -40,9 +42,9 @@ export function useSurrealDatabases(event?: H3Event) {
} = useRuntimeConfig(event)

const databases = defu<
PublicRuntimeConfig['surrealdb']['databases'],
RuntimeConfig['surrealdb']['databases'][]
>(publicDatabases, privateDatabases)
RuntimeConfig['surrealdb']['databases'],
PublicRuntimeConfig['surrealdb']['databases'][]
>(privateDatabases, publicDatabases)

return {
...databases,
Expand All @@ -58,7 +60,7 @@ export function useSurrealFetch<
options: SurrealFetchOptions,
): Promise<T> {
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event)
const defaultDB = useSurrealDatabases(event)[defaultDatabase as SurrealDatabasesKeys]
const defaultDB = useSurrealDatabases(event)[defaultDatabase as DatabasePresetServerKeys]
const authToken = authTokenFn(defaultDB.auth)
const userAuth = getCookie(event, cookieName)

Expand Down Expand Up @@ -111,11 +113,11 @@ export function useSurrealFetchOptionsOverride<
} = overrides
const { surrealdb: { defaultDatabase }, public: { surrealdb: { auth: { cookieName } } } } = useRuntimeConfig(event)
const databases = useSurrealDatabases(event)
const authToken = authTokenFn(databases[defaultDatabase as SurrealDatabasesKeys].auth)
const authToken = authTokenFn(databases[defaultDatabase as DatabasePresetServerKeys].auth)
const userAuth = getCookie(event, cookieName)

const headers = defaults?.headers as Record<string, string> || {}
let db: DatabasePreset | undefined = undefined
let db: DatabasePreset = {}
let baseURL: string | undefined = undefined
let dbAuth = authToken

Expand Down
14 changes: 12 additions & 2 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import type { PublicRuntimeConfig } from 'nuxt/schema'
import type { PublicRuntimeConfig, RuntimeConfig } from 'nuxt/schema'
import type { AsyncDataOptions, UseFetchOptions } from 'nuxt/app'
import type { FetchOptions, ResponseType } from 'ofetch'

export type * from './auth'

/* Database Overrides */

export type DatabasePresetKeys = keyof PublicRuntimeConfig['surrealdb']['databases']
export type DatabasePresetServerKeys =
| keyof PublicRuntimeConfig['surrealdb']['databases']
| keyof RuntimeConfig['surrealdb']['databases']

export type AuthToken = string | {
user: string
pass: string
}

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

export interface ServerOverrides {
database?: DatabasePresetServerKeys | DatabasePreset
token?: AuthToken | boolean
}

Expand Down

0 comments on commit a0cff31

Please sign in to comment.