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: port server-variable model to ts #507

Merged
merged 4 commits into from
Apr 4, 2022
Merged
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
11 changes: 11 additions & 0 deletions src/models/server-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {BaseModel} from './base';
import { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';

export interface ServerVariableInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string;
hasDefaultValue(): boolean;
defaultValue(): string | undefined;
hasAllowedValue(): boolean;
allowedValue(): any[]
examples(): Array<string>
}
4 changes: 4 additions & 0 deletions src/models/server-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Collection } from './collection';
import { ServerVariableInterface } from './server-variable';

export interface ServerVariablesInterface extends Collection<ServerVariableInterface> { }
2 changes: 2 additions & 0 deletions src/models/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { BaseModel } from "./base";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
import { ServerVariablesInterface } from "./server-variables";

export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface, ExtensionsMixinInterface {
id(): string
url(): string;
protocol(): string | undefined;
protocolVersion(): string;
hasProtocolVersion(): boolean;
variables(): ServerVariablesInterface
}
35 changes: 35 additions & 0 deletions src/models/v2/server-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BaseModel, ModelMetadata } from '../base';
import { Mixin } from '../utils';
import { ServerVariableInterface } from '../server-variable';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';


export class ServerVariable extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ServerVariableInterface {
constructor(
private readonly _id: string,
_json: Record<string,any>,
_meta: ModelMetadata = {} as any
){
super(_json, _meta);
}
id(): string {
return this._id;
}
hasDefaultValue(): boolean {
return !!this._json.default
}
defaultValue(): string | undefined {
return this._json.default;
}
hasAllowedValue(): boolean {
return !!this._json.enum;
}
allowedValue(): any[] {
return this._json.enum;
}
examples(): string[] {
return this._json.examples
}

}
15 changes: 15 additions & 0 deletions src/models/v2/server-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ServerVariablesInterface } from '../server-variables';
import { Collection } from '../collection';
import { ServerVariableInterface } from '../server-variable';

export class ServerVariables extends Collection<ServerVariableInterface> implements ServerVariablesInterface {
get(id: string): ServerVariableInterface | undefined {
return this.collections.find(serverVariable => serverVariable.id() === id);
}

has(id: string): boolean {
return this.collections.some(serverVariable => serverVariable.id() === id);
}

}

17 changes: 17 additions & 0 deletions src/models/v2/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { ExtensionsMixin } from './mixins/extensions';

import type { ModelMetadata } from "../base";
import type { ServerInterface } from '../server';
import { ServerVariablesInterface } from '../server-variables';
import { ServerVariables } from './server-variables';
import { ServerVariable } from './server-variable';

export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin) implements ServerInterface {
constructor(
Expand Down Expand Up @@ -36,4 +39,18 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Ex
protocolVersion(): string {
return this._json.protocolVersion;
}

variables(): ServerVariablesInterface {
return new ServerVariables(
Object.entries(
this._json.variables
).map(
([serverVariableName, serverVariable]) => this.createModel(
ServerVariable, serverVariable, {
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
}
)
))
}
}
28 changes: 28 additions & 0 deletions src/models/v3/server-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BaseModel } from '../base';
import { Mixin } from '../utils';
import { ServerVariableInterface } from '../server-variable';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';


export class ServerVariable extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ServerVariableInterface {
id(): string {
throw new Error('Method not implemented.');
}
hasDefaultValue(): boolean {
return !!this._json.default
}
defaultValue(): string | undefined {
return this._json.default;
}
hasAllowedValue(): boolean {
return !!this._json.enum
}
allowedValue(): any[] {
return this._json.enum
}
examples(): string[] {
return this._json.examples
}

}
14 changes: 14 additions & 0 deletions src/models/v3/server-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ServerVariablesInterface } from '../server-variables';
import { Collection } from '../collection';
import { ServerVariableInterface } from '../server-variable';

export class ServerVariables extends Collection<ServerVariableInterface> implements ServerVariablesInterface {
get(id: string): ServerVariableInterface | undefined {
return this.collections.find(serverVariable => serverVariable.id() === id);
}

has(id: string): boolean {
return this.collections.some(serverVariable => serverVariable.id() === id);
}

}
17 changes: 17 additions & 0 deletions src/models/v3/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Mixin } from '../utils';
import { BindingsMixin } from './mixins/bindings';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';
import { ServerVariable } from './server-variable';
import { ServerVariables } from './server-variables';
import { ServerVariablesInterface } from '../server-variables';

import type { ModelMetadata } from "../base";
import type { ServerInterface } from '../server';
Expand Down Expand Up @@ -36,4 +39,18 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Ex
protocolVersion(): string {
return this._json.protocolVersion;
}

variables(): ServerVariablesInterface {
return new ServerVariables(
Object.entries(
this._json.variables
).map(
([serverVariableName, serverVariable]) => this.createModel(
ServerVariable, serverVariable, {
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
}
)
))
}
}
39 changes: 39 additions & 0 deletions test/models/v2/server-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {ServerVariable} from '../../../src/models/v2/server-variable';

const doc = {
description: 'Secure connection (TLS) is available through port 8883.',
default: '1883',
enum: ['1883', '8883']
}

const sv = new ServerVariable('doc', doc);

describe('server variable ', function() {
describe('.id()', function() {
expect(sv.id()).toMatch('doc');
})

describe('.hasDefaultValue()', function() {
it('should return true if default value is passed', function(){
expect(sv.hasDefaultValue()).toBeTruthy();
})
})

describe('.defaultValue()', function(){
it('should return default value', function() {
expect(sv.defaultValue()).toMatch(doc.default);
})
})

describe('.hasAllowedValue()', function() {
it('should return true when enum is passed', function(){
expect(sv.hasAllowedValue()).toBeTruthy();
})
})

describe('.allowedValue()', function(){
it('should return enum object', function(){
expect(sv.allowedValue()).toEqual(doc.enum)
})
})
})
15 changes: 14 additions & 1 deletion test/models/v2/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Server } from '../../../src/models/v2/server';
import {ServerVariables} from '../../../src/models/v2/server-variables';

import {
assertDescriptionMixinInheritance,
Expand All @@ -9,7 +10,13 @@ const doc = {
'development': {
protocol: 'mqtt',
protocolVersion: '1.0.0',
url: 'development.gigantic-server.com'
url: 'development.gigantic-server.com',
variables: {
username: {
default: 'demo',
description: 'This value is assigned by the service provider, in this example `gigantic-server.com`'
}
}
}
};
const docItem = new Server('development', doc.development);
Expand Down Expand Up @@ -54,6 +61,12 @@ describe('Server Model', function () {
});
});

describe('.servers()', function(){
it('should return ServerVariables object', function(){
expect(docItem.variables() instanceof ServerVariables).toBeTruthy();
})
})

describe('mixins inheritance', function() {
assertDescriptionMixinInheritance(Server);
assertExtensionsMixinInheritance(Server);
Expand Down