Skip to content

Commit

Permalink
feat(useSurrealFetch): optional overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 27, 2024
1 parent c4be853 commit d20e563
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
import type { UseFetchOptions } from 'nuxt/app'

import type { Overrides } from '../types'
import { useFetch, useNuxtApp } from '#app'

export function useSurrealFetch<T>(
url: string | (() => string),
options: UseFetchOptions<T> = {},
options: UseFetchOptions<T> & Overrides = {},
) {
const {
NS,
DB,
token,
...opts
} = options

const headers: Record<string, string> = {}
if (NS) {
headers.NS = NS
}
if (DB) {
headers.DB = DB
}
if (token) {
headers.Authorization = `Bearer ${token}`
}
opts.headers = Object.keys(headers).length
? {
...opts.headers,
...headers,
}
: undefined

return useFetch(url, {
...options,
...opts,
$fetch: useNuxtApp().$surrealFetch,
})
}
5 changes: 5 additions & 0 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Overrides {
DB?: string
NS?: string
token?: string
}

0 comments on commit d20e563

Please sign in to comment.