Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify operation traits based on parser-api #590

Merged
merged 1 commit into from
Sep 5, 2022
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
3 changes: 0 additions & 3 deletions src/models/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { BaseModel } from "./base";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { OperationAction } from "./operation";
import type { SecuritySchemeInterface } from "./security-scheme";
import { SecurityRequirements } from "./v2/security-requirements";

export interface OperationTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
action(): OperationAction;
hasOperationId(): boolean;
operationId(): string | undefined;
hasSummary(): boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/models/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { ServersInterface } from "./servers";
export type OperationAction = 'send' | 'receive' | 'publish' | 'subscribe';

export interface OperationInterface extends BaseModel, OperationTraitInterface {
action(): OperationAction;
isSend(): boolean;
isReceive(): boolean;
servers(): ServersInterface;
channels(): ChannelsInterface;
messages(): MessagesInterface;
Expand Down
8 changes: 8 additions & 0 deletions src/models/v2/operation-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export class OperationTrait<J extends v2.OperationTraitObject = v2.OperationTrai
return hasExternalDocs(this);
}

isSend(): boolean {
return this.action() === 'subscribe';
}

isReceive(): boolean {
return this.action() === 'publish';
}

externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}
Expand Down
8 changes: 0 additions & 8 deletions test/models/v2/operation-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ describe('OperationTrait model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.hasOperationId()', function() {
it('should return true when there is a value', function() {
const doc = { operationId: '...' };
Expand Down
36 changes: 36 additions & 0 deletions test/models/v2/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ describe('Operation model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.isSend()', function() {
it('should return true when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.messages()', function() {
it('should return collection of messages - single message', function() {
const doc = { message: { messageId: '...' } };
Expand Down