Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build optimizations #101

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 41 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
addImportsDir,
addRouteMiddleware,
useLogger,
addTypeTemplate,
} from '@nuxt/kit';
import { defu } from 'defu';
import type {
SanctumAppConfig,
SanctumGlobalMiddlewarePageMeta,
SanctumModuleOptions,
} from './runtime/types';
Expand All @@ -18,16 +18,6 @@ type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

declare module '@nuxt/schema' {
interface PublicRuntimeConfig {
sanctum: Partial<SanctumModuleOptions>;
}

interface AppConfig {
sanctum?: SanctumAppConfig;
}
}

declare module '#app' {
interface PageMeta {
/**
Expand All @@ -44,6 +34,7 @@ declare module '#app' {
const MODULE_NAME = 'nuxt-auth-sanctum';

export type ModuleOptions = DeepPartial<SanctumModuleOptions>;
export type ModulePublicRuntimeConfig = { sanctum: ModuleOptions };

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand All @@ -59,6 +50,9 @@ export default defineNuxtModule<ModuleOptions>({
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
Expand Down Expand Up @@ -93,5 +87,41 @@ export default defineNuxtModule<ModuleOptions>({

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<SanctumGlobalMiddlewarePageMeta>;
}
}

export {};`,
});
},
});
8 changes: 6 additions & 2 deletions src/runtime/httpFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ 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();
const user = useSanctumUser();
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);
Expand Down
21 changes: 11 additions & 10 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ 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<void>;

/**
* Interceptors to be used by the ofetch client.
*/
export interface SanctumInterceptors {
/**
* Function to execute before sending a request.
*/
onRequest?: (
app: NuxtApp,
ctx: FetchContext,
logger: ConsolaInstance
) => Promise<void>;
onRequest?: SanctumInterceptor;
/**
* Function to execute after receiving a response.
*/
onResponse?: (
app: NuxtApp,
ctx: FetchContext,
logger: ConsolaInstance
) => Promise<void>;
onResponse?: SanctumInterceptor;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
Loading