UNPKG

@blockprotocol/external-api

Version:

Implementation of the Block Protocol external API service specification for blocks and embedding applications

147 lines 5.14 kB
import { ServiceHandler } from "@blockprotocol/core"; /** * Creates a handler for the externalApi service for the block. * Register callbacks in the constructor or afterwards using the 'on' method to react to messages from the embedder. * Call the relevant methods to send messages to the embedder. */ export class ExternalApiBlockHandler extends ServiceHandler { constructor({ callbacks, element, }) { super({ element, callbacks, serviceName: "externalApi", sourceType: "block", }); } getInitPayload() { // there are no block messages which are sentOnInitialization in the externalApi service return {}; } /** * Registers multiple callbacks at once. * Useful for bulk updates to callbacks after the service is first initialised. */ registerCallbacks(callbacks) { super.registerCallbacks(callbacks); } /** * Removes multiple callbacks at once. * Useful when replacing previously registered callbacks */ removeCallbacks(callbacks) { super.removeCallbacks(callbacks); } /** * 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 = "embedder"; // 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, }); } // @todo automate creation of these methods from externalApi-service.json and types.ts /** Mapbox Geocoding API */ mapboxForwardGeocoding({ data }) { return this.sendMessage({ message: { messageName: "mapboxForwardGeocoding", data, }, respondedToBy: "mapboxForwardGeocodingResponse", }); } mapboxReverseGeocoding({ data }) { return this.sendMessage({ message: { messageName: "mapboxReverseGeocoding", data, }, respondedToBy: "mapboxReverseGeocodingResponse", }); } // mapboxBatchGeocoding({ data }: { data?: MapboxBatchGeocodingData }) { // return this.sendMessage<MapboxBatchGeocodingResponseData, null>({ // message: { // messageName: "mapbox.batchGeocoding", // data, // }, // respondedToBy: "mapbox.batchGeocodingResponse", // }); // } /** Mapbox Directions API */ mapboxRetrieveDirections({ data }) { return this.sendMessage({ message: { messageName: "mapboxRetrieveDirections", data, }, respondedToBy: "mapboxRetrieveDirectionsResponse", }); } /** Mapbox Isochrone API */ mapboxRetrieveIsochrones({ data }) { return this.sendMessage({ message: { messageName: "mapboxRetrieveIsochrones", data, }, respondedToBy: "mapboxRetrieveIsochronesResponse", }); } /** Mapbox Autofill API */ mapboxSuggestAddress({ data }) { return this.sendMessage({ message: { messageName: "mapboxSuggestAddress", data, }, respondedToBy: "mapboxSuggestAddressResponse", }); } mapboxRetrieveAddress({ data }) { return this.sendMessage({ message: { messageName: "mapboxRetrieveAddress", data, }, respondedToBy: "mapboxRetrieveAddressResponse", }); } mapboxCanRetrieveAddress({ data }) { return this.sendMessage({ message: { messageName: "mapboxCanRetrieveAddress", data, }, respondedToBy: "mapboxCanRetrieveAddressResponse", }); } /** Mapbox Static Maps API */ mapboxRetrieveStaticMap({ data }) { return this.sendMessage({ message: { messageName: "mapboxRetrieveStaticMap", data, }, respondedToBy: "mapboxRetrieveStaticMapResponse", }); } } //# sourceMappingURL=external-api-block-handler.js.map