From 1f38cc0cf8398a4120500695be995f3318e47028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= <3604053+cusspvz@users.noreply.github.com> Date: Mon, 1 Feb 2021 12:25:09 +0000 Subject: [PATCH 1/3] Create a typescript definitions file --- lib/index.d.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/index.d.ts diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..0f5c068 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,32 @@ +type MethodName = string +type Method = (...Args) => Returning | Promise +type Args = any[] + +type CallerOptions = { + targetOrigin?: string + postMessage(data: any, targetOrigin?: string): void | Promise +} + +type ExposeOptions = { + isCallback?: boolean + targetOrigin?: string + postMessage(data: any, targetOrigin?: string): void | Promise +} + +// client.js +export function caller( + methodName: MethodName, + options: CallerOptions +): (...args: Args) => Promise + +export function call( + methodName: MethodName, + ...args: Args +): Promise + +// server.js +export function expose( + methodName: MethodName, + method: Method, + options: CallerOptions +): (...args: Args) => Promise From ad84e20c29a50907fadf7db9d69793b955e5b985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= <3604053+cusspvz@users.noreply.github.com> Date: Mon, 1 Feb 2021 12:43:31 +0000 Subject: [PATCH 2/3] chore: add event listeners related options --- lib/index.d.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 0f5c068..edb1dde 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -2,18 +2,21 @@ type MethodName = string type Method = (...Args) => Returning | Promise type Args = any[] -type CallerOptions = { - targetOrigin?: string +type Listener = (event: any) => void + +type BidirectionalMessagingOptions = { + addListener?: (eventName: string, handler: Listener) => void + removeListener?: (eventName: string, handler: Listener) => void + getMessageData?: (event: any) => any postMessage(data: any, targetOrigin?: string): void | Promise + targetOrigin?: string } -type ExposeOptions = { +type CallerOptions = BidirectionalMessagingOptions +type ExposeOptions = BidirectionalMessagingOptions & { isCallback?: boolean - targetOrigin?: string - postMessage(data: any, targetOrigin?: string): void | Promise } -// client.js export function caller( methodName: MethodName, options: CallerOptions @@ -24,7 +27,6 @@ export function call( ...args: Args ): Promise -// server.js export function expose( methodName: MethodName, method: Method, From cc2711171b2bd437c51ba147d162d59ceae79f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= <3604053+cusspvz@users.noreply.github.com> Date: Mon, 1 Feb 2021 13:02:00 +0000 Subject: [PATCH 3/3] chore: export some of the relevant types --- lib/index.d.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index edb1dde..a9c587b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,8 +1,9 @@ -type MethodName = string -type Method = (...Args) => Returning | Promise -type Args = any[] +export type MethodName = string +export type Method = ( + ...Args +) => Returning | Promise -type Listener = (event: any) => void +export type Listener = (event: any) => void type BidirectionalMessagingOptions = { addListener?: (eventName: string, handler: Listener) => void @@ -12,8 +13,8 @@ type BidirectionalMessagingOptions = { targetOrigin?: string } -type CallerOptions = BidirectionalMessagingOptions -type ExposeOptions = BidirectionalMessagingOptions & { +export type CallerOptions = BidirectionalMessagingOptions +export type ExposeOptions = BidirectionalMessagingOptions & { isCallback?: boolean }