@wix/essentials 0.1.28 → 1.0.1

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.
@@ -2,5 +2,5 @@ import { Host } from '@wix/sdk-types';
2
2
  interface Messages {
3
3
  [key: string]: string;
4
4
  }
5
- export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
6
  export {};
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,4 +1,11 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import i18next from 'i18next';
3
+ import { initReactI18next } from 'react-i18next';
4
+ import { multilingual } from '@wix/site';
5
+ function getPrimaryLanguage(fallback) {
6
+ return (multilingual.listSupportedLanguages().find((language) => language.primary)
7
+ ?.id ?? fallback);
8
+ }
2
9
  export const i18n = createHostModule({
3
10
  getLanguage: (host) => () => {
4
11
  const language = host.essentials?.language;
@@ -14,4 +21,25 @@ export const i18n = createHostModule({
14
21
  }
15
22
  return locale;
16
23
  },
24
+ useTranslation: (host) => () => {
25
+ const resources = host.translations?.();
26
+ if (!resources) {
27
+ throw new Error('Host translation resources are not available.');
28
+ }
29
+ const instance = i18next.createInstance();
30
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
31
+ const initOptions = {
32
+ lng: host.essentials?.language || mainLanguage,
33
+ fallbackLng: mainLanguage,
34
+ resources,
35
+ };
36
+ // Init is asynchronous, but when provided with a synchronous resource,
37
+ // it inits immediately and the callback ensures synchronous completion.
38
+ void instance.use(initReactI18next).init(initOptions, (err) => {
39
+ if (err) {
40
+ throw err;
41
+ }
42
+ });
43
+ return instance.t.bind(instance);
44
+ },
17
45
  });
@@ -1,34 +1,34 @@
1
1
  export declare function createEssentialsMockFactory(mockFn: any): {
2
2
  i18n: {
3
- getLanguage: any;
4
- getLocale: any;
3
+ getLanguage: Function | undefined;
4
+ getLocale: Function | undefined;
5
5
  };
6
6
  bi: {
7
- getLogger: any;
7
+ getLogger: Function | undefined;
8
8
  };
9
9
  monitoring: {
10
- getMonitoringClient: any;
10
+ getMonitoringClient: Function | undefined;
11
11
  };
12
12
  experiments: {
13
- enabled: any;
14
- get: any;
13
+ enabled: Function | undefined;
14
+ get: Function | undefined;
15
15
  };
16
16
  settings: {
17
- getTimezone: any;
17
+ getTimezone: Function | undefined;
18
18
  };
19
19
  auth: {
20
- elevate: any;
21
- getContextualAuth: any;
22
- getTokenInfo: any;
20
+ elevate: Function | undefined;
21
+ getContextualAuth: Function | undefined;
22
+ getTokenInfo: Function | undefined;
23
23
  };
24
24
  httpClient: {
25
- fetchWithAuth: any;
26
- graphql: any;
25
+ fetchWithAuth: Function | undefined;
26
+ graphql: Function | undefined;
27
27
  };
28
28
  errorHandler: {
29
- withErrorHandler: any;
30
- getResolvedError: any;
31
- showError: any;
32
- reportRetryAttempt: any;
29
+ withErrorHandler: Function | undefined;
30
+ getResolvedError: Function | undefined;
31
+ showError: Function | undefined;
32
+ reportRetryAttempt: Function | undefined;
33
33
  };
34
34
  };
@@ -1,45 +1,52 @@
1
1
  export function createEssentialsMockFactory(mockFn) {
2
+ const mockFns = new Map();
3
+ const createUniqueFn = (name, implementation) => {
4
+ if (!mockFns.has(name)) {
5
+ mockFns.set(name, mockFn(implementation));
6
+ }
7
+ return mockFns.get(name);
8
+ };
2
9
  return {
3
10
  i18n: {
4
- getLanguage: mockFn(() => 'en'),
5
- getLocale: mockFn(() => 'en-US'),
11
+ getLanguage: createUniqueFn('i18n.getLanguage', () => 'en'),
12
+ getLocale: createUniqueFn('i18n.getLocale', () => 'en-US'),
6
13
  },
7
14
  bi: {
8
- getLogger: mockFn(() => ({
9
- log: mockFn(),
10
- report: mockFn(),
11
- flush: mockFn(),
12
- updateDefaults: mockFn((params) => ({
13
- log: mockFn(),
14
- report: mockFn(),
15
- flush: mockFn(),
16
- updateDefaults: mockFn(),
15
+ getLogger: createUniqueFn('bi.getLogger', () => ({
16
+ log: createUniqueFn('bi.getLogger.log'),
17
+ report: createUniqueFn('bi.getLogger.report'),
18
+ flush: createUniqueFn('bi.getLogger.flush'),
19
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults', (params) => ({
20
+ log: createUniqueFn('bi.getLogger.updateDefaults.log'),
21
+ report: createUniqueFn('bi.getLogger.updateDefaults.report'),
22
+ flush: createUniqueFn('bi.getLogger.updateDefaults.flush'),
23
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults.updateDefaults'),
17
24
  })),
18
25
  })),
19
26
  },
20
27
  monitoring: {
21
- getMonitoringClient: mockFn(() => ({
22
- captureException: mockFn(),
23
- captureMessage: mockFn(),
24
- setContext: mockFn(),
25
- setTag: mockFn(),
26
- setUser: mockFn(),
27
- addBreadcrumb: mockFn(),
28
+ getMonitoringClient: createUniqueFn('monitoring.getMonitoringClient', () => ({
29
+ captureException: createUniqueFn('monitoring.getMonitoringClient.captureException'),
30
+ captureMessage: createUniqueFn('monitoring.getMonitoringClient.captureMessage'),
31
+ setContext: createUniqueFn('monitoring.getMonitoringClient.setContext'),
32
+ setTag: createUniqueFn('monitoring.getMonitoringClient.setTag'),
33
+ setUser: createUniqueFn('monitoring.getMonitoringClient.setUser'),
34
+ addBreadcrumb: createUniqueFn('monitoring.getMonitoringClient.addBreadcrumb'),
28
35
  })),
29
36
  },
30
37
  experiments: {
31
- enabled: mockFn(() => false),
32
- get: mockFn(() => null),
38
+ enabled: createUniqueFn('experiments.enabled', () => false),
39
+ get: createUniqueFn('experiments.get', () => null),
33
40
  },
34
41
  settings: {
35
- getTimezone: mockFn(() => 'UTC'),
42
+ getTimezone: createUniqueFn('settings.getTimezone', () => 'UTC'),
36
43
  },
37
44
  auth: {
38
- elevate: mockFn((restModule) => restModule),
39
- getContextualAuth: mockFn(() => ({
45
+ elevate: createUniqueFn('auth.elevate', (restModule) => restModule),
46
+ getContextualAuth: createUniqueFn('auth.getContextualAuth', () => ({
40
47
  headers: {},
41
48
  })),
42
- getTokenInfo: mockFn(() => Promise.resolve({
49
+ getTokenInfo: createUniqueFn('auth.getTokenInfo', () => Promise.resolve({
43
50
  active: true,
44
51
  subjectType: 'USER',
45
52
  subjectId: 'mock-user-id',
@@ -51,21 +58,21 @@ export function createEssentialsMockFactory(mockFn) {
51
58
  })),
52
59
  },
53
60
  httpClient: {
54
- fetchWithAuth: mockFn(() => global.fetch || mockFn()),
55
- graphql: mockFn(() => Promise.resolve({
61
+ fetchWithAuth: createUniqueFn('httpClient.fetchWithAuth', () => global.fetch || mockFn()),
62
+ graphql: createUniqueFn('httpClient.graphql', () => Promise.resolve({
56
63
  data: {},
57
64
  errors: [],
58
65
  })),
59
66
  },
60
67
  errorHandler: {
61
- withErrorHandler: mockFn((fn) => fn()),
62
- getResolvedError: mockFn(() => ({
68
+ withErrorHandler: createUniqueFn('errorHandler.withErrorHandler', (fn) => fn()),
69
+ getResolvedError: createUniqueFn('errorHandler.getResolvedError', () => ({
63
70
  code: 'UNKNOWN_ERROR',
64
71
  message: 'Mock error',
65
72
  details: {},
66
73
  })),
67
- showError: mockFn(),
68
- reportRetryAttempt: mockFn(),
74
+ showError: createUniqueFn('errorHandler.showError'),
75
+ reportRetryAttempt: createUniqueFn('errorHandler.reportRetryAttempt'),
69
76
  },
70
77
  };
71
78
  }
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,7 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.i18n = void 0;
4
7
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
8
+ const i18next_1 = __importDefault(require("i18next"));
9
+ const react_i18next_1 = require("react-i18next");
10
+ const site_1 = require("@wix/site");
11
+ function getPrimaryLanguage(fallback) {
12
+ return (site_1.multilingual.listSupportedLanguages().find((language) => language.primary)
13
+ ?.id ?? fallback);
14
+ }
5
15
  exports.i18n = (0, host_modules_1.createHostModule)({
6
16
  getLanguage: (host) => () => {
7
17
  const language = host.essentials?.language;
@@ -17,4 +27,25 @@ exports.i18n = (0, host_modules_1.createHostModule)({
17
27
  }
18
28
  return locale;
19
29
  },
30
+ useTranslation: (host) => () => {
31
+ const resources = host.translations?.();
32
+ if (!resources) {
33
+ throw new Error('Host translation resources are not available.');
34
+ }
35
+ const instance = i18next_1.default.createInstance();
36
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
37
+ const initOptions = {
38
+ lng: host.essentials?.language || mainLanguage,
39
+ fallbackLng: mainLanguage,
40
+ resources,
41
+ };
42
+ // Init is asynchronous, but when provided with a synchronous resource,
43
+ // it inits immediately and the callback ensures synchronous completion.
44
+ void instance.use(react_i18next_1.initReactI18next).init(initOptions, (err) => {
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ });
49
+ return instance.t.bind(instance);
50
+ },
20
51
  });
@@ -1,34 +1,34 @@
1
1
  export declare function createEssentialsMockFactory(mockFn: any): {
2
2
  i18n: {
3
- getLanguage: any;
4
- getLocale: any;
3
+ getLanguage: Function | undefined;
4
+ getLocale: Function | undefined;
5
5
  };
6
6
  bi: {
7
- getLogger: any;
7
+ getLogger: Function | undefined;
8
8
  };
9
9
  monitoring: {
10
- getMonitoringClient: any;
10
+ getMonitoringClient: Function | undefined;
11
11
  };
12
12
  experiments: {
13
- enabled: any;
14
- get: any;
13
+ enabled: Function | undefined;
14
+ get: Function | undefined;
15
15
  };
16
16
  settings: {
17
- getTimezone: any;
17
+ getTimezone: Function | undefined;
18
18
  };
19
19
  auth: {
20
- elevate: any;
21
- getContextualAuth: any;
22
- getTokenInfo: any;
20
+ elevate: Function | undefined;
21
+ getContextualAuth: Function | undefined;
22
+ getTokenInfo: Function | undefined;
23
23
  };
24
24
  httpClient: {
25
- fetchWithAuth: any;
26
- graphql: any;
25
+ fetchWithAuth: Function | undefined;
26
+ graphql: Function | undefined;
27
27
  };
28
28
  errorHandler: {
29
- withErrorHandler: any;
30
- getResolvedError: any;
31
- showError: any;
32
- reportRetryAttempt: any;
29
+ withErrorHandler: Function | undefined;
30
+ getResolvedError: Function | undefined;
31
+ showError: Function | undefined;
32
+ reportRetryAttempt: Function | undefined;
33
33
  };
34
34
  };
@@ -2,47 +2,54 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createEssentialsMockFactory = createEssentialsMockFactory;
4
4
  function createEssentialsMockFactory(mockFn) {
5
+ const mockFns = new Map();
6
+ const createUniqueFn = (name, implementation) => {
7
+ if (!mockFns.has(name)) {
8
+ mockFns.set(name, mockFn(implementation));
9
+ }
10
+ return mockFns.get(name);
11
+ };
5
12
  return {
6
13
  i18n: {
7
- getLanguage: mockFn(() => 'en'),
8
- getLocale: mockFn(() => 'en-US'),
14
+ getLanguage: createUniqueFn('i18n.getLanguage', () => 'en'),
15
+ getLocale: createUniqueFn('i18n.getLocale', () => 'en-US'),
9
16
  },
10
17
  bi: {
11
- getLogger: mockFn(() => ({
12
- log: mockFn(),
13
- report: mockFn(),
14
- flush: mockFn(),
15
- updateDefaults: mockFn((params) => ({
16
- log: mockFn(),
17
- report: mockFn(),
18
- flush: mockFn(),
19
- updateDefaults: mockFn(),
18
+ getLogger: createUniqueFn('bi.getLogger', () => ({
19
+ log: createUniqueFn('bi.getLogger.log'),
20
+ report: createUniqueFn('bi.getLogger.report'),
21
+ flush: createUniqueFn('bi.getLogger.flush'),
22
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults', (params) => ({
23
+ log: createUniqueFn('bi.getLogger.updateDefaults.log'),
24
+ report: createUniqueFn('bi.getLogger.updateDefaults.report'),
25
+ flush: createUniqueFn('bi.getLogger.updateDefaults.flush'),
26
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults.updateDefaults'),
20
27
  })),
21
28
  })),
22
29
  },
23
30
  monitoring: {
24
- getMonitoringClient: mockFn(() => ({
25
- captureException: mockFn(),
26
- captureMessage: mockFn(),
27
- setContext: mockFn(),
28
- setTag: mockFn(),
29
- setUser: mockFn(),
30
- addBreadcrumb: mockFn(),
31
+ getMonitoringClient: createUniqueFn('monitoring.getMonitoringClient', () => ({
32
+ captureException: createUniqueFn('monitoring.getMonitoringClient.captureException'),
33
+ captureMessage: createUniqueFn('monitoring.getMonitoringClient.captureMessage'),
34
+ setContext: createUniqueFn('monitoring.getMonitoringClient.setContext'),
35
+ setTag: createUniqueFn('monitoring.getMonitoringClient.setTag'),
36
+ setUser: createUniqueFn('monitoring.getMonitoringClient.setUser'),
37
+ addBreadcrumb: createUniqueFn('monitoring.getMonitoringClient.addBreadcrumb'),
31
38
  })),
32
39
  },
33
40
  experiments: {
34
- enabled: mockFn(() => false),
35
- get: mockFn(() => null),
41
+ enabled: createUniqueFn('experiments.enabled', () => false),
42
+ get: createUniqueFn('experiments.get', () => null),
36
43
  },
37
44
  settings: {
38
- getTimezone: mockFn(() => 'UTC'),
45
+ getTimezone: createUniqueFn('settings.getTimezone', () => 'UTC'),
39
46
  },
40
47
  auth: {
41
- elevate: mockFn((restModule) => restModule),
42
- getContextualAuth: mockFn(() => ({
48
+ elevate: createUniqueFn('auth.elevate', (restModule) => restModule),
49
+ getContextualAuth: createUniqueFn('auth.getContextualAuth', () => ({
43
50
  headers: {},
44
51
  })),
45
- getTokenInfo: mockFn(() => Promise.resolve({
52
+ getTokenInfo: createUniqueFn('auth.getTokenInfo', () => Promise.resolve({
46
53
  active: true,
47
54
  subjectType: 'USER',
48
55
  subjectId: 'mock-user-id',
@@ -54,21 +61,21 @@ function createEssentialsMockFactory(mockFn) {
54
61
  })),
55
62
  },
56
63
  httpClient: {
57
- fetchWithAuth: mockFn(() => global.fetch || mockFn()),
58
- graphql: mockFn(() => Promise.resolve({
64
+ fetchWithAuth: createUniqueFn('httpClient.fetchWithAuth', () => global.fetch || mockFn()),
65
+ graphql: createUniqueFn('httpClient.graphql', () => Promise.resolve({
59
66
  data: {},
60
67
  errors: [],
61
68
  })),
62
69
  },
63
70
  errorHandler: {
64
- withErrorHandler: mockFn((fn) => fn()),
65
- getResolvedError: mockFn(() => ({
71
+ withErrorHandler: createUniqueFn('errorHandler.withErrorHandler', (fn) => fn()),
72
+ getResolvedError: createUniqueFn('errorHandler.getResolvedError', () => ({
66
73
  code: 'UNKNOWN_ERROR',
67
74
  message: 'Mock error',
68
75
  details: {},
69
76
  })),
70
- showError: mockFn(),
71
- reportRetryAttempt: mockFn(),
77
+ showError: createUniqueFn('errorHandler.showError'),
78
+ reportRetryAttempt: createUniqueFn('errorHandler.reportRetryAttempt'),
72
79
  },
73
80
  };
74
81
  }
@@ -1 +1 @@
1
- {"root":["../src/auth.ts","../src/bi.ts","../src/experiments.ts","../src/http-client.ts","../src/index.ts","../src/mock-factory.ts","../src/monitoring.ts","../src/settings.ts","../src/error-handler/createErrorHandler.ts","../src/error-handler/error-handler.ts","../src/i18n/createI18n.ts","../src/i18n/i18n.ts"],"version":"5.9.2"}
1
+ {"root":["../src/auth.ts","../src/bi.ts","../src/experiments.ts","../src/http-client.ts","../src/index.ts","../src/mock-factory.ts","../src/monitoring.ts","../src/settings.ts","../src/error-handler/createErrorHandler.ts","../src/error-handler/error-handler.ts","../src/i18n/createI18n.ts","../src/i18n/i18n.ts"],"version":"5.9.3"}
@@ -2,5 +2,5 @@ import { Host } from '@wix/sdk-types';
2
2
  interface Messages {
3
3
  [key: string]: string;
4
4
  }
5
- export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
6
  export {};
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,4 +1,11 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import i18next from 'i18next';
3
+ import { initReactI18next } from 'react-i18next';
4
+ import { multilingual } from '@wix/site';
5
+ function getPrimaryLanguage(fallback) {
6
+ return (multilingual.listSupportedLanguages().find((language) => language.primary)
7
+ ?.id ?? fallback);
8
+ }
2
9
  export const i18n = createHostModule({
3
10
  getLanguage: (host) => () => {
4
11
  const language = host.essentials?.language;
@@ -14,4 +21,25 @@ export const i18n = createHostModule({
14
21
  }
15
22
  return locale;
16
23
  },
24
+ useTranslation: (host) => () => {
25
+ const resources = host.translations?.();
26
+ if (!resources) {
27
+ throw new Error('Host translation resources are not available.');
28
+ }
29
+ const instance = i18next.createInstance();
30
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
31
+ const initOptions = {
32
+ lng: host.essentials?.language || mainLanguage,
33
+ fallbackLng: mainLanguage,
34
+ resources,
35
+ };
36
+ // Init is asynchronous, but when provided with a synchronous resource,
37
+ // it inits immediately and the callback ensures synchronous completion.
38
+ void instance.use(initReactI18next).init(initOptions, (err) => {
39
+ if (err) {
40
+ throw err;
41
+ }
42
+ });
43
+ return instance.t.bind(instance);
44
+ },
17
45
  });
@@ -1,34 +1,34 @@
1
1
  export declare function createEssentialsMockFactory(mockFn: any): {
2
2
  i18n: {
3
- getLanguage: any;
4
- getLocale: any;
3
+ getLanguage: Function | undefined;
4
+ getLocale: Function | undefined;
5
5
  };
6
6
  bi: {
7
- getLogger: any;
7
+ getLogger: Function | undefined;
8
8
  };
9
9
  monitoring: {
10
- getMonitoringClient: any;
10
+ getMonitoringClient: Function | undefined;
11
11
  };
12
12
  experiments: {
13
- enabled: any;
14
- get: any;
13
+ enabled: Function | undefined;
14
+ get: Function | undefined;
15
15
  };
16
16
  settings: {
17
- getTimezone: any;
17
+ getTimezone: Function | undefined;
18
18
  };
19
19
  auth: {
20
- elevate: any;
21
- getContextualAuth: any;
22
- getTokenInfo: any;
20
+ elevate: Function | undefined;
21
+ getContextualAuth: Function | undefined;
22
+ getTokenInfo: Function | undefined;
23
23
  };
24
24
  httpClient: {
25
- fetchWithAuth: any;
26
- graphql: any;
25
+ fetchWithAuth: Function | undefined;
26
+ graphql: Function | undefined;
27
27
  };
28
28
  errorHandler: {
29
- withErrorHandler: any;
30
- getResolvedError: any;
31
- showError: any;
32
- reportRetryAttempt: any;
29
+ withErrorHandler: Function | undefined;
30
+ getResolvedError: Function | undefined;
31
+ showError: Function | undefined;
32
+ reportRetryAttempt: Function | undefined;
33
33
  };
34
34
  };
@@ -1,45 +1,52 @@
1
1
  export function createEssentialsMockFactory(mockFn) {
2
+ const mockFns = new Map();
3
+ const createUniqueFn = (name, implementation) => {
4
+ if (!mockFns.has(name)) {
5
+ mockFns.set(name, mockFn(implementation));
6
+ }
7
+ return mockFns.get(name);
8
+ };
2
9
  return {
3
10
  i18n: {
4
- getLanguage: mockFn(() => 'en'),
5
- getLocale: mockFn(() => 'en-US'),
11
+ getLanguage: createUniqueFn('i18n.getLanguage', () => 'en'),
12
+ getLocale: createUniqueFn('i18n.getLocale', () => 'en-US'),
6
13
  },
7
14
  bi: {
8
- getLogger: mockFn(() => ({
9
- log: mockFn(),
10
- report: mockFn(),
11
- flush: mockFn(),
12
- updateDefaults: mockFn((params) => ({
13
- log: mockFn(),
14
- report: mockFn(),
15
- flush: mockFn(),
16
- updateDefaults: mockFn(),
15
+ getLogger: createUniqueFn('bi.getLogger', () => ({
16
+ log: createUniqueFn('bi.getLogger.log'),
17
+ report: createUniqueFn('bi.getLogger.report'),
18
+ flush: createUniqueFn('bi.getLogger.flush'),
19
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults', (params) => ({
20
+ log: createUniqueFn('bi.getLogger.updateDefaults.log'),
21
+ report: createUniqueFn('bi.getLogger.updateDefaults.report'),
22
+ flush: createUniqueFn('bi.getLogger.updateDefaults.flush'),
23
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults.updateDefaults'),
17
24
  })),
18
25
  })),
19
26
  },
20
27
  monitoring: {
21
- getMonitoringClient: mockFn(() => ({
22
- captureException: mockFn(),
23
- captureMessage: mockFn(),
24
- setContext: mockFn(),
25
- setTag: mockFn(),
26
- setUser: mockFn(),
27
- addBreadcrumb: mockFn(),
28
+ getMonitoringClient: createUniqueFn('monitoring.getMonitoringClient', () => ({
29
+ captureException: createUniqueFn('monitoring.getMonitoringClient.captureException'),
30
+ captureMessage: createUniqueFn('monitoring.getMonitoringClient.captureMessage'),
31
+ setContext: createUniqueFn('monitoring.getMonitoringClient.setContext'),
32
+ setTag: createUniqueFn('monitoring.getMonitoringClient.setTag'),
33
+ setUser: createUniqueFn('monitoring.getMonitoringClient.setUser'),
34
+ addBreadcrumb: createUniqueFn('monitoring.getMonitoringClient.addBreadcrumb'),
28
35
  })),
29
36
  },
30
37
  experiments: {
31
- enabled: mockFn(() => false),
32
- get: mockFn(() => null),
38
+ enabled: createUniqueFn('experiments.enabled', () => false),
39
+ get: createUniqueFn('experiments.get', () => null),
33
40
  },
34
41
  settings: {
35
- getTimezone: mockFn(() => 'UTC'),
42
+ getTimezone: createUniqueFn('settings.getTimezone', () => 'UTC'),
36
43
  },
37
44
  auth: {
38
- elevate: mockFn((restModule) => restModule),
39
- getContextualAuth: mockFn(() => ({
45
+ elevate: createUniqueFn('auth.elevate', (restModule) => restModule),
46
+ getContextualAuth: createUniqueFn('auth.getContextualAuth', () => ({
40
47
  headers: {},
41
48
  })),
42
- getTokenInfo: mockFn(() => Promise.resolve({
49
+ getTokenInfo: createUniqueFn('auth.getTokenInfo', () => Promise.resolve({
43
50
  active: true,
44
51
  subjectType: 'USER',
45
52
  subjectId: 'mock-user-id',
@@ -51,21 +58,21 @@ export function createEssentialsMockFactory(mockFn) {
51
58
  })),
52
59
  },
53
60
  httpClient: {
54
- fetchWithAuth: mockFn(() => global.fetch || mockFn()),
55
- graphql: mockFn(() => Promise.resolve({
61
+ fetchWithAuth: createUniqueFn('httpClient.fetchWithAuth', () => global.fetch || mockFn()),
62
+ graphql: createUniqueFn('httpClient.graphql', () => Promise.resolve({
56
63
  data: {},
57
64
  errors: [],
58
65
  })),
59
66
  },
60
67
  errorHandler: {
61
- withErrorHandler: mockFn((fn) => fn()),
62
- getResolvedError: mockFn(() => ({
68
+ withErrorHandler: createUniqueFn('errorHandler.withErrorHandler', (fn) => fn()),
69
+ getResolvedError: createUniqueFn('errorHandler.getResolvedError', () => ({
63
70
  code: 'UNKNOWN_ERROR',
64
71
  message: 'Mock error',
65
72
  details: {},
66
73
  })),
67
- showError: mockFn(),
68
- reportRetryAttempt: mockFn(),
74
+ showError: createUniqueFn('errorHandler.showError'),
75
+ reportRetryAttempt: createUniqueFn('errorHandler.reportRetryAttempt'),
69
76
  },
70
77
  };
71
78
  }
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,7 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.i18n = void 0;
4
7
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
8
+ const i18next_1 = __importDefault(require("i18next"));
9
+ const react_i18next_1 = require("react-i18next");
10
+ const site_1 = require("@wix/site");
11
+ function getPrimaryLanguage(fallback) {
12
+ return (site_1.multilingual.listSupportedLanguages().find((language) => language.primary)
13
+ ?.id ?? fallback);
14
+ }
5
15
  exports.i18n = (0, host_modules_1.createHostModule)({
6
16
  getLanguage: (host) => () => {
7
17
  const language = host.essentials?.language;
@@ -17,4 +27,25 @@ exports.i18n = (0, host_modules_1.createHostModule)({
17
27
  }
18
28
  return locale;
19
29
  },
30
+ useTranslation: (host) => () => {
31
+ const resources = host.translations?.();
32
+ if (!resources) {
33
+ throw new Error('Host translation resources are not available.');
34
+ }
35
+ const instance = i18next_1.default.createInstance();
36
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
37
+ const initOptions = {
38
+ lng: host.essentials?.language || mainLanguage,
39
+ fallbackLng: mainLanguage,
40
+ resources,
41
+ };
42
+ // Init is asynchronous, but when provided with a synchronous resource,
43
+ // it inits immediately and the callback ensures synchronous completion.
44
+ void instance.use(react_i18next_1.initReactI18next).init(initOptions, (err) => {
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ });
49
+ return instance.t.bind(instance);
50
+ },
20
51
  });
@@ -1,34 +1,34 @@
1
1
  export declare function createEssentialsMockFactory(mockFn: any): {
2
2
  i18n: {
3
- getLanguage: any;
4
- getLocale: any;
3
+ getLanguage: Function | undefined;
4
+ getLocale: Function | undefined;
5
5
  };
6
6
  bi: {
7
- getLogger: any;
7
+ getLogger: Function | undefined;
8
8
  };
9
9
  monitoring: {
10
- getMonitoringClient: any;
10
+ getMonitoringClient: Function | undefined;
11
11
  };
12
12
  experiments: {
13
- enabled: any;
14
- get: any;
13
+ enabled: Function | undefined;
14
+ get: Function | undefined;
15
15
  };
16
16
  settings: {
17
- getTimezone: any;
17
+ getTimezone: Function | undefined;
18
18
  };
19
19
  auth: {
20
- elevate: any;
21
- getContextualAuth: any;
22
- getTokenInfo: any;
20
+ elevate: Function | undefined;
21
+ getContextualAuth: Function | undefined;
22
+ getTokenInfo: Function | undefined;
23
23
  };
24
24
  httpClient: {
25
- fetchWithAuth: any;
26
- graphql: any;
25
+ fetchWithAuth: Function | undefined;
26
+ graphql: Function | undefined;
27
27
  };
28
28
  errorHandler: {
29
- withErrorHandler: any;
30
- getResolvedError: any;
31
- showError: any;
32
- reportRetryAttempt: any;
29
+ withErrorHandler: Function | undefined;
30
+ getResolvedError: Function | undefined;
31
+ showError: Function | undefined;
32
+ reportRetryAttempt: Function | undefined;
33
33
  };
34
34
  };
@@ -2,47 +2,54 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createEssentialsMockFactory = createEssentialsMockFactory;
4
4
  function createEssentialsMockFactory(mockFn) {
5
+ const mockFns = new Map();
6
+ const createUniqueFn = (name, implementation) => {
7
+ if (!mockFns.has(name)) {
8
+ mockFns.set(name, mockFn(implementation));
9
+ }
10
+ return mockFns.get(name);
11
+ };
5
12
  return {
6
13
  i18n: {
7
- getLanguage: mockFn(() => 'en'),
8
- getLocale: mockFn(() => 'en-US'),
14
+ getLanguage: createUniqueFn('i18n.getLanguage', () => 'en'),
15
+ getLocale: createUniqueFn('i18n.getLocale', () => 'en-US'),
9
16
  },
10
17
  bi: {
11
- getLogger: mockFn(() => ({
12
- log: mockFn(),
13
- report: mockFn(),
14
- flush: mockFn(),
15
- updateDefaults: mockFn((params) => ({
16
- log: mockFn(),
17
- report: mockFn(),
18
- flush: mockFn(),
19
- updateDefaults: mockFn(),
18
+ getLogger: createUniqueFn('bi.getLogger', () => ({
19
+ log: createUniqueFn('bi.getLogger.log'),
20
+ report: createUniqueFn('bi.getLogger.report'),
21
+ flush: createUniqueFn('bi.getLogger.flush'),
22
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults', (params) => ({
23
+ log: createUniqueFn('bi.getLogger.updateDefaults.log'),
24
+ report: createUniqueFn('bi.getLogger.updateDefaults.report'),
25
+ flush: createUniqueFn('bi.getLogger.updateDefaults.flush'),
26
+ updateDefaults: createUniqueFn('bi.getLogger.updateDefaults.updateDefaults'),
20
27
  })),
21
28
  })),
22
29
  },
23
30
  monitoring: {
24
- getMonitoringClient: mockFn(() => ({
25
- captureException: mockFn(),
26
- captureMessage: mockFn(),
27
- setContext: mockFn(),
28
- setTag: mockFn(),
29
- setUser: mockFn(),
30
- addBreadcrumb: mockFn(),
31
+ getMonitoringClient: createUniqueFn('monitoring.getMonitoringClient', () => ({
32
+ captureException: createUniqueFn('monitoring.getMonitoringClient.captureException'),
33
+ captureMessage: createUniqueFn('monitoring.getMonitoringClient.captureMessage'),
34
+ setContext: createUniqueFn('monitoring.getMonitoringClient.setContext'),
35
+ setTag: createUniqueFn('monitoring.getMonitoringClient.setTag'),
36
+ setUser: createUniqueFn('monitoring.getMonitoringClient.setUser'),
37
+ addBreadcrumb: createUniqueFn('monitoring.getMonitoringClient.addBreadcrumb'),
31
38
  })),
32
39
  },
33
40
  experiments: {
34
- enabled: mockFn(() => false),
35
- get: mockFn(() => null),
41
+ enabled: createUniqueFn('experiments.enabled', () => false),
42
+ get: createUniqueFn('experiments.get', () => null),
36
43
  },
37
44
  settings: {
38
- getTimezone: mockFn(() => 'UTC'),
45
+ getTimezone: createUniqueFn('settings.getTimezone', () => 'UTC'),
39
46
  },
40
47
  auth: {
41
- elevate: mockFn((restModule) => restModule),
42
- getContextualAuth: mockFn(() => ({
48
+ elevate: createUniqueFn('auth.elevate', (restModule) => restModule),
49
+ getContextualAuth: createUniqueFn('auth.getContextualAuth', () => ({
43
50
  headers: {},
44
51
  })),
45
- getTokenInfo: mockFn(() => Promise.resolve({
52
+ getTokenInfo: createUniqueFn('auth.getTokenInfo', () => Promise.resolve({
46
53
  active: true,
47
54
  subjectType: 'USER',
48
55
  subjectId: 'mock-user-id',
@@ -54,21 +61,21 @@ function createEssentialsMockFactory(mockFn) {
54
61
  })),
55
62
  },
56
63
  httpClient: {
57
- fetchWithAuth: mockFn(() => global.fetch || mockFn()),
58
- graphql: mockFn(() => Promise.resolve({
64
+ fetchWithAuth: createUniqueFn('httpClient.fetchWithAuth', () => global.fetch || mockFn()),
65
+ graphql: createUniqueFn('httpClient.graphql', () => Promise.resolve({
59
66
  data: {},
60
67
  errors: [],
61
68
  })),
62
69
  },
63
70
  errorHandler: {
64
- withErrorHandler: mockFn((fn) => fn()),
65
- getResolvedError: mockFn(() => ({
71
+ withErrorHandler: createUniqueFn('errorHandler.withErrorHandler', (fn) => fn()),
72
+ getResolvedError: createUniqueFn('errorHandler.getResolvedError', () => ({
66
73
  code: 'UNKNOWN_ERROR',
67
74
  message: 'Mock error',
68
75
  details: {},
69
76
  })),
70
- showError: mockFn(),
71
- reportRetryAttempt: mockFn(),
77
+ showError: createUniqueFn('errorHandler.showError'),
78
+ reportRetryAttempt: createUniqueFn('errorHandler.reportRetryAttempt'),
72
79
  },
73
80
  };
74
81
  }
@@ -1 +1 @@
1
- {"root":["../../src/auth.ts","../../src/bi.ts","../../src/experiments.ts","../../src/http-client.ts","../../src/index.ts","../../src/mock-factory.ts","../../src/monitoring.ts","../../src/settings.ts","../../src/error-handler/createErrorHandler.ts","../../src/error-handler/error-handler.ts","../../src/i18n/createI18n.ts","../../src/i18n/i18n.ts"],"version":"5.9.2"}
1
+ {"root":["../../src/auth.ts","../../src/bi.ts","../../src/experiments.ts","../../src/http-client.ts","../../src/index.ts","../../src/mock-factory.ts","../../src/monitoring.ts","../../src/settings.ts","../../src/error-handler/createErrorHandler.ts","../../src/error-handler/error-handler.ts","../../src/i18n/createI18n.ts","../../src/i18n/i18n.ts"],"version":"5.9.3"}
@@ -1 +1 @@
1
- {"root":["../src/auth.ts","../src/bi.ts","../src/experiments.ts","../src/http-client.ts","../src/index.ts","../src/mock-factory.ts","../src/monitoring.ts","../src/settings.ts","../src/error-handler/createErrorHandler.ts","../src/error-handler/error-handler.ts","../src/i18n/createI18n.ts","../src/i18n/i18n.ts"],"version":"5.9.2"}
1
+ {"root":["../src/auth.ts","../src/bi.ts","../src/experiments.ts","../src/http-client.ts","../src/index.ts","../src/mock-factory.ts","../src/monitoring.ts","../src/settings.ts","../src/error-handler/createErrorHandler.ts","../src/error-handler/error-handler.ts","../src/i18n/createI18n.ts","../src/i18n/i18n.ts"],"version":"5.9.3"}
package/package.json CHANGED
@@ -1,10 +1,35 @@
1
1
  {
2
2
  "name": "@wix/essentials",
3
- "version": "0.1.28",
4
- "license": "MIT",
5
- "main": "cjs/build/index.js",
6
- "module": "build/index.mjs",
7
- "type": "module",
3
+ "version": "1.0.1",
4
+ "dependencies": {
5
+ "@wix/error-handler": "^1.67.0",
6
+ "@wix/monitoring": "^0.21.0",
7
+ "@wix/sdk-runtime": "1.0.3",
8
+ "@wix/sdk-types": "1.17.1",
9
+ "@wix/site": "^1.37.0",
10
+ "i18next": "^25.6.3",
11
+ "i18next-icu": "^2.4.1",
12
+ "intl-messageformat": "^10.7.18",
13
+ "react-i18next": "^16.1.2"
14
+ },
15
+ "devDependencies": {
16
+ "@types/is-ci": "^3.0.4",
17
+ "@types/node": "^20.19.25",
18
+ "@vitest/ui": "^1.6.1",
19
+ "@wix/sdk": "1.21.1",
20
+ "eslint": "^8.57.1",
21
+ "eslint-config-sdk": "1.0.0",
22
+ "graphql": "^16.8.0",
23
+ "is-ci": "^3.0.1",
24
+ "jsdom": "^22.1.0",
25
+ "msw": "^2.12.2",
26
+ "typescript": "^5.9.3",
27
+ "vitest": "^1.6.1",
28
+ "vitest-teamcity-reporter": "^0.3.1"
29
+ },
30
+ "eslintConfig": {
31
+ "extends": "sdk"
32
+ },
8
33
  "exports": {
9
34
  ".": {
10
35
  "import": "./build/index.js",
@@ -16,59 +41,36 @@
16
41
  },
17
42
  "./package.json": "./package.json"
18
43
  },
19
- "sideEffects": false,
20
- "keywords": [
21
- "wix-sdk-module=backend,page,public"
22
- ],
23
44
  "files": [
24
45
  "build",
25
46
  "cjs",
26
47
  "internal"
27
48
  ],
49
+ "keywords": [
50
+ "wix-sdk-module=backend,page,public"
51
+ ],
52
+ "license": "MIT",
53
+ "lint-staged": {
54
+ "*.{js,ts}": "yarn lint"
55
+ },
56
+ "main": "cjs/build/index.js",
57
+ "module": "build/index.mjs",
58
+ "optionalDependencies": {
59
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
60
+ },
28
61
  "publishConfig": {
29
62
  "registry": "https://registry.npmjs.org/",
30
63
  "access": "public"
31
64
  },
32
65
  "scripts": {
33
66
  "build": "tsc -b tsconfig.json tsconfig.cjs.json tsconfig.internal.json tsconfig.internal.cjs.json",
34
- "test": "vitest",
35
67
  "lint": "eslint --max-warnings=0 .",
36
68
  "lint:fix": "eslint --max-warnings=0 . --fix",
69
+ "test": "vitest",
37
70
  "typecheck": "tsc --noEmit"
38
71
  },
39
- "lint-staged": {
40
- "*.{js,ts}": "yarn lint"
41
- },
42
- "dependencies": {
43
- "@wix/error-handler": "^1.67.0",
44
- "@wix/monitoring": "^0.21.0",
45
- "@wix/sdk-runtime": "^0.3.62",
46
- "@wix/sdk-types": "^1.13.41",
47
- "i18next": "^25.3.2",
48
- "i18next-icu": "^2.3.0",
49
- "intl-messageformat": "^10.7.16"
50
- },
51
- "optionalDependencies": {
52
- "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
53
- },
54
- "devDependencies": {
55
- "@types/is-ci": "^3.0.4",
56
- "@types/node": "^20.19.10",
57
- "@vitest/ui": "^1.6.1",
58
- "@wix/sdk": "1.15.27",
59
- "eslint": "^8.57.1",
60
- "eslint-config-sdk": "0.0.0",
61
- "graphql": "^16.8.0",
62
- "is-ci": "^3.0.1",
63
- "jsdom": "^22.1.0",
64
- "msw": "^2.10.4",
65
- "typescript": "^5.9.2",
66
- "vitest": "^1.6.1",
67
- "vitest-teamcity-reporter": "^0.3.1"
68
- },
69
- "eslintConfig": {
70
- "extends": "sdk"
71
- },
72
+ "sideEffects": false,
73
+ "type": "module",
72
74
  "wix": {
73
75
  "artifact": {
74
76
  "groupId": "com.wixpress",
@@ -83,5 +85,5 @@
83
85
  ]
84
86
  }
85
87
  },
86
- "falconPackageHash": "52c1ad0cd86cd3dc571912ef73d7743c6a3afb566bc9cbc288af37f6"
88
+ "falconPackageHash": "7b256d0376ce1c1f5e87495b65f5d237327350612d600b2d03120ff5"
87
89
  }