host-interop

A collection of helper functions to interact with applications hosts. These should not be used directly but instead imported in function packages that need to interact with a host.

Currently hosts include Hub and RQ.

Installation#

npm install @iqmetrix/host-interop

Functions#

All inner types can be found in types.ts

interop#

Send an interop request from an application to a host.

interface InteropOptions<T> {
request: RequestOptions<T>;
timeout?: number;
target?: Window;
source?: EventTarget;
}
function interop<RequestBody, ResponseBody>(
options: InteropOptions<RequestBody>
): Promise<InteropSuccessResponse<ResponseBody>>;

addInteropHandler#

Respond to an interop request sent from an application in a host.

interface AddInteropHandlerOptions<Request, Response> {
requestHandler: (event: Request) => Response | Promise<Response>;
name: string;
version: string;
target?: EventTarget;
}
function addInteropHandler<Request, Response>(
options: AddInteropHandlerOptions<Request, Response>
);

removeInteropHandler#

Remove an interop request handler from a host.

interface RemoveInteropHandlerOptions {
listener: EventListenerOrEventListenerObject;
target?: EventTarget;
}
function removeInteropHandler(options: RemoveInteropHandlerOptions): void;

Examples#

Making a request from a function package#

import { interop } from "@iqmetrix/host-interop";
export default async function foo(val: string): Promise<string> {
const response = await interop<string, string>({
request: {
name: "foo",
version: "1",
body: val,
},
});
// do some validation
return response.body;
}

Responding to a request from a Javascript host#

import { addInteropHandler } from "@iqmetrix/host-interop";
// Respond to a 'foo' request with 'bar: {request.body}'
addInteropHandler<string, string>({
name: "foo",
version: "1",
requestHandler: (body: string) => `bar: ${body}`,
});
// Respond to a 'foo' request with 'bar: {asyncOperationResult}'
addInteropHandler<string, string>({
name: "foo",
version: "1",
requestHandler: async (body: string) => {
const asyncOperationResult = await asyncOperation(body);
return `bar: ${asyncOperationResult}`;
});
})

Sources#

Last updated on by Garrett Samm