Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/famous-news-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---

Added custom warning for not available Crypto in non-secure environments (it can be used in localhost or https only)
19 changes: 18 additions & 1 deletion src/app-bridge/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";

import { actions, NotificationPayload, RedirectPayload } from "./actions";

describe("actions.ts", () => {
afterEach(() => {
vi.restoreAllMocks();
});

describe("actions.Notification", () => {
it("Constructs action with \"notification\" type, random id and payload", () => {
const payload: NotificationPayload = {
Expand Down Expand Up @@ -34,4 +38,17 @@ describe("actions.ts", () => {
expect(action.payload).toEqual(expect.objectContaining(payload));
});
});

it("Throws custom error if crypto is not available", () => {
vi.stubGlobal("crypto", {
...globalThis.crypto,
randomUUID: undefined,
});

return expect(() =>
actions.Notification({
title: "Test",
}),
).throws("Failed to generate action ID. Please ensure you are using https or localhost");
});
});
22 changes: 13 additions & 9 deletions src/app-bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ type ActionWithId<Name extends ActionType, Payload extends {}> = {
function withActionId<Name extends ActionType, Payload extends {}, T extends Action<Name, Payload>>(
action: T,
): ActionWithId<Name, Payload> {
const actionId = globalThis.crypto.randomUUID();

return {
...action,
payload: {
...action.payload,
actionId,
},
};
try {
const actionId = globalThis.crypto.randomUUID();

return {
...action,
payload: {
...action.payload,
actionId,
},
};
} catch (e) {
throw new Error("Failed to generate action ID. Please ensure you are using https or localhost");
}
}

export type RedirectPayload = {
Expand Down
Loading