@wix/sdk 1.7.14 → 1.8.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.
@@ -0,0 +1,3 @@
1
+ import { EventDefinition, EventHandler } from '@wix/sdk-types';
2
+ export declare const isEventHandlerModule: (val: any) => val is EventDefinition<unknown, string>;
3
+ export declare function buildEventDefinition<T extends EventDefinition<any, string>>(eventDefinition: T, registerHandler: (eventType: string, handler: EventHandler<T>) => void): (handler: EventHandler<T>) => void;
@@ -0,0 +1,7 @@
1
+ import { isObject } from './helpers.js';
2
+ export const isEventHandlerModule = (val) => isObject(val) && val.__type === 'event-definition';
3
+ export function buildEventDefinition(eventDefinition, registerHandler) {
4
+ return (handler) => {
5
+ registerHandler(eventDefinition.type, handler);
6
+ };
7
+ }
@@ -1,6 +1,6 @@
1
- import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildRESTFunction, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition, EventDefinition } from '@wix/sdk-types';
2
- import { EmptyObject } from 'type-fest/source/empty-object.js';
1
+ import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition } from '@wix/sdk-types';
3
2
  import { ConditionalExcept } from 'type-fest/source/conditional-except.js';
3
+ import { EmptyObject } from 'type-fest/source/empty-object.js';
4
4
  import { AmbassadorFunctionDescriptor, BuildAmbassadorFunction } from './ambassador-modules.js';
5
5
  import { PublicMetadata } from './common.js';
6
6
  import type { GraphQLFormattedError } from 'graphql';
@@ -11,7 +11,7 @@ type Headers = Record<string, string>;
11
11
  * Any non-descriptor properties are removed from the returned object, including descriptors that
12
12
  * do not match the given host (as they will not work with the given host).
13
13
  */
14
- export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
14
+ export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & BuildEventDefinitions<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
15
15
  type BuildRESTDescriptors<T extends Descriptors> = T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : ConditionalExcept<{
16
16
  [Key in keyof T]: T[Key] extends Descriptors ? BuildRESTDescriptors<T[Key]> : never;
17
17
  }, EmptyObject>;
@@ -21,6 +21,9 @@ type BuildAmbassadorDescriptors<T extends Descriptors> = T extends AmbassadorFun
21
21
  type BuildHostDescriptors<T extends Descriptors> = T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
22
22
  [Key in keyof T]: T[Key] extends Descriptors ? BuildHostDescriptors<T[Key]> : never;
23
23
  }, EmptyObject>;
