@blockprotocol/external-api
Version:
Implementation of the Block Protocol external API service specification for blocks and embedding applications
49 lines • 1.9 kB
JavaScript
import { ServiceHandler } from "@blockprotocol/core";
/**
* Creates a handler for the externalApi service for the embedder.
* Register callbacks in the constructor or afterwards using the 'on' method to
* react to messages from the block. Call the relevant methods to send messages
* to the block.
*/
export class ExternalApiEmbedderHandler extends ServiceHandler {
constructor({ callbacks, element, }) {
super({
element,
callbacks,
serviceName: "externalApi",
sourceType: "embedder",
});
}
/**
* Call the provided function when the named message is received, passing the
* data/errors object from the message. If the named message expects a
* response, the callback should provide the expected data/errors object as
* the return.
* @param messageName the message name to listen for
* @param handlerFunction the function to call when the message is received,
* with the message data / errors
*/
on(messageName, handlerFunction) {
// @todo restore this when module resolution issue resolved
// @see https://app.asana.com/0/1202542409311090/1202614421149286/f
// const expectedMessageSource = "block";
// const messageJsonDefinition = externalApiServiceJson.messages.find(
// (message) =>
// message.messageName === messageName &&
// message.source === expectedMessageSource,
// );
// if (!messageJsonDefinition) {
// throw new Error(
// `No message with name '${messageName}' expected from ${expectedMessageSource}.`,
// );
// }
this.registerCallback({
callback: handlerFunction,
messageName,
});
}
getInitPayload() {
return {};
}
}
//# sourceMappingURL=external-api-embedder-handler.js.map