@osdk/widget.client 2.1.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,113 @@
1
+ /*
2
+ * Copyright 2024 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { defineConfig } from "@osdk/widget.api";
18
+ import { describe, expectTypeOf, it } from "vitest";
19
+ import { FoundryHostEventTarget } from "./host.js";
20
+ describe("FoundryHostEventTarget", () => {
21
+ it("should narrow the event payload based on host message type for addEventListener", () => {
22
+ defineConfig({
23
+ id: "widgetId",
24
+ name: "Widget Name",
25
+ description: "Widget Description",
26
+ type: "workshop",
27
+ parameters: {
28
+ test: {
29
+ displayName: "Testing",
30
+ type: "array",
31
+ subType: "boolean"
32
+ },
33
+ test2: {
34
+ displayName: "Testing 2",
35
+ type: "array",
36
+ subType: "string"
37
+ },
38
+ test3: {
39
+ displayName: "Testing 3",
40
+ type: "number"
41
+ }
42
+ },
43
+ events: {
44
+ myEvent: {
45
+ displayName: "My Event",
46
+ parameterUpdateIds: ["test"]
47
+ }
48
+ }
49
+ });
50
+ const hostTarget = new FoundryHostEventTarget();
51
+ hostTarget.addEventListener("host.update-parameters", event => {
52
+ expectTypeOf(event.detail).toMatchTypeOf();
53
+ });
54
+ });
55
+ it("should narrow the event payload based on host message type for removeEventListener", () => {
56
+ defineConfig({
57
+ id: "widgetId",
58
+ name: "Widget Name",
59
+ description: "Widget Description",
60
+ type: "workshop",
61
+ parameters: {
62
+ test: {
63
+ displayName: "Testing",
64
+ type: "array",
65
+ subType: "boolean"
66
+ },
67
+ test2: {
68
+ displayName: "Testing 2",
69
+ type: "array",
70
+ subType: "string"
71
+ },
72
+ test3: {
73
+ displayName: "Testing 3",
74
+ type: "number"
75
+ }
76
+ },
77
+ events: {
78
+ myEvent: {
79
+ displayName: "My Event",
80
+ parameterUpdateIds: ["test"]
81
+ }
82
+ }
83
+ });
84
+ const hostTarget = new FoundryHostEventTarget();
85
+ hostTarget.removeEventListener("host.update-parameters", event => {
86
+ expectTypeOf(event.detail).toMatchTypeOf();
87
+ });
88
+ });
89
+ it("should narrow the event payload when dispatching an event", () => {
90
+ defineConfig({
91
+ id: "widgetId",
92
+ name: "Widget Name",
93
+ description: "Widget Description",
94
+ type: "workshop",
95
+ parameters: {
96
+ test: {
97
+ displayName: "Testing",
98
+ type: "array",
99
+ subType: "boolean"
100
+ }
101
+ },
102
+ events: {
103
+ myEvent: {
104
+ displayName: "My Event",
105
+ parameterUpdateIds: ["test"]
106
+ }
107
+ }
108
+ });
109
+ new FoundryHostEventTarget();
110
+ expectTypeOf().toMatchTypeOf();
111
+ });
112
+ });
113
+ //# sourceMappingURL=host.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.test.js","names":["defineConfig","describe","expectTypeOf","it","FoundryHostEventTarget","id","name","description","type","parameters","test","displayName","subType","test2","test3","events","myEvent","parameterUpdateIds","hostTarget","addEventListener","event","detail","toMatchTypeOf","removeEventListener"],"sources":["host.test.ts"],"sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { HostMessage } from \"@osdk/widget.api\";\nimport { defineConfig } from \"@osdk/widget.api\";\nimport { describe, expectTypeOf, it } from \"vitest\";\nimport type { HostMessageEventListener } from \"./host.js\";\nimport { FoundryHostEventTarget } from \"./host.js\";\n\ndescribe(\"FoundryHostEventTarget\", () => {\n it(\"should narrow the event payload based on host message type for addEventListener\", () => {\n const test = defineConfig({\n id: \"widgetId\",\n name: \"Widget Name\",\n description: \"Widget Description\",\n type: \"workshop\",\n parameters: {\n test: {\n displayName: \"Testing\",\n type: \"array\",\n subType: \"boolean\",\n },\n test2: {\n displayName: \"Testing 2\",\n type: \"array\",\n subType: \"string\",\n },\n test3: {\n displayName: \"Testing 3\",\n type: \"number\",\n },\n },\n events: {\n myEvent: {\n displayName: \"My Event\",\n parameterUpdateIds: [\"test\"],\n },\n },\n });\n\n const hostTarget = new FoundryHostEventTarget<typeof test>();\n hostTarget.addEventListener(\"host.update-parameters\", (event) => {\n expectTypeOf(event.detail).toMatchTypeOf<\n HostMessage.Payload.UpdateParameters<typeof test>\n >();\n });\n });\n\n it(\"should narrow the event payload based on host message type for removeEventListener\", () => {\n const test = defineConfig({\n id: \"widgetId\",\n name: \"Widget Name\",\n description: \"Widget Description\",\n type: \"workshop\",\n parameters: {\n test: {\n displayName: \"Testing\",\n type: \"array\",\n subType: \"boolean\",\n },\n test2: {\n displayName: \"Testing 2\",\n type: \"array\",\n subType: \"string\",\n },\n test3: {\n displayName: \"Testing 3\",\n type: \"number\",\n },\n },\n events: {\n myEvent: {\n displayName: \"My Event\",\n parameterUpdateIds: [\"test\"],\n },\n },\n });\n\n const hostTarget = new FoundryHostEventTarget<typeof test>();\n const listener: HostMessageEventListener<\n HostMessage.Payload.UpdateParameters<typeof test>\n > = (event) => {\n expectTypeOf(event.detail).toMatchTypeOf<\n HostMessage.Payload.UpdateParameters<typeof test>\n >();\n };\n\n hostTarget.removeEventListener(\"host.update-parameters\", listener);\n });\n\n it(\"should narrow the event payload when dispatching an event\", () => {\n const test = defineConfig({\n id: \"widgetId\",\n name: \"Widget Name\",\n description: \"Widget Description\",\n type: \"workshop\",\n parameters: {\n test: {\n displayName: \"Testing\",\n type: \"array\",\n subType: \"boolean\",\n },\n },\n events: {\n myEvent: {\n displayName: \"My Event\",\n parameterUpdateIds: [\"test\"],\n },\n },\n });\n\n const hostTarget = new FoundryHostEventTarget<typeof test>();\n type dispatchUpdateMessage = Parameters<\n typeof hostTarget.dispatchEventMessage<\"host.update-parameters\">\n >[1];\n expectTypeOf<dispatchUpdateMessage>().toMatchTypeOf<\n HostMessage.Payload.UpdateParameters<typeof test>\n >();\n });\n});\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,SAASA,YAAY,QAAQ,kBAAkB;AAC/C,SAASC,QAAQ,EAAEC,YAAY,EAAEC,EAAE,QAAQ,QAAQ;AAEnD,SAASC,sBAAsB,QAAQ,WAAW;AAElDH,QAAQ,CAAC,wBAAwB,EAAE,MAAM;EACvCE,EAAE,CAAC,iFAAiF,EAAE,MAAM;IAC7EH,YAAY,CAAC;MACxBK,EAAE,EAAE,UAAU;MACdC,IAAI,EAAE,aAAa;MACnBC,WAAW,EAAE,oBAAoB;MACjCC,IAAI,EAAE,UAAU;MAChBC,UAAU,EAAE;QACVC,IAAI,EAAE;UACJC,WAAW,EAAE,SAAS;UACtBH,IAAI,EAAE,OAAO;UACbI,OAAO,EAAE;QACX,CAAC;QACDC,KAAK,EAAE;UACLF,WAAW,EAAE,WAAW;UACxBH,IAAI,EAAE,OAAO;UACbI,OAAO,EAAE;QACX,CAAC;QACDE,KAAK,EAAE;UACLH,WAAW,EAAE,WAAW;UACxBH,IAAI,EAAE;QACR;MACF,CAAC;MACDO,MAAM,EAAE;QACNC,OAAO,EAAE;UACPL,WAAW,EAAE,UAAU;UACvBM,kBAAkB,EAAE,CAAC,MAAM;QAC7B;MACF;IACF,CAAC,CAAC;IAEF,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S;UACtBH,IAAI,EAAE,OAAO;UACbI,OAAO,EAAE;QACX;MACF,CAAC;MACDG,MAAM,EAAE;QACNC,OAAO,EAAE;UACPL,WAAW,EAAE,UAAU;UACvBM,kBAAkB,EAAE,CAAC,MAAM;QAC7B;MACF;IACF,CAAC,CAAC;IAEiB,IAAIb,sBAAsB,CAAc,CAAC;IAI5DF,YAAY,CAAwB,CAAC,CAACoB,aAAa,CAEjD,CAAC;EACL,CAAC,CAAC;AACJ,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,20 @@
1
+ /*
2
+ * Copyright 2024 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export { defineConfig, HostMessage, isHostParametersUpdatedMessage } from "@osdk/widget.api";
18
+ export { createFoundryWidgetClient } from "./client.js";
19
+ export { FoundryHostEventTarget } from "./host.js";
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["defineConfig","HostMessage","isHostParametersUpdatedMessage","createFoundryWidgetClient","FoundryHostEventTarget"],"sources":["index.ts"],"sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport type {\n AsyncFailedValue,\n AsyncLoadedValue,\n AsyncLoadingValue,\n AsyncNotStartedLoadingValue,\n AsyncParameterValueMap,\n AsyncReloadingValue,\n AsyncValue,\n EventId,\n EventParameterValueMap,\n ParameterConfig,\n ParameterValue,\n ParameterValueMap,\n WidgetConfig,\n WidgetMessage,\n} from \"@osdk/widget.api\";\nexport {\n defineConfig,\n HostMessage,\n isHostParametersUpdatedMessage,\n} from \"@osdk/widget.api\";\nexport { createFoundryWidgetClient } from \"./client.js\";\nexport type { FoundryWidgetClient } from \"./client.js\";\nexport { FoundryHostEventTarget } from \"./host.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAkBA,SACEA,YAAY,EACZC,WAAW,EACXC,8BAA8B,QACzB,kBAAkB;AACzB,SAASC,yBAAyB,QAAQ,aAAa;AAEvD,SAASC,sBAAsB,QAAQ,WAAW","ignoreList":[]}
@@ -0,0 +1,145 @@
1
+ 'use strict';
2
+
3
+ var widget_api = require('@osdk/widget.api');
4
+ var invariant = require('tiny-invariant');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var invariant__default = /*#__PURE__*/_interopDefault(invariant);
9
+
10
+ // src/index.ts
11
+
12
+ // src/host.ts
13
+ var FoundryHostEventTarget = class extends EventTarget {
14
+ addEventListener(type, callback, options) {
15
+ super.addEventListener(type, callback, options);
16
+ }
17
+ removeEventListener(type, callback, options) {
18
+ super.removeEventListener(type, callback, options);
19
+ }
20
+ dispatchEventMessage(type, payload) {
21
+ this.dispatchEvent(new CustomEvent(type, {
22
+ detail: payload
23
+ }));
24
+ }
25
+ };
26
+
27
+ // src/client.ts
28
+ function createFoundryWidgetClient() {
29
+ if ("__PALANTIR_WIDGET_API__" in window) {
30
+ return _createFoundryWidgetClient(window.__PALANTIR_WIDGET_API__);
31
+ }
32
+ return _createFoundryWidgetClientLegacy();
33
+ }
34
+ function _createFoundryWidgetClient(widgetApi) {
35
+ const hostEventTarget = new FoundryHostEventTarget();
36
+ const listenForHostMessages = (event) => {
37
+ widget_api.visitHostMessage(event.detail, {
38
+ "host.update-parameters": (payload) => {
39
+ hostEventTarget.dispatchEventMessage("host.update-parameters", payload);
40
+ },
41
+ _unknown: () => {
42
+ }
43
+ });
44
+ };
45
+ const sendMessageToHost = (message) => {
46
+ widgetApi.sendMessage(message);
47
+ };
48
+ return {
49
+ hostEventTarget,
50
+ ready: () => {
51
+ sendMessageToHost({
52
+ type: "widget.ready",
53
+ payload: {
54
+ apiVersion: widget_api.HostMessage.Version
55
+ }
56
+ });
57
+ },
58
+ emitEvent: (eventId, payload) => {
59
+ sendMessageToHost({
60
+ type: "widget.emit-event",
61
+ payload: {
62
+ eventId,
63
+ ...payload
64
+ }
65
+ });
66
+ },
67
+ sendMessage: sendMessageToHost,
68
+ subscribe: () => {
69
+ widgetApi.addEventListener("message", listenForHostMessages);
70
+ },
71
+ unsubscribe: () => {
72
+ widgetApi.removeEventListener("message", listenForHostMessages);
73
+ }
74
+ };
75
+ }
76
+ function _createFoundryWidgetClientLegacy() {
77
+ !window.parent ? process.env.NODE_ENV !== "production" ? invariant__default.default(false, "[FoundryWidgetClient] Must be run in an iframe") : invariant__default.default(false) : void 0;
78
+ const parentWindow = window.parent;
79
+ const metaTag = document.querySelector(`meta[name="${widget_api.META_TAG_HOST_ORIGIN}"]`);
80
+ !metaTag ? process.env.NODE_ENV !== "production" ? invariant__default.default(false, "[FoundryWidgetClient] Missing host origin meta tag " + widget_api.META_TAG_HOST_ORIGIN) : invariant__default.default(false) : void 0;
81
+ const hostOrigin = metaTag.getAttribute("content");
82
+ !hostOrigin ? process.env.NODE_ENV !== "production" ? invariant__default.default(false, "[FoundryWidgetClient] Missing host origin meta tag content") : invariant__default.default(false) : void 0;
83
+ const hostEventTarget = new FoundryHostEventTarget();
84
+ const listenForHostMessages = (event) => {
85
+ widget_api.visitHostMessage(event.data, {
86
+ "host.update-parameters": (payload) => {
87
+ hostEventTarget.dispatchEventMessage("host.update-parameters", payload);
88
+ },
89
+ _unknown: () => {
90
+ }
91
+ });
92
+ };
93
+ const sendMessageToHost = (message) => {
94
+ parentWindow.postMessage(message, hostOrigin);
95
+ };
96
+ return {
97
+ hostEventTarget,
98
+ ready: () => {
99
+ sendMessageToHost({
100
+ type: "widget.ready",
101
+ payload: {
102
+ apiVersion: widget_api.HostMessage.Version
103
+ }
104
+ });
105
+ },
106
+ emitEvent: (eventId, payload) => {
107
+ sendMessageToHost({
108
+ type: "widget.emit-event",
109
+ payload: {
110
+ eventId,
111
+ ...payload
112
+ }
113
+ });
114
+ },
115
+ sendMessage: sendMessageToHost,
116
+ subscribe: () => {
117
+ window.addEventListener("message", (event) => {
118
+ if (event.origin !== hostOrigin) {
119
+ return;
120
+ }
121
+ listenForHostMessages(event);
122
+ });
123
+ },
124
+ unsubscribe: () => {
125
+ window.removeEventListener("message", listenForHostMessages);
126
+ }
127
+ };
128
+ }
129
+
130
+ Object.defineProperty(exports, "HostMessage", {
131
+ enumerable: true,
132
+ get: function () { return widget_api.HostMessage; }
133
+ });
134
+ Object.defineProperty(exports, "defineConfig", {
135
+ enumerable: true,
136
+ get: function () { return widget_api.defineConfig; }
137
+ });
138
+ Object.defineProperty(exports, "isHostParametersUpdatedMessage", {
139
+ enumerable: true,
140
+ get: function () { return widget_api.isHostParametersUpdatedMessage; }
141
+ });
142
+ exports.FoundryHostEventTarget = FoundryHostEventTarget;
143
+ exports.createFoundryWidgetClient = createFoundryWidgetClient;
144
+ //# sourceMappingURL=index.cjs.map
145
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/host.ts","../../src/client.ts"],"names":["visitHostMessage","HostMessage","invariant","META_TAG_HOST_ORIGIN"],"mappings":";;;;;;;;;;;;AAgBa,IAAA,sBAAA,GAAN,cAAqC,WAAY,CAAA;AAAA,EACt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ved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport class FoundryHostEventTarget extends EventTarget {\n addEventListener(type, callback, options) {\n super.addEventListener(type, callback, options);\n }\n removeEventListener(type, callback, options) {\n super.removeEventListener(type, callback, options);\n }\n dispatchEventMessage(type, payload) {\n this.dispatchEvent(new CustomEvent(type, {\n detail: payload\n }));\n }\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { HostMessage, visitHostMessage } from \"@osdk/widget.api\";\nimport { META_TAG_HOST_ORIGIN } from \"@osdk/widget.api\";\nimport invariant from \"tiny-invariant\";\nimport { FoundryHostEventTarget } from \"./host.js\";\nexport function createFoundryWidgetClient() {\n if (\"__PALANTIR_WIDGET_API__\" in window) {\n return _createFoundryWidgetClient(window.__PALANTIR_WIDGET_API__);\n }\n // todo: drop support for sandbox v1\n return _createFoundryWidgetClientLegacy();\n}\nfunction _createFoundryWidgetClient(widgetApi) {\n const hostEventTarget = new FoundryHostEventTarget();\n const listenForHostMessages = event => {\n visitHostMessage(event.detail, {\n \"host.update-parameters\": payload => {\n hostEventTarget.dispatchEventMessage(\"host.update-parameters\", payload);\n },\n _unknown: () => {\n // Do nothing\n }\n });\n };\n const sendMessageToHost = message => {\n widgetApi.sendMessage(message);\n };\n return {\n hostEventTarget,\n ready: () => {\n sendMessageToHost({\n type: \"widget.ready\",\n payload: {\n apiVersion: HostMessage.Version\n }\n });\n },\n emitEvent: (eventId, payload) => {\n sendMessageToHost({\n type: \"widget.emit-event\",\n payload: {\n eventId,\n ...payload\n }\n });\n },\n sendMessage: sendMessageToHost,\n subscribe: () => {\n widgetApi.addEventListener(\"message\", listenForHostMessages);\n },\n unsubscribe: () => {\n widgetApi.removeEventListener(\"message\", listenForHostMessages);\n }\n };\n}\n\n// todo: drop support for sandbox v1\nfunction _createFoundryWidgetClientLegacy() {\n !window.parent ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"[FoundryWidgetClient] Must be run in an iframe\") : invariant(false) : void 0;\n const parentWindow = window.parent;\n const metaTag = document.querySelector(`meta[name=\"${META_TAG_HOST_ORIGIN}\"]`);\n !metaTag ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"[FoundryWidgetClient] Missing host origin meta tag \" + META_TAG_HOST_ORIGIN) : invariant(false) : void 0;\n const hostOrigin = metaTag.getAttribute(\"content\");\n !hostOrigin ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"[FoundryWidgetClient] Missing host origin meta tag content\") : invariant(false) : void 0;\n const hostEventTarget = new FoundryHostEventTarget();\n const listenForHostMessages = event => {\n visitHostMessage(event.data, {\n \"host.update-parameters\": payload => {\n hostEventTarget.dispatchEventMessage(\"host.update-parameters\", payload);\n },\n _unknown: () => {\n // Do nothing\n }\n });\n };\n const sendMessageToHost = message => {\n parentWindow.postMessage(message, hostOrigin);\n };\n return {\n hostEventTarget,\n ready: () => {\n sendMessageToHost({\n type: \"widget.ready\",\n payload: {\n apiVersion: HostMessage.Version\n }\n });\n },\n emitEvent: (eventId, payload) => {\n sendMessageToHost({\n type: \"widget.emit-event\",\n payload: {\n eventId,\n ...payload\n }\n });\n },\n sendMessage: sendMessageToHost,\n subscribe: () => {\n window.addEventListener(\"message\", event => {\n if (event.origin !== hostOrigin) {\n // Reject messages that aren't coming from the configured host\n return;\n }\n listenForHostMessages(event);\n });\n },\n unsubscribe: () => {\n window.removeEventListener(\"message\", listenForHostMessages);\n }\n };\n}"]}
@@ -0,0 +1,55 @@
1
+ import { WidgetConfig, HostMessage, WidgetMessage } from '@osdk/widget.api';
2
+ export { AsyncFailedValue, AsyncLoadedValue, AsyncLoadingValue, AsyncNotStartedLoadingValue, AsyncParameterValueMap, AsyncReloadingValue, AsyncValue, EventId, EventParameterValueMap, HostMessage, ParameterConfig, ParameterValue, ParameterValueMap, WidgetConfig, WidgetMessage, defineConfig, isHostParametersUpdatedMessage } from '@osdk/widget.api';
3
+
4
+ interface HostMessageEventListener<P extends HostMessage.Payload> {
5
+ (event: CustomEvent<P>): void;
6
+ }
7
+ interface HostMessageEventListenerObject<P extends HostMessage.Payload> {
8
+ handleEvent(object: CustomEvent<P>): void;
9
+ }
10
+ declare class FoundryHostEventTarget<C extends WidgetConfig<C["parameters"]>> extends EventTarget {
11
+ addEventListener<T extends HostMessage<C>["type"]>(type: T, callback: HostMessageEventListener<(HostMessage<C> & {
12
+ type: T;
13
+ })["payload"]> | HostMessageEventListenerObject<(HostMessage<C> & {
14
+ type: T;
15
+ })["payload"]> | null, options?: AddEventListenerOptions | boolean): void;
16
+ removeEventListener<T extends HostMessage<C>["type"]>(type: T, callback: HostMessageEventListener<(HostMessage<C> & {
17
+ type: T;
18
+ })["payload"]> | HostMessageEventListenerObject<(HostMessage<C> & {
19
+ type: T;
20
+ })["payload"]> | null, options?: EventListenerOptions | boolean): void;
21
+ dispatchEventMessage<T extends HostMessage<C>["type"]>(type: T, payload: (HostMessage<C> & {
22
+ type: T;
23
+ })["payload"]): void;
24
+ }
25
+
26
+ interface FoundryWidgetClient<C extends WidgetConfig<C["parameters"]>> {
27
+ /**
28
+ * Notifies the host that this client is ready to receive the first parameter values
29
+ */
30
+ ready: () => void;
31
+ /**
32
+ * Emits an event with the given ID and payload
33
+ */
34
+ emitEvent: <M extends WidgetMessage.EmitEvent<C>>(eventId: M["payload"]["eventId"], payload: Omit<M["payload"], "eventId">) => void;
35
+ /**
36
+ * Sends a message to the parent frame.
37
+ * It is recommended to use the convenience methods for individual messages (e.g. ready or emitEvent) instead
38
+ */
39
+ sendMessage: <M extends WidgetMessage<C>>(message: M) => void;
40
+ /**
41
+ * Subscribes to events from the host, invoking the listener when a message is received
42
+ */
43
+ subscribe: () => void;
44
+ /**
45
+ * Unsubscribes a previously subscribed listener from host events, if one exists
46
+ */
47
+ unsubscribe: () => void;
48
+ /**
49
+ * Event targets on which you can subscribe to specific host messages
50
+ */
51
+ hostEventTarget: FoundryHostEventTarget<C>;
52
+ }
53
+ declare function createFoundryWidgetClient<C extends WidgetConfig<C["parameters"]>>(): FoundryWidgetClient<C>;
54
+
55
+ export { FoundryHostEventTarget, type FoundryWidgetClient, createFoundryWidgetClient };
@@ -0,0 +1,128 @@
1
+ /*
2
+ * Copyright 2024 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { HostMessage, visitHostMessage } from "@osdk/widget.api";
18
+ import { META_TAG_HOST_ORIGIN } from "@osdk/widget.api";
19
+ import invariant from "tiny-invariant";
20
+ import { FoundryHostEventTarget } from "./host.js";
21
+ export function createFoundryWidgetClient() {
22
+ if ("__PALANTIR_WIDGET_API__" in window) {
23
+ return _createFoundryWidgetClient(window.__PALANTIR_WIDGET_API__);
24
+ }
25
+ // todo: drop support for sandbox v1
26
+ return _createFoundryWidgetClientLegacy();
27
+ }
28
+ function _createFoundryWidgetClient(widgetApi) {
29
+ const hostEventTarget = new FoundryHostEventTarget();
30
+ const listenForHostMessages = event => {
31
+ visitHostMessage(event.detail, {
32
+ "host.update-parameters": payload => {
33
+ hostEventTarget.dispatchEventMessage("host.update-parameters", payload);
34
+ },
35
+ _unknown: () => {
36
+ // Do nothing
37
+ }
38
+ });
39
+ };
40
+ const sendMessageToHost = message => {
41
+ widgetApi.sendMessage(message);
42
+ };
43
+ return {
44
+ hostEventTarget,
45
+ ready: () => {
46
+ sendMessageToHost({
47
+ type: "widget.ready",
48
+ payload: {
49
+ apiVersion: HostMessage.Version
50
+ }
51
+ });
52
+ },
53
+ emitEvent: (eventId, payload) => {
54
+ sendMessageToHost({
55
+ type: "widget.emit-event",
56
+ payload: {
57
+ eventId,
58
+ ...payload
59
+ }
60
+ });
61
+ },
62
+ sendMessage: sendMessageToHost,
63
+ subscribe: () => {
64
+ widgetApi.addEventListener("message", listenForHostMessages);
65
+ },
66
+ unsubscribe: () => {
67
+ widgetApi.removeEventListener("message", listenForHostMessages);
68
+ }
69
+ };
70
+ }
71
+
72
+ // todo: drop support for sandbox v1
73
+ function _createFoundryWidgetClientLegacy() {
74
+ !window.parent ? process.env.NODE_ENV !== "production" ? invariant(false, "[FoundryWidgetClient] Must be run in an iframe") : invariant(false) : void 0;
75
+ const parentWindow = window.parent;
76
+ const metaTag = document.querySelector(`meta[name="${META_TAG_HOST_ORIGIN}"]`);
77
+ !metaTag ? process.env.NODE_ENV !== "production" ? invariant(false, "[FoundryWidgetClient] Missing host origin meta tag " + META_TAG_HOST_ORIGIN) : invariant(false) : void 0;
78
+ const hostOrigin = metaTag.getAttribute("content");
79
+ !hostOrigin ? process.env.NODE_ENV !== "production" ? invariant(false, "[FoundryWidgetClient] Missing host origin meta tag content") : invariant(false) : void 0;
80
+ const hostEventTarget = new FoundryHostEventTarget();
81
+ const listenForHostMessages = event => {
82
+ visitHostMessage(event.data, {
83
+ "host.update-parameters": payload => {
84
+ hostEventTarget.dispatchEventMessage("host.update-parameters", payload);
85
+ },
86
+ _unknown: () => {
87
+ // Do nothing
88
+ }
89
+ });
90
+ };
91
+ const sendMessageToHost = message => {
92
+ parentWindow.postMessage(message, hostOrigin);
93
+ };
94
+ return {
95
+ hostEventTarget,
96
+ ready: () => {
97
+ sendMessageToHost({
98
+ type: "widget.ready",
99
+ payload: {
100
+ apiVersion: HostMessage.Version
101
+ }
102
+ });
103
+ },
104
+ emitEvent: (eventId, payload) => {
105
+ sendMessageToHost({
106
+ type: "widget.emit-event",
107
+ payload: {
108
+ eventId,
109
+ ...payload
110
+ }
111
+ });
112
+ },
113
+ sendMessage: sendMessageToHost,
114
+ subscribe: () => {
115
+ window.addEventListener("message", event => {
116
+ if (event.origin !== hostOrigin) {
117
+ // Reject messages that aren't coming from the configured host
118
+ return;
119
+ }
120
+ listenForHostMessages(event);
121
+ });
122
+ },
123
+ unsubscribe: () => {
124
+ window.removeEventListener("message", listenForHostMessages);
125
+ }
126
+ };
127
+ }
128
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","names":["HostMessage","visitHostMessage","META_TAG_HOST_ORIGIN","invariant","FoundryHostEventTarget","createFoundryWidgetClient","window","_createFoundryWidgetClient","__PALANTIR_WIDGET_API__","_createFoundryWidgetClientLegacy","widgetApi","hostEventTarget","listenForHostMessages","event","detail","payload","dispatchEventMessage","_unknown","sendMessageToHost","message","sendMessage","ready","type","apiVersion","Version","emitEvent","eventId","subscribe","addEventListener","unsubscribe","removeEventListener","parent","process","env","NODE_ENV","parentWindow","metaTag","document","querySelector","hostOrigin","getAttribute","data","postMessage","origin"],"sources":["client.ts"],"sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n HostMessage,\n visitHostMessage,\n type WidgetConfig,\n type WidgetMessage,\n} from \"@osdk/widget.api\";\nimport { META_TAG_HOST_ORIGIN } from \"@osdk/widget.api\";\nimport invariant from \"tiny-invariant\";\nimport { FoundryHostEventTarget } from \"./host.js\";\n\nexport interface FoundryWidgetClient<C extends WidgetConfig<C[\"parameters\"]>> {\n /**\n * Notifies the host that this client is ready to receive the first parameter values\n */\n ready: () => void;\n\n /**\n * Emits an event with the given ID and payload\n */\n emitEvent: <M extends WidgetMessage.EmitEvent<C>>(\n eventId: M[\"payload\"][\"eventId\"],\n payload: Omit<M[\"payload\"], \"eventId\">,\n ) => void;\n\n /**\n * Sends a message to the parent frame.\n * It is recommended to use the convenience methods for individual messages (e.g. ready or emitEvent) instead\n */\n sendMessage: <M extends WidgetMessage<C>>(message: M) => void;\n\n /**\n * Subscribes to events from the host, invoking the listener when a message is received\n */\n subscribe: () => void;\n\n /**\n * Unsubscribes a previously subscribed listener from host events, if one exists\n */\n unsubscribe: () => void;\n\n /**\n * Event targets on which you can subscribe to specific host messages\n */\n hostEventTarget: FoundryHostEventTarget<C>;\n}\n\nexport function createFoundryWidgetClient<\n C extends WidgetConfig<C[\"parameters\"]>,\n>(): FoundryWidgetClient<C> {\n if (\"__PALANTIR_WIDGET_API__\" in window) {\n return _createFoundryWidgetClient<C>(\n window.__PALANTIR_WIDGET_API__ as PalantirWidgetApi<C>,\n );\n }\n // todo: drop support for sandbox v1\n return _createFoundryWidgetClientLegacy<C>();\n}\n\ninterface PalantirWidgetApiEvents<C extends WidgetConfig<C[\"parameters\"]>> {\n message: CustomEvent<HostMessage<C>>;\n}\n\ninterface PalantirWidgetApi<C extends WidgetConfig<C[\"parameters\"]>> {\n sendMessage: <M extends WidgetMessage<C>>(message: M) => void;\n addEventListener<K extends keyof PalantirWidgetApiEvents<C>>(\n type: K,\n listener: (ev: PalantirWidgetApiEvents<C>[K]) => any,\n options?: boolean | AddEventListenerOptions,\n ): void;\n removeEventListener<K extends keyof PalantirWidgetApiEvents<C>>(\n type: K,\n listener: (ev: PalantirWidgetApiEvents<C>[K]) => any,\n options?: boolean | EventListenerOptions,\n ): void;\n}\n\nfunction _createFoundryWidgetClient<\n C extends WidgetConfig<C[\"parameters\"]>,\n>(\n widgetApi: PalantirWidgetApi<C>,\n): FoundryWidgetClient<C> {\n const hostEventTarget = new FoundryHostEventTarget<C>();\n\n const listenForHostMessages = (event: CustomEvent<HostMessage<C>>) => {\n visitHostMessage(event.detail, {\n \"host.update-parameters\": (payload) => {\n hostEventTarget.dispatchEventMessage(\"host.update-parameters\", payload);\n },\n _unknown: () => {\n // Do nothing\n },\n });\n };\n const sendMessageToHost = <M extends WidgetMessage<C>>(message: M) => {\n widgetApi.sendMessage(message);\n };\n\n return {\n hostEventTarget,\n ready: () => {\n sendMessageToHost({\n type: \"widget.ready\",\n payload: {\n apiVersion: HostMessage.Version,\n },\n });\n },\n emitEvent: (eventId, payload) => {\n sendMessageToHost({\n type: \"widget.emit-event\",\n payload: {\n eventId,\n ...payload,\n },\n });\n },\n sendMessage: sendMessageToHost,\n subscribe: () => {\n widgetApi.addEventListener(\"message\", listenForHostMessages);\n },\n unsubscribe: () => {\n widgetApi.removeEventListener(\"message\", listenForHostMessages);\n },\n };\n}\n\n// todo: drop support for sandbox v1\nfunction _createFoundryWidgetClientLegacy<\n C extends WidgetConfig<C[\"parameters\"]>,\n>(): FoundryWidgetClient<C> {\n invariant(window.parent, \"[FoundryWidgetClient] Must be run in an iframe\");\n const parentWindow = window.parent;\n const metaTag = document.querySelector(\n `meta[name=\"${META_TAG_HOST_ORIGIN}\"]`,\n );\n invariant(\n metaTag,\n \"[FoundryWidgetClient] Missing host origin meta tag \"\n + META_TAG_HOST_ORIGIN,\n );\n const hostOrigin = metaTag.getAttribute(\"content\");\n invariant(\n hostOrigin,\n \"[FoundryWidgetClient] Missing host origin meta tag content\",\n );\n const hostEventTarget = new FoundryHostEventTarget<C>();\n\n const listenForHostMessages = (event: MessageEvent<HostMessage<C>>) => {\n visitHostMessage(event.data, {\n \"host.update-parameters\": (payload) => {\n hostEventTarget.dispatchEventMessage(\"host.update-parameters\", payload);\n },\n _unknown: () => {\n // Do nothing\n },\n });\n };\n const sendMessageToHost = <M extends WidgetMessage<C>>(message: M) => {\n parentWindow.postMessage(message, hostOrigin);\n };\n\n return {\n hostEventTarget,\n ready: () => {\n sendMessageToHost({\n type: \"widget.ready\",\n payload: {\n apiVersion: HostMessage.Version,\n },\n });\n },\n emitEvent: (eventId, payload) => {\n sendMessageToHost({\n type: \"widget.emit-event\",\n payload: {\n eventId,\n ...payload,\n },\n });\n },\n sendMessage: sendMessageToHost,\n subscribe: () => {\n window.addEventListener(\"message\", (event) => {\n if (event.origin !== hostOrigin) {\n // Reject messages that aren't coming from the configured host\n return;\n }\n listenForHostMessages(event);\n });\n },\n unsubscribe: () => {\n window.removeEventListener(\"message\", listenForHostMessages);\n },\n };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SACEA,WAAW,EACXC,gBAAgB,QAGX,kBAAkB;AACzB,SAASC,oBAAoB,QAAQ,kBAAkB;AACvD,OAAOC,SAAS,MAAM,gBAAgB;AACtC,SAASC,sBAAsB,QAAQ,WAAW;AAsClD,OAAO,SAASC,yBAAyBA,CAAA,EAEb;EAC1B,IAAI,yBAAyB,IAAIC,MAAM,EAAE;IACvC,OAAOC,0BAA0B,CAC/BD,MAAM,CAACE,uBACT,CAAC;EACH;EACA;EACA,OAAOC,gCAAgC,CAAI,CAAC;AAC9C;AAoBA,SAASF,0BAA0BA,CAGjCG,SAA+B,EACP;EACxB,MAAMC,eAAe,GAAG,IAAIP,sBAAsB,CAAI,CAAC;EAEvD,MAAMQ,qBAAq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Gf,KAAK,IAAK;QAC5C,IAAIA,KAAK,CAAC8B,MAAM,KAAKJ,UAAU,EAAE;UAC/B;UACA;QACF;QACA3B,qBAAqB,CAACC,KAAK,CAAC;MAC9B,CAAC,CAAC;IACJ,CAAC;IACDgB,WAAW,EAAEA,CAAA,KAAM;MACjBvB,MAAM,CAACwB,mBAAmB,CAAC,SAAS,EAAElB,qBAAqB,CAAC;IAC9D;EACF,CAAC;AACH","ignoreList":[]}
@@ -0,0 +1,30 @@
1
+ /*
2
+ * Copyright 2024 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export class FoundryHostEventTarget extends EventTarget {
18
+ addEventListener(type, callback, options) {
19
+ super.addEventListener(type, callback, options);
20
+ }
21
+ removeEventListener(type, callback, options) {
22
+ super.removeEventListener(type, callback, options);
23
+ }
24
+ dispatchEventMessage(type, payload) {
25
+ this.dispatchEvent(new CustomEvent(type, {
26
+ detail: payload
27
+ }));
28
+ }
29
+ }
30
+ //# sourceMappingURL=host.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","names":["FoundryHostEventTarget","EventTarget","addEventListener","type","callback","options","removeEventListener","dispatchEventMessage","payload","dispatchEvent","CustomEvent","detail"],"sources":["host.ts"],"sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { HostMessage, WidgetConfig } from \"@osdk/widget.api\";\n\nexport interface HostMessageEventListener<P extends HostMessage.Payload> {\n (event: CustomEvent<P>): void;\n}\n\nexport interface HostMessageEventListenerObject<P extends HostMessage.Payload> {\n handleEvent(object: CustomEvent<P>): void;\n}\n\nexport class FoundryHostEventTarget<\n C extends WidgetConfig<C[\"parameters\"]>,\n> extends EventTarget {\n addEventListener<T extends HostMessage<C>[\"type\"]>(\n type: T,\n callback:\n | HostMessageEventListener<\n (HostMessage<C> & { type: T })[\"payload\"]\n >\n | HostMessageEventListenerObject<\n (HostMessage<C> & { type: T })[\"payload\"]\n >\n | null,\n options?: AddEventListenerOptions | boolean,\n ): void {\n super.addEventListener(type, callback as EventListener, options);\n }\n\n removeEventListener<T extends HostMessage<C>[\"type\"]>(\n type: T,\n callback:\n | HostMessageEventListener<\n (HostMessage<C> & { type: T })[\"payload\"]\n >\n | HostMessageEventListenerObject<\n (HostMessage<C> & { type: T })[\"payload\"]\n >\n | null,\n options?: EventListenerOptions | boolean,\n ): void {\n super.removeEventListener(type, callback as EventListener, options);\n }\n\n public dispatchEventMessage<T extends HostMessage<C>[\"type\"]>(\n type: T,\n payload: (HostMessage<C> & { type: T })[\"payload\"],\n ): void {\n this.dispatchEvent(\n new CustomEvent<HostMessage.Payload>(type, {\n detail: payload,\n }),\n );\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAYA,OAAO,MAAMA,sBAAsB,SAEzBC,WAAW,CAAC;EACpBC,gBAAgBA,CACdC,IAAO,EACPC,QAOQ,EACRC,OAA2C,EACrC;IACN,KAAK,CAACH,gBAAgB,CAACC,IAAI,EAAEC,QAAQ,EAAmBC,OAAO,CAAC;EAClE;EAEAC,mBAAmBA,CACjBH,IAAO,EACPC,QAOQ,EACRC,OAAwC,EAClC;IACN,KAAK,CAACC,mBAAmB,CAACH,IAAI,EAAEC,QAAQ,EAAmBC,OAAO,CAAC;EACrE;EAEOE,oBAAoBA,CACzBJ,IAAO,EACPK,OAAkD,EAC5C;IACN,IAAI,CAACC,aAAa,CAChB,IAAIC,WAAW,CAAsBP,IAAI,EAAE;MACzCQ,MAAM,EAAEH;IACV,CAAC,CACH,CAAC;EACH;AACF","ignoreList":[]}