@messagevisor/sdk 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/jest.config.js +8 -0
- package/lib/conditions.d.ts +10 -0
- package/lib/conditions.js +163 -0
- package/lib/conditions.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +19 -0
- package/lib/index.js.map +1 -0
- package/lib/instance.d.ts +218 -0
- package/lib/instance.js +1000 -0
- package/lib/instance.js.map +1 -0
- package/package.json +42 -13
- package/src/conditions.spec.ts +373 -0
- package/src/conditions.ts +207 -0
- package/src/conformance.spec.ts +553 -0
- package/src/evaluation-edges.spec.ts +294 -0
- package/src/formatters.spec.ts +295 -0
- package/src/index.ts +2 -0
- package/src/instance.spec.ts +2493 -0
- package/src/instance.ts +1615 -0
- package/src/lifecycle.spec.ts +268 -0
- package/tsconfig.cjs.json +14 -0
- package/tsconfig.typecheck.json +4 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.1.0](https://github.com/messagevisor/messagevisor/compare/v0.0.2...v0.1.0) (2026-05-31)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @messagevisor/sdk
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## 0.0.2 (2026-05-31)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @messagevisor/sdk
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fahad Heylaal (https://fahad19.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Condition, Context, GroupSegment, Segment } from "@messagevisor/types";
|
|
2
|
+
export interface EvaluateOptions {
|
|
3
|
+
context?: Context;
|
|
4
|
+
segments?: Record<string, Segment>;
|
|
5
|
+
resolveFlag?: (featureKey: string, context?: Context) => boolean;
|
|
6
|
+
resolveVariation?: (experimentKey: string, context?: Context) => string | null;
|
|
7
|
+
}
|
|
8
|
+
export declare function evaluateCondition(condition: Condition | Condition[] | "*" | undefined, options?: EvaluateOptions): boolean;
|
|
9
|
+
export declare function evaluateGroupSegment(groupSegment: GroupSegment | GroupSegment[] | "*" | undefined, options?: EvaluateOptions): boolean;
|
|
10
|
+
export declare function evaluateSegment(segmentKey: string, options?: EvaluateOptions): boolean;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluateCondition = evaluateCondition;
|
|
4
|
+
exports.evaluateGroupSegment = evaluateGroupSegment;
|
|
5
|
+
exports.evaluateSegment = evaluateSegment;
|
|
6
|
+
function getContextValue(context, attribute) {
|
|
7
|
+
if (!context) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
return attribute
|
|
11
|
+
.split(".")
|
|
12
|
+
.reduce((value, part) => (value ? value[part] : undefined), context);
|
|
13
|
+
}
|
|
14
|
+
function compareDate(value, expected, operator) {
|
|
15
|
+
const valueTime = new Date(value).getTime();
|
|
16
|
+
const expectedTime = new Date(expected).getTime();
|
|
17
|
+
if (isNaN(valueTime) || isNaN(expectedTime)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return operator === "before" ? valueTime < expectedTime : valueTime > expectedTime;
|
|
21
|
+
}
|
|
22
|
+
function stringContains(value, expected) {
|
|
23
|
+
return String(value).indexOf(String(expected)) !== -1;
|
|
24
|
+
}
|
|
25
|
+
function stringStartsWith(value, expected) {
|
|
26
|
+
const valueAsString = String(value);
|
|
27
|
+
const expectedAsString = String(expected);
|
|
28
|
+
return valueAsString.slice(0, expectedAsString.length) === expectedAsString;
|
|
29
|
+
}
|
|
30
|
+
function stringEndsWith(value, expected) {
|
|
31
|
+
const valueAsString = String(value);
|
|
32
|
+
const expectedAsString = String(expected);
|
|
33
|
+
return valueAsString.slice(valueAsString.length - expectedAsString.length) === expectedAsString;
|
|
34
|
+
}
|
|
35
|
+
function arrayContains(value, expected) {
|
|
36
|
+
return value.indexOf(expected) !== -1;
|
|
37
|
+
}
|
|
38
|
+
function parseStructuredString(value) {
|
|
39
|
+
if (!(value.startsWith("{") || value.startsWith("["))) {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(value);
|
|
44
|
+
}
|
|
45
|
+
catch (_a) {
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function evaluateCondition(condition, options = {}) {
|
|
50
|
+
if (!condition || condition === "*") {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(condition)) {
|
|
54
|
+
return condition.every((item) => evaluateCondition(item, options));
|
|
55
|
+
}
|
|
56
|
+
if (typeof condition === "string") {
|
|
57
|
+
const parsedCondition = parseStructuredString(condition);
|
|
58
|
+
if (parsedCondition !== condition) {
|
|
59
|
+
return evaluateCondition(parsedCondition, options);
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if ("and" in condition) {
|
|
64
|
+
return condition.and.every((item) => evaluateCondition(item, options));
|
|
65
|
+
}
|
|
66
|
+
if ("or" in condition) {
|
|
67
|
+
return condition.or.some((item) => evaluateCondition(item, options));
|
|
68
|
+
}
|
|
69
|
+
if ("not" in condition) {
|
|
70
|
+
return !condition.not.every((item) => evaluateCondition(item, options));
|
|
71
|
+
}
|
|
72
|
+
if ("feature" in condition) {
|
|
73
|
+
const enabled = options.resolveFlag
|
|
74
|
+
? options.resolveFlag(condition.feature, options.context)
|
|
75
|
+
: false;
|
|
76
|
+
return condition.operator === "isEnabled"
|
|
77
|
+
? enabled
|
|
78
|
+
: condition.operator === "isDisabled"
|
|
79
|
+
? !enabled
|
|
80
|
+
: false;
|
|
81
|
+
}
|
|
82
|
+
if ("experiment" in condition) {
|
|
83
|
+
const variation = options.resolveVariation
|
|
84
|
+
? options.resolveVariation(condition.experiment, options.context)
|
|
85
|
+
: undefined;
|
|
86
|
+
return condition.operator === "hasVariation" ? variation === condition.value : false;
|
|
87
|
+
}
|
|
88
|
+
const value = getContextValue(options.context, condition.attribute);
|
|
89
|
+
const expected = condition.value;
|
|
90
|
+
switch (condition.operator) {
|
|
91
|
+
case "equals":
|
|
92
|
+
return value === expected;
|
|
93
|
+
case "notEquals":
|
|
94
|
+
return value !== expected;
|
|
95
|
+
case "exists":
|
|
96
|
+
return value !== undefined && value !== null;
|
|
97
|
+
case "notExists":
|
|
98
|
+
return value === undefined || value === null;
|
|
99
|
+
case "greaterThan":
|
|
100
|
+
return Number(value) > Number(expected);
|
|
101
|
+
case "greaterThanOrEquals":
|
|
102
|
+
return Number(value) >= Number(expected);
|
|
103
|
+
case "lessThan":
|
|
104
|
+
return Number(value) < Number(expected);
|
|
105
|
+
case "lessThanOrEquals":
|
|
106
|
+
return Number(value) <= Number(expected);
|
|
107
|
+
case "contains":
|
|
108
|
+
return stringContains(value, expected);
|
|
109
|
+
case "notContains":
|
|
110
|
+
return !stringContains(value, expected);
|
|
111
|
+
case "startsWith":
|
|
112
|
+
return stringStartsWith(value, expected);
|
|
113
|
+
case "endsWith":
|
|
114
|
+
return stringEndsWith(value, expected);
|
|
115
|
+
case "before":
|
|
116
|
+
return compareDate(value, expected, "before");
|
|
117
|
+
case "after":
|
|
118
|
+
return compareDate(value, expected, "after");
|
|
119
|
+
case "includes":
|
|
120
|
+
return Array.isArray(value) && arrayContains(value, expected);
|
|
121
|
+
case "notIncludes":
|
|
122
|
+
return !Array.isArray(value) || !arrayContains(value, expected);
|
|
123
|
+
case "in":
|
|
124
|
+
return Array.isArray(expected) && arrayContains(expected, value);
|
|
125
|
+
case "notIn":
|
|
126
|
+
return !Array.isArray(expected) || !arrayContains(expected, value);
|
|
127
|
+
default:
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function evaluateGroupSegment(groupSegment, options = {}) {
|
|
132
|
+
if (!groupSegment || groupSegment === "*") {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(groupSegment)) {
|
|
136
|
+
return groupSegment.every((item) => evaluateGroupSegment(item, options));
|
|
137
|
+
}
|
|
138
|
+
if (typeof groupSegment === "string") {
|
|
139
|
+
const parsedGroupSegment = parseStructuredString(groupSegment);
|
|
140
|
+
if (parsedGroupSegment !== groupSegment) {
|
|
141
|
+
return evaluateGroupSegment(parsedGroupSegment, options);
|
|
142
|
+
}
|
|
143
|
+
return evaluateSegment(groupSegment, options);
|
|
144
|
+
}
|
|
145
|
+
if ("and" in groupSegment) {
|
|
146
|
+
return groupSegment.and.every((item) => evaluateGroupSegment(item, options));
|
|
147
|
+
}
|
|
148
|
+
if ("or" in groupSegment) {
|
|
149
|
+
return groupSegment.or.some((item) => evaluateGroupSegment(item, options));
|
|
150
|
+
}
|
|
151
|
+
if ("not" in groupSegment) {
|
|
152
|
+
return !groupSegment.not.every((item) => evaluateGroupSegment(item, options));
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function evaluateSegment(segmentKey, options = {}) {
|
|
157
|
+
const segment = options.segments ? options.segments[segmentKey] : undefined;
|
|
158
|
+
if (!segment || segment.archived) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return evaluateCondition(segment.conditions, options);
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=conditions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditions.js","sourceRoot":"","sources":["../src/conditions.ts"],"names":[],"mappings":";;AAgEA,8CA+FC;AAED,oDAmCC;AAED,0CAQC;AArMD,SAAS,eAAe,CAAC,OAA4B,EAAE,SAAiB;IACtE,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,SAAS;SACb,KAAK,CAAC,GAAG,CAAC;SACV,MAAM,CAAC,CAAC,KAAU,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,OAAc,CAAC,CAAC;AACrF,CAAC;AAED,SAAS,WAAW,CAAC,KAAc,EA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AAO,YAAY,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,oBAAoB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,IAAI,KAAK,IAAI,YAAY,EAAE,CAAC;QAC1B,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,oBAAoB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAChF,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAgB,eAAe,CAAC,UAAkB,EAAE,UAA2B,EAAE;IAC/E,MAAM,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE5E,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,iBAAiB,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AACxD,CAAC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./conditions"), exports);
|
|
18
|
+
__exportStar(require("./instance"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,6CAA2B"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import type { Context, DatafileContent, FormatDateTimePresetOptions, FormatNumberPresetOptions, FormatPresets, FormatRelativeTimePresetOptions, LocaleDirection, LocaleKey, MessageKey, MessageMeta } from "@messagevisor/types";
|
|
2
|
+
export interface MessagevisorOptions {
|
|
3
|
+
datafile?: DatafileContent | string;
|
|
4
|
+
defaultTranslations?: Record<LocaleKey, Record<MessageKey, string>>;
|
|
5
|
+
defaultFormats?: Record<LocaleKey, FormatPresets>;
|
|
6
|
+
currency?: string;
|
|
7
|
+
timeZone?: string;
|
|
8
|
+
context?: Context;
|
|
9
|
+
locale?: LocaleKey;
|
|
10
|
+
resolveFlag?: (featureKey: string, context?: Context) => boolean;
|
|
11
|
+
resolveVariation?: (experimentKey: string, context?: Context) => string | null;
|
|
12
|
+
onDiagnostic?: MessagevisorDiagnosticHandler;
|
|
13
|
+
logLevel?: MessagevisorLogLevel;
|
|
14
|
+
cache?: MessagevisorCache;
|
|
15
|
+
modules?: MessagevisorModule[];
|
|
16
|
+
}
|
|
17
|
+
export interface EvaluationOptions {
|
|
18
|
+
locale?: LocaleKey;
|
|
19
|
+
currency?: string;
|
|
20
|
+
timeZone?: string;
|
|
21
|
+
formats?: FormatPresets;
|
|
22
|
+
moduleOptions?: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
export interface TranslateOptions extends EvaluationOptions {
|
|
25
|
+
context?: Context;
|
|
26
|
+
defaultTranslation?: string;
|
|
27
|
+
}
|
|
28
|
+
export type MessagePrimitiveValue = string | number | boolean | Date | null | undefined | unknown[] | Record<string, unknown>;
|
|
29
|
+
export type MessageValue<T = never> = MessagePrimitiveValue | ((chunks: Array<string | T>) => T);
|
|
30
|
+
export type MessageValues<T = never> = Record<string, MessageValue<T>>;
|
|
31
|
+
/**
|
|
32
|
+
* JavaScript-enhanced return shape for modules that produce rich values.
|
|
33
|
+
*
|
|
34
|
+
* The portable Messagevisor SDK contract for other languages may return strings
|
|
35
|
+
* only from translate() and formatMessage(). Rich callback values, framework
|
|
36
|
+
* nodes, and arrays are JavaScript-specific capabilities.
|
|
37
|
+
*/
|
|
38
|
+
export type MessageFormatResult<T = never> = [T] extends [never] ? string : string | T | Array<string | T>;
|
|
39
|
+
export type MessagevisorTranslationSource = "translation" | "formatMessage";
|
|
40
|
+
export interface MessagevisorTransformPayload {
|
|
41
|
+
translation: unknown;
|
|
42
|
+
locale: LocaleKey;
|
|
43
|
+
source: MessagevisorTranslationSource;
|
|
44
|
+
messageKey?: MessageKey;
|
|
45
|
+
meta?: MessageMeta;
|
|
46
|
+
}
|
|
47
|
+
export interface MessagevisorFormatPayload {
|
|
48
|
+
translation: unknown;
|
|
49
|
+
values?: MessageValues<any>;
|
|
50
|
+
locale: LocaleKey;
|
|
51
|
+
source: MessagevisorTranslationSource;
|
|
52
|
+
messageKey?: MessageKey;
|
|
53
|
+
meta?: MessageMeta;
|
|
54
|
+
formats: FormatPresets;
|
|
55
|
+
moduleOptions?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
export interface MessagevisorModuleApi {
|
|
58
|
+
setFlagResolver: (resolver?: (featureKey: string, context?: Context) => boolean) => void;
|
|
59
|
+
setVariationResolver: (resolver?: (experimentKey: string, context?: Context) => string | null) => void;
|
|
60
|
+
getRevision: (locale?: LocaleKey) => string;
|
|
61
|
+
onDiagnostic: (handler: MessagevisorDiagnosticHandler, options?: MessagevisorModuleDiagnosticOptions) => MessagevisorUnsubscribe;
|
|
62
|
+
reportDiagnostic: (diagnostic: Omit<MessagevisorDiagnostic, "module">) => void;
|
|
63
|
+
}
|
|
64
|
+
export type MessagevisorModuleSetupApi = MessagevisorModuleApi;
|
|
65
|
+
export interface MessagevisorModule {
|
|
66
|
+
name?: string;
|
|
67
|
+
setup?: (api: MessagevisorModuleApi) => void;
|
|
68
|
+
format?: (payload: MessagevisorFormatPayload, api?: MessagevisorModuleApi) => unknown;
|
|
69
|
+
transform?: (payload: MessagevisorTransformPayload, api?: MessagevisorModuleApi) => unknown;
|
|
70
|
+
close?: () => void | Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
export type MessagevisorDiagnosticCode = "sdk_initialized" | "missing_translation" | "missing_datafile" | "missing_locale" | "invalid_datafile" | "invalid_message" | "unsupported_formatter" | "message_override_matched" | "deprecated_message" | "duplicate_module" | (string & {});
|
|
73
|
+
export interface MessagevisorDiagnostic {
|
|
74
|
+
level: MessagevisorLogLevel;
|
|
75
|
+
code: MessagevisorDiagnosticCode;
|
|
76
|
+
message: string;
|
|
77
|
+
module?: string;
|
|
78
|
+
moduleName?: string;
|
|
79
|
+
locale?: LocaleKey | null;
|
|
80
|
+
messageKey?: MessageKey;
|
|
81
|
+
overrideKey?: string;
|
|
82
|
+
deprecationWarning?: string;
|
|
83
|
+
source?: MessagevisorTranslationSource;
|
|
84
|
+
originalError?: unknown;
|
|
85
|
+
}
|
|
86
|
+
export type MessagevisorDiagnosticHandler = (diagnostic: MessagevisorDiagnostic) => void;
|
|
87
|
+
export type MessagevisorLogLevel = "fatal" | "error" | "warn" | "info" | "debug";
|
|
88
|
+
export interface MessagevisorModuleDiagnosticOptions {
|
|
89
|
+
logLevel?: MessagevisorLogLevel;
|
|
90
|
+
}
|
|
91
|
+
export interface MessagevisorCache {
|
|
92
|
+
numberFormat: Record<string, Intl.NumberFormat>;
|
|
93
|
+
dateTimeFormat: Record<string, Intl.DateTimeFormat>;
|
|
94
|
+
relativeTimeFormat: Record<string, Intl.RelativeTimeFormat>;
|
|
95
|
+
pluralRules: Record<string, Intl.PluralRules>;
|
|
96
|
+
listFormat: Record<string, any>;
|
|
97
|
+
displayNames: Record<string, any>;
|
|
98
|
+
}
|
|
99
|
+
export type MessagevisorEventName = "change" | "error" | "datafile_set" | "locale_set" | "context_set" | "currency_set" | "timeZone_set";
|
|
100
|
+
export type MessagevisorUnsubscribe = () => void;
|
|
101
|
+
export interface MessagevisorSnapshot {
|
|
102
|
+
version: number;
|
|
103
|
+
locale: LocaleKey | null;
|
|
104
|
+
direction?: LocaleDirection;
|
|
105
|
+
context: Context;
|
|
106
|
+
currency?: string;
|
|
107
|
+
timeZone?: string;
|
|
108
|
+
datafileLocales: LocaleKey[];
|
|
109
|
+
datafileRevisionsByLocale: Record<LocaleKey, string>;
|
|
110
|
+
}
|
|
111
|
+
export interface MessagevisorEvent {
|
|
112
|
+
type: MessagevisorEventName;
|
|
113
|
+
version: number;
|
|
114
|
+
snapshot: MessagevisorSnapshot;
|
|
115
|
+
previousSnapshot: MessagevisorSnapshot;
|
|
116
|
+
locale?: LocaleKey | null;
|
|
117
|
+
previousLocale?: LocaleKey | null;
|
|
118
|
+
datafile?: DatafileContent;
|
|
119
|
+
context?: Context;
|
|
120
|
+
previousContext?: Context;
|
|
121
|
+
currency?: string;
|
|
122
|
+
previousCurrency?: string;
|
|
123
|
+
timeZone?: string;
|
|
124
|
+
previousTimeZone?: string;
|
|
125
|
+
diagnostic?: MessagevisorDiagnostic;
|
|
126
|
+
}
|
|
127
|
+
export type MessagevisorEventCallback = (event: MessagevisorEvent) => void;
|
|
128
|
+
export declare function createMessagevisorCache(): MessagevisorCache;
|
|
129
|
+
export declare class Messagevisor {
|
|
130
|
+
private datafiles;
|
|
131
|
+
private defaultTranslationsByLocale;
|
|
132
|
+
private defaultFormatsByLocale;
|
|
133
|
+
private locale;
|
|
134
|
+
private context;
|
|
135
|
+
private currency?;
|
|
136
|
+
private timeZone?;
|
|
137
|
+
private resolveFlag?;
|
|
138
|
+
private resolveVariation?;
|
|
139
|
+
private onDiagnostic?;
|
|
140
|
+
private logLevel;
|
|
141
|
+
private cache;
|
|
142
|
+
private modules;
|
|
143
|
+
private moduleDiagnosticSubscriptions;
|
|
144
|
+
private moduleApis;
|
|
145
|
+
private moduleApiId;
|
|
146
|
+
private closed;
|
|
147
|
+
private closePromise?;
|
|
148
|
+
private version;
|
|
149
|
+
private listeners;
|
|
150
|
+
constructor(options?: MessagevisorOptions);
|
|
151
|
+
subscribe(callback: () => void): MessagevisorUnsubscribe;
|
|
152
|
+
on(eventName: MessagevisorEventName, callback: MessagevisorEventCallback): MessagevisorUnsubscribe;
|
|
153
|
+
getSnapshot(): MessagevisorSnapshot;
|
|
154
|
+
addModule(module: MessagevisorModule): void;
|
|
155
|
+
removeModule(name: string): void;
|
|
156
|
+
setFlagResolver(resolver?: (featureKey: string, context?: Context) => boolean): void;
|
|
157
|
+
setVariationResolver(resolver?: (experimentKey: string, context?: Context) => string | null): void;
|
|
158
|
+
setCurrency(currency: string): void;
|
|
159
|
+
getCurrency(): string;
|
|
160
|
+
setTimeZone(timeZone: string): void;
|
|
161
|
+
getTimeZone(): string;
|
|
162
|
+
setDatafile(datafile: DatafileContent | string, replace?: boolean): void;
|
|
163
|
+
setContext(context: Context, replace?: boolean): void;
|
|
164
|
+
getContext(): {
|
|
165
|
+
[key: string]: import("@messagevisor/types").AttributeValue;
|
|
166
|
+
};
|
|
167
|
+
setLocale(locale: LocaleKey): void;
|
|
168
|
+
getLocale(): string;
|
|
169
|
+
getDirection(locale?: LocaleKey | null): LocaleDirection;
|
|
170
|
+
getDatafile(locale?: LocaleKey | null): DatafileContent;
|
|
171
|
+
getRevision(locale?: LocaleKey): string;
|
|
172
|
+
private getCurrentLocale;
|
|
173
|
+
private emitError;
|
|
174
|
+
private reportDiagnostic;
|
|
175
|
+
private resolveDatafileInput;
|
|
176
|
+
getDefaultTranslations(locale?: LocaleKey | null): Record<string, string>;
|
|
177
|
+
getDefaultFormats(locale?: LocaleKey | null): FormatPresets;
|
|
178
|
+
private getMessageFromDatafile;
|
|
179
|
+
private getMessageMeta;
|
|
180
|
+
private getMessageDefinition;
|
|
181
|
+
private reportDeprecatedMessage;
|
|
182
|
+
private resolveMessage;
|
|
183
|
+
private emit;
|
|
184
|
+
private getEvaluationFormats;
|
|
185
|
+
private getCachedNumberFormat;
|
|
186
|
+
private getCachedDateTimeFormat;
|
|
187
|
+
private getCachedRelativeTimeFormat;
|
|
188
|
+
private createModuleApi;
|
|
189
|
+
private getModuleApiKey;
|
|
190
|
+
private getModuleApi;
|
|
191
|
+
private runModuleSetup;
|
|
192
|
+
private clearModuleDiagnosticSubscriptions;
|
|
193
|
+
private runTransforms;
|
|
194
|
+
private runFormats;
|
|
195
|
+
private formatMessageInternal;
|
|
196
|
+
translate(messageKey: MessageKey, values?: Record<string, MessagePrimitiveValue>, options?: TranslateOptions): string;
|
|
197
|
+
translate<T>(messageKey: MessageKey, values: MessageValues<T>, options?: TranslateOptions): MessageFormatResult<T>;
|
|
198
|
+
t(messageKey: MessageKey, values?: Record<string, MessagePrimitiveValue>, options?: TranslateOptions): string;
|
|
199
|
+
t<T>(messageKey: MessageKey, values: MessageValues<T>, options?: TranslateOptions): MessageFormatResult<T>;
|
|
200
|
+
getRawTranslation(messageKey: MessageKey, options?: TranslateOptions): string;
|
|
201
|
+
formatMessage(message: string, values?: Record<string, MessagePrimitiveValue>, options?: EvaluationOptions): string;
|
|
202
|
+
formatMessage<T>(message: string, values: MessageValues<T>, options?: EvaluationOptions): MessageFormatResult<T>;
|
|
203
|
+
close(): Promise<void>;
|
|
204
|
+
private closeModules;
|
|
205
|
+
formatNumber(value: number, presetOrOptions?: string | FormatNumberPresetOptions, options?: EvaluationOptions): string;
|
|
206
|
+
formatNumberToParts(value: number, presetOrOptions?: string | FormatNumberPresetOptions, options?: EvaluationOptions): Intl.NumberFormatPart[];
|
|
207
|
+
formatDate(value: Date | number | string, presetOrOptions?: string | FormatDateTimePresetOptions, options?: EvaluationOptions): string;
|
|
208
|
+
formatDateToParts(value: Date | number | string, presetOrOptions?: string | FormatDateTimePresetOptions, options?: EvaluationOptions): Intl.DateTimeFormatPart[];
|
|
209
|
+
formatTime(value: Date | number | string, presetOrOptions?: string | FormatDateTimePresetOptions, options?: EvaluationOptions): string;
|
|
210
|
+
formatTimeToParts(value: Date | number | string, presetOrOptions?: string | FormatDateTimePresetOptions, options?: EvaluationOptions): Intl.DateTimeFormatPart[];
|
|
211
|
+
formatDateTimeRange(start: Date | number | string, end: Date | number | string, presetOrOptions?: string | FormatDateTimePresetOptions, options?: EvaluationOptions): string;
|
|
212
|
+
formatRelativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, presetOrOptions?: string | FormatRelativeTimePresetOptions, options?: EvaluationOptions): string;
|
|
213
|
+
formatPlural(value: number, options?: Intl.PluralRulesOptions & Pick<EvaluationOptions, "locale">): Intl.LDMLPluralRule;
|
|
214
|
+
formatList(values: Array<string>, options?: any): any;
|
|
215
|
+
formatListToParts(values: Array<string>, options?: any): any;
|
|
216
|
+
formatDisplayName(value: string, options?: any): any;
|
|
217
|
+
}
|
|
218
|
+
export declare function createMessagevisor(options?: MessagevisorOptions): Messagevisor;
|