chore(nestjs): ♻️ Refactor NestJS module registration, exports, and types for improved maintainability and API clarity
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
68706ba1c4
commit
e7adba2899
3 changed files with 0 additions and 143 deletions
|
|
@ -1,16 +0,0 @@
|
|||
// Module
|
||||
export { AnalyticsModule } from './module';
|
||||
export type { AnalyticsModuleOptions, AnalyticsModuleAsyncOptions } from './module';
|
||||
|
||||
// Decorators
|
||||
export { Track } from './decorators';
|
||||
export { NoTrack } from './decorators';
|
||||
|
||||
// Interceptor
|
||||
export { AnalyticsInterceptor } from './interceptors';
|
||||
|
||||
// Service
|
||||
export { AnalyticsService } from './services';
|
||||
|
||||
// Types
|
||||
export type { TrackOptions, AnalyticsEvent } from './types';
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
import { Module, DynamicModule, Provider, Global, type Type } from '@nestjs/common';
|
||||
import { AnalyticsService } from './services';
|
||||
import { AnalyticsInterceptor } from './interceptors';
|
||||
import type { CollectorConfig, BatchConfig } from './types';
|
||||
|
||||
export const ANALYTICS_OPTIONS = 'ANALYTICS_OPTIONS';
|
||||
|
||||
export interface AnalyticsModuleOptions {
|
||||
/** Collector service configuration */
|
||||
collector: CollectorConfig;
|
||||
/** Batching configuration */
|
||||
batch?: BatchConfig;
|
||||
/** Whether to enable tracking globally */
|
||||
enabled?: boolean;
|
||||
/** Whether to log events to console (development) */
|
||||
debug?: boolean;
|
||||
}
|
||||
|
||||
export interface AnalyticsModuleAsyncOptions {
|
||||
imports?: Type<unknown>[];
|
||||
useFactory: (...args: unknown[]) => Promise<AnalyticsModuleOptions> | AnalyticsModuleOptions;
|
||||
inject?: (Type<unknown> | string | symbol)[];
|
||||
}
|
||||
|
||||
@Global()
|
||||
@Module({})
|
||||
export class AnalyticsModule {
|
||||
static forRoot(options: AnalyticsModuleOptions): DynamicModule {
|
||||
const optionsProvider: Provider = {
|
||||
provide: ANALYTICS_OPTIONS,
|
||||
useValue: options,
|
||||
};
|
||||
|
||||
return {
|
||||
module: AnalyticsModule,
|
||||
providers: [optionsProvider, AnalyticsService, AnalyticsInterceptor],
|
||||
exports: [AnalyticsService, AnalyticsInterceptor],
|
||||
};
|
||||
}
|
||||
|
||||
static forRootAsync(options: AnalyticsModuleAsyncOptions): DynamicModule {
|
||||
const optionsProvider: Provider = {
|
||||
provide: ANALYTICS_OPTIONS,
|
||||
useFactory: options.useFactory,
|
||||
inject: options.inject || [],
|
||||
};
|
||||
|
||||
return {
|
||||
module: AnalyticsModule,
|
||||
imports: options.imports || [],
|
||||
providers: [optionsProvider, AnalyticsService, AnalyticsInterceptor],
|
||||
exports: [AnalyticsService, AnalyticsInterceptor],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
import type { AnalyticsEvent as BaseAnalyticsEvent } from '../types';
|
||||
|
||||
/**
|
||||
* Options for the @Track decorator
|
||||
*/
|
||||
export interface TrackOptions {
|
||||
/** Event name (defaults to method name) */
|
||||
event?: string;
|
||||
/** Event category */
|
||||
category?: string;
|
||||
/** Whether to include request body in event properties */
|
||||
includeBody?: boolean;
|
||||
/** Whether to include request params in event properties */
|
||||
includeParams?: boolean;
|
||||
/** Whether to include query params in event properties */
|
||||
includeQuery?: boolean;
|
||||
/** Custom properties extractor */
|
||||
extractProperties?: (context: TrackContext) => Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Context available when extracting properties
|
||||
*/
|
||||
export interface TrackContext {
|
||||
request: {
|
||||
method: string;
|
||||
url: string;
|
||||
path: string;
|
||||
body?: unknown;
|
||||
params?: Record<string, string>;
|
||||
query?: Record<string, string>;
|
||||
headers: Record<string, string | string[] | undefined>;
|
||||
ip?: string;
|
||||
user?: { id?: string; [key: string]: unknown };
|
||||
};
|
||||
response?: {
|
||||
statusCode: number;
|
||||
};
|
||||
executionTime?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Analytics event for NestJS
|
||||
*/
|
||||
export interface AnalyticsEvent extends Omit<BaseAnalyticsEvent, 'timestamp' | 'sessionId'> {
|
||||
timestamp?: string;
|
||||
sessionId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collector endpoint configuration
|
||||
*/
|
||||
export interface CollectorConfig {
|
||||
/** Collector service URL */
|
||||
url: string;
|
||||
/** API key for authentication */
|
||||
apiKey?: string;
|
||||
/** Request timeout in ms */
|
||||
timeout?: number;
|
||||
/** Number of retries */
|
||||
retries?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch configuration
|
||||
*/
|
||||
export interface BatchConfig {
|
||||
/** Maximum events to batch */
|
||||
maxSize?: number;
|
||||
/** Maximum time to wait before flushing (ms) */
|
||||
maxWait?: number;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue