Skip to content

Commit

Permalink
chore: v3 request reply support (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenRover authored Apr 24, 2023
1 parent 58b8b85 commit b59983c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/models/operation-reply.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { BaseModel } from './base';
import type { ExtensionsMixinInterface } from './mixins';
import type { ChannelInterface } from './channel';
import type { MessagesInterface } from './messages';

import type { OperationReplyAddressInterface } from './operation-reply-address';

export interface OperationReplyInterface extends BaseModel, ExtensionsMixinInterface {
Expand All @@ -9,4 +11,5 @@ export interface OperationReplyInterface extends BaseModel, ExtensionsMixinInter
address(): OperationReplyAddressInterface | undefined;
hasChannel(): boolean;
channel(): ChannelInterface | undefined;
messages(): MessagesInterface;
}
1 change: 1 addition & 0 deletions src/models/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface OperationInterface extends BaseModel, OperationTraitInterface {
messages(): MessagesInterface;
reply(): OperationReplyInterface | undefined;
traits(): OperationTraitsInterface;
hasReply(): boolean;
}
10 changes: 10 additions & 0 deletions src/models/v3/operation-reply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BaseModel } from '../base';
import { Channel } from './channel';
import { Message } from './message';
import { Messages } from '../messages';
import { MessagesInterface } from 'models/messages';
import { OperationReplyAddress } from './operation-reply-address';

import { extensions } from './mixins';
Expand Down Expand Up @@ -36,6 +39,13 @@ export class OperationReply extends BaseModel<v3.OperationReplyObject, { id?: st
}
return this._json.channel;
}
messages(): MessagesInterface {
return new Messages(
Object.entries(this._json.messages || {}).map(([messageName, message]) => {
return this.createModel(Message, message, { id: messageName, pointer: this.jsonPath(`messages/${messageName}`) });
})
);
}

extensions(): ExtensionsInterface {
return extensions(this);
Expand Down
4 changes: 4 additions & 0 deletions src/models/v3/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class Operation extends OperationTrait<v3.OperationObject> implements Ope
return new Messages(messages);
}

hasReply(): boolean {
return !!this._json.reply;
}

reply(): OperationReplyInterface | undefined {
if (this._json.reply) {
return this.createModel(OperationReply, this._json.reply as v3.OperationReplyObject, { pointer: this.jsonPath('reply') });
Expand Down
1 change: 1 addition & 0 deletions src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export interface OperationTraitObject extends SpecificationExtensions {

export interface OperationReplyObject extends SpecificationExtensions {
channel?: ChannelObject | ReferenceObject;
messages?: MessagesObject;
address?: OperationReplyAddressObject | ReferenceObject;
}

Expand Down
26 changes: 26 additions & 0 deletions test/models/v3/operation-reply.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Channel } from '../../../src/models/v3/channel';
import { OperationReply } from '../../../src/models/v3/operation-reply';
import { OperationReplyAddress } from '../../../src/models/v3/operation-reply-address';
import { Messages } from '../../../src/models/messages';
import { Message } from '../../../src/models/v3/message';

import { assertExtensions } from './utils';

Expand Down Expand Up @@ -59,6 +61,30 @@ describe('OperationReply model', function() {
expect(d.channel()).toBeUndefined();
});
});

describe('.messages()', function() {
it('should return collection of messages - single message', function() {
const d = new OperationReply({ messages: { someMessage: { messageId: 'messageId' } } });
expect(d.messages()).toBeInstanceOf(Messages);
expect(d.messages().all()).toHaveLength(1);
expect(d.messages().all()[0]).toBeInstanceOf(Message);
});

it('should return collection of messages - more than one messages', function() {
const d = new OperationReply({ messages: { someMessage1: { messageId: 'messageId1' }, someMessage2: { messageId: 'messageId2' } } });
expect(d.messages()).toBeInstanceOf(Messages);
expect(d.messages().all()).toHaveLength(2);
expect(d.messages().all()[0]).toBeInstanceOf(Message);
expect(d.messages().all()[0].messageId()).toEqual('messageId1');
expect(d.messages().all()[1]).toBeInstanceOf(Message);
expect(d.messages().all()[1].messageId()).toEqual('messageId2');
});

it('should return undefined if address is not present', function() {
const d = new OperationReply({}, { asyncapi: {} as any, pointer: '' });
expect(d.channel()).toBeUndefined();
});
});

describe('mixins', function() {
assertExtensions(OperationReply);
Expand Down

0 comments on commit b59983c

Please sign in to comment.