Skip to content

Commit

Permalink
refactor: port components model (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored and derberg committed Oct 4, 2022
1 parent ae4b0ff commit 7fc5916
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 32 deletions.
51 changes: 24 additions & 27 deletions src/models/components.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import type { BaseModel } from './base';
import type { Collection } from './collection';
import type { ServersInterface } from './servers';
import type { ChannelsInterface } from './channels';
import type { OperationsInterface } from './operations';
import type { OperationTraitsInterface } from './operation-traits';
import type { MessagesInterface } from './messages';
import type { MessageTraitsInterface } from './message-traits';
import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ServerVariablesInterface } from './server-variables';
import type { ServerInterface } from './server';
import type { ChannelInterface } from './channel';
import type { OperationTraitInterface } from './operation-trait';
import type { MessageInterface } from './message';
import type { MessageTraitInterface } from './message-trait';
import type { SchemaInterface } from './schema';
import type { ChannelParameterInterface } from './channel-parameter';
import type { ServerVariableInterface } from './server-variable';
import type { CorrelationIdInterface } from './correlation-id';
import type { BindingsInterface } from './bindings';
import type { SecuritySchemesInterface } from './security-schemes';
import type { SecuritySchemeInterface } from './security-scheme';
import type { ExtensionsMixinInterface } from './mixins';