24
+ type BuildEventDefinitions<T extends Descriptors> = T extends EventDefinition ? BuildEventDefinition<T> : ConditionalExcept<{
25
+ [Key in keyof T]: T[Key] extends Descriptors ? BuildEventDefinitions<T[Key]> : never;
26
+ }, EmptyObject>;
24
27
  /**
25
28
  * Descriptors are objects that describe the API of a module, and the module
26
29
  * can either be a REST module or a host module.
@@ -1,12 +1,14 @@
1
1
  import { EventDefinition, } from '@wix/sdk-types';
2
- import { toHTTPModule, isAmbassadorModule, ambassadorModuleOptions, } from './ambassador-modules.js';
2
+ import { ambassadorModuleOptions, isAmbassadorModule, toHTTPModule, } from './ambassador-modules.js';
3
3
  import { API_URL, PUBLIC_METADATA_KEY } from './common.js';
4
+ import { FetchErrorResponse } from './fetch-error.js';
4
5
  import { getDefaultContentHeader, isObject } from './helpers.js';
5
6
  import { buildHostModule, isHostModule } from './host-modules.js';
6
7
  import { buildRESTDescriptor } from './rest-modules.js';
7
- import { FetchErrorResponse } from './fetch-error.js';
8
+ import { buildEventDefinition, isEventHandlerModule, } from './event-handlers-modules.js';
8
9
  export function createClient(config) {
9
10
  const _headers = config.headers || { Authorization: '' };
11
+ const eventHandlers = new Map();
10
12
  const authStrategy = config.auth ||
11
13
  {
12
14
  getAuthHeaders: (_) => Promise.resolve({ headers: {} }),
@@ -30,7 +32,14 @@ export function createClient(config) {
30
32
  // on the WixClient, typescript starts failing with `Type instantiation is
31
33
  // excessively deep and possibly infinite.`
32
34
  const use = (modules, metadata) => {
33
- if (isHostModule(modules) && config.host) {
35
+ if (isEventHandlerModule(modules)) {
36
+ return buildEventDefinition(modules, (eventType, handler) => {
37
+ const handlers = eventHandlers.get(eventType) ?? [];
38
+ handlers.push(handler);
39
+ eventHandlers.set(eventType, handlers);
40
+ });
41
+ }
42
+ else if (isHostModule(modules) && config.host) {
34
43
  return buildHostModule(modules, config.host);
35
44
  }
36
45
  else if (typeof modules === 'function') {
@@ -124,12 +133,20 @@ export function createClient(config) {
124
133
  const eventType = parsedDecoded.eventType;
125
134
  const instanceId = parsedDecoded.instanceId;
126
135
  const payload = JSON.parse(parsedDecoded.data);
127
- if (opts.expectedEvents.length > 0 &&
128
- !opts.expectedEvents.some(({ type }) => type === eventType)) {
129
- throw new Error(`Unexpected event type: ${eventType}. Expected one of: ${opts.expectedEvents
136
+ const allExpectedEvents = [
137
+ ...opts.expectedEvents,
138
+ ...Array.from(eventHandlers.keys()).map((type) => ({ type })),
139
+ ];
140
+ if (allExpectedEvents.length > 0 &&
141
+ !allExpectedEvents.some(({ type }) => type === eventType)) {
142
+ throw new Error(`Unexpected event type: ${eventType}. Expected one of: ${allExpectedEvents
130
143
  .map((x) => x.type)
131
144
  .join(', ')}`);
132
145
  }
146
+ const handlers = eventHandlers.get(eventType) ?? [];
147
+ await Promise.all(handlers.map((handler) => handler(payload, {
148
+ instanceId,
149
+ })));
133
150
  return {
134
151
  instanceId,
135
152
  eventType,
@@ -0,0 +1,3 @@
1
+ import { EventDefinition, EventHandler } from '@wix/sdk-types';
2
+ export declare const isEventHandlerModule: (val: any) => val is EventDefinition<unknown, string>;
3
+ export declare function buildEventDefinition<T extends EventDefinition<any, string>>(eventDefinition: T, registerHandler: (eventType: string, handler: EventHandler<T>) => void): (handler: EventHandler<T>) => void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildEventDefinition = exports.isEventHandlerModule = void 0;
4
+ const helpers_js_1 = require("./helpers.js");
5
+ const isEventHandlerModule = (val) => (0, helpers_js_1.isObject)(val) && val.__type === 'event-definition';
6
+ exports.isEventHandlerModule = isEventHandlerModule;
7
+ function buildEventDefinition(eventDefinition, registerHandler) {
8
+ return (handler) => {
9
+ registerHandler(eventDefinition.type, handler);
10
+ };
11
+ }
12
+ exports.buildEventDefinition = buildEventDefinition;
@@ -1,6 +1,6 @@
1
- import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildRESTFunction, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition, EventDefinition } from '@wix/sdk-types';
2
- import { EmptyObject } from 'type-fest/source/empty-object.js';
1
+ import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition } from '@wix/sdk-types';
3
2
  import { ConditionalExcept } from 'type-fest/source/conditional-except.js';
3
+ import { EmptyObject } from 'type-fest/source/empty-object.js';
4
4
  import { AmbassadorFunctionDescriptor, BuildAmbassadorFunction } from './ambassador-modules.js';
5
5
  import { PublicMetadata } from './common.js';
6
6
  import type { GraphQLFormattedError } from 'graphql';
@@ -11,7 +11,7 @@ type Headers = Record<string, string>;
11
11
  * Any non-descriptor properties are removed from the returned object, including descriptors that
12
12
  * do not match the given host (as they will not work with the given host).
13
13
  */
14
- export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
14
+ export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & BuildEventDefinitions<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
15
15
  type BuildRESTDescriptors<T extends Descriptors> = T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : ConditionalExcept<{
16
16
  [Key in keyof T]: T[Key] extends Descriptors ? BuildRESTDescriptors<T[Key]> : never;
17
17
  }, EmptyObject>;
@@ -21,6 +21,9 @@ type BuildAmbassadorDescriptors<T extends Descriptors> = T extends AmbassadorFun
21
21
  type BuildHostDescriptors<T extends Descriptors> = T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
22
22
  [Key in keyof T]: T[Key] extends Descriptors ? BuildHostDescriptors<T[Key]> : never;
23
23
  }, EmptyObject>;
