@wix/essentials 0.1.25 → 0.1.27

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.
Files changed (52) hide show
  1. package/build/error-handler/createErrorHandler.d.ts +2 -0
  2. package/build/error-handler/createErrorHandler.js +20 -0
  3. package/build/{error-handler.d.ts → error-handler/error-handler.d.ts} +1 -1
  4. package/build/{error-handler.js → error-handler/error-handler.js} +7 -3
  5. package/build/i18n/createI18n.d.ts +6 -0
  6. package/build/i18n/createI18n.js +30 -0
  7. package/build/index.d.ts +2 -2
  8. package/build/index.js +2 -2
  9. package/build/mock-factory.d.ts +34 -0
  10. package/build/mock-factory.js +71 -0
  11. package/cjs/build/error-handler/createErrorHandler.d.ts +2 -0
  12. package/cjs/build/error-handler/createErrorHandler.js +23 -0
  13. package/cjs/build/{error-handler.d.ts → error-handler/error-handler.d.ts} +3 -3
  14. package/cjs/build/{error-handler.js → error-handler/error-handler.js} +7 -3
  15. package/cjs/build/i18n/createI18n.d.ts +6 -0
  16. package/cjs/build/i18n/createI18n.js +36 -0
  17. package/cjs/build/index.d.ts +2 -2
  18. package/cjs/build/index.js +2 -2
  19. package/cjs/build/mock-factory.d.ts +34 -0
  20. package/cjs/build/mock-factory.js +74 -0
  21. package/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  22. package/internal/build/error-handler/createErrorHandler.d.ts +2 -0
  23. package/internal/build/error-handler/createErrorHandler.js +23 -0
  24. package/internal/{cjs/build → build/error-handler}/error-handler.d.ts +1 -1
  25. package/internal/build/{error-handler.js → error-handler/error-handler.js} +7 -3
  26. package/internal/build/i18n/createI18n.d.ts +6 -0
  27. package/internal/build/i18n/createI18n.js +36 -0
  28. package/internal/build/index.d.ts +2 -2
  29. package/internal/build/index.js +2 -2
  30. package/internal/build/mock-factory.d.ts +34 -0
  31. package/internal/build/mock-factory.js +74 -0
  32. package/internal/cjs/build/error-handler/createErrorHandler.d.ts +2 -0
  33. package/internal/cjs/build/error-handler/createErrorHandler.js +20 -0
  34. package/internal/{build → cjs/build/error-handler}/error-handler.d.ts +3 -3
  35. package/internal/cjs/build/{error-handler.js → error-handler/error-handler.js} +7 -3
  36. package/internal/cjs/build/i18n/createI18n.d.ts +6 -0
  37. package/internal/cjs/build/i18n/createI18n.js +30 -0
  38. package/internal/cjs/build/index.d.ts +2 -2
  39. package/internal/cjs/build/index.js +2 -2
  40. package/internal/cjs/build/mock-factory.d.ts +34 -0
  41. package/internal/cjs/build/mock-factory.js +71 -0
  42. package/internal/cjs/tsconfig.internal.tsbuildinfo +1 -1
  43. package/internal/tsconfig.internal.cjs.tsbuildinfo +1 -1
  44. package/package.json +15 -7
  45. /package/build/{i18n.d.ts → i18n/i18n.d.ts} +0 -0
  46. /package/build/{i18n.js → i18n/i18n.js} +0 -0
  47. /package/cjs/build/{i18n.d.ts → i18n/i18n.d.ts} +0 -0
  48. /package/cjs/build/{i18n.js → i18n/i18n.js} +0 -0
  49. /package/internal/build/{i18n.d.ts → i18n/i18n.d.ts} +0 -0
  50. /package/internal/build/{i18n.js → i18n/i18n.js} +0 -0
  51. /package/internal/cjs/build/{i18n.d.ts → i18n/i18n.d.ts} +0 -0
  52. /package/internal/cjs/build/{i18n.js → i18n/i18n.js} +0 -0
@@ -0,0 +1,2 @@
1
+ import type { Host } from '@wix/sdk-types';
2
+ export declare function createErrorHandler(host: Host): import("@wix/error-handler/dist/types/error-handler/ErrorHandlerV2.js").ErrorHandlerV2;
@@ -0,0 +1,20 @@
1
+ import { ErrorHandler } from '@wix/error-handler';
2
+ import { createI18n } from '../i18n/createI18n.js';
3
+ export function createErrorHandler(host) {
4
+ return new ErrorHandler({
5
+ environment: {
6
+ artifactId: 'sdk-essentials',
7
+ },
8
+ biDefaultsOverrides: {
9
+ hostingEnvironment: host.name,
10
+ },
11
+ biLoggerOrFactory: {
12
+ async report() { },
13
+ },
14
+ showError: host.showError,
15
+ createErrorMonitor: () => ({
16
+ captureException() { },
17
+ }),
18
+ createI18n: ({ asyncMessagesLoader }) => createI18n(host, asyncMessagesLoader),
19
+ }).v2;
20
+ }
@@ -1,4 +1,4 @@
1
- import type { ResolvedError, ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps } from '@wix/sdk-types';
1
+ import type { ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, ResolvedError } from '@wix/sdk-types';
2
2
  export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2/ErrorHandlerPublicAPI.js").WithErrorHandlerOptions) => Promise<T>;
