From 67b760d528c8316fb0fd255205ab87b4dc11e8d5 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 14:16:27 +0200 Subject: [PATCH 1/8] Get id from query params --- src/app.ts | 1 + src/index.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 2c74c47..e77e864 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,6 +3,7 @@ import { Events, EventType, PayloadOfEvent } from "./events"; type State = { token?: string; + id?: string; ready: boolean; domain: string; }; diff --git a/src/index.ts b/src/index.ts index 66ae734..74dc5f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,15 +4,17 @@ import { EventType } from "./events"; export function createApp(targetDomain?: string) { let domain: string; + let id: string | undefined; if (targetDomain) { domain = targetDomain; } else { const url = new URL(window.location.href); domain = url.searchParams.get("domain") || ""; + id = url.searchParams.get("id") ?? ""; } - app.setState({ domain }); + app.setState({ domain, id }); /** * Dispatches Action to Saleor Dashboard. From b713fa6d999bf41ba303e3b04b7931a27bc99ea2 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 14:16:35 +0200 Subject: [PATCH 2/8] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 396c6a9..b4104ef 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ const app = createApp(); Access app state: ```js -const { token, domain, ready } = app.getState(); +const { token, domain, ready, id } = app.getState(); ``` ## Events From 42a2617847ec3d3d70713e4a5ed5e721e306a05d Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 16:22:47 +0200 Subject: [PATCH 3/8] Require app ID --- src/app.ts | 3 ++- src/index.ts | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index e77e864..5379e05 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ import { Events, EventType, PayloadOfEvent } from "./events"; type State = { token?: string; - id?: string; + id: string; ready: boolean; domain: string; }; @@ -38,6 +38,7 @@ export const app = (() => { } let state: State = { + id: "", domain: "", ready: false, }; diff --git a/src/index.ts b/src/index.ts index 74dc5f7..e7b06ca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,16 +4,17 @@ import { EventType } from "./events"; export function createApp(targetDomain?: string) { let domain: string; - let id: string | undefined; + let id: string; if (targetDomain) { domain = targetDomain; } else { const url = new URL(window.location.href); domain = url.searchParams.get("domain") || ""; - id = url.searchParams.get("id") ?? ""; } + id = new URL(domain).searchParams.get("id") ?? ""; + app.setState({ domain, id }); /** From b7ed7e7e189978d3405a8a429097a4099cdc1da9 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 16:32:41 +0200 Subject: [PATCH 4/8] Update tests --- test/index.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index d1d520f..68adb90 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -10,7 +10,7 @@ Object.defineProperty(window.document, "referrer", { import { actions, DispatchResponseEvent, createApp } from "../src"; describe("createApp", () => { - const domain = "test-domain"; + const domain = "http://test-domain.com/?id=appid"; const app = createApp(domain); it("correctly sets the domain", () => { @@ -75,6 +75,7 @@ describe("createApp", () => { expect(callback).toHaveBeenCalledTimes(1); expect(callback).toHaveBeenCalledWith(payload); expect(app.getState().token).toEqual(token); + expect(app.getState().id).toEqual("appid"); // unsubscribe unsubscribe(); From ec162f13679372bdf70a4abca2802f20dd1fa98e Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 16:37:24 +0200 Subject: [PATCH 5/8] Cleanup id initialisation --- src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e7b06ca..0a77395 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,7 @@ import { app } from "./app"; import { EventType } from "./events"; export function createApp(targetDomain?: string) { - let domain: string; - let id: string; + let domain: string, id: string; if (targetDomain) { domain = targetDomain; From d4c0483833559a1b6792dbe5f45e98a7ce2356db Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 17:02:50 +0200 Subject: [PATCH 6/8] Export types --- src/app.ts | 8 ++++---- src/index.ts | 1 + src/types.ts | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 src/types.ts diff --git a/src/app.ts b/src/app.ts index 5379e05..4f84d1a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import { SSR } from "./constants"; import { Events, EventType, PayloadOfEvent } from "./events"; -type State = { +export type AppBridgeState = { token?: string; id: string; ready: boolean; @@ -12,7 +12,7 @@ type SubscribeMap = { [type in EventType]: Record>>; }; -function reducer(state: State, event: Events) { +function reducer(state: AppBridgeState, event: Events) { switch (event.type) { case EventType.handshake: { const newState = { @@ -37,7 +37,7 @@ export const app = (() => { return null as never; } - let state: State = { + let state: AppBridgeState = { id: "", domain: "", ready: false, @@ -122,7 +122,7 @@ export const app = (() => { return state; } - function setState(newState: Partial) { + function setState(newState: Partial) { state = { ...state, ...newState, diff --git a/src/index.ts b/src/index.ts index 0a77395..f2a6880 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,4 +74,5 @@ export function createApp(targetDomain?: string) { export * from "./events"; export * from "./actions"; +export * from "./types"; export default createApp; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..30e2ad0 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,5 @@ +import createApp from "."; +import { AppBridgeState } from "./app"; + +export type App = typeof createApp; +export { AppBridgeState }; From ab1799cf1c09dac5a62f2c2b25fdb9b2187fd36c Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 13 Apr 2022 17:06:54 +0200 Subject: [PATCH 7/8] Fix types --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 30e2ad0..71d7285 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ import createApp from "."; import { AppBridgeState } from "./app"; -export type App = typeof createApp; +export type App = ReturnType; export { AppBridgeState }; From 9bb248d656121245181d9954e39c5e8969e3f051 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Thu, 14 Apr 2022 13:42:19 +0200 Subject: [PATCH 8/8] Change id let to const --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f2a6880..efdf590 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { app } from "./app"; import { EventType } from "./events"; export function createApp(targetDomain?: string) { - let domain: string, id: string; + let domain: string; if (targetDomain) { domain = targetDomain; @@ -12,7 +12,7 @@ export function createApp(targetDomain?: string) { domain = url.searchParams.get("domain") || ""; } - id = new URL(domain).searchParams.get("id") ?? ""; + const id = new URL(domain).searchParams.get("id") ?? ""; app.setState({ domain, id });