Skip to content

Commit

Permalink
feat: unfinished generic indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonfLK committed Jan 6, 2021
1 parent eb44411 commit af1382d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 104 deletions.
31 changes: 0 additions & 31 deletions packages/core/src/config/ConfigLog.spec.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/core/src/config/ConfigLog.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable dot-notation */
// import { ERROR_WS_ADDRESS_NOT_SET } from '../errorhandling/SDKErrors'
import { LogLevel, Logger } from 'typescript-logging'
import { ERROR_WS_ADDRESS_NOT_SET } from '../../packages/core/src/errorhandling/SDKErrors'
import { ERROR_WS_ADDRESS_NOT_SET } from '../errorhandling/SDKErrors'
import * as ConfigService from './ConfigService'

describe('Log Configuration', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DEFAULT_DEBUG_LEVEL =
: LogLevel.Error

export interface configOpts {
address?: string
address: string
logLevel: LogLevel
}

Expand All @@ -44,41 +44,47 @@ export function modifyLogLevel(level: LogLevel): LogLevel {
return actualLevel
}

export class ConfigService {
private readonly props: configOpts
let configuration: configOpts = {
logLevel: DEFAULT_DEBUG_LEVEL,
address: '',
}

public constructor(config: configOpts) {
this.props = config
function getAddress(): configOpts['address'] {
if (!configuration.address) {
throw ERROR_WS_ADDRESS_NOT_SET()
}
return configuration.address

get host(): string {
if (!this.props.address) {
throw ERROR_WS_ADDRESS_NOT_SET()
}
return this.props.address
}
}

set host(address: string) {
this.props.address = address
export function get<T = configOpts, K = keyof T>(configOpt: K): T[K] {
if (typeof configuration[configOpt] === 'undefined')
throw new Error(`GENERIC NOT CONFIGURED ERROR FOR ${configOpt}`)
if (configOpt === 'address') {
return getAddress()
}
return configuration[configOpt]
}

get logging(): LogLevel {
return this.props.logLevel
function setLogLevel(logLevel: LogLevel | undefined): void {
if (logLevel || logLevel === 0) {
modifyLogLevel(logLevel)
}
}

set logging(level: LogLevel) {
this.props.logLevel = modifyLogLevel(level)
}
function setAddress(address: Required<configOpts>['address']): void {
configuration.address = address
}

export const configuration = new ConfigService({
logLevel: DEFAULT_DEBUG_LEVEL,
})
export function set<K extends Partial<configOpts>>(opts: K): void {
setLogLevel(opts.logLevel)
configuration = { ...configuration, ...opts }
}

// Create options instance and specify 1 LogGroupRule:
// * LogLevel Error on default, env DEBUG = 'true' changes Level to Debug.throws
const options = new LoggerFactoryOptions().addLogGroupRule(
new LogGroupRule(new RegExp('.+'), configuration.logging)
new LogGroupRule(new RegExp('.+'), get('logLevel'))
)
// Create a named loggerfactory and pass in the options and export the factory.
// Named is since version 0.2.+ (it's recommended for future usage)
Expand Down

0 comments on commit af1382d

Please sign in to comment.