Skip to content

Commit 79b58b8

Browse files
committed
remove domain from app bridge state
1 parent 7a49084 commit 79b58b8

8 files changed

+28
-75
lines changed

src/app-bridge/app-bridge-provider.test.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe("AppBridgeProvider", () => {
3939
<AppBridgeProvider
4040
appBridgeInstance={
4141
new AppBridge({
42-
targetDomain: domain,
4342
saleorApiUrl,
4443
})
4544
}
@@ -77,19 +76,18 @@ describe("useAppBridge hook", () => {
7776

7877
it("Returned instance provided in Provider", () => {
7978
const appBridge = new AppBridge({
80-
targetDomain: domain,
79+
saleorApiUrl,
8180
});
8281

8382
const { result } = renderHook(() => useAppBridge(), {
8483
wrapper: (props: {}) => <AppBridgeProvider {...props} appBridgeInstance={appBridge} />,
8584
});
8685

87-
expect(result.current.appBridge?.getState().domain).toBe(domain);
86+
expect(result.current.appBridge?.getState().saleorApiUrl).toBe(saleorApiUrl);
8887
});
8988

9089
it("Stores active state in React State", () => {
9190
const appBridge = new AppBridge({
92-
targetDomain: domain,
9391
saleorApiUrl,
9492
});
9593

@@ -120,7 +118,6 @@ describe("useAppBridge hook", () => {
120118
return waitFor(() => {
121119
expect(renderCallback).toHaveBeenCalledTimes(2);
122120
expect(renderCallback).toHaveBeenCalledWith({
123-
domain,
124121
id: "appid",
125122
path: "",
126123
ready: false,

src/app-bridge/app-bridge-state.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe("app-bridge-state.ts", () => {
88

99
expect(instance.getState()).toEqual({
1010
id: "",
11-
domain: "",
1211
ready: false,
1312
path: "/",
1413
theme: "light",
@@ -21,7 +20,6 @@ describe("app-bridge-state.ts", () => {
2120
const instance = new AppBridgeStateContainer();
2221

2322
const newState: Partial<AppBridgeState> = {
24-
domain: "my-saleor-instance.cloud",
2523
saleorApiUrl: "https://my-saleor-instance.cloud/graphql/",
2624
id: "foo-bar",
2725
path: "/",
@@ -42,15 +40,15 @@ describe("app-bridge-state.ts", () => {
4240
expect(
4341
new AppBridgeStateContainer({
4442
initialLocale: "pl",
45-
}).getState().locale
43+
}).getState().locale,
4644
).toBe("pl");
4745
});
4846

4947
it("Can be constructed with initial theme", () => {
5048
expect(
5149
new AppBridgeStateContainer({
5250
initialTheme: "dark",
53-
}).getState().theme
51+
}).getState().theme,
5452
).toBe("dark");
5553
});
5654
});

src/app-bridge/app-bridge-state.ts

-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ export type AppBridgeState = {
66
token?: string;
77
id: string;
88
ready: boolean;
9-
domain: string;
109
path: string;
1110
theme: ThemeType;
1211
locale: LocaleCode;
1312
saleorApiUrl: string;
14-
/**
15-
* Versions of Saleor that app is mounted. Passed from the Dashboard.
16-
* Works form Saleor 3.15
17-
*/
1813
saleorVersion?: string;
1914
dashboardVersion?: string;
2015
user?: {
@@ -39,7 +34,6 @@ type Options = {
3934
export class AppBridgeStateContainer {
4035
private state: AppBridgeState = {
4136
id: "",
42-
domain: "",
4337
saleorApiUrl: "",
4438
ready: false,
4539
path: "/",

src/app-bridge/app-bridge.test.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const mockDashboardActionResponse = (actionType: ActionType, actionID: string) =
6565
payload: { ok: true, actionId: actionID },
6666
} as DispatchResponseEvent,
6767
origin,
68-
})
68+
}),
6969
);
7070
}
7171
}
@@ -96,10 +96,6 @@ describe("AppBridge", () => {
9696
vi.clearAllMocks();
9797
});
9898

99-
it("correctly sets the default domain, if not set in constructor", () => {
100-
expect(appBridge.getState().domain).toEqual(domain);
101-
});
102-
10399
it("authenticates", () => {
104100
expect(appBridge.getState().ready).toBe(false);
105101

@@ -109,7 +105,7 @@ describe("AppBridge", () => {
109105
new MessageEvent("message", {
110106
data: { type: "handshake", payload: { token } },
111107
origin,
112-
})
108+
}),
113109
);
114110

115111
expect(appBridge.getState().ready).toBe(true);
@@ -135,7 +131,7 @@ describe("AppBridge", () => {
135131
new MessageEvent("message", {
136132
data: handshakeEvent,
137133
origin,
138-
})
134+
}),
139135
);
140136

141137
// incorrect event type
@@ -144,7 +140,7 @@ describe("AppBridge", () => {
144140
new MessageEvent("message", {
145141
data: { type: "invalid", payload: { token: "invalid" } },
146142
origin,
147-
})
143+
}),
148144
);
149145

150146
// incorrect origin
@@ -153,7 +149,7 @@ describe("AppBridge", () => {
153149
new MessageEvent("message", {
154150
data: { type: "handshake", payload: { token } },
155151
origin: "http://wrong.origin.com",
156-
})
152+
}),
157153
);
158154

159155
expect(callback).toHaveBeenCalledTimes(1);
@@ -168,7 +164,7 @@ describe("AppBridge", () => {
168164
new MessageEvent("message", {
169165
data: { type: "handshake", payload: { token: validJwtToken } },
170166
origin,
171-
})
167+
}),
172168
);
173169

174170
expect(callback).toHaveBeenCalledTimes(1);
@@ -188,7 +184,7 @@ describe("AppBridge", () => {
188184
new MessageEvent("message", {
189185
data: themeEvent,
190186
origin,
191-
})
187+
}),
192188
);
193189

194190
expect(callback).toHaveBeenCalledOnce();
@@ -197,10 +193,6 @@ describe("AppBridge", () => {
197193
unsubscribe();
198194
});
199195

200-
it("persists domain", () => {
201-
expect(appBridge.getState().domain).toEqual(domain);
202-
});
203-
204196
it("dispatches valid action", () => {
205197
const target = "/test";
206198
const action = actions.Redirect({ to: target });
@@ -225,7 +217,7 @@ describe("AppBridge", () => {
225217
new MessageEvent("message", {
226218
data: handshakeEvent,
227219
origin,
228-
})
220+
}),
229221
);
230222

231223
expect(cb1).toHaveBeenCalledTimes(1);
@@ -238,21 +230,13 @@ describe("AppBridge", () => {
238230
new MessageEvent("message", {
239231
data: handshakeEvent,
240232
origin,
241-
})
233+
}),
242234
);
243235

244236
expect(cb1).toHaveBeenCalledTimes(1);
245237
expect(cb2).toHaveBeenCalledTimes(1);
246238
});
247239

248-
it("attaches domain from options in constructor", () => {
249-
appBridge = new AppBridge({
250-
targetDomain: "https://foo.bar",
251-
});
252-
253-
expect(appBridge.getState().domain).toEqual("https://foo.bar");
254-
});
255-
256240
it.each<LocaleCode>(["pl", "en", "it"])("sets initial locale \"%s\" from constructor", (locale) => {
257241
const instance = new AppBridge({
258242
initialLocale: locale,
@@ -306,7 +290,7 @@ describe("AppBridge", () => {
306290
new MessageEvent("message", {
307291
data: handshakeEvent,
308292
origin,
309-
})
293+
}),
310294
);
311295

312296
expect(appBridge.getState().token).toEqual(handshakeEvent.payload.token);
@@ -316,7 +300,7 @@ describe("AppBridge", () => {
316300
new MessageEvent("message", {
317301
data: tokenRefreshEvent,
318302
origin,
319-
})
303+
}),
320304
);
321305

322306
expect(appBridge.getState().token).toEqual(tokenRefreshEvent.payload.token);
@@ -334,7 +318,7 @@ describe("AppBridge", () => {
334318
dashboard: "3.15.1",
335319
}),
336320
origin,
337-
})
321+
}),
338322
);
339323

340324
expect(appBridge.getState().token).toEqual(validJwtToken);

src/app-bridge/app-bridge.ts

+9-24
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
8585
});
8686

