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

refactor: add models for server object #497

Merged
merged 17 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/next-major' into next/model-se…
…rver
  • Loading branch information
Souvikns committed Mar 21, 2022
commit 2330abac68a723f0996b2b3c023864ff1fe9081b
43 changes: 28 additions & 15 deletions src/models/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import type { BaseModel } from './base';

function getMapValue(obj: any, key: string, type: any, options?: any) {
if (typeof key !== 'string' || !obj) return null;
const v = obj[String(key)];
if (v === undefined) return null;
return type ? new type(v, options) : v;
export interface Constructor<T = any> extends Function {
new (...any: any[]): T;
}

export function createMapOfTypes(obj: any, type: any, options?: any) {
const result: Record<string, any> = {};
if (!obj) return result;

for (let [key, value] of Object.entries(obj)) {
result[String(key)] = new type(value, options);
}
export interface MixinType<T = any> extends Function {
prototype: T;
}

return result;
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<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(baseCtor: typeof BaseModel, ...constructors: any[]) {
return mixin(class extends baseCtor {}, constructors);
}

export function getMapValueOfType(obj: any, key: string, type: any, options?: any) {
return getMapValue(obj, key, type, options);
function mixin(derivedCtor: any, constructors: any[]): typeof BaseModel {
constructors.forEach((ctor) => {
Object.getOwnPropertyNames(ctor.prototype).forEach((name) => {
if (name === 'constructor') {
return;
}
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(ctor.prototype, name) || Object.create(null),
);
});
});
return derivedCtor;
}
10 changes: 6 additions & 4 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { BaseModel } from "../base";
import { Info } from "./info";

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

export class AsyncAPIDocument
extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin)
import { AsyncAPIDocumentInterface, InfoInterface } from "../../models";

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

version(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Info } from "./info";
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';

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

version(): string {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.