Add API endpoints to get current widget postMessage API version and supported API versions.
parent
5239729e8e
commit
58616a45ef
|
@ -17,23 +17,23 @@ limitations under the License.
|
||||||
/*
|
/*
|
||||||
Listens for incoming postMessage requests from embedded widgets. The following API is exposed:
|
Listens for incoming postMessage requests from embedded widgets. The following API is exposed:
|
||||||
{
|
{
|
||||||
widgetData: {
|
api: "widget",
|
||||||
action: "content_loaded"
|
action: "content_loaded",
|
||||||
// additional request fields
|
// additional request fields
|
||||||
},
|
|
||||||
widgetId: $WIDGET_ID
|
widgetId: $WIDGET_ID
|
||||||
}
|
}
|
||||||
|
|
||||||
The complete request object is returned to the caller with an additional "response" key like so:
|
The complete request object is returned to the caller with an additional "response" key like so:
|
||||||
{
|
{
|
||||||
widgetData: {
|
api: "widget",
|
||||||
action: "content_loaded"
|
action: "content_loaded",
|
||||||
// additional request fields
|
// additional request fields
|
||||||
},
|
|
||||||
widgetId: $WIDGET_ID
|
widgetId: $WIDGET_ID
|
||||||
response: { ... }
|
response: { ... }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
The "api" field is required to use this API, and must be set to "widget" in all requests.
|
||||||
|
|
||||||
The "action" determines the format of the request and response. All actions can return an error response.
|
The "action" determines the format of the request and response. All actions can return an error response.
|
||||||
|
|
||||||
A success response is an object with zero or more keys.
|
A success response is an object with zero or more keys.
|
||||||
|
@ -50,6 +50,7 @@ The "message" key should be a human-friendly string.
|
||||||
|
|
||||||
ACTIONS
|
ACTIONS
|
||||||
=======
|
=======
|
||||||
|
** All actions must include an "api" field with valie "widget".**
|
||||||
All actions can return an error response instead of the response outlined below.
|
All actions can return an error response instead of the response outlined below.
|
||||||
|
|
||||||
content_loaded
|
content_loaded
|
||||||
|
@ -65,13 +66,52 @@ Response:
|
||||||
}
|
}
|
||||||
Example:
|
Example:
|
||||||
{
|
{
|
||||||
widgetData: {
|
api: "widget",
|
||||||
action: "content_loaded"
|
action: "content_loaded",
|
||||||
},
|
|
||||||
widgetId: $WIDGET_ID
|
widgetId: $WIDGET_ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
api_version
|
||||||
|
-----------
|
||||||
|
Get the current version of the widget postMessage API
|
||||||
|
|
||||||
|
Request:
|
||||||
|
- No additional fields.
|
||||||
|
Response:
|
||||||
|
{
|
||||||
|
api_version: "0.0.1"
|
||||||
|
}
|
||||||
|
Example:
|
||||||
|
{
|
||||||
|
api: "widget",
|
||||||
|
action: "api_version",
|
||||||
|
}
|
||||||
|
|
||||||
|
supported_api_versions
|
||||||
|
----------------------
|
||||||
|
Get versions of the widget postMessage API that are currently supported
|
||||||
|
|
||||||
|
Request:
|
||||||
|
- No additional fields.
|
||||||
|
Response:
|
||||||
|
{
|
||||||
|
api: "widget"
|
||||||
|
supported_versions: ["0.0.1"]
|
||||||
|
}
|
||||||
|
Example:
|
||||||
|
{
|
||||||
|
api: "widget",
|
||||||
|
action: "supported_api_versions",
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const WIDGET_API_VERSION = '0.0.1'; // Current API version
|
||||||
|
const SUPPORTED_WIDGET_API_VERSIONS = [
|
||||||
|
'0.0.1',
|
||||||
|
];
|
||||||
|
|
||||||
import dis from './dispatcher';
|
import dis from './dispatcher';
|
||||||
|
|
||||||
let listenerCount = 0;
|
let listenerCount = 0;
|
||||||
|
@ -91,20 +131,30 @@ function onMessage(event) {
|
||||||
if (
|
if (
|
||||||
event.origin.length === 0 ||
|
event.origin.length === 0 ||
|
||||||
trustedEndpoint(event.origin) ||
|
trustedEndpoint(event.origin) ||
|
||||||
!event.data.widgetData ||
|
event.data.api !== "widget" ||
|
||||||
!event.data.widgetId
|
!event.data.widgetId
|
||||||
) {
|
) {
|
||||||
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
||||||
}
|
}
|
||||||
|
|
||||||
const widgetData = event.data.widgetData;
|
const action = event.data.action;
|
||||||
const widgetId = event.data.widgetId;
|
const widgetId = event.data.widgetId;
|
||||||
if (widgetData.action == 'content_loaded') {
|
if (action == 'content_loaded') {
|
||||||
dis.dispatch({
|
dis.dispatch({
|
||||||
action: 'widget_content_loaded',
|
action: 'widget_content_loaded',
|
||||||
widgetId: widgetId,
|
widgetId: widgetId,
|
||||||
});
|
});
|
||||||
sendResponse(event, {success: true});
|
sendResponse(event, {success: true});
|
||||||
|
} else if (action == 'supported_api_versions') {
|
||||||
|
sendResponse(event, {
|
||||||
|
api: "widget",
|
||||||
|
supported_versions: SUPPORTED_WIDGET_API_VERSIONS,
|
||||||
|
});
|
||||||
|
} else if (action == 'api_version') {
|
||||||
|
sendResponse(event, {
|
||||||
|
api: "widget",
|
||||||
|
version: WIDGET_API_VERSION,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
console.warn("Widget postMessage event unhandled");
|
console.warn("Widget postMessage event unhandled");
|
||||||
sendError(event, {message: "The postMessage was unhandled"});
|
sendError(event, {message: "The postMessage was unhandled"});
|
||||||
|
|
Loading…
Reference in New Issue