8787
export type AppBridgeOptions = {
88-
targetDomain?: string;
8988
saleorApiUrl?: string;
9089
initialLocale?: LocaleCode;
9190
/**
@@ -103,12 +102,6 @@ const getLocaleFromUrl = () =>
103102
(new URL(window.location.href).searchParams.get(AppIframeParams.LOCALE) as LocaleCode) ||
104103
undefined;
105104

106-
/**
107-
* TODO: Probably remove empty string fallback
108-
*/
109-
const getDomainFromUrl = () =>
110-
new URL(window.location.href).searchParams.get(AppIframeParams.DOMAIN) || "";
111-
112105
const getSaleorApiUrlFromUrl = () =>
113106
new URL(window.location.href).searchParams.get(AppIframeParams.SALEOR_API_URL) || "";
114107

@@ -125,7 +118,6 @@ const getThemeFromUrl = () => {
125118
};
126119

127120
const getDefaultOptions = (): AppBridgeOptions => ({
128-
targetDomain: getDomainFromUrl(),
129121
saleorApiUrl: getSaleorApiUrlFromUrl(),
130122
initialLocale: getLocaleFromUrl() ?? "en",
131123
autoNotifyReady: true,
@@ -146,7 +138,7 @@ export class AppBridge {
146138

147139
if (SSR) {
148140
throw new Error(
149-
"AppBridge detected you're running this app in SSR mode. Make sure to call `new AppBridge()` when window object exists."
141+
"AppBridge detected you're running this app in SSR mode. Make sure to call `new AppBridge()` when window object exists.",
150142
);
151143
}
152144

@@ -170,14 +162,8 @@ export class AppBridge {
170162
debug("?saleorApiUrl was not found in iframe url");
171163
}
172164

173-
if (!this.combinedOptions.targetDomain) {
174-
debug("?domain was not found in iframe url");
175-
}
176-
177-
if (!(this.combinedOptions.saleorApiUrl || this.combinedOptions.targetDomain)) {
178-
console.error(
179-
"domain and saleorApiUrl params were not found in iframe url. Ensure at least one of them is present"
180-
);
165+
if (!this.combinedOptions.saleorApiUrl) {
166+
console.error("saleorApiUrl param was not found in iframe url");
181167
}
182168

183169
this.setInitialState();
@@ -197,7 +183,7 @@ export class AppBridge {
197183
*/
198184
subscribe<TEventType extends EventType, TPayload extends PayloadOfEvent<TEventType>>(
199185
eventType: TEventType,
200-
cb: EventCallback<TPayload>
186+
cb: EventCallback<TPayload>,
201187
) {
202188
debug("subscribe() called with event %s and callback %s", eventType, cb.name);
203189

@@ -251,7 +237,7 @@ export class AppBridge {
251237
type: action.type,
252238
payload: action.payload,
253239
},
254-
"*"
240+
"*",
255241
);
256242

257243
let timeoutId: number;
@@ -261,7 +247,7 @@ export class AppBridge {
261247
"Subscribing to %s with action id: %s and status 'ok' is: %s",
262248
EventType.response,
263249
actionId,
264-
ok
250+
ok,
265251
);
266252

267253
if (action.payload.actionId === actionId) {
@@ -274,8 +260,8 @@ export class AppBridge {
274260
} else {
275261
reject(
276262
new Error(
277-
"Action responded with negative status. This indicates the action method was not used properly."
278-
)
263+
"Action responded with negative status. This indicates the action method was not used properly.",
264+
),
279265
);
280266
}
281267
}
@@ -312,7 +298,6 @@ export class AppBridge {
312298
const path = window.location.pathname || "";
313299

314300
const state: Partial<AppBridgeState> = {
315-
domain: this.combinedOptions.targetDomain,
316301
id,
317302
path,
318303
theme: this.combinedOptions.initialTheme,
@@ -355,7 +340,7 @@ export class AppBridge {
355340
this.subscribeMap[type][key](payload);
356341
});
357342
}
358-
}
343+
},
359344
);
360345
}
361346
}

