import { TransportConfiguration, PostMessageBusTransport, AppPeer } from "@talentsoft-opensource/integration-com-layer"; import { AppService, WidgetContext, PartnerUrl, RequestOptionsWithPartner, AppHeaderActionConfiguration } from "@talentsoft-opensource/integration-widget-component"; import { HttpResponse, RequestOptions } from "@talentsoft-opensource/integration-widget-contract"; import { WidgetDefinition } from "./widget-definition"; export function getAppService(definition: WidgetDefinition) { const appPeer = getAppPeer(definition.name); const appService = wrapAppPeer(appPeer); return appService; } const HOST_API = { getContext: 'getContext', expand: 'expand', reduce: 'reduce', restoreSize: 'restoreSize', call: 'call', openUrlInNewTab: 'openUrlInNewTab', openUrlInCurrentTab: 'openUrlInCurrentTab', getUrlForCurrentContext: 'getUrlForCurrentContext', getPreloadedResources: 'getPreloadedResources', requestExternalResource: 'requestExternalResource', requestInternalResource: 'requestInternalResource', setHeaderActionConfiguration: 'setHeaderActionConfiguration' }; function getAppPeer(name: string) { const transport = createClientTransport(name); const app = AppPeer.new(transport, 'host', HOST_API, 20000); app.init(); return app; } function createClientTransport(name: string) { const destinations = { host: window.parent, [name]: window }; const config: TransportConfiguration = { locationId: name, locationResolver: (id: string) => destinations[id] }; const transport = new PostMessageBusTransport(config); transport.init(); return transport; } function wrapAppPeer(app: AppPeer): AppService { const host = app.host; const appService: AppService = { getContext: host.getContext as (id: string) => Promise, on: (app.on as any), restoreSize: host.restoreSize as (appid: string) => Promise, expand: host.expand as (appid: string) => Promise, reduce: host.reduce as (appid: string) => Promise, openUrlInNewTab: host.openUrlInNewTab as (url: PartnerUrl) => Promise, openUrlInCurrentTab: host.openUrlInCurrentTab as (url: PartnerUrl) => Promise, loadData: host.call as (partnerName: string) => Promise, getUrlForCurrentContext: host.getUrlForCurrentContext as (param: PartnerUrl) => Promise, getPreloadedResources: host.getPreloadedResources as (language: string) => Promise>, requestExternalResource: host.requestExternalResource as (options: RequestOptionsWithPartner) => Promise, requestInternalResource: host.requestInternalResource as (options: RequestOptions) => Promise, setHeaderActionConfiguration: host.setHeaderActionConfiguration as (appConfig: AppHeaderActionConfiguration) => Promise }; return appService; }