Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Add app ID to state #4

Merged
merged 8 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { SSR } from "./constants";
import { Events, EventType, PayloadOfEvent } from "./events";

type State = {
export type AppBridgeState = {
token?: string;
id: string;
ready: boolean;
domain: string;
};
Expand All @@ -11,7 +12,7 @@ type SubscribeMap = {
[type in EventType]: Record<any, EventCallback<PayloadOfEvent<type>>>;
};

function reducer(state: State, event: Events) {
function reducer(state: AppBridgeState, event: Events) {
switch (event.type) {
case EventType.handshake: {
const newState = {
Expand All @@ -36,7 +37,8 @@ export const app = (() => {
return null as never;
}

let state: State = {
let state: AppBridgeState = {
id: "",
domain: "",
ready: false,
};
Expand Down Expand Up @@ -120,7 +122,7 @@ export const app = (() => {
return state;
}

function setState(newState: Partial<State>) {
function setState(newState: Partial<AppBridgeState>) {
state = {
...state,
...newState,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function createApp(targetDomain?: string) {
domain = url.searchParams.get("domain") || "";
}

app.setState({ domain });
const id = new URL(domain).searchParams.get("id") ?? "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of that should we have id = url.searchParams.get("id") in else clause above?


app.setState({ domain, id });

/**
* Dispatches Action to Saleor Dashboard.
Expand Down Expand Up @@ -72,4 +74,5 @@ export function createApp(targetDomain?: string) {

export * from "./events";
export * from "./actions";
export * from "./types";
export default createApp;
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createApp from ".";
import { AppBridgeState } from "./app";

export type App = ReturnType<typeof createApp>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this, explicitly typing return type by function createApp(): Type is making breaking api harder. Now it's possible to change function signature and no one will know until consumers start complaining about breaking changes.

export { AppBridgeState };
3 changes: 2 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in dashboard we have shop.domain.host passed here, isn't that just a domain (without schema)? also as you pass src={urlJoin(src, `?domain=${shop.domain.host}&id=${appId}`)} in dashboard, should that be test-domain.com&id=appid here?

const app = createApp(domain);

it("correctly sets the domain", () => {
Expand Down Expand Up @@ -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();
Expand Down