src/app-bridge/fetch.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe("createAuthenticatedFetch", () => {
1010
const mockedAppBridge: Pick<AppBridge, "getState"> = {
1111
getState(): AppBridgeState {
1212
return {
13-
domain: "master.staging.saleor.cloud",
1413
token: "XXX_YYY",
1514
locale: "pl",
1615
path: "/",

src/app-bridge/index.ts

-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,3 @@ export * from "./fetch";
1010
export * from "./types";
1111
export * from "./use-dashboard-token";
1212
export * from "./with-authorization";
13-
14-
/**
15-
* @deprecated use new AppBridge(), createApp will be removed
16-
*/
17-
export const createApp = (targetDomain?: string) =>
18-
new AppBridge(targetDomain ? { targetDomain } : undefined);
19-
export default createApp;

src/headers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export const SALEOR_EVENT_HEADER = "saleor-event";
22
export const SALEOR_SIGNATURE_HEADER = "saleor-signature";
33
export const SALEOR_AUTHORIZATION_BEARER_HEADER = "authorization-bearer";
44
export const SALEOR_API_URL_HEADER = "saleor-api-url";
5+
/**
6+
* Available when Saleor executes "manifest" or "token exchange" requests.
7+
*/
58
export const SALEOR_SCHEMA_VERSION_HEADER = "saleor-schema-version";
69

710
const toStringOrUndefined = (value: string | string[] | undefined) =>

0 commit comments

Comments
 (0)