Skip to content

Commit

Permalink
refactor: use db presets
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed May 27, 2024
1 parent 8fdd2e0 commit 9cdd32c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { defineNuxtModule, addPlugin, addImportsDir, createResolver } from '@nuxt/kit'
import { defu } from 'defu'

interface Database {
export interface DatabasePreset {
url?: string
NS?: string
DB?: string
}

// Module options TypeScript interface definition
export interface ModuleOptions {
url: string
databases: {
default: Database
[key: string]: Database | undefined
default: DatabasePreset
[key: string]: DatabasePreset | undefined
}
tokenCookieName: string
}
Expand All @@ -23,9 +23,9 @@ export default defineNuxtModule<ModuleOptions>({
},
// Default configuration options of the Nuxt module
defaults: {
url: '',
databases: {
default: {
url: '',
NS: '',
DB: '',
},
Expand Down
33 changes: 23 additions & 10 deletions src/runtime/composables/surreal-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import type { UseFetchOptions } from 'nuxt/app'
import { ref } from 'vue'

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

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

const headers: Record<string, string> = {}
if (NS) {
headers.NS = NS
}
if (DB) {
headers.DB = DB
if (database !== undefined) {
const db = ref<DatabasePreset>()
if (typeof database !== 'string' && typeof database !== 'number' && typeof database !== 'symbol') {
db.value = database
}
else {
const { databases } = useRuntimeConfig().public.surrealdb
db.value = databases[database]
}
if (db.value.url && !opts.baseURL) {
opts.baseURL = db.value.url
}
if (db.value.NS) {
headers.NS = db.value.NS
}
if (db.value.DB) {
headers.DB = db.value.DB
}
}
if (token) {
headers.Authorization = `Bearer ${token}`
Expand Down
18 changes: 9 additions & 9 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { defineNuxtPlugin, useCookie, useRuntimeConfig } from '#app'
export default defineNuxtPlugin(() => {
const {
surrealdb: {
url: baseURL,
databases: {
default: { ns, db },
},
databases: { default: { url: baseURL, NS, DB } },
tokenCookieName,
},
} = useRuntimeConfig().public
Expand All @@ -17,15 +14,18 @@ export default defineNuxtPlugin(() => {
onRequest({ options }) {
options.headers = options.headers || {}

if (ns) {
// @ts-expect-error NS header type missing
if (NS && !options.headers.NS) {
// @ts-expect-error NS header type missing
options.headers.NS = ns
options.headers.NS = NS
}
if (db) {
// @ts-expect-error DB header type missing
if (DB && !options.headers.DB) {
// @ts-expect-error DB header type missing
options.headers.DB = db
options.headers.DB = DB
}
if (userAuth.value) {
// @ts-expect-error Authorization header type missing
if (userAuth.value && !options.headers.Authorization) {
// @ts-expect-error Authorization header type missing
options.headers.Authorization = `Bearer ${userAuth.value}`
}
Expand Down
22 changes: 20 additions & 2 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import type { PublicRuntimeConfig } from '@nuxt/schema'
import type { DatabasePreset } from '../../module'

export { DatabasePreset } from '../../module'

export interface Overrides {
DB?: string
NS?: string
database?: keyof PublicRuntimeConfig['surrealdb']['databases'] | DatabasePreset
token?: string
}

type OKResponse<T = unknown[]> = {
result: T
status: 'OK'
time: string
}

type ErrorResponse = {
result: string
status: 'ERR'
time: string
}

export type Response<T> = OKResponse<T> | ErrorResponse

0 comments on commit 9cdd32c

Please sign in to comment.