Skip to content

Commit

Permalink
chore: add missed metadata methods to the core models (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Nov 24, 2022
1 parent 2590f56 commit f804bdb
Show file tree
Hide file tree
Showing 30 changed files with 356 additions and 569 deletions.
Binary file removed asyncapi-parser-2.0.0-next-major.8.tgz
Binary file not shown.
4 changes: 2 additions & 2 deletions src/models/channel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { BaseModel } from './base';
import type { ChannelParametersInterface } from './channel-parameters';
import type { MessagesInterface } from './messages';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { CoreMixinInterface } from './mixins';
import type { OperationsInterface } from './operations';
import type { ServersInterface } from './servers';

export interface ChannelInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
export interface ChannelInterface extends BaseModel, CoreMixinInterface {
id(): string;
address(): string | null | undefined;
servers(): ServersInterface;
Expand Down
8 changes: 2 additions & 6 deletions src/models/message-trait.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { BaseModel } from './base';
import type { CorrelationIdInterface } from './correlation-id';
import type { MessageExamplesInterface } from './message-examples';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { CoreMixinInterface } from './mixins';
import type { SchemaInterface } from './schema';

export interface MessageTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
export interface MessageTraitInterface extends BaseModel, CoreMixinInterface {
id(): string;
schemaFormat(): string;
hasMessageId(): boolean;
Expand All @@ -17,9 +17,5 @@ export interface MessageTraitInterface extends BaseModel, BindingsMixinInterface
headers(): SchemaInterface | undefined;
hasName(): boolean;
name(): string | undefined;
hasTitle(): boolean;
title(): string | undefined;
hasSummary(): boolean;
summary(): string | undefined;
examples(): MessageExamplesInterface;
}
12 changes: 12 additions & 0 deletions src/models/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface BindingsMixinInterface {
bindings(): BindingsInterface;
}

export interface CoreMixinInterface extends BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, SummaryMixinInterface, TagsMixinInterface, TitleMixinInterface {}

export interface DescriptionMixinInterface {
hasDescription(): boolean;
description(): string | undefined;
Expand All @@ -21,6 +23,16 @@ export interface ExternalDocumentationMixinInterface {
externalDocs(): ExternalDocumentationInterface | undefined;
}

export interface SummaryMixinInterface {
hasSummary(): boolean;
summary(): string | undefined;
}

export interface TagsMixinInterface {
tags(): TagsInterface;
}

export interface TitleMixinInterface {
hasTitle(): boolean;
title(): string | undefined;
}
6 changes: 2 additions & 4 deletions src/models/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { BaseModel } from './base';
import type { SecurityRequirements } from './security-requirements';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { CoreMixinInterface } from './mixins';

export interface OperationTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
export interface OperationTraitInterface extends BaseModel, CoreMixinInterface {
hasId(): boolean;
id(): string | undefined;
hasSummary(): boolean;
summary(): string | undefined;
security(): SecurityRequirements[];
}
4 changes: 2 additions & 2 deletions src/models/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { BaseModel } from './base';
import type { ChannelsInterface } from './channels';
import type { MessagesInterface } from './messages';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, TagsMixinInterface } from './mixins';
import type { CoreMixinInterface } from './mixins';
import type { OperationsInterface } from './operations';
import type { ServerVariablesInterface } from './server-variables';
import type { SecurityRequirementsInterface } from './security-requirements';

export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface, ExtensionsMixinInterface, TagsMixinInterface {
export interface ServerInterface extends BaseModel, CoreMixinInterface {
id(): string
url(): string;
protocol(): string;
Expand Down
38 changes: 2 additions & 36 deletions src/models/v2/channel.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import { BaseModel } from '../base';
import { ChannelParameters } from '../channel-parameters';
import { ChannelParameter } from './channel-parameter';
import { Messages } from '../messages';
import { Operations } from '../operations';
import { Operation } from './operation';
import { Servers } from '../servers';
import { Server } from './server';
import { Tags } from '../tags';

import { bindings, hasDescription, description, extensions } from './mixins';
import { CoreModel } from './mixins';

import type { BindingsInterface } from '../bindings';
import type { ChannelInterface } from '../channel';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { MessagesInterface } from '../messages';
import type { MessageInterface } from '../message';
import type { OperationsInterface } from '../operations';
import type { OperationAction, OperationInterface } from '../operation';
import type { ServersInterface } from '../servers';
import type { ServerInterface } from '../server';
import type { TagsInterface } from '../tags';

import type { v2 } from '../../spec-types';

export class Channel extends BaseModel<v2.ChannelObject, { id: string, address: string }> implements ChannelInterface {
export class Channel extends CoreModel<v2.ChannelObject, { id: string, address: string }> implements ChannelInterface {
id(): string {
return this._meta.id;
}
Expand All @@ -34,22 +28,6 @@ export class Channel extends BaseModel<v2.ChannelObject, { id: string, address:
return this._meta.address;
}

hasDescription(): boolean {
return hasDescription(this);
}

description(): string | undefined {
return description(this);
}

hasExternalDocs(): boolean {
return false;
}

externalDocs(): ExternalDocumentationInterface | undefined {
return;
}

servers(): ServersInterface {
const servers: ServerInterface[] = [];
const allowedServers: string[] = this._json.servers || [];
Expand Down Expand Up @@ -90,16 +68,4 @@ export class Channel extends BaseModel<v2.ChannelObject, { id: string, address:
})
);
}

tags(): TagsInterface {
return new Tags([]);
}

bindings(): BindingsInterface {
return bindings(this);
}

extensions(): ExtensionsInterface {
return extensions(this);
}
}
53 changes: 2 additions & 51 deletions src/models/v2/message-trait.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { BaseModel } from '../base';
import { CorrelationId } from './correlation-id';
import { MessageExamples } from '../message-examples';
import { MessageExample } from './message-example';
import { Schema } from './schema';

import { xParserMessageName } from '../../constants';
import { getDefaultSchemaFormat } from '../../schema-parser';
import { bindings, hasDescription, description, extensions, hasExternalDocs, externalDocs, tags } from './mixins';
import { CoreModel } from './mixins';

import type { BindingsInterface } from '../bindings';
import type { CorrelationIdInterface } from '../correlation-id';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { MessageExamplesInterface } from '../message-examples';
import type { MessageTraitInterface } from '../message-trait';
import type { SchemaInterface } from '../schema';
import type { TagsInterface } from '../tags';

import type { v2 } from '../../spec-types';

export class MessageTrait<J extends v2.MessageTraitObject = v2.MessageTraitObject> extends BaseModel<J, { id: string }> implements MessageTraitInterface {
export class MessageTrait<J extends v2.MessageTraitObject = v2.MessageTraitObject> extends CoreModel<J, { id: string }> implements MessageTraitInterface {
id(): string {
return this.messageId() || this._meta.id || this.extensions().get(xParserMessageName)?.value<string>() as string;
}
Expand Down Expand Up @@ -70,55 +65,11 @@ export class MessageTrait<J extends v2.MessageTraitObject = v2.MessageTraitObjec
return this._json.name;
}

hasTitle(): boolean {
return !!this._json.title;
}

title(): string | undefined {
return this._json.title;
}

hasSummary(): boolean {
return !!this._json.summary;
}

summary(): string | undefined {
return this._json.summary;
}

hasDescription(): boolean {
return hasDescription(this);
}

description(): string | undefined {
return description(this);
}

hasExternalDocs(): boolean {
return hasExternalDocs(this);
}

externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}

examples(): MessageExamplesInterface {
return new MessageExamples(
(this._json.examples || []).map((example: any, index: number) => {
return this.createModel(MessageExample, example, { pointer: `${this._meta.pointer}/examples/${index}` });
})
);
}

tags(): TagsInterface {
return tags(this);
}

bindings(): BindingsInterface {
return bindings(this);
}

extensions(): ExtensionsInterface {
return extensions(this);
}
}
68 changes: 65 additions & 3 deletions src/models/v2/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BaseModel } from '../base';
import { Bindings } from './bindings';
import { Binding } from './binding';
import { Extensions } from '../extensions';
Expand All @@ -9,7 +10,6 @@ import { Tag } from './tag';
import { createModel } from '../utils';
import { EXTENSION_REGEX } from '../../constants';

import type { BaseModel } from '../base';
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';
Expand All @@ -18,13 +18,75 @@ import type { TagsInterface } from '../tags';

import type { v2 } from '../../spec-types';

export function bindings(model: BaseModel<{ bindings?: Record<string, any> }>): BindingsInterface {
type BindingsObject =
| v2.ServerBindingsObject
| v2.ChannelBindingsObject
| v2.OperationBindingsObject
| v2.MessageBindingsObject
| v2.ReferenceObject;

export interface CoreObject extends v2.SpecificationExtensions {
title?: string;
summary?: string;
description?: string;
externalDocs?: v2.ExternalDocumentationObject;
tags?: v2.TagsObject;
bindings?: BindingsObject;
}

export abstract class CoreModel<J extends CoreObject = CoreObject, M extends Record<string, any> = {}> extends BaseModel<J, M> {
hasTitle(): boolean {
return !!this._json.title;
}

title(): string | undefined {
return this._json.title;
}

hasSummary(): boolean {
return !!this._json.summary;
}

summary(): string | undefined {
return this._json.summary;
}

hasDescription(): boolean {
return hasDescription(this);
}

description(): string | undefined {
return description(this);
}

hasExternalDocs(): boolean {
return hasExternalDocs(this);
}

externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}

tags(): TagsInterface {
return tags(this);
}

bindings(): BindingsInterface {
return bindings(this);
}

extensions(): ExtensionsInterface {
return extensions(this);
}
}

export function bindings(model: BaseModel<{ bindings?: BindingsObject }>): BindingsInterface {
const bindings = model.json('bindings') || {};
return new Bindings(
Object.entries(bindings || {}).map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
{ originalData: bindings, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
{ originalData: bindings as Record<string, Binding>, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
);
}

Expand Down
Loading

0 comments on commit f804bdb

Please sign in to comment.