3
3
  export declare const getResolvedError: <T>(error: unknown) => ResolvedError & Partial<T>;
4
4
  export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2/ShowError.js").ShowErrorOverrideProps> | null) => void;
@@ -1,9 +1,13 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import { createErrorHandler } from './createErrorHandler.js';
2
3
  const getFromHostErrorHandlerOrDefault = (fn) => (host) => {
3
- if (!host.getErrorHandler) {
4
- throw new Error('Error handler is not available. Make sure to include the error handler in the host.');
4
+ if (host.getErrorHandler) {
5
+ return fn(host.getErrorHandler());
5
6
  }
6
- return fn(host.getErrorHandler());
7
+ const errorHandler = createErrorHandler(host);
8
+ // memoize the error handler on the host
9
+ host.getErrorHandler = () => errorHandler;
10
+ return fn(createErrorHandler(host));
7
11
  };
8
12
  const errorHandler = createHostModule({
9
13
  withErrorHandler: getFromHostErrorHandlerOrDefault((e) => e.withErrorHandler),
@@ -0,0 +1,6 @@
1
+ import { Host } from '@wix/sdk-types';
2
+ interface Messages {
3
+ [key: string]: string;
4
+ }
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
6
+ export {};
@@ -0,0 +1,30 @@
1
+ import i18n from 'i18next';
2
+ import ICU from 'i18next-icu';
3
+ export function createI18n(host, asyncMessagesLoader) {
4
+ const i18nInstance = i18n
5
+ .createInstance({
6
+ lng: host.essentials?.locale || 'en',
7
+ fallbackLng: 'en',
8
+ keySeparator: false,
9
+ })
10
+ .use(ICU);
11
+ if (asyncMessagesLoader) {
12
+ i18nInstance.use(customBackend(asyncMessagesLoader));
13
+ }
14
+ return i18nInstance;
15
+ }
16
+ function customBackend(asyncMessagesLoader) {
17
+ return {
18
+ type: 'backend',
19
+ init: () => { },
20
+ read: async (language, _namespace, callback) => {
21
+ try {
22
+ const translation = await asyncMessagesLoader(language);
23
+ return callback(null, translation);
24
+ }
25
+ catch (err) {
26
+ return callback(err, null);
27
+ }
28
+ },
29
+ };
30
+ }
package/build/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
package/build/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
@@ -0,0 +1,34 @@
1
+ export declare function createEssentialsMockFactory(mockFn: any): {
2
+ i18n: {
3
+ getLanguage: any;
4
+ getLocale: any;
5
+ };
6
+ bi: {
7
+ getLogger: any;
8
+ };
9
+ monitoring: {
10
+ getMonitoringClient: any;
11
+ };
12
+ experiments: {
13
+ enabled: any;
14
+ get: any;
15
+ };
16
+ settings: {
17
+ getTimezone: any;
18
+ };
19
+ auth: {
20
+ elevate: any;
21
+ getContextualAuth: any;
22
+ getTokenInfo: any;
23
+ };
24
+ httpClient: {
25
+ fetchWithAuth: any;
26
+ graphql: any;
27
+ };
28
+ errorHandler: {
29
+ withErrorHandler: any;
30
+ getResolvedError: any;
31
+ showError: any;
32
+ reportRetryAttempt: any;
33
+ };
34
+ };
@@ -0,0 +1,71 @@
1
+ export function createEssentialsMockFactory(mockFn) {
2
+ return {
3
+ i18n: {
4
+ getLanguage: mockFn(() => 'en'),
5
+ getLocale: mockFn(() => 'en-US'),
6
+ },
7
+ 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(),
17
+ })),
18
+ })),
19
+ },
20
+ monitoring: {
21
+ getMonitoringClient: mockFn(() => ({
22
+ captureException: mockFn(),
23
+ captureMessage: mockFn(),
24
+ setContext: mockFn(),
25
+ setTag: mockFn(),
26
+ setUser: mockFn(),
27
+ addBreadcrumb: mockFn(),
28
+ })),
29
+ },
30
+ experiments: {
31
+ enabled: mockFn(() => false),
32
+ get: mockFn(() => null),
33
+ },
34
+ settings: {
35
+ getTimezone: mockFn(() => 'UTC'),
36
+ },
37
+ auth: {
38
+ elevate: mockFn((restModule) => restModule),
39
+ getContextualAuth: mockFn(() => ({
40
+ headers: {},
41
+ })),
42
+ getTokenInfo: mockFn(() => Promise.resolve({
43
+ active: true,
44
+ subjectType: 'USER',
45
+ subjectId: 'mock-user-id',
46
+ exp: Date.now() + 3600000,
47
+ iat: Date.now(),
48
+ clientId: 'mock-client-id',
49
+ siteId: 'mock-site-id',
50
+ instanceId: 'mock-instance-id',
51
+ })),
52
+ },
53
+ httpClient: {
54
+ fetchWithAuth: mockFn(() => global.fetch || mockFn()),
55
+ graphql: mockFn(() => Promise.resolve({
56
+ data: {},
57
+ errors: [],
58
+ })),
59
+ },
60
+ errorHandler: {
61
+ withErrorHandler: mockFn((fn) => fn),
62
+ getResolvedError: mockFn(() => ({
63
+ code: 'UNKNOWN_ERROR',
64
+ message: 'Mock error',
65
+ details: {},
66
+ })),
67
+ showError: mockFn(),
68
+ reportRetryAttempt: mockFn(),
69
+ },
70
+ };
71
+ }
@@ -0,0 +1,2 @@
1
+ import type { Host } from '@wix/sdk-types';
2
+ export declare function createErrorHandler(host: Host): import("@wix/error-handler/dist/types/error-handler/ErrorHandlerV2.js").ErrorHandlerV2;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createErrorHandler = createErrorHandler;
4
+ const error_handler_1 = require("@wix/error-handler");
5
+ const createI18n_js_1 = require("../i18n/createI18n.js");
6
+ function createErrorHandler(host) {
7
+ return new error_handler_1.ErrorHandler({
8
+ environment: {
9
+ artifactId: 'sdk-essentials',
10
+ },
11
+ biDefaultsOverrides: {
12
+ hostingEnvironment: host.name,
13
+ },
14
+ biLoggerOrFactory: {
15
+ async report() { },
16
+ },
17
+ showError: host.showError,
18
+ createErrorMonitor: () => ({
19
+ captureException() { },
20
+ }),
21
+ createI18n: ({ asyncMessagesLoader }) => (0, createI18n_js_1.createI18n)(host, asyncMessagesLoader),
22
+ }).v2;
23
+ }
@@ -1,5 +1,5 @@
1
- import type { ResolvedError, ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps } from '@wix/sdk-types';
2
- export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2").WithErrorHandlerOptions) => Promise<T>;
1
+ import type { ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, ResolvedError } from '@wix/sdk-types';
2
+ export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2/ErrorHandlerPublicAPI.js").WithErrorHandlerOptions) => Promise<T>;
3
3
  export declare const getResolvedError: <T>(error: unknown) => ResolvedError & Partial<T>;
