Skip to content

Commit

Permalink
refactor: adjust mixins to the IntentAPI (asyncapi#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Oct 3, 2022
1 parent 5bd8e6e commit d7e6bb0
Show file tree
Hide file tree
Showing 55 changed files with 1,091 additions and 544 deletions.
9 changes: 4 additions & 5 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { InfoInterface } from "./info";
import { BaseModel } from "./base";

import { AsyncAPIDocumentV2 } from "./v2";
import { AsyncAPIDocumentV3 } from "./v3";

import { ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface } from "./mixins";
import type { InfoInterface } from "./info";
import type { BaseModel } from "./base";
import type { ExtensionsMixinInterface } from "./mixins";

export interface AsyncAPIDocumentInterface extends BaseModel, ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface {
export interface AsyncAPIDocumentInterface extends BaseModel, ExtensionsMixinInterface {
version(): string;
info(): InfoInterface;
}
Expand Down
8 changes: 8 additions & 0 deletions src/models/binding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { BaseModel } from "./base";
import type { ExtensionsMixinInterface } from './mixins';

export interface BindingInterface extends BaseModel, ExtensionsMixinInterface {
protocol(): string;
version(): string;
value(): any;
}
4 changes: 4 additions & 0 deletions src/models/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { BindingInterface } from './binding';

export interface BindingsInterface extends Collection<BindingInterface> {}
20 changes: 20 additions & 0 deletions src/models/collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { BaseModel } from "./base";

export abstract class Collection<T extends BaseModel> extends Array<T> {
constructor(
protected readonly collections: T[]
) {
super(...collections);
}

abstract get(id: string): T | undefined;
abstract has(id: string): boolean;

all(): T[] {
return this.collections;
}

isEmpty(): boolean {
return this.collections.length === 0;
}
}
15 changes: 10 additions & 5 deletions src/models/contact.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { BaseModel } from "./base";
import type { BaseModel } from "./base";

export interface ContactInterface extends BaseModel {
name(): string;
url(): string;
email(): string;
import type { ExtensionsMixinInterface } from "./mixins";

export interface ContactInterface extends BaseModel, ExtensionsMixinInterface {
hasName(): boolean;
name(): string | undefined;
hasUrl(): boolean;
url(): string | undefined;
hasEmail(): boolean;
email(): string | undefined;
}
7 changes: 7 additions & 0 deletions src/models/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { BaseModel } from "./base";

export interface ExtensionInterface extends BaseModel {
id(): string;
version(): string;
value(): any;
}
4 changes: 4 additions & 0 deletions src/models/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { ExtensionInterface } from './extension';

export interface ExtensionsInterface extends Collection<ExtensionInterface> {}
8 changes: 8 additions & 0 deletions src/models/external-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { BaseModel } from "./base";
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins'

export interface ExternalDocumentationInterface
extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {

url(): string;
}
28 changes: 17 additions & 11 deletions src/models/info.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { ContactInterface } from "./contact";
import { LicenseInterface } from "./license";
import { BaseModel } from "./base";
import type { ContactInterface } from "./contact";
import type { LicenseInterface } from "./license";
import type { BaseModel } from "./base";

export interface InfoInterface extends BaseModel {
title(): string;
version(): string;
description(): string;
termsOfService(): string;
contact(): ContactInterface | undefined;
license(): LicenseInterface | undefined;
}
import type { DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from "./mixins";

export interface InfoInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
title(): string;
version(): string;
hasId(): boolean;
id(): string | undefined;
hasTermsOfService(): boolean;
termsOfService(): string | undefined;
hasContact(): boolean;
contact(): ContactInterface | undefined;
hasLicense(): boolean;
license(): LicenseInterface | undefined;
}
13 changes: 8 additions & 5 deletions src/models/license.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BaseModel } from "./base";
import type { BaseModel } from "./base";

export interface LicenseInterface extends BaseModel {
name(): string;
url(): string;
}
import type { ExtensionsMixinInterface } from "./mixins";

export interface LicenseInterface extends BaseModel, ExtensionsMixinInterface {
name(): string;
hasUrl(): boolean;
url(): string | undefined;
}
26 changes: 26 additions & 0 deletions src/models/mixins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { BindingsInterface } from './bindings';
import type { ExtensionsInterface } from './extensions';
import type { ExternalDocumentationInterface } from './external-docs';
import type { TagsInterface } from './tags';

export interface BindingsMixinInterface {
bindings(): BindingsInterface;
}

export interface DescriptionMixinInterface {
hasDescription(): boolean;
description(): string | undefined;
}

export interface ExtensionsMixinInterface {
extensions(): ExtensionsInterface;
}

export interface ExternalDocumentationMixinInterface {
hasExternalDocs(): boolean;
externalDocs(): ExternalDocumentationInterface | undefined;
}

export interface TagsMixinInterface {
tags(): TagsInterface;
}
33 changes: 0 additions & 33 deletions src/models/mixins/bindings.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/models/mixins/external-docs.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/models/mixins/specification-extensions.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/models/mixins/tags.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/models/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { BaseModel } from "./base";
import type { DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface } from './mixins'

export interface TagInterface
extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface {

name(): string;
}
4 changes: 4 additions & 0 deletions src/models/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { TagInterface } from './tag';

export interface TagsInterface extends Collection<TagInterface> {}
20 changes: 7 additions & 13 deletions src/models/mixins/index.ts → src/models/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type { BaseModel } from '../base';

export * from './bindings';
export * from './description';
export * from './external-docs';
export * from './specification-extensions';
export * from './tags';
import type { BaseModel } from './base';

export interface Constructor<T = any> extends Function {
new (...any: any[]): T;
Expand All @@ -18,22 +12,22 @@ export function Mixin(a: typeof BaseModel): typeof BaseModel;
export function Mixin<A>(a: typeof BaseModel, b: MixinType<A>): typeof BaseModel & Constructor<A>;
export function Mixin<A, B>(a: typeof BaseModel, b: MixinType<A>, c: MixinType<B>): typeof BaseModel & Constructor<A> & Constructor<B>;
export function Mixin<A, B, C>(a: typeof BaseModel, b: MixinType<A>, c: MixinType<B>, d: MixinType<C>): typeof BaseModel & Constructor<A> & Constructor<B> & Constructor<C>;
export function Mixin<A, B, C, D>(a: typeof BaseModel, b: MixinType<A>, c: MixinType<B>, d: MixinType<C>, e: MixinType<D>): typeof BaseModel & Constructor<B> & Constructor<C> & Constructor<D> & Constructor<D>;
export function Mixin<A, B, C, D>(a: typeof BaseModel, b: MixinType<A>, c: MixinType<B>, d: MixinType<C>, e: MixinType<D>): typeof BaseModel & Constructor<A> & Constructor<B> & Constructor<C> & Constructor<D>;
export function Mixin<A, B, C, D, E>(a: typeof BaseModel, b: MixinType<A>, c: MixinType<B>, d: MixinType<C>, e: MixinType<D>, f: MixinType<E>): typeof BaseModel & Constructor<A> & Constructor<B> & Constructor<C> & Constructor<D> & Constructor<E>;
export function Mixin(baseModel: typeof BaseModel, ...constructors: any[]) {
return mixin(class extends baseModel {}, constructors);
export function Mixin(baseCtor: typeof BaseModel, ...constructors: any[]) {
return mixin(class extends baseCtor {}, constructors);
}

function mixin(derivedCtor: any, constructors: any[]): typeof BaseModel {
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
constructors.forEach((ctor) => {
Object.getOwnPropertyNames(ctor.prototype).forEach((name) => {
if (name === 'constructor') {
return;
}
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null),
Object.getOwnPropertyDescriptor(ctor.prototype, name) || Object.create(null),
);
});
});
Expand Down
14 changes: 8 additions & 6 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { AsyncAPIDocumentInterface } from "../../models";
import { BaseModel } from "../base";
import { Info } from "./info";

import { Mixin, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin } from '../mixins';
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';

import { AsyncAPIDocumentInterface, InfoInterface } from "../../models";

export class AsyncAPIDocument
extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin)
extends Mixin(BaseModel, ExtensionsMixin)
implements AsyncAPIDocumentInterface {

version(): string {
return this.json("asyncapi");
return this._json.asyncapi;
}

info(): Info {
return new Info(this.json("info"));
info(): InfoInterface {
return new Info(this._json.info);
}
}
Loading

0 comments on commit d7e6bb0

Please sign in to comment.