Add saleor versions to app bridge (#260)

This commit is contained in:
Lukasz Ostrowski 2023-06-22 15:46:16 +02:00 committed by GitHub
parent 86d963e1e4
commit b365c7c830
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"@saleor/app-sdk": minor
---
Added saleorVersion and dashboardVersion fields to AppBridge state. They are optional - will be provided from 3.15.

View file

@ -45,6 +45,11 @@ type AppBridgeState = {
* Full URL including protocol and path where GraphQL API is available * Full URL including protocol and path where GraphQL API is available
**/ **/
saleorApiUrl: string; saleorApiUrl: string;
/**
* Versions of Saleor that app is being installed. Available from 3.15.
*/
saleorVersion?: string;
dashboardVersion?: string;
}; };
``` ```

View file

@ -11,6 +11,12 @@ export type AppBridgeState = {
theme: ThemeType; theme: ThemeType;
locale: LocaleCode; locale: LocaleCode;
saleorApiUrl: string; saleorApiUrl: string;
/**
* Versions of Saleor that app is mounted. Passed from the Dashboard.
* Works form Saleor 3.15
*/
saleorVersion?: string;
dashboardVersion?: string;
user?: { user?: {
/** /**
* Original permissions of the user that is using the app. * Original permissions of the user that is using the app.

View file

@ -313,4 +313,24 @@ describe("AppBridge", () => {
expect(appBridge.getState().token).toEqual(tokenRefreshEvent.payload.token); expect(appBridge.getState().token).toEqual(tokenRefreshEvent.payload.token);
}); });
it("Saves saleorVersion field if provided in the Handshake event", () => {
expect(appBridge.getState().saleorVersion).toBeUndefined();
expect(appBridge.getState().dashboardVersion).toBeUndefined();
fireEvent(
window,
new MessageEvent("message", {
data: DashboardEventFactory.createHandshakeEvent(validJwtToken, 1, {
core: "3.15.0",
dashboard: "3.15.1",
}),
origin,
})
);
expect(appBridge.getState().token).toEqual(validJwtToken);
expect(appBridge.getState().saleorVersion).toEqual("3.15.0");
expect(appBridge.getState().dashboardVersion).toEqual("3.15.1");
});
}); });

View file

@ -28,6 +28,8 @@ function eventStateReducer(state: AppBridgeState, event: Events) {
...state, ...state,
ready: true, ready: true,
token: event.payload.token, token: event.payload.token,
saleorVersion: event.payload.saleorVersion,
dashboardVersion: event.payload.dashboardVersion,
user: { user: {
email: userJwtPayload.email, email: userJwtPayload.email,
permissions: userJwtPayload.userPermissions, permissions: userJwtPayload.userPermissions,

View file

@ -4,10 +4,17 @@ import { DashboardEventFactory } from "./events";
describe("DashboardEventFactory", () => { describe("DashboardEventFactory", () => {
it("Creates handshake event", () => { it("Creates handshake event", () => {
expect(DashboardEventFactory.createHandshakeEvent("mock-token")).toEqual({ expect(
DashboardEventFactory.createHandshakeEvent("mock-token", 1, {
dashboard: "3.15.3",
core: "3.15.1",
})
).toEqual({
payload: { payload: {
token: "mock-token", token: "mock-token",
version: 1, version: 1,
saleorVersion: "3.15.1",
dashboardVersion: "3.15.3",
}, },
type: "handshake", type: "handshake",
}); });

View file

@ -24,6 +24,8 @@ export type HandshakeEvent = Event<
{ {
token: string; token: string;
version: Version; version: Version;
saleorVersion?: string;
dashboardVersion?: string;
} }
>; >;
@ -104,12 +106,22 @@ export const DashboardEventFactory = {
}, },
}; };
}, },
createHandshakeEvent(token: string, version: Version = 1): HandshakeEvent { createHandshakeEvent(
token: string,
// eslint-disable-next-line default-param-last
version: Version = 1,
saleorVersions?: {
dashboard: string;
core: string;
}
): HandshakeEvent {
return { return {
type: "handshake", type: "handshake",
payload: { payload: {
token, token,
version, version,
saleorVersion: saleorVersions?.core,
dashboardVersion: saleorVersions?.dashboard,
}, },
}; };
}, },