Skip to content

Commit

Permalink
fix: improve subscribe types
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jul 24, 2023
1 parent 9de2aa8 commit b8a7c33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 43 deletions.
48 changes: 7 additions & 41 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,60 +331,26 @@ export interface IClientSubscribeOptions extends IClientSubscribeProperties {
* */
rh?: number
}

export interface ISubscriptionGrant {
export interface ISubscriptionRequest extends IClientSubscribeOptions {
/**
* is a subscribed to topic
*/
topic: string
}

export interface ISubscriptionGrant
extends Omit<ISubscriptionRequest, 'qos' | 'properties'> {
/**
* is the granted qos level on it, may return 128 on error
*/
qos: QoS | 128
/*
* no local flag
* */
nl?: boolean
/*
* Retain As Published flag
* */
rap?: boolean
/*
* Retain Handling option
* */
rh?: number
}
export interface ISubscriptionRequest extends IClientSubscribeProperties {
/**
* is a subscribed to topic
*/
topic: string
/**
* is the granted qos level on it
*/
qos: QoS
/*
* no local flag
* */
nl?: boolean
/*
* Retain As Published flag
* */
rap?: boolean
/*
* Retain Handling option
* */
rh?: number
}

export interface ISubscriptionOptions
extends Omit<ISubscriptionRequest, 'topic'> {}

export type ISubscriptionMap = {
/**
* object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
*/
[topic: string]: ISubscriptionOptions
[topic: string]: IClientSubscribeOptions
} & {
resubscribe?: boolean
}
Expand Down Expand Up @@ -1180,7 +1146,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
const topics = []
subs.forEach((sub) => {
if (this.options.reconnectPeriod > 0) {
const topic: ISubscriptionOptions = { qos: sub.qos }
const topic: IClientSubscribeOptions = { qos: sub.qos }
if (version === 5) {
topic.nl = sub.nl || false
topic.rap = sub.rap || false
Expand Down
4 changes: 2 additions & 2 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import handlePublish from '../src/lib/handlers/publish'
import {
IClientOptions,
IClientPublishOptions,
ISubscriptionOptions,
IClientSubscribeOptions,
ISubscriptionMap,
ISubscriptionRequest,
} from '../src/lib/client'
Expand Down Expand Up @@ -2223,7 +2223,7 @@ export default function abstractTest(server, config) {
it('should accept an options parameter', function _test(done) {
const client = connect()
const topic = 'test'
const opts: ISubscriptionOptions = { qos: 1 }
const opts: IClientSubscribeOptions = { qos: 1 }

client.once('connect', () => {
client.subscribe(topic, opts)
Expand Down

0 comments on commit b8a7c33

Please sign in to comment.