Skip to content

Commit

Permalink
💥 prefer constant union over enum in APIs
Browse files Browse the repository at this point in the history
reasoning: make APIs easier to use with TypeScript by using directly constants instead of importing our enums
  • Loading branch information
bcaudan committed Nov 23, 2020
1 parent cdf9af0 commit 3c1c9c4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface UserConfiguration {
allowedTracingOrigins?: Array<string | RegExp>
sampleRate?: number
resourceSampleRate?: number
datacenter?: Datacenter // deprecated
datacenter?: 'us' | 'eu' // deprecated
site?: string
enableExperimentalFeatures?: string[]
silentMultipleInit?: boolean
Expand Down
8 changes: 3 additions & 5 deletions packages/logs/src/boot/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ export interface LogsUserConfiguration extends UserConfiguration {
}

export interface LoggerConfiguration {
level?: StatusType
handler?: HandlerType
level?: 'debug' | 'info' | 'warn' | 'error'
handler?: 'http' | 'console' | 'silent'
context?: Context
}

export type Status = keyof typeof StatusType

export type LogsGlobal = ReturnType<typeof makeLogsGlobal>

export const datadogLogs = makeLogsGlobal(startLogs)
Expand Down Expand Up @@ -77,7 +75,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) {
removeLoggerGlobalContext: monitor(globalContextManager.remove),

createLogger: monitor((name: string, conf: LoggerConfiguration = {}) => {
customLoggers[name] = new Logger(sendLog, conf.handler, conf.level, {
customLoggers[name] = new Logger(sendLog, conf.handler as HandlerType, conf.level as StatusType, {
...conf.context,
logger: { name },
})
Expand Down
14 changes: 7 additions & 7 deletions packages/logs/src/domain/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum StatusType {
error = 'error',
}

export const STATUS_PRIORITIES: { [key in StatusType]: number } = {
const STATUS_PRIORITIES: { [key in StatusType]: number } = {
[StatusType.debug]: 0,
[StatusType.info]: 1,
[StatusType.warn]: 2,
Expand Down Expand Up @@ -41,13 +41,13 @@ export class Logger {
}

@monitored
log(message: string, messageContext?: Context, status = StatusType.info) {
log(message: string, messageContext?: Context, status: 'debug' | 'info' | 'warn' | 'error' = 'info') {
if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) {
switch (this.handlerType) {
case HandlerType.http:
this.sendLog({
message,
status,
status: status as StatusType,
...combine(this.contextManager.get(), messageContext),
})
break
Expand Down Expand Up @@ -93,11 +93,11 @@ export class Logger {
this.contextManager.remove(key)
}

setHandler(handler: HandlerType) {
this.handlerType = handler
setHandler(handler: 'http' | 'console' | 'silent') {
this.handlerType = handler as HandlerType
}

setLevel(level: StatusType) {
this.level = level
setLevel(level: 'debug' | 'info' | 'warn' | 'error') {
this.level = level as StatusType
}
}
5 changes: 2 additions & 3 deletions packages/logs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { Datacenter } from '@datadog/browser-core'
export { StatusType, HandlerType, Logger, LogsMessage } from './domain/logger'
export { LogsUserConfiguration, Status, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry'
export { Logger, LogsMessage } from './domain/logger'
export { LogsUserConfiguration, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry'
8 changes: 2 additions & 6 deletions packages/rum/src/boot/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,10 @@ export function makeRumGlobal(startRumImpl: StartRum) {
},

addError: monitor(
(
error: unknown,
context?: Context,
source: ErrorSource.CUSTOM | ErrorSource.NETWORK | ErrorSource.SOURCE = ErrorSource.CUSTOM
) => {
(error: unknown, context?: Context, source: 'custom' | 'network' | 'source' = ErrorSource.CUSTOM) => {
let checkedSource
if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) {
checkedSource = source
checkedSource = source as ErrorSource
} else {
console.error(`DD_RUM.addError: Invalid source '${source}'`)
checkedSource = ErrorSource.CUSTOM
Expand Down
1 change: 0 additions & 1 deletion packages/rum/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { Datacenter, ErrorSource } from '@datadog/browser-core'
export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry'

0 comments on commit 3c1c9c4

Please sign in to comment.