Skip to content

Commit

Permalink
chore: add reusable tags and externalDocs and move them to the info o…
Browse files Browse the repository at this point in the history
…bject (#664)
  • Loading branch information
magicmatatjahu authored Nov 8, 2022
1 parent f259f4a commit 6de1923
Show file tree
Hide file tree
Showing 40 changed files with 1,270 additions and 80 deletions.
Binary file added asyncapi-parser-2.0.0-next-major.8.tgz
Binary file not shown.
8 changes: 6 additions & 2 deletions src/models/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BindingsInterface } from './bindings';
import type { ExtensionsMixinInterface } from './mixins';
import type { ServersInterface } from './servers';
import type { ChannelsInterface } from './channels';
import type { OperationsInterface } from './operations';
import type { MessagesInterface } from './messages';
import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
Expand All @@ -11,20 +12,23 @@ import type { OperationTraitsInterface } from './operation-traits';
import type { MessageTraitsInterface } from './message-traits';
import type { SecuritySchemesInterface } from './security-schemes';
import type { CorrelationIdsInterface } from './correlation-ids';
import type { OperationsInterface } from './operations';
import type { ExternalDocumentationsInterface } from './external-documentations';
import type { TagsInterface } from './tags';

export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface {
servers(): ServersInterface;
channels(): ChannelsInterface;
operations(): OperationsInterface;
messages(): MessagesInterface;
schemas(): SchemasInterface;
channelParameters(): ChannelParametersInterface;
serverVariables(): ServerVariablesInterface;
operations(): OperationsInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
correlationIds(): CorrelationIdsInterface;
securitySchemes(): SecuritySchemesInterface;
tags(): TagsInterface;
externalDocs(): ExternalDocumentationsInterface;
serverBindings(): Record<string, BindingsInterface>;
channelBindings(): Record<string, BindingsInterface>;
operationBindings(): Record<string, BindingsInterface>;
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions src/models/external-documentations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Collection } from './collection';

import type { ExternalDocumentationInterface } from './external-documentation';

export type ExternalDocumentationsInterface = Collection<ExternalDocumentationInterface>

export class ExternalDocumentations extends Collection<ExternalDocumentationInterface> implements ExternalDocumentationsInterface {
override get(id: string): ExternalDocumentationInterface | undefined {
return this.collections.find(externalDocs => externalDocs.meta('id' as any) === id);
}
}
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export * from './correlation-id';
export * from './correlation-ids';
export * from './extension';
export * from './extensions';
export * from './external-docs';
export * from './external-documentation';
export * from './info';
export * from './license';
export * from './message-example';
Expand Down
2 changes: 1 addition & 1 deletion src/models/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BindingsInterface } from './bindings';
import type { ExtensionsInterface } from './extensions';
import type { ExternalDocumentationInterface } from './external-docs';
import type { ExternalDocumentationInterface } from './external-documentation';
import type { TagsInterface } from './tags';

export interface BindingsMixinInterface {
Expand Down
1 change: 1 addition & 0 deletions src/models/tags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection } from './collection';

import type { TagInterface } from './tag';

export type TagsInterface = Collection<TagInterface>
Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { MessagesInterface } from '../messages';
import type { MessageInterface } from '../message';
import type { OperationsInterface } from '../operations';
Expand Down
31 changes: 18 additions & 13 deletions src/models/v2/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { SecuritySchemes } from '../security-schemes';
import { CorrelationIds } from '../correlation-ids';
import { Operations } from '../operations';
import { Message } from './message';
import { ExternalDocumentations } from '../external-documentations';
import { Tags } from '../tags';

import { tilde } from '../../utils';

Expand All @@ -42,7 +44,8 @@ import type { OperationTraitsInterface } from '../operation-traits';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { MessageTraitsInterface } from '../message-traits';
import type { OperationsInterface } from '../operations';
import type { OperationInterface } from '../operation';
import type { ExternalDocumentationsInterface } from '../external-documentations';
import type { TagsInterface } from '../tags';

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

Expand All @@ -52,11 +55,11 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
}

