@wix/sdk 1.7.13 → 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.
- package/build/event-handlers-modules.d.ts +3 -0
- package/build/event-handlers-modules.js +7 -0
- package/build/wix-context.d.ts +4 -51
- package/build/wix-context.js +2 -51
- package/build/wixClient.d.ts +6 -3
- package/build/wixClient.js +23 -6
- package/cjs/build/event-handlers-modules.d.ts +3 -0
- package/cjs/build/event-handlers-modules.js +12 -0
- package/cjs/build/wix-context.d.ts +4 -51
- package/cjs/build/wix-context.js +4 -53
- package/cjs/build/wixClient.d.ts +6 -3
- package/cjs/build/wixClient.js +22 -5
- package/package.json +13 -13
|
@@ -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
|
+
}
|
package/build/wix-context.d.ts
CHANGED
|
@@ -1,52 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export type MaybeWithWixContext<T extends RESTFunctionDescriptor> = typeof globalThis extends {
|
|
5
|
-
__wix_context__: {
|
|
6
|
-
initWixModules: unknown;
|
|
7
|
-
};
|
|
8
|
-
} ? BuildRESTFunction<T> : T;
|
|
9
|
-
declare namespace globalThis {
|
|
10
|
-
let __wix_context__: {
|
|
11
|
-
asyncLocalStorage?: import('node:async_hooks').AsyncLocalStorage<{
|
|
12
|
-
authStrategy: {
|
|
13
|
-
getAuthHeaders: () => Promise<{
|
|
14
|
-
headers: {
|
|
15
|
-
Authorization: string;
|
|
16
|
-
};
|
|
17
|
-
}>;
|
|
18
|
-
};
|
|
19
|
-
}>;
|
|
20
|
-
initWixModules: unknown;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export declare function setGlobalWixModulesInit(initWixModules: <T extends Descriptors, H extends Host>(wixModules: T) => BuildDescriptors<T, H>): void;
|
|
24
|
-
export declare const AsyncLocalStorageContext: {
|
|
25
|
-
asyncLocalStorage: AsyncLocalStorage<{
|
|
26
|
-
authStrategy: {
|
|
27
|
-
getAuthHeaders: () => Promise<{
|
|
28
|
-
headers: {
|
|
29
|
-
Authorization: string;
|
|
30
|
-
};
|
|
31
|
-
}>;
|
|
32
|
-
};
|
|
33
|
-
}> | undefined;
|
|
34
|
-
init(AsyncLocalStorageConstructor: typeof AsyncLocalStorage): void;
|
|
35
|
-
run(authStrategy: {
|
|
36
|
-
getAuthHeaders: () => Promise<{
|
|
37
|
-
headers: {
|
|
38
|
-
Authorization: string;
|
|
39
|
-
};
|
|
40
|
-
}>;
|
|
41
|
-
}, fn: () => unknown): unknown;
|
|
1
|
+
import { BuildDescriptors, Descriptors, Host } from './index.js';
|
|
2
|
+
export type WixContext = {
|
|
3
|
+
initWixModules: <T extends Descriptors, H extends Host>(wixModules: T, elevated?: boolean) => BuildDescriptors<T, H>;
|
|
42
4
|
};
|
|
43
|
-
export declare
|
|
44
|
-
init(authStrategy: {
|
|
45
|
-
getAuthHeaders: () => Promise<{
|
|
46
|
-
headers: {
|
|
47
|
-
Authorization: string;
|
|
48
|
-
};
|
|
49
|
-
}>;
|
|
50
|
-
}): void;
|
|
51
|
-
};
|
|
52
|
-
export {};
|
|
5
|
+
export declare function setGlobalWixContext(wixContext: WixContext): void;
|
package/build/wix-context.js
CHANGED
|
@@ -1,52 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
globalThis.__wix_context__ = {
|
|
4
|
-
initWixModules,
|
|
5
|
-
};
|
|
1
|
+
export function setGlobalWixContext(wixContext) {
|
|
2
|
+
globalThis.__wix_context__ = wixContext;
|
|
6
3
|
}
|
|
7
|
-
export const AsyncLocalStorageContext = {
|
|
8
|
-
asyncLocalStorage: undefined,
|
|
9
|
-
init(AsyncLocalStorageConstructor) {
|
|
10
|
-
const asyncLocalStorage = new AsyncLocalStorageConstructor();
|
|
11
|
-
this.asyncLocalStorage = asyncLocalStorage;
|
|
12
|
-
setGlobalWixModulesInit((wixModules) => {
|
|
13
|
-
const client = createClient({
|
|
14
|
-
auth: {
|
|
15
|
-
async getAuthHeaders() {
|
|
16
|
-
const store = asyncLocalStorage.getStore();
|
|
17
|
-
if (!store) {
|
|
18
|
-
throw new Error('No wix context found, make sure to init and use wix context');
|
|
19
|
-
}
|
|
20
|
-
return store.authStrategy.getAuthHeaders();
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
return client.use(wixModules);
|
|
25
|
-
});
|
|
26
|
-
},
|
|
27
|
-
run(authStrategy, fn) {
|
|
28
|
-
if (!globalThis.__wix_context__) {
|
|
29
|
-
throw new Error('Wix context not initialized');
|
|
30
|
-
}
|
|
31
|
-
if (this.asyncLocalStorage) {
|
|
32
|
-
return this.asyncLocalStorage.run({ authStrategy }, fn);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
throw new Error('AsyncLocalStorageContext: `run` has been called before `init` method. Make sure to call `init` method before using `run` method.');
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
export const GlobalSingletonContext = {
|
|
40
|
-
init(authStrategy) {
|
|
41
|
-
setGlobalWixModulesInit((wixModules) => {
|
|
42
|
-
const client = createClient({
|
|
43
|
-
auth: {
|
|
44
|
-
async getAuthHeaders() {
|
|
45
|
-
return authStrategy.getAuthHeaders();
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
return client.use(wixModules);
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
};
|
package/build/wixClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildRESTFunction, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition
|
|
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.
|
package/build/wixClient.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { EventDefinition, } from '@wix/sdk-types';
|
|
2
|
-
import {
|
|
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 {
|
|
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 (
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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,52 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export type MaybeWithWixContext<T extends RESTFunctionDescriptor> = typeof globalThis extends {
|
|
5
|
-
__wix_context__: {
|
|
6
|
-
initWixModules: unknown;
|
|
7
|
-
};
|
|
8
|
-
} ? BuildRESTFunction<T> : T;
|
|
9
|
-
declare namespace globalThis {
|
|
10
|
-
let __wix_context__: {
|
|
11
|
-
asyncLocalStorage?: import('node:async_hooks').AsyncLocalStorage<{
|
|
12
|
-
authStrategy: {
|
|
13
|
-
getAuthHeaders: () => Promise<{
|
|
14
|
-
headers: {
|
|
15
|
-
Authorization: string;
|
|
16
|
-
};
|
|
17
|
-
}>;
|
|
18
|
-
};
|
|
19
|
-
}>;
|
|
20
|
-
initWixModules: unknown;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export declare function setGlobalWixModulesInit(initWixModules: <T extends Descriptors, H extends Host>(wixModules: T) => BuildDescriptors<T, H>): void;
|
|
24
|
-
export declare const AsyncLocalStorageContext: {
|
|
25
|
-
asyncLocalStorage: AsyncLocalStorage<{
|
|
26
|
-
authStrategy: {
|
|
27
|
-
getAuthHeaders: () => Promise<{
|
|
28
|
-
headers: {
|
|
29
|
-
Authorization: string;
|
|
30
|
-
};
|
|
31
|
-
}>;
|
|
32
|
-
};
|
|
33
|
-
}> | undefined;
|
|
34
|
-
init(AsyncLocalStorageConstructor: typeof AsyncLocalStorage): void;
|
|
35
|
-
run(authStrategy: {
|
|
36
|
-
getAuthHeaders: () => Promise<{
|
|
37
|
-
headers: {
|
|
38
|
-
Authorization: string;
|
|
39
|
-
};
|
|
40
|
-
}>;
|
|
41
|
-
}, fn: () => unknown): unknown;
|
|
1
|
+
import { BuildDescriptors, Descriptors, Host } from './index.js';
|
|
2
|
+
export type WixContext = {
|
|
3
|
+
initWixModules: <T extends Descriptors, H extends Host>(wixModules: T, elevated?: boolean) => BuildDescriptors<T, H>;
|
|
42
4
|
};
|
|
43
|
-
export declare
|
|
44
|
-
init(authStrategy: {
|
|
45
|
-
getAuthHeaders: () => Promise<{
|
|
46
|
-
headers: {
|
|
47
|
-
Authorization: string;
|
|
48
|
-
};
|
|
49
|
-
}>;
|
|
50
|
-
}): void;
|
|
51
|
-
};
|
|
52
|
-
export {};
|
|
5
|
+
export declare function setGlobalWixContext(wixContext: WixContext): void;
|
package/cjs/build/wix-context.js
CHANGED
|
@@ -1,56 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
globalThis.__wix_context__ = {
|
|
7
|
-
initWixModules,
|
|
8
|
-
};
|
|
3
|
+
exports.setGlobalWixContext = void 0;
|
|
4
|
+
function setGlobalWixContext(wixContext) {
|
|
5
|
+
globalThis.__wix_context__ = wixContext;
|
|
9
6
|
}
|
|
10
|
-
exports.
|
|
11
|
-
exports.AsyncLocalStorageContext = {
|
|
12
|
-
asyncLocalStorage: undefined,
|
|
13
|
-
init(AsyncLocalStorageConstructor) {
|
|
14
|
-
const asyncLocalStorage = new AsyncLocalStorageConstructor();
|
|
15
|
-
this.asyncLocalStorage = asyncLocalStorage;
|
|
16
|
-
setGlobalWixModulesInit((wixModules) => {
|
|
17
|
-
const client = (0, index_js_1.createClient)({
|
|
18
|
-
auth: {
|
|
19
|
-
async getAuthHeaders() {
|
|
20
|
-
const store = asyncLocalStorage.getStore();
|
|
21
|
-
if (!store) {
|
|
22
|
-
throw new Error('No wix context found, make sure to init and use wix context');
|
|
23
|
-
}
|
|
24
|
-
return store.authStrategy.getAuthHeaders();
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
return client.use(wixModules);
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
run(authStrategy, fn) {
|
|
32
|
-
if (!globalThis.__wix_context__) {
|
|
33
|
-
throw new Error('Wix context not initialized');
|
|
34
|
-
}
|
|
35
|
-
if (this.asyncLocalStorage) {
|
|
36
|
-
return this.asyncLocalStorage.run({ authStrategy }, fn);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
throw new Error('AsyncLocalStorageContext: `run` has been called before `init` method. Make sure to call `init` method before using `run` method.');
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
exports.GlobalSingletonContext = {
|
|
44
|
-
init(authStrategy) {
|
|
45
|
-
setGlobalWixModulesInit((wixModules) => {
|
|
46
|
-
const client = (0, index_js_1.createClient)({
|
|
47
|
-
auth: {
|
|
48
|
-
async getAuthHeaders() {
|
|
49
|
-
return authStrategy.getAuthHeaders();
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
return client.use(wixModules);
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
};
|
|
7
|
+
exports.setGlobalWixContext = setGlobalWixContext;
|
package/cjs/build/wixClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildRESTFunction, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition
|
|
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.
|
package/cjs/build/wixClient.js
CHANGED
|
@@ -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
|
|
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,
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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.
|
|
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.
|
|
68
|
-
"@wix/image-kit": "^1.
|
|
69
|
-
"@wix/redirects": "^1.0.
|
|
70
|
-
"@wix/sdk-types": "^1.
|
|
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.
|
|
85
|
-
"@wix/ecom": "^1.0.
|
|
86
|
-
"@wix/events": "^1.0.
|
|
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.
|
|
89
|
-
"@wix/sdk-runtime": "0.2.
|
|
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.
|
|
98
|
-
"vitest-teamcity-reporter": "^0.
|
|
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": "
|
|
123
|
+
"falconPackageHash": "869d627fd63d9aa37f08f30fc48dba3f34415f3d3ec6fa776a9ab4eb"
|
|
124
124
|
}
|