Skip to content

Commit

Permalink
BKNDLSS-34660 - [JS]API for delete PUB_SUB channels
Browse files Browse the repository at this point in the history
  • Loading branch information
macbookpro committed Feb 16, 2024
1 parent 16d9f83 commit 45080d7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backendless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ declare module Backendless {

function subscribe(channelName: string): ChannelClass;

function deleteChannel(channelName: string): Promise<object>;

function publish(channelName: string, message: string | Object, publishOptions?: Backendless.PublishOptions, deliveryOptions?: Backendless.DeliveryOptions): Promise<Object>;

function sendEmail(subject: string, bodyParts: Backendless.Bodyparts, recipients: string[], attachments?: string[]): Promise<object>;
Expand Down
8 changes: 8 additions & 0 deletions src/messaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export default class Messaging {
})
}

async deleteChannel(channelName) {
validateChannelName(channelName)

return this.app.request.delete({
url : this.app.urls.messagingChannelName(channelName),
})
}

async pushWithTemplate(templateName, templateValues) {
if (!templateName || typeof templateName !== 'string') {
throw new Error('Push Template Name must be provided and must be a string.')
Expand Down
8 changes: 8 additions & 0 deletions src/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ export default class Urls {
return `${this.messaging()}/${channel}`
}

messagingChannels() {
return `${this.messaging()}/channels`
}

messagingChannelName(channelName) {
return `${this.messagingChannels()}/${channelName}`
}

messagingMessage(messageId) {
return `${this.messaging()}/${messageId}`
}
Expand Down
2 changes: 2 additions & 0 deletions test/tsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,8 @@ function testMessaging() {

channel = Backendless.Messaging.subscribe(channelName);

promiseObject = Backendless.Messaging.deleteChannel(channelName);

promiseObject = Backendless.Messaging.publish(channelName, message, publishOptions, deliveryOptions);

promiseObject = Backendless.Messaging.sendEmail(subject, bodyParts, recipients, attachments);
Expand Down
37 changes: 37 additions & 0 deletions test/unit/specs/messaging/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,43 @@ describe('<Messaging> Channel', function() {

})

describe('Delete', () => {
it('deletes the specified channel', async () => {
const req1 = prepareMockRequest()

await Backendless.Messaging.deleteChannel(channelName)

expect(req1).to.deep.include({
method: 'DELETE',
path: `${APP_PATH}/messaging/channels/${channelName}`,
})
})

it('fails when channelName is invalid', async () => {
const errorMsg = 'Channel Name must be provided and must be a string.'

await expect( Backendless.Messaging.deleteChannel()).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(null)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(true)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(false)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(0)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(123)).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel('')).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel({})).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel([])).to.eventually.be.rejectedWith(errorMsg)
await expect( Backendless.Messaging.deleteChannel(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
})

it('fails when channelName has a slash char', async () => {
const errorMsg = 'Channel Name can not contain slash chars'

await expect(Backendless.Messaging.deleteChannel('/channelName')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Messaging.deleteChannel('channel/Name')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Messaging.deleteChannel('channelName/')).to.eventually.be.rejectedWith(errorMsg)
})
})

describe('Connection', () => {
it('can join and leave the channel', async () => {
const sub1Promise = rtClient.getNext_SUB_ON() // PUB_SUB_CONNECT
Expand Down

0 comments on commit 45080d7

Please sign in to comment.