Skip to content

Commit 0e884cc

Browse files
committed
Add extras in the message data if provided for channel push notifications.
1 parent d2bc376 commit 0e884cc

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

src/commands/channels/publish.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default class ChannelsPublish extends AblyBaseCommand {
3131
'$ ably channels publish --transport realtime my-channel "Using realtime transport"',
3232
'$ ably channels publish my-channel "Hello World" --json',
3333
'$ ably channels publish my-channel "Hello World" --pretty-json',
34+
'$ ably channels publish my-channel \'{"data":"Push notification","extras":{"push":{"notification":{"title":"Hello","body":"World"}}}}\'',
3435
];
3536

3637
static override flags = {
@@ -206,11 +207,18 @@ export default class ChannelsPublish extends AblyBaseCommand {
206207
delete messageData.name;
207208
}
208209

210+
// Add extras if provided in the message data (before processing data)
211+
if (messageData.extras && typeof messageData.extras === "object" && Object.keys(messageData.extras).length > 0) {
212+
message.extras = messageData.extras;
213+
// Remove extras from messageData to avoid duplication in data
214+
delete messageData.extras;
215+
}
216+
209217
// If data is explicitly provided in the message, use it
210218
if ("data" in messageData) {
211219
message.data = messageData.data;
212-
} else {
213-
// Otherwise use the entire messageData as the data
220+
} else if (Object.keys(messageData).length > 0) {
221+
// Otherwise use the entire messageData object (not empty) as the data
214222
message.data = messageData;
215223
}
216224

test/unit/commands/channels/publish.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,82 @@ describe("ChannelsPublish", function () {
399399
expect(stdout).toMatch(/error/i);
400400
});
401401
});
402+
403+
describe("should publish a message with data and extras", function () {
404+
it("should include extras.push when provided in message data", async function () {
405+
const restMock = getMockAblyRest();
406+
const channel = restMock.channels._getChannel("test-channel");
407+
408+
await runCommand(
409+
[
410+
"channels:publish",
411+
"test-channel",
412+
'{"data":"hello","extras":{"push":{"notification":{"title":"Test","body":"Push notification"}}}}',
413+
"--transport",
414+
"rest",
415+
],
416+
import.meta.url,
417+
);
418+
419+
expect(channel.publish).toHaveBeenCalledOnce();
420+
const publishArgs = channel.publish.mock.calls[0][0];
421+
expect(publishArgs).toHaveProperty("data", "hello");
422+
expect(publishArgs).toHaveProperty("extras");
423+
expect(publishArgs.extras).toHaveProperty("push");
424+
expect(publishArgs.extras.push).toEqual({
425+
notification: { title: "Test", body: "Push notification" },
426+
});
427+
});
428+
429+
it("should publish a message when only extras is provided without data", async function () {
430+
const restMock = getMockAblyRest();
431+
const channel = restMock.channels._getChannel("test-channel");
432+
433+
await runCommand(
434+
[
435+
"channels:publish",
436+
"test-channel",
437+
'{"extras":{"push":{"notification":{"title":"Extras only","body":"No data field"}}}}',
438+
"--transport",
439+
"rest",
440+
],
441+
import.meta.url,
442+
);
443+
444+
expect(channel.publish).toHaveBeenCalledOnce();
445+
const publishArgs = channel.publish.mock.calls[0][0];
446+
expect(publishArgs).toHaveProperty("extras");
447+
expect(publishArgs.extras).toHaveProperty("push");
448+
expect(publishArgs.extras.push).toEqual({
449+
notification: { title: "Extras only", body: "No data field" },
450+
});
451+
expect(publishArgs).not.toHaveProperty("data");
452+
});
453+
454+
it("should preserve name when extras is provided without data", async function () {
455+
const restMock = getMockAblyRest();
456+
const channel = restMock.channels._getChannel("test-channel");
457+
458+
await runCommand(
459+
[
460+
"channels:publish",
461+
"test-channel",
462+
'{"name":"eventName","extras":{"push":{"notification":{"title":"With name","body":"No data field"}}}}',
463+
"--transport",
464+
"rest",
465+
],
466+
import.meta.url,
467+
);
468+
469+
expect(channel.publish).toHaveBeenCalledOnce();
470+
const publishArgs = channel.publish.mock.calls[0][0];
471+
expect(publishArgs).toHaveProperty("name", "eventName");
472+
expect(publishArgs).toHaveProperty("extras");
473+
expect(publishArgs.extras).toHaveProperty("push");
474+
expect(publishArgs.extras.push).toEqual({
475+
notification: { title: "With name", body: "No data field" },
476+
});
477+
expect(publishArgs).not.toHaveProperty("data");
478+
});
479+
});
402480
});

0 commit comments

Comments
 (0)