From 596ece2460e7b4d08bcb2ac2322c10912e0a5977 Mon Sep 17 00:00:00 2001 From: Thimo Sietsma Date: Thu, 29 Aug 2024 16:40:20 +0200 Subject: [PATCH] fix(types): add explicit types for composables & remove `baseUrl` from tsconfig (#404) --- src/module.ts | 11 +++++++++-- src/runtime/composables/useSupabaseClient.ts | 2 +- src/runtime/composables/useSupabaseSession.ts | 4 ++-- src/runtime/composables/useSupabaseUser.ts | 4 ++-- tsconfig.json | 2 -- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/module.ts b/src/module.ts index dafe2e43..9d6a6381 100644 --- a/src/module.ts +++ b/src/module.ts @@ -203,8 +203,15 @@ export default defineNuxtModule({ 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` diff --git a/src/runtime/composables/useSupabaseClient.ts b/src/runtime/composables/useSupabaseClient.ts index 26f73eb6..0c9627fc 100644 --- a/src/runtime/composables/useSupabaseClient.ts +++ b/src/runtime/composables/useSupabaseClient.ts @@ -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 = () => { +export const useSupabaseClient: () => SupabaseClient = () => { return useNuxtApp().$supabase?.client as SupabaseClient } diff --git a/src/runtime/composables/useSupabaseSession.ts b/src/runtime/composables/useSupabaseSession.ts index 6c31526f..efcc748d 100644 --- a/src/runtime/composables/useSupabaseSession.ts +++ b/src/runtime/composables/useSupabaseSession.ts @@ -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('supabase_session', () => null) +export const useSupabaseSession = (): Ref => useState('supabase_session', () => null) diff --git a/src/runtime/composables/useSupabaseUser.ts b/src/runtime/composables/useSupabaseUser.ts index 7954b284..a72d4a1c 100644 --- a/src/runtime/composables/useSupabaseUser.ts +++ b/src/runtime/composables/useSupabaseUser.ts @@ -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('supabase_user', () => null) +export const useSupabaseUser = (): Ref => useState('supabase_user', () => null) diff --git a/tsconfig.json b/tsconfig.json index f3029e2d..dec7d13c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,5 @@ "extends": "./.nuxt/tsconfig.json", "compilerOptions": { "strictNullChecks": false, - "baseUrl": "src" } } -