4
- export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2").ShowErrorOverrideProps> | null) => void;
4
+ export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2/ShowError.js").ShowErrorOverrideProps> | null) => void;
5
5
  export type { ErrorHandler, ResolvedError, ErrorCodesMap, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, };
@@ -2,11 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.reportRetryAttempt = exports.showError = exports.getResolvedError = exports.withErrorHandler = void 0;
4
4
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
5
+ const createErrorHandler_js_1 = require("./createErrorHandler.js");
5
6
  const getFromHostErrorHandlerOrDefault = (fn) => (host) => {
6
- if (!host.getErrorHandler) {
7
- throw new Error('Error handler is not available. Make sure to include the error handler in the host.');
7
+ if (host.getErrorHandler) {
8
+ return fn(host.getErrorHandler());
8
9
  }
9
- return fn(host.getErrorHandler());
10
+ const errorHandler = (0, createErrorHandler_js_1.createErrorHandler)(host);
11
+ // memoize the error handler on the host
12
+ host.getErrorHandler = () => errorHandler;
13
+ return fn((0, createErrorHandler_js_1.createErrorHandler)(host));
10
14
  };
11
15
  const errorHandler = (0, host_modules_1.createHostModule)({
12
16
  withErrorHandler: getFromHostErrorHandlerOrDefault((e) => e.withErrorHandler),
@@ -0,0 +1,6 @@
1
+ import { Host } from '@wix/sdk-types';
2
+ interface Messages {
3
+ [key: string]: string;
4
+ }
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createI18n = createI18n;
7
+ const i18next_1 = __importDefault(require("i18next"));
8
+ const i18next_icu_1 = __importDefault(require("i18next-icu"));
9
+ function createI18n(host, asyncMessagesLoader) {
10
+ const i18nInstance = i18next_1.default
11
+ .createInstance({
12
+ lng: host.essentials?.locale || 'en',
13
+ fallbackLng: 'en',
14
+ keySeparator: false,
15
+ })
16
+ .use(i18next_icu_1.default);
17
+ if (asyncMessagesLoader) {
18
+ i18nInstance.use(customBackend(asyncMessagesLoader));
19
+ }
20
+ return i18nInstance;
21
+ }
22
+ function customBackend(asyncMessagesLoader) {
23
+ return {
24
+ type: 'backend',
25
+ init: () => { },
26
+ read: async (language, _namespace, callback) => {
27
+ try {
28
+ const translation = await asyncMessagesLoader(language);
29
+ return callback(null, translation);
30
+ }
31
+ catch (err) {
32
+ return callback(err, null);
33
+ }
34
+ },
35
+ };
36
+ }
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
@@ -38,9 +38,9 @@ const auth = __importStar(require("./auth.js"));
38
38
  exports.auth = auth;
39
39
  const httpClient = __importStar(require("./http-client.js"));
40
40
  exports.httpClient = httpClient;
41
- const errorHandler = __importStar(require("./error-handler.js"));
41
+ const errorHandler = __importStar(require("./error-handler/error-handler.js"));
42
42
  exports.errorHandler = errorHandler;
43
- var i18n_js_1 = require("./i18n.js");
43
+ var i18n_js_1 = require("./i18n/i18n.js");
44
44
  Object.defineProperty(exports, "i18n", { enumerable: true, get: function () { return i18n_js_1.i18n; } });
45
45
  var monitoring_js_1 = require("./monitoring.js");
46
46
  Object.defineProperty(exports, "monitoring", { enumerable: true, get: function () { return monitoring_js_1.monitoring; } });
@@ -0,0 +1,34 @@
1
+ export declare function createEssentialsMockFactory(mockFn: any): {
2
+ i18n: {
3
+ getLanguage: any;
4
+ getLocale: any;
5
+ };
6
+ bi: {
7
+ getLogger: any;
8
+ };
9
+ monitoring: {
10
+ getMonitoringClient: any;
11
+ };
12
+ experiments: {
13
+ enabled: any;
14
+ get: any;
15
+ };
16
+ settings: {
17
+ getTimezone: any;
18
+ };
19
+ auth: {
20
+ elevate: any;
21
+ getContextualAuth: any;
22
+ getTokenInfo: any;
23
+ };
24
+ httpClient: {
25
+ fetchWithAuth: any;
26
+ graphql: any;
27
+ };
28
+ errorHandler: {
29
+ withErrorHandler: any;
30
+ getResolvedError: any;
31
+ showError: any;
32
+ reportRetryAttempt: any;
33
+ };
34
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEssentialsMockFactory = createEssentialsMockFactory;
4
+ function createEssentialsMockFactory(mockFn) {
5
+ return {
6
+ i18n: {
7
+ getLanguage: mockFn(() => 'en'),
8
+ getLocale: mockFn(() => 'en-US'),
9
+ },
10
+ 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(),
20
+ })),
21
+ })),
22
+ },
23
+ monitoring: {
24
+ getMonitoringClient: mockFn(() => ({
25
+ captureException: mockFn(),
26
+ captureMessage: mockFn(),
27
+ setContext: mockFn(),
28
+ setTag: mockFn(),
29
+ setUser: mockFn(),
30
+ addBreadcrumb: mockFn(),
31
+ })),
32
+ },
33
+ experiments: {
34
+ enabled: mockFn(() => false),
35
+ get: mockFn(() => null),
36
+ },
37
+ settings: {
38
+ getTimezone: mockFn(() => 'UTC'),
39
+ },
40
+ auth: {
41
+ elevate: mockFn((restModule) => restModule),
42
+ getContextualAuth: mockFn(() => ({
43
+ headers: {},
44
+ })),
45
+ getTokenInfo: mockFn(() => Promise.resolve({
46
+ active: true,
47
+ subjectType: 'USER',
48
+ subjectId: 'mock-user-id',
49
+ exp: Date.now() + 3600000,
50
+ iat: Date.now(),
51
+ clientId: 'mock-client-id',
52
+ siteId: 'mock-site-id',
53
+ instanceId: 'mock-instance-id',
54
+ })),
55
+ },
56
+ httpClient: {
57
+ fetchWithAuth: mockFn(() => global.fetch || mockFn()),
58
+ graphql: mockFn(() => Promise.resolve({
59
+ data: {},
60
+ errors: [],
61
+ })),
62
+ },
63
+ errorHandler: {
64
+ withErrorHandler: mockFn((fn) => fn),
65
+ getResolvedError: mockFn(() => ({
66
+ code: 'UNKNOWN_ERROR',
67
+ message: 'Mock error',
68
+ details: {},
69
+ })),
70
+ showError: mockFn(),
71
+ reportRetryAttempt: mockFn(),
72
+ },
73
+ };
74
+ }
@@ -1 +1 @@
1
- {"root":["../src/auth.ts","../src/bi.ts","../src/error-handler.ts","../src/experiments.ts","../src/http-client.ts","../src/i18n.ts","../src/index.ts","../src/monitoring.ts","../src/settings.ts"],"version":"5.8.3"}
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"}
@@ -0,0 +1,2 @@
1
+ import type { Host } from '@wix/sdk-types';
2
+ export declare function createErrorHandler(host: Host): import("@wix/error-handler/dist/types/error-handler/ErrorHandlerV2.js").ErrorHandlerV2;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createErrorHandler = createErrorHandler;
4
+ const error_handler_1 = require("@wix/error-handler");
5
+ const createI18n_js_1 = require("../i18n/createI18n.js");
6
+ function createErrorHandler(host) {
7
+ return new error_handler_1.ErrorHandler({
8
+ environment: {
9
+ artifactId: 'sdk-essentials',
10
+ },
11
+ biDefaultsOverrides: {
12
+ hostingEnvironment: host.name,
13
+ },
14
+ biLoggerOrFactory: {
15
+ async report() { },
16
+ },
17
+ showError: host.showError,
18
+ createErrorMonitor: () => ({
19
+ captureException() { },
20
+ }),
21
+ createI18n: ({ asyncMessagesLoader }) => (0, createI18n_js_1.createI18n)(host, asyncMessagesLoader),
22
+ }).v2;
23
+ }
@@ -1,4 +1,4 @@
1
- import type { ResolvedError, ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps } from '@wix/sdk-types';
1
+ import type { ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, ResolvedError } from '@wix/sdk-types';
2
2
  export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2/ErrorHandlerPublicAPI.js").WithErrorHandlerOptions) => Promise<T>;
