Skip to content

Commit 0e77407

Browse files
committed
fix: ParamaterizeString typing to allow more than string values
1 parent cdf8bc5 commit 0e77407

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

packages/core/src/exports.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
Extras,
1313
FinishedCheckIn,
1414
MonitorConfig,
15+
ParameterizedString,
1516
Primitive,
1617
Session,
1718
SessionContext,
@@ -23,7 +24,7 @@ import { logger } from './utils-hoist/logger';
2324
import { uuid4 } from './utils-hoist/misc';
2425
import { timestampInSeconds } from './utils-hoist/time';
2526
import { GLOBAL_OBJ } from './utils-hoist/worldwide';
26-
import { parameterize } from './utils/parameterize';
27+
import { parameterizeStringTemplate } from './utils/parameterize';
2728
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
2829
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
2930

@@ -436,7 +437,7 @@ export const _experiment_log = {
436437
* Sentry._experiment_log.fmt`This is a log statement with ${x} and ${y} params`
437438
* ```
438439
*/
439-
fmt: parameterize,
440+
fmt: parameterizeStringTemplate,
440441

441442
/**
442443
* A flexible utility to record a log with a custom level and send it to sentry.

packages/core/src/log.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ function addToLogBuffer(client: Client, log: Log, scope: Scope): void {
114114
export function sendLog(
115115
level: LogSeverityLevel,
116116
severityNumber?: number,
117-
): (message: ParameterizedString | string, customAttributes?: Record<string, unknown>) => void {
118-
return (message: ParameterizedString | string, attributes: Record<string, unknown> = {}): void =>
117+
): (message: ParameterizedString<unknown[]> | string, customAttributes?: Record<string, unknown>) => void {
118+
return (message: ParameterizedString<unknown[]> | string, attributes: Record<string, unknown> = {}): void =>
119119
captureLog({ level, message, attributes, severityNumber });
120120
}
121121

@@ -129,7 +129,7 @@ export function captureLog({
129129
severityNumber,
130130
}: {
131131
level: LogSeverityLevel;
132-
message: ParameterizedString | string;
132+
message: ParameterizedString<unknown[]> | string;
133133
attributes?: Record<string, unknown>;
134134
severityNumber?: number;
135135
}): void {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type ParameterizedString = string & {
1+
export type ParameterizedString<Values = string[]> = string & {
22
__sentry_template_string__?: string;
3-
__sentry_template_values__?: string[];
3+
__sentry_template_values__?: Values;
44
};

packages/core/src/utils/parameterize.ts

+19
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,22 @@ export function parameterize(strings: TemplateStringsArray, ...values: string[])
1515
formatted.__sentry_template_values__ = values;
1616
return formatted;
1717
}
18+
19+
/**
20+
* Tagged template function which returns parameterized representation of the message
21+
* For example: parameterize`This is a log statement with ${x} and ${y} params`, would return:
22+
* "__sentry_template_string__": 'This is a log statement with %s and %s params',
23+
* "__sentry_template_values__": ['first', 'second']
24+
* @param strings An array of string values splitted between expressions
25+
* @param values Expressions extracted from template string
26+
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
27+
*/
28+
export function parameterizeStringTemplate(
29+
strings: TemplateStringsArray,
30+
...values: unknown[]
31+
): ParameterizedString<unknown[]> {
32+
const formatted = new String(String.raw(strings, ...values)) as ParameterizedString<unknown[]>;
33+
formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s');
34+
formatted.__sentry_template_values__ = values;
35+
return formatted;
36+
}

0 commit comments

Comments
 (0)