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

chore: add reusable tags and externalDocs and move them to the info object #639

Closed
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
4 changes: 4 additions & 0 deletions src/models/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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 { TagsInterface } from './tags';
import type { ExternalDocumentationsInterface } from './external-documentations';

export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface {
servers(): ServersInterface;
Expand All @@ -25,6 +27,8 @@ export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface
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.
4 changes: 4 additions & 0 deletions src/models/external-documentations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { ExternalDocumentationInterface } from './external-documentation';

export type ExternalDocumentationsInterface = Collection<ExternalDocumentationInterface>
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
12 changes: 12 additions & 0 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 { Tags } from './tags';
import { ExternalDocumentations } from './external-documentations';

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

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

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

Expand Down Expand Up @@ -97,6 +101,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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';
Expand Down
10 changes: 10 additions & 0 deletions src/models/v2/external-documentations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from '../collection';

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

export class ExternalDocumentations extends Collection<ExternalDocumentationInterface> implements ExternalDocumentationsInterface {
override get(_: string): ExternalDocumentationInterface | undefined {
return;
}
}
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 @@ -5,7 +5,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 { OperationAction } from '../operation';
import type { OperationTraitInterface } from '../operation-trait';
import type { TagsInterface } from '../tags';
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
2 changes: 1 addition & 1 deletion src/models/v2/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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';
Expand Down
2 changes: 2 additions & 0 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BaseModel } from '../base';
import { Info } from './info';

import type { AsyncAPIDocumentInterface } from '../asyncapi';
import type { InfoInterface } from '../info';

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

Expand Down
133 changes: 133 additions & 0 deletions src/models/v3/components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { BaseModel } from '../base';
import { Collection } from '../collection';

import { Bindings } from './bindings';
import { Binding } from './binding';
import { extensions } from './mixins';
import { Tags } from './tags';
import { Tag } from './tag';
import { ExternalDocumentations } from './external-documentations';
import { ExternalDocumentation } from './external-documentation';

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

import type { BindingsInterface } from '../bindings';
import type { ComponentsInterface } from '../components';
import type { ExtensionsInterface } from '../extensions';
import type { Constructor } from '../utils';
import type { ServersInterface } from '../servers';
import type { ChannelsInterface } from '../channels';
import type { MessagesInterface } from '../messages';
import type { SchemasInterface } from '../schemas';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ServerVariablesInterface } from '../server-variables';
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 { TagsInterface } from '../tags';
import type { ExternalDocumentationsInterface } from '../external-documentations';
import type { CorrelationIdsInterface } from '../correlation-ids';

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

export class Components extends BaseModel<v3.ComponentsObject> implements ComponentsInterface {
servers(): ServersInterface {
return [] as any;
}

channels(): ChannelsInterface {
return [] as any;
}

messages(): MessagesInterface {
return [] as any;
}

schemas(): SchemasInterface {
return [] as any;
}

channelParameters(): ChannelParametersInterface {
return [] as any;
}

serverVariables(): ServerVariablesInterface {
return [] as any;
}

operations(): OperationsInterface {
return [] as any;
}

operationTraits(): OperationTraitsInterface {
return [] as any;
}

messageTraits(): MessageTraitsInterface {
return [] as any;
}

correlationIds(): CorrelationIdsInterface {
return [] as any;
}

securitySchemes(): SecuritySchemesInterface {
return [] as any;
}

tags(): TagsInterface {
return this.createCollection('tags', Tags, Tag, 'componentId');
}

externalDocs(): ExternalDocumentationsInterface {
return this.createCollection('externalDocs', ExternalDocumentations, ExternalDocumentation, 'componentId');
}

serverBindings(): Record<string, BindingsInterface> {
return this.createBindings('serverBindings');
}

channelBindings(): Record<string, BindingsInterface> {
return this.createBindings('channelBindings');
}

operationBindings(): Record<string, BindingsInterface> {
return this.createBindings('operationBindings');
}

messageBindings(): Record<string, BindingsInterface> {
return this.createBindings('messageBindings');
}

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

isEmpty(): boolean {
return Object.keys(this._json).length === 0;
}

protected createCollection<M extends Collection<any>, T extends BaseModel>(itemsName: keyof v3.ComponentsObject, collectionModel: Constructor<M>, itemModel: Constructor<T>, idKey: string = 'id'): M {
const collectionItems: T[] = [];
Object.entries(this._json[itemsName] || {}).forEach(([id, item]) => {
collectionItems.push(this.createModel(itemModel, item as any, { [idKey]: id, pointer: `/components/${itemsName}/${id}` } as any));
});
return new collectionModel(collectionItems);
}

protected createBindings(itemsName: 'serverBindings' | 'channelBindings' | 'operationBindings' | 'messageBindings'): Record<string, BindingsInterface> {
return Object.entries(this._json[itemsName] || {}).reduce((bindings, [name, item]) => {
const bindingsData = item || {};
const asyncapi = this.meta('asyncapi');
const pointer = `components/${itemsName}/${name}`;
bindings[name] = new Bindings(
Object.entries(bindingsData).map(([protocol, binding]) =>
this.createModel(Binding, binding, { protocol, pointer: `${pointer}/${protocol}` })
),
{ originalData: bindingsData as any, asyncapi, pointer }
);
return bindings;
}, {} as Record<string, BindingsInterface>);
}
}
38 changes: 38 additions & 0 deletions src/models/v3/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BaseModel } from '../base';

import { extensions } from './mixins';

import type { ContactInterface } from '../contact';
import type { ExtensionsInterface } from '../extensions';

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

export class Contact extends BaseModel<v3.ContactObject> implements ContactInterface {
hasName(): boolean {
return !!this._json.name;
}

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

hasUrl(): boolean {
return !!this._json.url;
}

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

hasEmail(): boolean {
return !!this._json.email;
}

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

extensions(): ExtensionsInterface {
return extensions(this);
}
}
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 { v3 } from '../../spec-types';

export class ExternalDocumentation extends BaseModel<v3.ExternalDocumentationObject> implements ExternalDocumentationInterface {
export class ExternalDocumentation extends BaseModel<v3.ExternalDocumentationObject, { componentId?: string }> implements ExternalDocumentationInterface {
url(): string {
return this._json.url;
}
Expand Down
10 changes: 10 additions & 0 deletions src/models/v3/external-documentations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from '../collection';

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

export class ExternalDocumentations extends Collection<ExternalDocumentationInterface> implements ExternalDocumentationsInterface {
override get(id: string): ExternalDocumentationInterface | undefined {
return this.collections.find(externalDocumentation => externalDocumentation.meta('componentId' as any) === id);
}
}
Loading