3
3
  export declare const getResolvedError: <T>(error: unknown) => ResolvedError & Partial<T>;
4
4
  export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2/ShowError.js").ShowErrorOverrideProps> | null) => void;
@@ -2,11 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.reportRetryAttempt = exports.showError = exports.getResolvedError = exports.withErrorHandler = void 0;
4
4
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
5
+ const createErrorHandler_js_1 = require("./createErrorHandler.js");
5
6
  const getFromHostErrorHandlerOrDefault = (fn) => (host) => {
6
- if (!host.getErrorHandler) {
7
- throw new Error('Error handler is not available. Make sure to include the error handler in the host.');
7
+ if (host.getErrorHandler) {
8
+ return fn(host.getErrorHandler());
8
9
  }
9
- return fn(host.getErrorHandler());
10
+ const errorHandler = (0, createErrorHandler_js_1.createErrorHandler)(host);
11
+ // memoize the error handler on the host
12
+ host.getErrorHandler = () => errorHandler;
13
+ return fn((0, createErrorHandler_js_1.createErrorHandler)(host));
10
14
  };
11
15
  const errorHandler = (0, host_modules_1.createHostModule)({
12
16
  withErrorHandler: getFromHostErrorHandlerOrDefault((e) => e.withErrorHandler),
@@ -0,0 +1,6 @@
1
+ import { Host } from '@wix/sdk-types';
2
+ interface Messages {
3
+ [key: string]: string;
4
+ }
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createI18n = createI18n;
7
+ const i18next_1 = __importDefault(require("i18next"));
8
+ const i18next_icu_1 = __importDefault(require("i18next-icu"));
9
+ function createI18n(host, asyncMessagesLoader) {
10
+ const i18nInstance = i18next_1.default
11
+ .createInstance({
12
+ lng: host.essentials?.locale || 'en',
13
+ fallbackLng: 'en',
14
+ keySeparator: false,
15
+ })
16
+ .use(i18next_icu_1.default);
17
+ if (asyncMessagesLoader) {
18
+ i18nInstance.use(customBackend(asyncMessagesLoader));
19
+ }
20
+ return i18nInstance;
21
+ }
22
+ function customBackend(asyncMessagesLoader) {
23
+ return {
24
+ type: 'backend',
25
+ init: () => { },
26
+ read: async (language, _namespace, callback) => {
27
+ try {
28
+ const translation = await asyncMessagesLoader(language);
29
+ return callback(null, translation);
30
+ }
31
+ catch (err) {
32
+ return callback(err, null);
33
+ }
34
+ },
35
+ };
36
+ }
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
@@ -38,9 +38,9 @@ const auth = __importStar(require("./auth.js"));
38
38
  exports.auth = auth;
39
39
  const httpClient = __importStar(require("./http-client.js"));
40
40
  exports.httpClient = httpClient;
41
- const errorHandler = __importStar(require("./error-handler.js"));
41
+ const errorHandler = __importStar(require("./error-handler/error-handler.js"));
42
42
  exports.errorHandler = errorHandler;
43
- var i18n_js_1 = require("./i18n.js");
43
+ var i18n_js_1 = require("./i18n/i18n.js");
44
44
  Object.defineProperty(exports, "i18n", { enumerable: true, get: function () { return i18n_js_1.i18n; } });
45
45
  var monitoring_js_1 = require("./monitoring.js");
46
46
  Object.defineProperty(exports, "monitoring", { enumerable: true, get: function () { return monitoring_js_1.monitoring; } });
@@ -0,0 +1,34 @@
1
+ export declare function createEssentialsMockFactory(mockFn: any): {
2
+ i18n: {
3
+ getLanguage: any;
4
+ getLocale: any;
5
+ };
6
+ bi: {
7
+ getLogger: any;
8
+ };
9
+ monitoring: {
10
+ getMonitoringClient: any;
11
+ };
12
+ experiments: {
13
+ enabled: any;
14
+ get: any;
15
+ };
16
+ settings: {
17
+ getTimezone: any;
18
+ };
19
+ auth: {
20
+ elevate: any;
21
+ getContextualAuth: any;
22
+ getTokenInfo: any;
23
+ };
24
+ httpClient: {
25
+ fetchWithAuth: any;
26
+ graphql: any;
27
+ };
28
+ errorHandler: {
29
+ withErrorHandler: any;
30
+ getResolvedError: any;
31
+ showError: any;
32
+ reportRetryAttempt: any;
33
+ };
34
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEssentialsMockFactory = createEssentialsMockFactory;
4
+ function createEssentialsMockFactory(mockFn) {
5
+ return {
6
+ i18n: {
7
+ getLanguage: mockFn(() => 'en'),
8
+ getLocale: mockFn(() => 'en-US'),
9
+ },
10
+ 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(),
20
+ })),
21
+ })),
22
+ },
23
+ monitoring: {
24
+ getMonitoringClient: mockFn(() => ({
25
+ captureException: mockFn(),
26
+ captureMessage: mockFn(),
27
+ setContext: mockFn(),
28
+ setTag: mockFn(),
29
+ setUser: mockFn(),
30
+ addBreadcrumb: mockFn(),
31
+ })),
32
+ },
33
+ experiments: {
34
+ enabled: mockFn(() => false),
35
+ get: mockFn(() => null),
36
+ },
37
+ settings: {
38
+ getTimezone: mockFn(() => 'UTC'),
39
+ },
40
+ auth: {
41
+ elevate: mockFn((restModule) => restModule),
42
+ getContextualAuth: mockFn(() => ({
43
+ headers: {},
44
+ })),
45
+ getTokenInfo: mockFn(() => Promise.resolve({
46
+ active: true,
47
+ subjectType: 'USER',
48
+ subjectId: 'mock-user-id',
49
+ exp: Date.now() + 3600000,
50
+ iat: Date.now(),
51
+ clientId: 'mock-client-id',
52
+ siteId: 'mock-site-id',
53
+ instanceId: 'mock-instance-id',
54
+ })),
55
+ },
56
+ httpClient: {
57
+ fetchWithAuth: mockFn(() => global.fetch || mockFn()),
58
+ graphql: mockFn(() => Promise.resolve({
59
+ data: {},
60
+ errors: [],
61
+ })),
62
+ },
63
+ errorHandler: {
64
+ withErrorHandler: mockFn((fn) => fn),
65
+ getResolvedError: mockFn(() => ({
66
+ code: 'UNKNOWN_ERROR',
67
+ message: 'Mock error',
68
+ details: {},
69
+ })),
70
+ showError: mockFn(),
71
+ reportRetryAttempt: mockFn(),
72
+ },
73
+ };
74
+ }
@@ -0,0 +1,2 @@
1
+ import type { Host } from '@wix/sdk-types';
2
+ export declare function createErrorHandler(host: Host): import("@wix/error-handler/dist/types/error-handler/ErrorHandlerV2.js").ErrorHandlerV2;
@@ -0,0 +1,20 @@
1
+ import { ErrorHandler } from '@wix/error-handler';
2
+ import { createI18n } from '../i18n/createI18n.js';
3
+ export function createErrorHandler(host) {
4
+ return new ErrorHandler({
5
+ environment: {
6
+ artifactId: 'sdk-essentials',
7
+ },
8
+ biDefaultsOverrides: {
9
+ hostingEnvironment: host.name,
10
+ },
11
+ biLoggerOrFactory: {
12
+ async report() { },
13
+ },
14
+ showError: host.showError,
15
+ createErrorMonitor: () => ({
16
+ captureException() { },
17
+ }),
18
+ createI18n: ({ asyncMessagesLoader }) => createI18n(host, asyncMessagesLoader),
19
+ }).v2;
20
+ }
@@ -1,7 +1,7 @@
1
- import type { ResolvedError, ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps } from '@wix/sdk-types';
2
- export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2").WithErrorHandlerOptions) => Promise<T>;
1
+ import type { ErrorCodesMap, ErrorHandler, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, ResolvedError } from '@wix/sdk-types';
2
+ export declare const withErrorHandler: <T>(fn: () => Promise<T>, errorCodesMap: ErrorCodesMap<T>, options?: import("@wix/error-handler-types/dist/types/v2/ErrorHandlerPublicAPI.js").WithErrorHandlerOptions) => Promise<T>;
3
3
  export declare const getResolvedError: <T>(error: unknown) => ResolvedError & Partial<T>;
