Skip to content

Commit

Permalink
fix: auth for SurrealDB 2.x-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Sep 13, 2024
1 parent e8f9d1b commit e0701be
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default defineNuxtConfig({
modules: ['../src/module'],
surrealdb: {
auth: {
database: 'default',
database: 'staging',
},
databases: {
staging: {},
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export function useSurrealFetch<
} = options
const { $surrealFetch } = useNuxtApp()
const _database = useSurrealPreset({ database, token })
const { baseURL, headers } = surrealFetchOptionsOverride(_database, opts.headers)
const {
baseURL,
headers,
} = surrealFetchOptionsOverride(_database, {
baseURL: opts.baseURL,
headers: opts.headers,
})

return useFetch(endpoint, {
...opts,
Expand Down
11 changes: 2 additions & 9 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ import {
useSurrealPreset,
} from '#imports'

// Extending the ofetch module to include the `overrides` option
declare module 'ofetch' {
interface FetchOptions {
overrides?: Overrides
}
}

export default defineNuxtPlugin(async ({ $config }) => {
const authDatabase = $config.public.surrealdb.auth.database as DatabasePresetKeys | false
const { token: userToken, session } = useSurrealAuth()
const database = useSurrealPreset()

const surrealFetch = ofetch.create({
onRequest({ options }) {
const database = useSurrealPreset(options.overrides)
const { baseURL, headers } = surrealFetchOptionsOverride(database)
const { baseURL, headers } = surrealFetchOptionsOverride(database, options)
options.baseURL = baseURL
options.headers = defu<HeadersInit, HeadersInit[]>(options.headers, { ...headers })
},
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/server/utils/surreal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export function useSurrealFetch<
): Promise<T> {
const { database, token, ...opts } = options
const _database = useSurrealPreset(event, { database, token })
const { baseURL, headers } = surrealFetchOptionsOverride(_database)
const {
baseURL,
headers,
} = surrealFetchOptionsOverride(_database, {
baseURL: opts.baseURL,
headers: opts.headers,
})

const surrealFetch = ofetch.create({
baseURL,
Expand Down
15 changes: 10 additions & 5 deletions src/runtime/utils/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ export function getDatabasePreset(options: GetDatabasePresetOptions | GetDatabas
}

export function surrealFetchOptionsOverride<
B = string,
T = HeadersInit,
>(
databasePreset: DatabasePreset,
headers?: T,
options?: {
baseURL?: B
headers?: T
},
): {
baseURL: string
baseURL: B
headers: T
} {
const authorization = authTokenFn(databasePreset.auth)
Expand All @@ -100,15 +104,16 @@ export function surrealFetchOptionsOverride<
'surreal-DB': databasePreset.DB,
'Authorization': authorization,
},
// @ts-expect-error using a generic
headers,
{
...options?.headers,
},
{
'Content-Type': 'application/json',
'Accept': 'application/json',
},
)
return {
baseURL: databasePreset.host,
baseURL: options?.baseURL || databasePreset.host as B,
headers: _headers as T,
}
}

0 comments on commit e0701be

Please sign in to comment.