24
+ type BuildEventDefinitions<T extends Descriptors> = T extends EventDefinition ? BuildEventDefinition<T> : ConditionalExcept<{
25
+ [Key in keyof T]: T[Key] extends Descriptors ? BuildEventDefinitions<T[Key]> : never;
26
+ }, EmptyObject>;
24
27
  /**
25
28
  * Descriptors are objects that describe the API of a module, and the module
26
29
  * can either be a REST module or a host module.
@@ -4,12 +4,14 @@ exports.createClient = void 0;
4
4
  const sdk_types_1 = require("@wix/sdk-types");
5
5
  const ambassador_modules_js_1 = require("./ambassador-modules.js");
6
6
  const common_js_1 = require("./common.js");
7
+ const fetch_error_js_1 = require("./fetch-error.js");
7
8
  const helpers_js_1 = require("./helpers.js");
8
9
  const host_modules_js_1 = require("./host-modules.js");
9
10
  const rest_modules_js_1 = require("./rest-modules.js");
10
- const fetch_error_js_1 = require("./fetch-error.js");
11
+ const event_handlers_modules_js_1 = require("./event-handlers-modules.js");
11
12
  function createClient(config) {
12
13
  const _headers = config.headers || { Authorization: '' };
14
+ const eventHandlers = new Map();
13
15
  const authStrategy = config.auth ||
14
16
  {
15
17
  getAuthHeaders: (_) => Promise.resolve({ headers: {} }),
@@ -33,7 +35,14 @@ function createClient(config) {
33
35
  // on the WixClient, typescript starts failing with `Type instantiation is
34
36
  // excessively deep and possibly infinite.`
35
37
  const use = (modules, metadata) => {
36
- if ((0, host_modules_js_1.isHostModule)(modules) && config.host) {
38
+ if ((0, event_handlers_modules_js_1.isEventHandlerModule)(modules)) {
39
+ return (0, event_handlers_modules_js_1.buildEventDefinition)(modules, (eventType, handler) => {
40
+ const handlers = eventHandlers.get(eventType) ?? [];
41
+ handlers.push(handler);
42
+ eventHandlers.set(eventType, handlers);
43
+ });
44
+ }
45
+ else if ((0, host_modules_js_1.isHostModule)(modules) && config.host) {
37
46
  return (0, host_modules_js_1.buildHostModule)(modules, config.host);
38
47
  }
39
48
  else if (typeof modules === 'function') {
@@ -127,12 +136,20 @@ function createClient(config) {
127
136
  const eventType = parsedDecoded.eventType;
128
137
  const instanceId = parsedDecoded.instanceId;
129
138
  const payload = JSON.parse(parsedDecoded.data);
130
- if (opts.expectedEvents.length > 0 &&
131
- !opts.expectedEvents.some(({ type }) => type === eventType)) {
132
- throw new Error(`Unexpected event type: ${eventType}. Expected one of: ${opts.expectedEvents
139
+ const allExpectedEvents = [
140
+ ...opts.expectedEvents,
141
+ ...Array.from(eventHandlers.keys()).map((type) => ({ type })),
142
+ ];
143
+ if (allExpectedEvents.length > 0 &&
144
+ !allExpectedEvents.some(({ type }) => type === eventType)) {
145
+ throw new Error(`Unexpected event type: ${eventType}. Expected one of: ${allExpectedEvents
133
146
  .map((x) => x.type)
134
147
  .join(', ')}`);
135
148
  }
149
+ const handlers = eventHandlers.get(eventType) ?? [];
150
+ await Promise.all(handlers.map((handler) => handler(payload, {
151
+ instanceId,
152
+ })));
136
153
  return {
137
154
  instanceId,
138
155
  eventType,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.7.14",
3
+ "version": "1.8.0",
4
4
  "license": "UNLICENSED",
5
5
  "author": {
6
6
  "name": "Ronny Ringel",
@@ -64,10 +64,10 @@
64
64
  },
65
65
  "dependencies": {
66
66
  "@babel/runtime": "^7.23.2",
67
- "@wix/identity": "^1.0.74",
68
- "@wix/image-kit": "^1.62.0",
69
- "@wix/redirects": "^1.0.36",
70
- "@wix/sdk-types": "^1.5.11",
67
+ "@wix/identity": "^1.0.78",
68
+ "@wix/image-kit": "^1.64.0",
69
+ "@wix/redirects": "^1.0.41",
70
+ "@wix/sdk-types": "^1.6.0",
71
71
  "crypto-js": "^4.2.0",
72
72
  "jose": "^5.2.1",
73
73
  "pkce-challenge": "^3.1.0",
@@ -81,12 +81,12 @@
81
81
  "@types/crypto-js": "^4.2.1",
82
82
  "@types/is-ci": "^3.0.4",
83
83
  "@types/node": "^20.10.6",
84
- "@vitest/ui": "^1.1.3",
85
- "@wix/ecom": "^1.0.519",
86
- "@wix/events": "^1.0.169",
84
+ "@vitest/ui": "^1.5.0",
85
+ "@wix/ecom": "^1.0.531",
86
+ "@wix/events": "^1.0.179",
87
87
  "@wix/metro": "^1.0.73",
88
- "@wix/metro-runtime": "^1.1669.0",
89
- "@wix/sdk-runtime": "0.2.8",
88
+ "@wix/metro-runtime": "^1.1677.0",
89
+ "@wix/sdk-runtime": "0.2.11",
90
90
  "eslint": "^8.56.0",
91
91
  "eslint-config-sdk": "0.0.0",
92
92
  "graphql": "^16.8.0",
@@ -94,8 +94,8 @@
94
94
  "jsdom": "^22.1.0",
95
95
  "msw": "^2.0.12",
96
96
  "typescript": "^5.3.3",
97
- "vitest": "^1.1.3",
98
- "vitest-teamcity-reporter": "^0.2.2"
97
+ "vitest": "^1.5.0",
98
+ "vitest-teamcity-reporter": "^0.3.0"
99
99
  },
100
100
  "yoshiFlowLibrary": {
101
101
  "buildEsmWithBabel": true
@@ -120,5 +120,5 @@
120
120
  "wallaby": {
121
121
  "autoDetect": true
122
122
  },
123
- "falconPackageHash": "26e8ad87ca64212f00e6d70a72fde208558be3a6c14e56440cbb70dd"
123
+ "falconPackageHash": "869d627fd63d9aa37f08f30fc48dba3f34415f3d3ec6fa776a9ab4eb"
124
124
  }