export interface Components extends BaseModel, ExtensionsMixinInterface {
servers(): ServersInterface;
channels(): ChannelsInterface;
operations(): OperationsInterface;
messages(): MessagesInterface;
schemas(): SchemasInterface;
channelParameters(): ChannelParametersInterface;
serverVariables(): ServerVariablesInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
correlationIds(): Collection<CorrelationIdInterface>;
securitySchemes(): SecuritySchemesInterface;
serverBindings(): Collection<BindingsInterface>;
channelBindings(): Collection<BindingsInterface>;
operationBindings(): Collection<BindingsInterface>;
messageBindings(): Collection<BindingsInterface>;
export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface {
servers(): Record<string, ServerInterface>;
channels(): Record<string, ChannelInterface>;
messages(): Record<string, MessageInterface>;
schemas(): Record<string, SchemaInterface>;
channelParameters(): Record<string, ChannelParameterInterface>;
serverVariables(): Record<string, ServerVariableInterface>;
operationTraits(): Record<string, OperationTraitInterface>;
messageTraits(): Record<string, MessageTraitInterface>;
correlationIds(): Record<string, CorrelationIdInterface>;
securitySchemes(): Record<string, SecuritySchemeInterface>;
serverBindings(): Record<string, BindingsInterface>;
channelBindings(): Record<string, BindingsInterface>;
operationBindings(): Record<string, BindingsInterface>;
messageBindings(): Record<string, BindingsInterface>;
}
2 changes: 1 addition & 1 deletion src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AsyncAPIDocument extends Mixin(BaseModel, ExtensionsMixin) implemen

servers(): ServersInterface {
return new Servers(
Object.entries(this._json.servers).map(([serverName, server]) =>
Object.entries(this._json.servers || {}).map(([serverName, server]) =>
this.createModel(Server, server, { id: serverName, pointer: `/servers/${serverName}` })
)
);
Expand Down
105 changes: 105 additions & 0 deletions src/models/v2/components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { BaseModel } from "../base";
import { Channel } from "./channel";
import { ChannelParameter } from "./channel-parameter";
import { CorrelationId } from "./correlation-id";
import { Message } from "./message";
import { MessageTrait } from "./message-trait";
import { OperationTrait } from "./operation-trait";
import { Schema } from "./schema";
import { SecurityScheme } from "./security-scheme";
import { Server } from "./server";
import { ServerVariable } from "./server-variable";

import { Mixin } from '../utils';
import { Bindings, Binding } from "./mixins/bindings";
import { ExtensionsMixin } from './mixins/extensions';

import type { BindingsInterface } from "../bindings";
import type { ComponentsInterface } from "../components";
import type { ChannelInterface } from "../channel";
import type { ChannelParameterInterface } from "../channel-parameter";
import type { CorrelationIdInterface } from "../correlation-id";
import type { MessageInterface } from "../message";
import type { MessageTraitInterface } from "../message-trait";
import type { OperationTraitInterface } from "../operation-trait";
import type { SchemaInterface } from "../schema";
import type { SecuritySchemeInterface } from "../security-scheme";
import type { ServerInterface } from "../server";
import type { ServerVariableInterface } from "../server-variable";
import type { Constructor } from "../utils";

export class Components extends Mixin(BaseModel, ExtensionsMixin) implements ComponentsInterface {
servers(): Record<string, ServerInterface> {
return this.createMap('servers', Server);
}

channels(): Record<string, ChannelInterface> {
return this.createMap('channels', Channel);
}

messages(): Record<string, MessageInterface> {
return this.createMap('messages', Message);
}

schemas(): Record<string, SchemaInterface> {
return this.createMap('schemas', Schema);
}

channelParameters(): Record<string, ChannelParameterInterface> {
return this.createMap('parameters', ChannelParameter);
}

serverVariables(): Record<string, ServerVariableInterface> {
return this.createMap('serverVariables', ServerVariable);
}

operationTraits(): Record<string, OperationTraitInterface> {
return this.createMap('operationTraits', OperationTrait);
}

messageTraits(): Record<string, MessageTraitInterface> {
return this.createMap('messageTraits', MessageTrait);
}

correlationIds(): Record<string, CorrelationIdInterface> {
return this.createMap('correlationIds', CorrelationId);
}

securitySchemes(): Record<string, SecuritySchemeInterface> {
return this.createMap('securitySchemes', SecurityScheme);
}

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');
}

protected createMap<M extends BaseModel>(itemsName: string, model: Constructor<M>): Record<string, M> {
return Object.entries(this._json[itemsName] || {}).reduce((items, [itemName, item]) => {
items[itemName] = this.createModel(model, item, { id: itemName, pointer: `/components/${itemsName}/${itemName}` })
return items;
}, {} as Record<string, M>);
}

protected createBindings(itemsName: string): Record<string, BindingsInterface> {
return Object.entries(this._json[itemsName] || {}).reduce((bindings, [name, item]) => {
bindings[name] = new Bindings(
Object.entries(item as any || {}).map(([protocol, binding]) =>
this.createModel(Binding, binding, { id: protocol, pointer: `components/${itemsName}/${name}/${protocol}` })
)
);
return bindings;
}, {} as Record<string, BindingsInterface>);
}
}
3 changes: 1 addition & 2 deletions src/models/v2/mixins/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ export class Bindings extends Collection<BindingInterface> implements BindingsIn

export abstract class BindingsMixin extends BaseModel implements BindingsMixinInterface {
bindings(): BindingsInterface {
const bindings: Record<string, any> = this._json.bindings || {};
return new Bindings(
Object.entries(bindings).map(([protocol, binding]) =>
Object.entries(this._json.bindings || {}).map(([protocol, binding]) =>
this.createModel(Binding, binding, { id: protocol, pointer: `${this._meta.pointer}/bindings/${protocol}` })
)
);
Expand Down
3 changes: 1 addition & 2 deletions src/models/v2/mixins/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export class Tags extends Collection<TagInterface> implements TagsInterface {

export abstract class TagsMixin extends BaseModel implements TagsMixinInterface {
tags(): TagsInterface {
const tags = this._json.tags || [];
return new Tags(
tags.map((tag: any, idx: number) =>
(this._json.tags || []).map((tag: any, idx: number) =>
this.createModel(Tag, tag, { pointer: `${this._meta.pointer}/tags/${idx}` })
)
);
Expand Down
14 changes: 14 additions & 0 deletions src/models/v2/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Collection } from '../collection';

import type { SchemasInterface } from '../schemas';
import type { SchemaInterface } from '../schema';

export class Schemas extends Collection<SchemaInterface> implements SchemasInterface {
override get(id: string): SchemaInterface | undefined {
return this.collections.find(schema => schema.uid() === id);
}

override has(id: string): boolean {
return this.collections.some(schema => schema.uid() === id);
}
}
Loading

0 comments on commit 7fc5916

Please sign in to comment.