Skip to content

Commit

Permalink
fix(types): add explicit types for composables & remove baseUrl fro…
Browse files Browse the repository at this point in the history
…m tsconfig (#404)
  • Loading branch information
th1m0 authored Aug 29, 2024
1 parent 8c479bc commit 596ece2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,15 @@ export default defineNuxtModule<ModuleOptions>({
addTemplate({
filename: 'types/supabase-database.d.ts',
getContents: async () => {
if (!!options.types && fs.existsSync(await resolvePath(options.types))) {
return `export * from '${await resolvePath(options.types)}'`
if (options.types) {
// resolvePath is used to minify user input error.
const path = await resolvePath(options.types)
const basePath = await resolvePath('~~') // ~~ should be the base path in a nuxt project.

if (fs.existsSync(path)) {
// we are replacing the basePath with ../.. to move back to the root (~~) directory.
return `export * from '${path.replace(basePath, '../..')}'`
}
}

return `export type Database = unknown`
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useSupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { SupabaseClient } from '@supabase/supabase-js'
import { useNuxtApp } from '#imports'
import type { Database } from '#build/types/supabase-database'

export const useSupabaseClient = <T = Database>() => {
export const useSupabaseClient: <T = Database>() => SupabaseClient<T> = <T = Database>() => {
return useNuxtApp().$supabase?.client as SupabaseClient<T>
}
4 changes: 2 additions & 2 deletions src/runtime/composables/useSupabaseSession.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Session } from '@supabase/supabase-js'
import { useState } from '#imports'
import { useState, type Ref } from '#imports'

/**
* Reactive `Session` state from Supabase. This is initialized in both client and server plugin
* and, on the client, also updated through `onAuthStateChange` events.
*/
export const useSupabaseSession = () => useState<Session | null>('supabase_session', () => null)
export const useSupabaseSession = (): Ref<Session> => useState<Session | null>('supabase_session', () => null)
4 changes: 2 additions & 2 deletions src/runtime/composables/useSupabaseUser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { User } from '@supabase/supabase-js'
import { useState } from '#imports'
import { useState, type Ref } from '#imports'

/**
* Reactive `User` state from Supabase. This is initialized in both client and server plugin
* and, on the client, also updated through `onAuthStateChange` events.
*/
export const useSupabaseUser = () => useState<User | null>('supabase_user', () => null)
export const useSupabaseUser = (): Ref<User> => useState<User | null>('supabase_user', () => null)
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
"strictNullChecks": false,
"baseUrl": "src"
}
}

0 comments on commit 596ece2

Please sign in to comment.