Skip to content

Commit

Permalink
Merge pull request #32 from medishen/dev/v2
Browse files Browse the repository at this point in the history
refactor(config): Config and settings are refactored and each section is divided into separate modules and global is used instead of settings.
  • Loading branch information
0xii00 authored Feb 10, 2025
2 parents 7195493 + 619cf4b commit f8e869f
Show file tree
Hide file tree
Showing 20 changed files with 513 additions and 155 deletions.
10 changes: 0 additions & 10 deletions packages/config/config-module.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/config/config-service.ts

This file was deleted.

70 changes: 0 additions & 70 deletions packages/config/config.default.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/config/constant.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
// Express-style special symbol for default trust proxy behavior
export const TRUST_PROXY_DEFAULT_SYMBOL = '@@symbol:trust_proxy_default';
export const CORS_METADATA = {
CORS_WATERMARK: 'cors:watermark',
};
export const VIEWS_METADATA = {
VIEWS_WATERMARK: 'cors:watermark',
};
export const PROXY_METADATA = {
PROXY_WATERMARK: 'proxy:watermark',
};
export const COOKIES_METADATA = {
COOKIES_WATERMARK: 'cookies:watermark',
};
export const CACHE_METADATA = {
CACHE_WATERMARK: 'cache:watermark',
};
export const GLOBAL_METADATA = {
GLOBAL_WATERMARK: 'global:watermark',
};
5 changes: 4 additions & 1 deletion packages/config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './config-module';
export * from './services';
export * from './modules';
export * from './types/config.types';
export * from './interface/config.interface';
48 changes: 13 additions & 35 deletions packages/config/interface/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
import { CorsOptions, Environment, EtagIdentifier, TtlIdentifier } from '@gland/common';
import { Environment, EtagIdentifier, TtlIdentifier } from '@gland/common';
import { TrustProxyOption } from '../types/config.types';
export interface CookieConfig {

export interface ProxyServiceConfig {
trustProxy?: TrustProxyOption;
proxyTrustCount?: number;
proxyIpHeader?: string;
}

export interface CookieOptions {
secure?: boolean;
httpOnly?: boolean;
sameSite?: 'Strict' | 'Lax' | 'None';
domain?: string;
path?: string;
maxAge?: number;
}
export interface IConfigSettings {
etag?: EtagIdentifier;
subdomainOffset?: number;

maxIpsCount?: number;

poweredBy?: string | boolean;

cors?: CorsOptions;
}

export interface IConfigCache {
ttl: TtlIdentifier;
}

export interface IConfigEngines {
export interface ViewsEnginesOptions {
engine: 'ejs' | 'hbs' | 'pug';
cacheTemplates?: boolean;
}

export interface IConfigCore {
export interface GlobalSettings {
env?: Environment;
caching?: IConfigCache;
proxy?: boolean;
cookies?: CookieConfig;
views?: {
directory: string | string[];
engine?: IConfigEngines;
};
trustProxy?: TrustProxyOption;
proxyIpHeader?: string;
proxyTrustCount?: number;
}
/**
* Defines the overall structure for the configuration object.
*/
export interface ConfigOptions {
settings?: IConfigSettings;
core?: IConfigCore;
etag?: EtagIdentifier;
poweredBy?: string | boolean;
}
19 changes: 19 additions & 0 deletions packages/config/modules/cookies.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DynamicModule, Module } from '@gland/common';
import { COOKIES_METADATA } from '../constant';
import { CookieOptions } from '../interface/config.interface';

@Module({})
export class CookieModule {
static forRoot(options: CookieOptions): DynamicModule {
return {
module: CookieModule,
providers: [
{
provide: COOKIES_METADATA.COOKIES_WATERMARK,
useValue: options,
},
],
exports: [CookieModule],
};
}
}
25 changes: 25 additions & 0 deletions packages/config/modules/cors.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DynamicModule, Module } from '@gland/common';
import { CORS_METADATA } from '../constant';
import { CorsService } from '../services/cors.service';
import { CorsServiceConfig } from '../types/config.types';
/**
* @module CorsModule
* @description The CorsModule enables Cross-Origin Resource Sharing (CORS) functionality for the application.
* It provides global configuration options and the service for handling CORS headers.
*/
@Module({})
export class CorsModule {
static forRoot<T>(options: CorsServiceConfig<T>): DynamicModule {
return {
module: CorsModule,

providers: [
{
provide: CORS_METADATA.CORS_WATERMARK,
useValue: options,
},
],
exports: [CorsService<T>],
};
}
}
23 changes: 23 additions & 0 deletions packages/config/modules/global.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DynamicModule, Module } from '@gland/common';
import { ConfigService } from '../services/global.service';
import { GlobalSettings } from '../interface/config.interface';
import { GLOBAL_METADATA } from '../constant';
/**
* GlobalModule provides configuration management across the application.
* @public
*/
@Module({})
export class GlobalModule {
static forRoot(config?: GlobalSettings): DynamicModule {
return {
module: GlobalModule,
providers: [
{
provide: GLOBAL_METADATA.GLOBAL_WATERMARK,
useValue: config,
},
],
exports: [ConfigService],
};
}
}
5 changes: 5 additions & 0 deletions packages/config/modules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './cookies.module';
export * from './cors.module';
export * from './global.module';
export * from './proxy.module';
export * from './views.module';
23 changes: 23 additions & 0 deletions packages/config/modules/proxy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DynamicModule, Module } from '@gland/common';
import { PROXY_METADATA } from '../constant';
import { ProxyServiceConfig } from '../interface/config.interface';
import { ProxyService } from '../services/proxy.service';
/**
* @module ProxyModule
* @description this module handles proxy settings and trusted proxies it lets you configure trust proxy options like the number of trusted hops and the header for client ip addresses you can use it to check if a request is from a trusted source based on rules or custom logic it supports dynamic configuration with the forRoot method so you can customize it easily
*/
@Module({})
export class ProxyModule {
static forRoot(options?: ProxyServiceConfig): DynamicModule {
return {
module: ProxyModule,
providers: [
{
provide: PROXY_METADATA.PROXY_WATERMARK,
useValue: options ?? {},
},
],
exports: [ProxyService],
};
}
}
32 changes: 32 additions & 0 deletions packages/config/modules/views.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DynamicModule, Module } from '@gland/common';
import { VIEWS_METADATA } from '../constant';
import { ViewsConfig } from '../types/config.types';
import { ViewsService } from '../services/views.service';
/**
* @module ViewsModule
* @description
* The ViewsModule is responsible for setting up the views system in the application.
* It allows you to configure the view engine and the directory locations where
* the views are stored. You can use the forRoot method to provide custom options
* for the views configuration including the engine type like ejs pug or hbs
* and the directories where the views are located. By default it uses ejs
* as the template engine and a single directory '/views'. This module exports
* the ViewsService which allows other parts of the application to access
* and work with the view configurations and render templates based on the
* provided options.
*/
@Module({})
export class ViewsModule {
static forRoot(options: ViewsConfig): DynamicModule {
return {
module: ViewsModule,
providers: [
{
provide: VIEWS_METADATA.VIEWS_WATERMARK,
useValue: options,
},
],
exports: [ViewsService],
};
}
}
Loading

0 comments on commit f8e869f

Please sign in to comment.