@wix/api-client
Advanced tools
Comparing version 1.8.1 to 1.9.0
@@ -1,3 +0,4 @@ | ||
import { EventDefinition, EventHandler } from '@wix/sdk-types'; | ||
import { BaseEventMetadata, EventDefinition, EventHandler } from '@wix/sdk-types'; | ||
export declare const isEventHandlerModule: (val: any) => val is EventDefinition<unknown, string>; | ||
export declare function buildEventDefinition<T extends EventDefinition<any, string>>(eventDefinition: T, registerHandler: (eventType: string, handler: EventHandler<T>) => void): (handler: EventHandler<T>) => void; | ||
export declare function buildEventDefinition<T extends EventDefinition<any, string>>(eventDefinition: T, registerHandler: (eventDefinition: T, handler: EventHandler<T>) => void): (handler: EventHandler<T>) => void; | ||
export declare function runHandler<T extends EventDefinition>(eventDefinition: T, handler: EventHandler<T>, payload: unknown, baseEventMetadata: BaseEventMetadata): void | Promise<void>; |
@@ -5,4 +5,36 @@ import { isObject } from './helpers.js'; | ||
return (handler) => { | ||
registerHandler(eventDefinition.type, handler); | ||
registerHandler(eventDefinition, handler); | ||
}; | ||
} | ||
export function runHandler(eventDefinition, handler, payload, baseEventMetadata) { | ||
if (eventDefinition.isDomainEvent) { | ||
const domainEventPayload = payload; | ||
const { deletedEvent, actionEvent, createdEvent, updatedEvent, ...domainEventMetadata } = domainEventPayload; | ||
const metadata = { | ||
...baseEventMetadata, | ||
...domainEventMetadata, | ||
}; | ||
if (deletedEvent) { | ||
return handler({ metadata }); | ||
} | ||
else if (actionEvent) { | ||
return handler({ | ||
data: domainEventPayload.actionEvent?.body, | ||
metadata, | ||
}); | ||
} | ||
else { | ||
return handler({ | ||
entity: domainEventPayload.createdEvent?.entity ?? | ||
domainEventPayload.updatedEvent?.currentEntity, | ||
metadata, | ||
}); | ||
} | ||
} | ||
else { | ||
return handler({ | ||
data: payload, | ||
metadata: baseEventMetadata, | ||
}); | ||
} | ||
} |
@@ -6,5 +6,5 @@ export * from './wixClient.js'; | ||
export * from './auth/ApiKeyAuthStrategy.js'; | ||
export * from './auth/WixAppOAuthStrategy.js'; | ||
export * from './auth/AppOAuthStrategy.js'; | ||
export * from '@wix/sdk-types'; | ||
export { getDefaultDomain } from './rest-modules.js'; | ||
export { API_URL } from './common.js'; |
@@ -6,5 +6,5 @@ export * from './wixClient.js'; | ||
export * from './auth/ApiKeyAuthStrategy.js'; | ||
export * from './auth/WixAppOAuthStrategy.js'; | ||
export * from './auth/AppOAuthStrategy.js'; | ||
export * from '@wix/sdk-types'; | ||
export { getDefaultDomain } from './rest-modules.js'; | ||
export { API_URL } from './common.js'; |
@@ -100,2 +100,3 @@ import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition } from '@wix/sdk-types'; | ||
instanceId: string; | ||
identity: unknown; | ||
} & (T['length'] extends 0 ? { | ||
@@ -102,0 +103,0 @@ eventType: string; |
@@ -8,3 +8,3 @@ import { EventDefinition, } from '@wix/sdk-types'; | ||
import { buildRESTDescriptor } from './rest-modules.js'; | ||
import { buildEventDefinition, isEventHandlerModule, } from './event-handlers-modules.js'; | ||
import { buildEventDefinition, isEventHandlerModule, runHandler, } from './event-handlers-modules.js'; | ||
export function createClient(config) { | ||
@@ -37,6 +37,6 @@ const _headers = config.headers || { Authorization: '' }; | ||
if (isEventHandlerModule(modules)) { | ||
return buildEventDefinition(modules, (eventType, handler) => { | ||
const handlers = eventHandlers.get(eventType) ?? []; | ||
handlers.push(handler); | ||
eventHandlers.set(eventType, handlers); | ||
return buildEventDefinition(modules, (eventDefinition, handler) => { | ||
const handlers = eventHandlers.get(eventDefinition.type) ?? []; | ||
handlers.push({ eventDefinition, handler }); | ||
eventHandlers.set(eventDefinition.type, handlers); | ||
}); | ||
@@ -137,2 +137,3 @@ } | ||
const instanceId = parsedDecoded.instanceId; | ||
const identity = JSON.parse(parsedDecoded.identity); | ||
const payload = JSON.parse(parsedDecoded.data); | ||
@@ -150,4 +151,5 @@ const allExpectedEvents = [ | ||
const handlers = eventHandlers.get(eventType) ?? []; | ||
await Promise.all(handlers.map((handler) => handler(payload, { | ||
await Promise.all(handlers.map(({ eventDefinition, handler }) => runHandler(eventDefinition, handler, payload, { | ||
instanceId, | ||
identity, | ||
}))); | ||
@@ -158,2 +160,3 @@ return { | ||
payload, | ||
identity, | ||
}; | ||
@@ -160,0 +163,0 @@ }, |
{ | ||
"name": "@wix/api-client", | ||
"version": "1.8.1", | ||
"version": "1.9.0", | ||
"license": "UNLICENSED", | ||
@@ -27,3 +27,3 @@ "main": "build/index.js", | ||
"@wix/redirects": "^1.0.41", | ||
"@wix/sdk-types": "^1.6.0", | ||
"@wix/sdk-types": "^1.6.2", | ||
"crypto-js": "^4.2.0", | ||
@@ -37,3 +37,3 @@ "jose": "^5.2.1", | ||
"@wix/metro-runtime": "^1.1677.0", | ||
"@wix/sdk": "1.8.1" | ||
"@wix/sdk": "1.9.0" | ||
}, | ||
@@ -51,3 +51,3 @@ "wix": { | ||
}, | ||
"falconPackageHash": "fc677777ded3cfadaf3c1a253e718122ce0c138cb2b62a679ad6a5ed" | ||
"falconPackageHash": "ab7147a0e8716f899379942738887046ddf5b6d02e703b9af0064466" | ||
} |
74972
1859
Updated@wix/sdk-types@^1.6.2