-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: port server-variable model to ts (#507)
- Loading branch information
Showing
11 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters