Files
nest/packages/core/application-config.ts
2026-02-15 13:07:08 +01:00

151 lines
4.2 KiB
TypeScript

import type {
CanActivate,
ExceptionFilter,
NestInterceptor,
PipeTransform,
VersioningOptions,
WebSocketAdapter,
} from '@nestjs/common';
import { InstanceWrapper } from './injector/instance-wrapper.js';
import { ExcludeRouteMetadata } from './router/interfaces/exclude-route-metadata.interface.js';
import type { GlobalPrefixOptions } from '@nestjs/common/internal';
export class ApplicationConfig {
private globalPrefix = '';
private globalPrefixOptions: GlobalPrefixOptions<ExcludeRouteMetadata> = {};
private globalPipes: Array<PipeTransform> = [];
private globalFilters: Array<ExceptionFilter> = [];
private globalInterceptors: Array<NestInterceptor> = [];
private globalGuards: Array<CanActivate> = [];
private versioningOptions: VersioningOptions;
private readonly globalRequestPipes: InstanceWrapper<PipeTransform>[] = [];
private readonly globalRequestFilters: InstanceWrapper<ExceptionFilter>[] =
[];
private readonly globalRequestInterceptors: InstanceWrapper<NestInterceptor>[] =
[];
private readonly globalRequestGuards: InstanceWrapper<CanActivate>[] = [];
constructor(private ioAdapter: WebSocketAdapter | null = null) {}
public setGlobalPrefix(prefix: string) {
this.globalPrefix = prefix;
}
public getGlobalPrefix() {
return this.globalPrefix;
}
public setGlobalPrefixOptions(
options: GlobalPrefixOptions<ExcludeRouteMetadata>,
) {
this.globalPrefixOptions = options;
}
public getGlobalPrefixOptions(): GlobalPrefixOptions<ExcludeRouteMetadata> {
return this.globalPrefixOptions;
}
public setIoAdapter(ioAdapter: WebSocketAdapter) {
this.ioAdapter = ioAdapter;
}
public getIoAdapter(): WebSocketAdapter {
return this.ioAdapter!;
}
public addGlobalPipe(pipe: PipeTransform<any>) {
this.globalPipes.push(pipe);
}
public useGlobalPipes(...pipes: PipeTransform<any>[]) {
this.globalPipes = this.globalPipes.concat(pipes);
}
public getGlobalFilters(): ExceptionFilter[] {
return this.globalFilters;
}
public addGlobalFilter(filter: ExceptionFilter) {
this.globalFilters.push(filter);
}
public useGlobalFilters(...filters: ExceptionFilter[]) {
this.globalFilters = this.globalFilters.concat(filters);
}
public getGlobalPipes(): PipeTransform<any>[] {
return this.globalPipes;
}
public getGlobalInterceptors(): NestInterceptor[] {
return this.globalInterceptors;
}
public addGlobalInterceptor(interceptor: NestInterceptor) {
this.globalInterceptors.push(interceptor);
}
public useGlobalInterceptors(...interceptors: NestInterceptor[]) {
this.globalInterceptors = this.globalInterceptors.concat(interceptors);
}
public getGlobalGuards(): CanActivate[] {
return this.globalGuards;
}
public addGlobalGuard(guard: CanActivate) {
this.globalGuards.push(guard);
}
public useGlobalGuards(...guards: CanActivate[]) {
this.globalGuards = this.globalGuards.concat(guards);
}
public addGlobalRequestInterceptor(
wrapper: InstanceWrapper<NestInterceptor>,
) {
this.globalRequestInterceptors.push(wrapper);
}
public getGlobalRequestInterceptors(): InstanceWrapper<NestInterceptor>[] {
return this.globalRequestInterceptors;
}
public addGlobalRequestPipe(wrapper: InstanceWrapper<PipeTransform>) {
this.globalRequestPipes.push(wrapper);
}
public getGlobalRequestPipes(): InstanceWrapper<PipeTransform>[] {
return this.globalRequestPipes;
}
public addGlobalRequestFilter(wrapper: InstanceWrapper<ExceptionFilter>) {
this.globalRequestFilters.push(wrapper);
}
public getGlobalRequestFilters(): InstanceWrapper<ExceptionFilter>[] {
return this.globalRequestFilters;
}
public addGlobalRequestGuard(wrapper: InstanceWrapper<CanActivate>) {
this.globalRequestGuards.push(wrapper);
}
public getGlobalRequestGuards(): InstanceWrapper<CanActivate>[] {
return this.globalRequestGuards;
}
public enableVersioning(options: VersioningOptions): void {
if (Array.isArray(options.defaultVersion)) {
// Drop duplicated versions
options.defaultVersion = Array.from(new Set(options.defaultVersion));
}
this.versioningOptions = options;
}
public getVersioning(): VersioningOptions | undefined {
return this.versioningOptions;
}
}