4
- export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2").ShowErrorOverrideProps> | null) => void;
4
+ export declare const showError: (error: unknown, props?: Partial<import("@wix/error-handler-types/dist/types/v2/ShowError.js").ShowErrorOverrideProps> | null) => void;
5
5
  /** @internal */
6
6
  export declare const reportRetryAttempt: (error: unknown) => void;
7
7
  export type { ErrorHandler, ResolvedError, ErrorCodesMap, StatusCodeMap, ShowErrorMapFunction, ShowErrorProps, };
@@ -1,9 +1,13 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import { createErrorHandler } from './createErrorHandler.js';
2
3
  const getFromHostErrorHandlerOrDefault = (fn) => (host) => {
3
- if (!host.getErrorHandler) {
4
- throw new Error('Error handler is not available. Make sure to include the error handler in the host.');
4
+ if (host.getErrorHandler) {
5
+ return fn(host.getErrorHandler());
5
6
  }
6
- return fn(host.getErrorHandler());
7
+ const errorHandler = createErrorHandler(host);
8
+ // memoize the error handler on the host
9
+ host.getErrorHandler = () => errorHandler;
10
+ return fn(createErrorHandler(host));
7
11
  };
8
12
  const errorHandler = createHostModule({
9
13
  withErrorHandler: getFromHostErrorHandlerOrDefault((e) => e.withErrorHandler),
@@ -0,0 +1,6 @@
1
+ import { Host } from '@wix/sdk-types';
2
+ interface Messages {
3
+ [key: string]: string;
4
+ }
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
6
+ export {};
@@ -0,0 +1,30 @@
1
+ import i18n from 'i18next';
2
+ import ICU from 'i18next-icu';
3
+ export function createI18n(host, asyncMessagesLoader) {
4
+ const i18nInstance = i18n
5
+ .createInstance({
6
+ lng: host.essentials?.locale || 'en',
7
+ fallbackLng: 'en',
8
+ keySeparator: false,
9
+ })
10
+ .use(ICU);
11
+ if (asyncMessagesLoader) {
12
+ i18nInstance.use(customBackend(asyncMessagesLoader));
13
+ }
14
+ return i18nInstance;
15
+ }
16
+ function customBackend(asyncMessagesLoader) {
17
+ return {
18
+ type: 'backend',
19
+ init: () => { },
20
+ read: async (language, _namespace, callback) => {
21
+ try {
22
+ const translation = await asyncMessagesLoader(language);
23
+ return callback(null, translation);
24
+ }
25
+ catch (err) {
26
+ return callback(err, null);
27
+ }
28
+ },
29
+ };
30
+ }
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
@@ -1,8 +1,8 @@
1
1
  import * as auth from './auth.js';
2
2
  import * as httpClient from './http-client.js';
3
- import * as errorHandler from './error-handler.js';
3
+ import * as errorHandler from './error-handler/error-handler.js';
4
4
  export { auth, httpClient, errorHandler };
5
- export { i18n } from './i18n.js';
5
+ export { i18n } from './i18n/i18n.js';
6
6
  export { monitoring } from './monitoring.js';
7
7
  export { bi } from './bi.js';
8
8
  export { experiments } from './experiments.js';
@@ -0,0 +1,34 @@
1
+ export declare function createEssentialsMockFactory(mockFn: any): {
2
+ i18n: {
3
+ getLanguage: any;
4
+ getLocale: any;
5
+ };
6
+ bi: {
7
+ getLogger: any;
8
+ };
9
+ monitoring: {
10
+ getMonitoringClient: any;
11
+ };
12
+ experiments: {
13
+ enabled: any;
14
+ get: any;
15
+ };
16
+ settings: {
17
+ getTimezone: any;
18
+ };
19
+ auth: {
20
+ elevate: any;
21
+ getContextualAuth: any;
22
+ getTokenInfo: any;
23
+ };
24
+ httpClient: {
25
+ fetchWithAuth: any;
26
+ graphql: any;
27
+ };
28
+ errorHandler: {
29
+ withErrorHandler: any;
30
+ getResolvedError: any;
31
+ showError: any;
32
+ reportRetryAttempt: any;
33
+ };
34
+ };
@@ -0,0 +1,71 @@
1
+ export function createEssentialsMockFactory(mockFn) {
2
+ return {
3
+ i18n: {
4
+ getLanguage: mockFn(() => 'en'),
5
+ getLocale: mockFn(() => 'en-US'),
6
+ },
7
+ 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(),
17
+ })),
18
+ })),
19
+ },
20
+ monitoring: {
21
+ getMonitoringClient: mockFn(() => ({
22
+ captureException: mockFn(),
23
+ captureMessage: mockFn(),
24
+ setContext: mockFn(),
25
+ setTag: mockFn(),
26
+ setUser: mockFn(),
27
+ addBreadcrumb: mockFn(),
28
+ })),
29
+ },
30
+ experiments: {
31
+ enabled: mockFn(() => false),
32
+ get: mockFn(() => null),
33
+ },
34
+ settings: {
35
+ getTimezone: mockFn(() => 'UTC'),
36
+ },
37
+ auth: {
38
+ elevate: mockFn((restModule) => restModule),
39
+ getContextualAuth: mockFn(() => ({
40
+ headers: {},
41
+ })),
42
+ getTokenInfo: mockFn(() => Promise.resolve({
43
+ active: true,
44
+ subjectType: 'USER',
45
+ subjectId: 'mock-user-id',
46
+ exp: Date.now() + 3600000,
47
+ iat: Date.now(),
48
+ clientId: 'mock-client-id',
49
+ siteId: 'mock-site-id',
50
+ instanceId: 'mock-instance-id',
51
+ })),
52
+ },
53
+ httpClient: {
54
+ fetchWithAuth: mockFn(() => global.fetch || mockFn()),
55
+ graphql: mockFn(() => Promise.resolve({
56
+ data: {},
57
+ errors: [],
58
+ })),
59
+ },
60
+ errorHandler: {
61
+ withErrorHandler: mockFn((fn) => fn),
62
+ getResolvedError: mockFn(() => ({
63
+ code: 'UNKNOWN_ERROR',
64
+ message: 'Mock error',
65
+ details: {},
66
+ })),
67
+ showError: mockFn(),
68
+ reportRetryAttempt: mockFn(),
69
+ },
70
+ };
71
+ }
@@ -1 +1 @@
1
- {"root":["../../src/auth.ts","../../src/bi.ts","../../src/error-handler.ts","../../src/experiments.ts","../../src/http-client.ts","../../src/i18n.ts","../../src/index.ts","../../src/monitoring.ts","../../src/settings.ts"],"version":"5.8.3"}
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 +1 @@
1
- {"root":["../src/auth.ts","../src/bi.ts","../src/error-handler.ts","../src/experiments.ts","../src/http-client.ts","../src/i18n.ts","../src/index.ts","../src/monitoring.ts","../src/settings.ts"],"version":"5.8.3"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/essentials",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "license": "MIT",
5
5
  "main": "cjs/build/index.js",
6
6
  "module": "build/index.mjs",
@@ -10,6 +10,10 @@
10
10
  "import": "./build/index.js",
11
11
  "require": "./cjs/build/index.js"
12
12
  },