channels(): ChannelsInterface {
return new Channels(
Object.entries(this._json.channels || {}).map(([channelAddress, channel]) =>
this.createModel(Channel, channel as v2.ChannelObject, { id: channelAddress, address: '', pointer: `/components/channels/${tilde(channelAddress)}` })
)
);
return this.createCollection('channels', Channels, Channel);
}

operations(): OperationsInterface {
return new Operations([]);
}

messages(): MessagesInterface {
Expand All @@ -75,12 +78,6 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
return this.createCollection('serverVariables', ServerVariables, ServerVariable);
}

operations(): OperationsInterface {
const operations: OperationInterface[] = [];
this.channels().forEach(channel => operations.push(...channel.operations().all()));
return new Operations(operations);
}

operationTraits(): OperationTraitsInterface {
return this.createCollection('operationTraits', OperationTraits, OperationTrait);
}
Expand All @@ -97,6 +94,14 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
return this.createCollection('securitySchemes', SecuritySchemes, SecurityScheme);
}

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

externalDocs(): ExternalDocumentationsInterface {
return new ExternalDocumentations([]);
}

serverBindings(): Record<string, BindingsInterface> {
return this.createBindings('serverBindings');
}
Expand Down Expand Up @@ -124,7 +129,7 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
protected createCollection<M extends Collection<any>, T extends BaseModel>(itemsName: keyof v2.ComponentsObject, collectionModel: Constructor<M>, itemModel: Constructor<T>): M {
const collectionItems: T[] = [];
Object.entries(this._json[itemsName] || {}).forEach(([id, item]) => {
collectionItems.push(this.createModel(itemModel, item as any, { id, pointer: `/components/${itemsName}/${id}` } as any));
collectionItems.push(this.createModel(itemModel, item as any, { id, pointer: `/components/${itemsName}/${tilde(id)}` } as any));
});
return new collectionModel(collectionItems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { BaseModel } from '../base';

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

import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { ExtensionsInterface } from '../extensions';

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

export class ExternalDocumentation extends BaseModel<v2.ExternalDocumentationObject> implements ExternalDocumentationInterface {
export class ExternalDocumentation extends BaseModel<v2.ExternalDocumentationObject, { id?: string }> implements ExternalDocumentationInterface {
url(): string {
return this._json.url;
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { Contact as ContactV2 } from './contact';
export { CorrelationId as CorrelationIdV2 } from './correlation-id';
export { Extension as ExtensionV2 } from './extension';
export { Extensions as ExtensionsV2 } from '../extensions';
export { ExternalDocumentation as ExternalDocumentationV2 } from './external-docs';
export { ExternalDocumentation as ExternalDocumentationV2 } from './external-documentation';
export { Info as InfoV2 } from './info';
export { License as LicenseV2 } from './license';
export { MessageExample as MessageExampleV2 } from './message-example';
Expand Down
4 changes: 2 additions & 2 deletions src/models/v2/info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModel } from '../base';
import { Contact } from './contact';
import { ExternalDocumentation } from './external-docs';
import { ExternalDocumentation } from './external-documentation';
import { License } from './license';
import { Tags } from '../tags';
import { Tag } from './tag';
Expand All @@ -10,7 +10,7 @@ import { hasDescription, description, extensions } from './mixins';
import type { ContactInterface } from '../contact';
import type { InfoInterface } from '../info';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { LicenseInterface } from '../license';
import type { TagsInterface } from '../tags';

Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { bindings, hasDescription, description, extensions, hasExternalDocs, ext
import type { BindingsInterface } from '../bindings';
import type { CorrelationIdInterface } from '../correlation-id';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { MessageExamplesInterface } from '../message-examples';
import type { MessageTraitInterface } from '../message-trait';
import type { SchemaInterface } from '../schema';
Expand Down
4 changes: 2 additions & 2 deletions src/models/v2/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Bindings } from './bindings';
import { Binding } from './binding';
import { Extensions } from '../extensions';
import { Extension } from './extension';
import { ExternalDocumentation } from './external-docs';
import { ExternalDocumentation } from './external-documentation';
import { Tags } from '../tags';
import { Tag } from './tag';

Expand All @@ -13,7 +13,7 @@ import type { BaseModel } from '../base';
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { TagsInterface } from '../tags';

import type { v2 } from '../../spec-types';
Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/operation-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { bindings, hasDescription, description, extensions, hasExternalDocs, ext

import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { ChannelsInterface } from '../channels';
import type { OperationAction } from '../operation';
import type { OperationTraitInterface } from '../operation-trait';
Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { retrievePossibleRef, hasRef } from '../../utils';

import type { ModelMetadata } from '../base';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { SchemaInterface } from '../schema';

import type { v2 } from '../../spec-types';
Expand Down
4 changes: 2 additions & 2 deletions src/models/v2/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { BaseModel } from '../base';
import { hasDescription, description, extensions, hasExternalDocs, externalDocs } from './mixins';

import type { ExtensionsInterface } from '../extensions';
import type{ ExternalDocumentationInterface } from '../external-docs';
import type{ ExternalDocumentationInterface } from '../external-documentation';
import type { TagInterface } from '../tag';

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

export class Tag extends BaseModel<v2.TagObject> implements TagInterface {
export class Tag extends BaseModel<v2.TagObject, { id?: string }> implements TagInterface {
name(): string {
return this._json.name;
}
Expand Down
54 changes: 43 additions & 11 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { BaseModel } from '../base';
import { Info } from './info';
import { Servers } from '../servers';
import { Server } from './server';
import { Channels } from '../channels';
import { Channel } from './channel';
import { Operations } from '../operations';
import { Operation } from './operation';
import { Messages } from '../messages';
import { SecuritySchemes } from '../security-schemes';
import { SecurityScheme } from './security-scheme';
import { Components } from './components';

import { extensions } from './mixins';
import { tilde } from '../../utils';

import type { AsyncAPIDocumentInterface } from '../asyncapi';
import type { InfoInterface } from '../info';
import type { ServersInterface } from '../servers';
import type { ChannelsInterface } from '../channels';
import type { OperationsInterface } from '../operations';
import type { MessagesInterface } from '../messages';
import type { MessageInterface } from '../message';
import type { ComponentsInterface } from '../components';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { ExtensionsInterface } from '../extensions';

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

Expand All @@ -26,12 +40,16 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
return !!this._json.defaultContentType;
}

info() {
return null as any;
info(): InfoInterface {
return this.createModel(Info, this._json.info, { pointer: '/info' });
}

servers() {
return null as any;
servers(): ServersInterface {
return new Servers(
Object.entries(this._json.servers || {}).map(([serverName, server]) =>
this.createModel(Server, server, { id: serverName, pointer: `/servers/${tilde(serverName)}` })
)
);
}

channels(): ChannelsInterface {
Expand All @@ -50,23 +68,37 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
);
}

messages() {
return null as any;
messages(): MessagesInterface {
const messages: MessageInterface[] = [];
const messagesData: any[] = [];
this.channels().forEach(channel => {
channel.messages().forEach(message => {
if (!messagesData.includes(message.json())) {
messagesData.push(message.json());
messages.push(message);
}
});
});
return new Messages(messages);
}

schemas() {
return null as any;
}

securitySchemes() {
return null as any;
securitySchemes(): SecuritySchemesInterface {
return new SecuritySchemes(
Object.entries(this._json.components?.securitySchemes || {}).map(([securitySchemeName, securityScheme]) =>
this.createModel(SecurityScheme, securityScheme as v3.SecuritySchemeObject, { id: securitySchemeName, pointer: `/components/securitySchemes/${securitySchemeName}` })
)
);
}

components() {
return null as any;
components(): ComponentsInterface {
return this.createModel(Components, this._json.components || {}, { pointer: '/components' });
}

extensions() {
extensions(): ExtensionsInterface {
return extensions(this);
}
}
2 changes: 1 addition & 1 deletion src/models/v3/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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-docs';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { MessagesInterface } from '../messages';
import type { OperationsInterface } from '../operations';
import type { OperationInterface } from '../operation';
Expand Down
Loading

0 comments on commit 6de1923

Please sign in to comment.