Skip to content

Commit

Permalink
Merge pull request #76 from HubSpot/improve-log-callbacks
Browse files Browse the repository at this point in the history
Support interpolation in log callbacks
  • Loading branch information
camden11 authored Jan 12, 2024
2 parents 5304d5e + 8a97925 commit 59e0bc1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions types/Lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export type GenericLanguageObject = {
export type LanguageObject = typeof lang;

export type LangKey = Leaves<LanguageObject>;

export type InterpolationData = {
[identifier: string]: string | number;
};
4 changes: 3 additions & 1 deletion types/LogCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InterpolationData } from './Lang';

export type LogCallbacks<T extends string> = {
[key in T]?: () => void;
[key in T]?: (interpolationData?: InterpolationData) => void;
};

export type LogCallbacksArg<T extends readonly string[]> = {
Expand Down
11 changes: 6 additions & 5 deletions utils/lang.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import en from '../lang/en.json';
import { LanguageObject, GenericLanguageObject, LangKey } from '../types/Lang';
import {
LanguageObject,
GenericLanguageObject,
LangKey,
InterpolationData,
} from '../types/Lang';

const LANGUAGES: { [language: string]: LanguageObject } = {
en,
Expand Down Expand Up @@ -69,10 +74,6 @@ function generateReplaceFn(
};
}

type InterpolationData = {
[identifier: string]: string | number;
};

export function interpolate(
stringValue: string,
interpolationData: InterpolationData
Expand Down
16 changes: 8 additions & 8 deletions utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { i18n } from './lang';
import { logger } from '../lib/logging/logger';
import { LogCallbacks } from '../types/LogCallbacks';
import { LangKey } from '../types/Lang';
import { InterpolationData, LangKey } from '../types/Lang';

export function log<T extends string>(
key: T,
callbacks?: LogCallbacks<T>,
debugKey?: LangKey,
debugInterpolation?: { [key: string]: string | number }
interpolationData?: InterpolationData
): void {
if (callbacks && callbacks[key]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callbacks[key]!();
callbacks[key]!(interpolationData);
} else if (debugKey) {
debug(debugKey, debugInterpolation);
debug(debugKey, interpolationData);
}
}

Expand All @@ -25,13 +25,13 @@ export function makeTypedLogger<T extends readonly string[]>(
return (
key: ValidateCallbackKeys,
debugKey?: LangKey,
debugInterpolation?: { [key: string]: string | number }
) => log<ValidateCallbackKeys>(key, callbacks, debugKey, debugInterpolation);
interpolationData?: InterpolationData
) => log<ValidateCallbackKeys>(key, callbacks, debugKey, interpolationData);
}

export function debug(
identifier: LangKey,
interpolation?: { [key: string]: string | number }
interpolationData?: InterpolationData
): void {
logger.debug(i18n(identifier, interpolation));
logger.debug(i18n(identifier, interpolationData));
}

0 comments on commit 59e0bc1

Please sign in to comment.