13
+ "./mock": {
14
+ "import": "./build/mock-factory.js",
15
+ "require": "./cjs/build/mock-factory.js"
16
+ },
13
17
  "./package.json": "./package.json"
14
18
  },
15
19
  "sideEffects": false,
@@ -36,25 +40,29 @@
36
40
  "*.{js,ts}": "yarn lint"
37
41
  },
38
42
  "dependencies": {
43
+ "@wix/error-handler": "^1.67.0",
39
44
  "@wix/monitoring": "^0.21.0",
40
- "@wix/sdk-runtime": "^0.3.58",
41
- "@wix/sdk-types": "^1.13.37"
45
+ "@wix/sdk-runtime": "^0.3.61",
46
+ "@wix/sdk-types": "^1.13.40",
47
+ "i18next": "^25.3.2",
48
+ "i18next-icu": "^2.3.0",
49
+ "intl-messageformat": "^10.7.16"
42
50
  },
43
51
  "optionalDependencies": {
44
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"
45
53
  },
46
54
  "devDependencies": {
47
55
  "@types/is-ci": "^3.0.4",
48
- "@types/node": "^20.19.9",
56
+ "@types/node": "^20.19.10",
49
57
  "@vitest/ui": "^1.6.1",
50
- "@wix/sdk": "1.15.25",
58
+ "@wix/sdk": "1.15.26",
51
59
  "eslint": "^8.57.1",
52
60
  "eslint-config-sdk": "0.0.0",
53
61
  "graphql": "^16.8.0",
54
62
  "is-ci": "^3.0.1",
55
63
  "jsdom": "^22.1.0",
56
64
  "msw": "^2.10.4",
57
- "typescript": "^5.8.3",
65
+ "typescript": "^5.9.2",
58
66
  "vitest": "^1.6.1",
59
67
  "vitest-teamcity-reporter": "^0.3.1"
60
68
  },
@@ -75,5 +83,5 @@
75
83
  ]
76
84
  }
77
85
  },
78
- "falconPackageHash": "b5cd0deaa6ee134095f322e48ff0293d079ec71a855e9052ea572288"
86
+ "falconPackageHash": "a801c32a834f4fe35de7aaf398f7e2e478fb8c7a573dee31a5a3902d"
79
87
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes