diff --git a/package.json b/package.json index 156f112..068938f 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ }, "dependencies": { "@nuxt/kit": "^3.9.0", - "defu": "^6.1.4" + "defu": "^6.1.4", + "nuxt-auth-sanctum": "workspace:*" }, "devDependencies": { "@nuxt/devtools": "latest", diff --git a/src/module.ts b/src/module.ts index 14ce858..68f4ae9 100644 --- a/src/module.ts +++ b/src/module.ts @@ -5,10 +5,10 @@ import { addImportsDir, addRouteMiddleware, useLogger, + addTypeTemplate, } from '@nuxt/kit'; import { defu } from 'defu'; import type { - SanctumAppConfig, SanctumGlobalMiddlewarePageMeta, SanctumModuleOptions, } from './runtime/types'; @@ -18,16 +18,6 @@ type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; -declare module '@nuxt/schema' { - interface PublicRuntimeConfig { - sanctum: Partial; - } - - interface AppConfig { - sanctum?: SanctumAppConfig; - } -} - declare module '#app' { interface PageMeta { /** @@ -44,6 +34,7 @@ declare module '#app' { const MODULE_NAME = 'nuxt-auth-sanctum'; export type ModuleOptions = DeepPartial; +export type ModulePublicRuntimeConfig = { sanctum: ModuleOptions }; export default defineNuxtModule({ meta: { @@ -59,6 +50,9 @@ export default defineNuxtModule({ setup(options, nuxt) { const resolver = createResolver(import.meta.url); + const runtimeDir = resolver.resolve('./runtime'); + nuxt.options.build.transpile.push(runtimeDir); + const sanctumConfig = defu( nuxt.options.runtimeConfig.public.sanctum as any, options @@ -93,5 +87,41 @@ export default defineNuxtModule({ logger.info('Sanctum module initialized w/o global middleware'); } + + addTypeTemplate({ + filename: 'types/sanctum.d.ts', + getContents: () => `// Generated by nuxt-auth-sanctum module +import type { + SanctumAppConfig, + SanctumGlobalMiddlewarePageMeta +} from '${resolver.resolve('./runtime/types.ts')}'; + +declare module 'nuxt/schema' { + interface AppConfigInput { + sanctum?: SanctumAppConfig; + } +} + +declare module '@nuxt/schema' { + interface AppConfigInput { + sanctum?: SanctumAppConfig; + } +} + +declare module 'nuxt/dist/pages/runtime' { + interface PageMeta { + /** + * @deprecated Use \`sanctum.excluded\` instead. + */ + excludeFromSanctum?: boolean; + /** + * Sanctum global middleware page configuration. + */ + sanctum?: Partial; + } +} + +export {};`, + }); }, }); diff --git a/src/runtime/httpFactory.ts b/src/runtime/httpFactory.ts index 7012948..4426778 100644 --- a/src/runtime/httpFactory.ts +++ b/src/runtime/httpFactory.ts @@ -7,6 +7,7 @@ import { useSanctumAppConfig } from './composables/useSanctumAppConfig'; import handleRequestCookies from './interceptors/cookie/request'; import handleResponseHeaders from './interceptors/cookie/response'; import handleRequestHeaders from './interceptors/common/request'; +import type { SanctumInterceptor } from './types'; export function createHttpClient(logger: ConsolaInstance): $Fetch { const options = useSanctumConfig(); @@ -14,8 +15,11 @@ export function createHttpClient(logger: ConsolaInstance): $Fetch { const appConfig = useSanctumAppConfig(); const nuxtApp = useNuxtApp(); - const requestInterceptors = [handleRequestHeaders, handleRequestCookies]; - const responseInterceptors = [handleResponseHeaders]; + const requestInterceptors: SanctumInterceptor[] = [ + handleRequestHeaders, + handleRequestCookies, + ]; + const responseInterceptors: SanctumInterceptor[] = [handleResponseHeaders]; if (appConfig.interceptors?.onRequest) { requestInterceptors.push(appConfig.interceptors.onRequest); diff --git a/src/runtime/types.ts b/src/runtime/types.ts index f13c278..7cb6596 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -2,6 +2,15 @@ import type { FetchContext } from 'ofetch'; import type { ConsolaInstance } from 'consola'; import type { NuxtApp } from '#app'; +/** + * Interceptor definition type. + */ +export type SanctumInterceptor = ( + app: NuxtApp, + ctx: FetchContext, + logger: ConsolaInstance +) => Promise; + /** * Interceptors to be used by the ofetch client. */ @@ -9,19 +18,11 @@ export interface SanctumInterceptors { /** * Function to execute before sending a request. */ - onRequest?: ( - app: NuxtApp, - ctx: FetchContext, - logger: ConsolaInstance - ) => Promise; + onRequest?: SanctumInterceptor; /** * Function to execute after receiving a response. */ - onResponse?: ( - app: NuxtApp, - ctx: FetchContext, - logger: ConsolaInstance - ) => Promise; + onResponse?: SanctumInterceptor; } /** diff --git a/yarn.lock b/yarn.lock index 988d00f..b4a489c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7506,7 +7506,7 @@ __metadata: languageName: node linkType: hard -"nuxt-auth-sanctum@workspace:.": +"nuxt-auth-sanctum@workspace:*, nuxt-auth-sanctum@workspace:.": version: 0.0.0-use.local resolution: "nuxt-auth-sanctum@workspace:." dependencies: @@ -7526,6 +7526,7 @@ __metadata: nitropack: "npm:^2.9.6" nuxi: "npm:^3.10.0" nuxt: "npm:^3.11.2" + nuxt-auth-sanctum: "workspace:*" prettier: "npm:^3.0.3" typescript: "npm:^5.4.5" vite: "npm:^4.4.9"