From 763da46164c5fdcad3c7ae400473773ae2dec7bd Mon Sep 17 00:00:00 2001 From: Souvikns Date: Wed, 16 Mar 2022 19:03:50 +0530 Subject: [PATCH 01/15] feat: add models for server object --- src/models/server-security-requirement.ts | 5 +++ src/models/server-variables.ts | 10 +++++ src/models/server.ts | 13 +++++++ src/models/utils.ts | 22 +++++++++++ src/models/v2/server-security-requirement.ts | 6 +++ src/models/v2/server-variable.ts | 29 +++++++++++++++ src/models/v2/server.ts | 39 ++++++++++++++++++++ test/models/v2/server.spec.ts | 0 8 files changed, 124 insertions(+) create mode 100644 src/models/server-security-requirement.ts create mode 100644 src/models/server-variables.ts create mode 100644 src/models/server.ts create mode 100644 src/models/utils.ts create mode 100644 src/models/v2/server-security-requirement.ts create mode 100644 src/models/v2/server-variable.ts create mode 100644 src/models/v2/server.ts create mode 100644 test/models/v2/server.spec.ts diff --git a/src/models/server-security-requirement.ts b/src/models/server-security-requirement.ts new file mode 100644 index 000000000..f12c22bec --- /dev/null +++ b/src/models/server-security-requirement.ts @@ -0,0 +1,5 @@ +import { BaseModel } from "./base"; + +export interface ServerSecurityRequirementInterface extends BaseModel { + +} \ No newline at end of file diff --git a/src/models/server-variables.ts b/src/models/server-variables.ts new file mode 100644 index 000000000..cfdc7f38c --- /dev/null +++ b/src/models/server-variables.ts @@ -0,0 +1,10 @@ +import { BaseModel } from "./base"; + +export interface ServerVariableInterface extends BaseModel { + allowedValues(): any[]; + allows(name: string): boolean; + hasAllowedValues(): boolean; + defaultValue(): string; + hasDefaultValue(): boolean; + examples(): [string] +} diff --git a/src/models/server.ts b/src/models/server.ts new file mode 100644 index 000000000..aafe252ce --- /dev/null +++ b/src/models/server.ts @@ -0,0 +1,13 @@ +import { BaseModel } from "./base"; +import { ServerSecurityRequirementInterface } from "./server-security-requirement"; +import { ServerVariableInterface } from "./server-variables"; + +export interface ServerInterface extends BaseModel { + url(): string; + protocol(): string; + protocolVersion(): string + variables(): Record; + variable(name: string): ServerVariableInterface; + hasVariables(): boolean; + security(): [ServerSecurityRequirementInterface] | undefined; +} \ No newline at end of file diff --git a/src/models/utils.ts b/src/models/utils.ts new file mode 100644 index 000000000..9b6ff48c4 --- /dev/null +++ b/src/models/utils.ts @@ -0,0 +1,22 @@ + +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 function createMapOfTypes(obj: any, type: any, options?: any) { + const result: Record = {}; + if (!obj) return result; + + for (let [key, value] of Object.entries(obj)) { + result[String(key)] = new type(value, options); + } + + return result; +} + +export function getMapValueOfType(obj: any, key: string, type: any, options?: any) { + return getMapValue(obj, key, type, options); +} \ No newline at end of file diff --git a/src/models/v2/server-security-requirement.ts b/src/models/v2/server-security-requirement.ts new file mode 100644 index 000000000..e92e9a20c --- /dev/null +++ b/src/models/v2/server-security-requirement.ts @@ -0,0 +1,6 @@ +import { BaseModel } from '../base'; +import { ServerSecurityRequirementInterface } from '../server-security-requirement'; + +export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface { + +} \ No newline at end of file diff --git a/src/models/v2/server-variable.ts b/src/models/v2/server-variable.ts new file mode 100644 index 000000000..ec1931015 --- /dev/null +++ b/src/models/v2/server-variable.ts @@ -0,0 +1,29 @@ +import {ServerVariableInterface} from '../server-variables'; +import {BaseModel} from '../base'; + +export class ServerVariable extends BaseModel implements ServerVariableInterface { + allowedValues(): any[] { + return this.json('enum'); + } + + allows(name: string): boolean { + if(this.json('enum') === undefined) return true; + return this.json('enum').includes(name); + } + + hasAllowedValues(): boolean { + return this.json('enum') !== undefined; + } + + defaultValue(): string { + return this.json('default'); + } + + hasDefaultValue(): boolean { + return this.json('default') !== undefined; + } + + examples(): [string] { + return this.json('examples'); + } +} \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts new file mode 100644 index 000000000..da3ded144 --- /dev/null +++ b/src/models/v2/server.ts @@ -0,0 +1,39 @@ +import { createMapOfTypes, getMapValueOfType } from '../utils'; +import { ServerVariableInterface } from 'models/server-variables'; +import { BaseModel } from '../base'; +import { ServerInterface } from "../server"; +import { ServerVariable } from './server-variable'; +import { ServerSecurityRequirementInterface } from 'models/server-security-requirement'; +import { ServerSecurityRequirement } from './server-security-requirement'; +import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from 'models/mixins'; + +export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin, SpecificationExtensionsMixin) implements ServerInterface { + url(): string { + return this.json('url'); + } + + protocol(): string { + return this.json('protocol'); + } + + protocolVersion(): string { + return this.json('protocolVersion'); + } + + variables(): Record { + return createMapOfTypes(this.json('variables'), ServerVariable) + } + + variable(name: string): ServerVariableInterface { + return getMapValueOfType(this.json('variables'), name, ServerVariable); + } + + hasVariables(): boolean { + return !!this.json('variables'); + } + + security(): [ServerSecurityRequirementInterface] { + return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) + } + +} diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts new file mode 100644 index 000000000..e69de29bb From 6a98d2a8f32d675e7bb9da3a65f22467bb2fc2a7 Mon Sep 17 00:00:00 2001 From: Souvikns Date: Thu, 17 Mar 2022 17:21:30 +0530 Subject: [PATCH 02/15] feat: add v3 server object --- src/models/server.ts | 3 +- src/models/v2/index.ts | 3 +- src/models/v2/server.ts | 2 +- src/models/v3/index.ts | 3 +- src/models/v3/server-recurity-requirement.ts | 6 +++ src/models/v3/server-variable.ts | 27 ++++++++++++++ src/models/v3/server.ts | 39 ++++++++++++++++++++ test/models/v2/server.spec.ts | 9 +++++ 8 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/models/v3/server-recurity-requirement.ts create mode 100644 src/models/v3/server-variable.ts create mode 100644 src/models/v3/server.ts diff --git a/src/models/server.ts b/src/models/server.ts index aafe252ce..d6ebeb9e8 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -1,8 +1,9 @@ import { BaseModel } from "./base"; import { ServerSecurityRequirementInterface } from "./server-security-requirement"; import { ServerVariableInterface } from "./server-variables"; +import {BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface} from './mixins'; -export interface ServerInterface extends BaseModel { +export interface ServerInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface { url(): string; protocol(): string; protocolVersion(): string diff --git a/src/models/v2/index.ts b/src/models/v2/index.ts index a06d7d121..cdf55c061 100644 --- a/src/models/v2/index.ts +++ b/src/models/v2/index.ts @@ -1,4 +1,5 @@ export { AsyncAPIDocument as AsyncAPIDocumentV2 } from './asyncapi'; export { Contact as ContactV2 } from './contact'; export { Info as InfoV2 } from './info'; -export { License as LicenseV2 } from './license'; \ No newline at end of file +export { License as LicenseV2 } from './license'; +export {Server as ServerV2} from './server'; \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index da3ded144..75e41dd20 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -7,7 +7,7 @@ import { ServerSecurityRequirementInterface } from 'models/server-security-requi import { ServerSecurityRequirement } from './server-security-requirement'; import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from 'models/mixins'; -export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin, SpecificationExtensionsMixin) implements ServerInterface { +export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, SpecificationExtensionsMixin) implements ServerInterface { url(): string { return this.json('url'); } diff --git a/src/models/v3/index.ts b/src/models/v3/index.ts index 7be2b264d..df1a4dc20 100644 --- a/src/models/v3/index.ts +++ b/src/models/v3/index.ts @@ -1,4 +1,5 @@ export { AsyncAPIDocument as AsyncAPIDocumentV3 } from './asyncapi'; export { Contact as ContactV3 } from './contact'; export { Info as InfoV3 } from './info'; -export { License as LicenseV3 } from './license'; \ No newline at end of file +export { License as LicenseV3 } from './license'; +export {Server as ServerV3} from './server'; \ No newline at end of file diff --git a/src/models/v3/server-recurity-requirement.ts b/src/models/v3/server-recurity-requirement.ts new file mode 100644 index 000000000..e4b935d22 --- /dev/null +++ b/src/models/v3/server-recurity-requirement.ts @@ -0,0 +1,6 @@ +import { BaseModel } from "models/base"; +import { ServerSecurityRequirementInterface } from "models/server-security-requirement"; + +export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface { + +} \ No newline at end of file diff --git a/src/models/v3/server-variable.ts b/src/models/v3/server-variable.ts new file mode 100644 index 000000000..53e6e9be9 --- /dev/null +++ b/src/models/v3/server-variable.ts @@ -0,0 +1,27 @@ +import { BaseModel } from "models/base"; +import { ServerVariableInterface } from "models/server-variables"; + +export class ServerVariable extends BaseModel implements ServerVariableInterface { + allowedValues(): any[] { + return this.json('enum'); + } + allows(name: string): boolean { + if (this.json('enum') === undefined) return true; + return this.json('enum').includes(name); + } + + hasAllowedValues(): boolean { + return this.json('enum') !== undefined; + } + + defaultValue(): string { + return this.json('default'); + } + + hasDefaultValue(): boolean { + return this.json('default') !== undefined; + } + examples(): [string] { + return this.json('examples'); + } +} \ No newline at end of file diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts new file mode 100644 index 000000000..bd4f9a179 --- /dev/null +++ b/src/models/v3/server.ts @@ -0,0 +1,39 @@ +import { BaseModel } from "models/base"; +import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from "models/mixins"; +import { ServerInterface } from "models/server"; +import { ServerSecurityRequirementInterface } from "models/server-security-requirement"; +import { ServerVariableInterface } from "models/server-variables"; +import { createMapOfTypes, getMapValueOfType } from "models/utils"; +import { ServerSecurityRequirement } from "./server-recurity-requirement"; +import { ServerVariable } from "./server-variable"; + + +export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, SpecificationExtensionsMixin) implements ServerInterface { + url(): string { + return this.json('url'); + } + + protocol(): string { + return this.json('protocol'); + } + + protocolVersion(): string { + return this.json('protocolVersion'); + } + + variables(): Record { + return createMapOfTypes(this.json('variables'), ServerVariable); + } + + variable(name: string): ServerVariableInterface { + return getMapValueOfType(this.json('variables'), name, ServerVariable); + } + + hasVariables(): boolean { + return !!this.json('variables'); + } + + security(): [ServerSecurityRequirementInterface] | undefined { + return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) + } +} diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index e69de29bb..00f35d134 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -0,0 +1,9 @@ +import {ServerV3} from '../../../src/models'; + +describe('Server model', function () { + describe('.url', function () { + it('should return the url', function () { + const doc = { url: "https://github.com/asyncapi" }; + }) + }) +}) \ No newline at end of file From 2099076352a64ef279ece529436aed1a05f75fe9 Mon Sep 17 00:00:00 2001 From: souvik Date: Thu, 17 Mar 2022 17:57:01 +0530 Subject: [PATCH 03/15] feat: add server related function to asyncapi document --- src/models/asyncapi.ts | 6 +++++ src/models/utils.ts | 2 +- src/models/v2/asyncapi.ts | 20 +++++++++++++++ src/models/v2/index.ts | 2 +- src/models/v2/server.ts | 6 ++--- src/models/v3/asyncapi.ts | 20 +++++++++++++++ src/models/v3/server-recurity-requirement.ts | 4 +-- src/models/v3/server-variable.ts | 4 +-- src/models/v3/server.ts | 12 ++++----- test/models/v2/server.spec.ts | 27 +++++++++++++++++--- 10 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index e5f95f030..ccf4a46db 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -5,10 +5,16 @@ import { AsyncAPIDocumentV2 } from "./v2"; import { AsyncAPIDocumentV3 } from "./v3"; import { ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface } from "./mixins"; +import { ServerInterface } from "./server"; export interface AsyncAPIDocumentInterface extends BaseModel, ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface { version(): string; info(): InfoInterface; + servers(): Record; + hasServers(): boolean; + serverNames(): Array; + server(name: string): ServerInterface; + } export function newAsyncAPIDocument(json: Record): AsyncAPIDocumentInterface { diff --git a/src/models/utils.ts b/src/models/utils.ts index 9b6ff48c4..9291b065f 100644 --- a/src/models/utils.ts +++ b/src/models/utils.ts @@ -4,7 +4,7 @@ function getMapValue(obj: any, key: string, type: any, options?: any) { const v = obj[String(key)]; if (v === undefined) return null; return type ? new type(v, options) : v; -}; +} export function createMapOfTypes(obj: any, type: any, options?: any) { const result: Record = {}; diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index 7614fd5fc..7bb91b295 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -3,6 +3,9 @@ import { BaseModel } from "../base"; import { Info } from "./info"; import { Mixin, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin } from '../mixins'; +import { ServerInterface } from "../server"; +import { createMapOfTypes, getMapValueOfType } from "../utils"; +import { Server } from "./server"; export class AsyncAPIDocument extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) @@ -15,4 +18,21 @@ export class AsyncAPIDocument info(): Info { return new Info(this.json("info")); } + + servers(): Record { + return createMapOfTypes(this.json('servers'), Server); + } + + hasServers(): boolean { + return !!this.json('servers'); + } + + serverNames(): string[] { + if(!this.json('servers')) return []; + return Object.keys(this.json('servers')); + } + + server(name: string): ServerInterface { + return getMapValueOfType(this.json('servers'), name, Server); + } } diff --git a/src/models/v2/index.ts b/src/models/v2/index.ts index cdf55c061..089742fbe 100644 --- a/src/models/v2/index.ts +++ b/src/models/v2/index.ts @@ -2,4 +2,4 @@ export { AsyncAPIDocument as AsyncAPIDocumentV2 } from './asyncapi'; export { Contact as ContactV2 } from './contact'; export { Info as InfoV2 } from './info'; export { License as LicenseV2 } from './license'; -export {Server as ServerV2} from './server'; \ No newline at end of file +export { Server as ServerV2 } from './server'; \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index 75e41dd20..793c05392 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -1,11 +1,11 @@ import { createMapOfTypes, getMapValueOfType } from '../utils'; -import { ServerVariableInterface } from 'models/server-variables'; +import { ServerVariableInterface } from '../server-variables'; import { BaseModel } from '../base'; import { ServerInterface } from "../server"; import { ServerVariable } from './server-variable'; -import { ServerSecurityRequirementInterface } from 'models/server-security-requirement'; +import { ServerSecurityRequirementInterface } from '../server-security-requirement'; import { ServerSecurityRequirement } from './server-security-requirement'; -import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from 'models/mixins'; +import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from '../mixins'; export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, SpecificationExtensionsMixin) implements ServerInterface { url(): string { diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index 113a738fe..2234f6442 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -3,6 +3,9 @@ import { BaseModel } from "../base"; import { Info } from "./info"; import { Mixin, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin } from '../mixins'; +import { ServerInterface } from "../server"; +import { createMapOfTypes, getMapValueOfType } from "../utils"; +import { Server } from "./server"; export class AsyncAPIDocument extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) @@ -15,4 +18,21 @@ export class AsyncAPIDocument info(): Info { return new Info(this.json("info")); } + + servers(): Record { + return createMapOfTypes(this.json('servers'), Server); + } + + hasServers(): boolean { + return !!this.json('servers'); + } + + serverNames(): string[] { + if(!this.json('servers')) return []; + return Object.keys(this.json('servers')); + } + + server(name: string): ServerInterface { + return getMapValueOfType(this.json('servers'), name, Server); + } } diff --git a/src/models/v3/server-recurity-requirement.ts b/src/models/v3/server-recurity-requirement.ts index e4b935d22..abd7d3af7 100644 --- a/src/models/v3/server-recurity-requirement.ts +++ b/src/models/v3/server-recurity-requirement.ts @@ -1,5 +1,5 @@ -import { BaseModel } from "models/base"; -import { ServerSecurityRequirementInterface } from "models/server-security-requirement"; +import { BaseModel } from "../base"; +import { ServerSecurityRequirementInterface } from "../server-security-requirement"; export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface { diff --git a/src/models/v3/server-variable.ts b/src/models/v3/server-variable.ts index 53e6e9be9..37e524682 100644 --- a/src/models/v3/server-variable.ts +++ b/src/models/v3/server-variable.ts @@ -1,5 +1,5 @@ -import { BaseModel } from "models/base"; -import { ServerVariableInterface } from "models/server-variables"; +import { BaseModel } from "../base"; +import { ServerVariableInterface } from "../server-variables"; export class ServerVariable extends BaseModel implements ServerVariableInterface { allowedValues(): any[] { diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index bd4f9a179..31ed77a5c 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -1,9 +1,9 @@ -import { BaseModel } from "models/base"; -import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from "models/mixins"; -import { ServerInterface } from "models/server"; -import { ServerSecurityRequirementInterface } from "models/server-security-requirement"; -import { ServerVariableInterface } from "models/server-variables"; -import { createMapOfTypes, getMapValueOfType } from "models/utils"; +import { BaseModel } from "../base"; +import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from "../mixins"; +import { ServerInterface } from "../server"; +import { ServerSecurityRequirementInterface } from "../server-security-requirement"; +import { ServerVariableInterface } from "../server-variables"; +import { createMapOfTypes, getMapValueOfType } from "../utils"; import { ServerSecurityRequirement } from "./server-recurity-requirement"; import { ServerVariable } from "./server-variable"; diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index 00f35d134..433dbc4af 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -1,9 +1,30 @@ -import {ServerV3} from '../../../src/models'; +import { ServerV2 } from '../../../src/models'; describe('Server model', function () { - describe('.url', function () { + describe('.url()', function () { it('should return the url', function () { const doc = { url: "https://github.com/asyncapi" }; + const d = new ServerV2(doc); + expect(d.url()).toMatch(doc.url); + }); + + it('should return undefined when tere is no value', function(){ + const doc = {}; + const d = new ServerV2(doc); + expect(d.url()).toBeUndefined(); + }); + }); + + describe('.protocol()', function(){ + it('should return the value', function(){ + const doc = {protocol: 'http'}; + const d = new ServerV2(doc); + expect(d.protocol()).toMatch('http'); + }); + it('should return undefined when there is no value', function(){ + const doc = {}; + const d = new ServerV2(doc); + expect(d.protocol()).toBeUndefined() }) - }) + }); }) \ No newline at end of file From ecaa181c4968275af09921e17b446669e74ab99f Mon Sep 17 00:00:00 2001 From: souvik Date: Fri, 18 Mar 2022 11:45:22 +0530 Subject: [PATCH 04/15] refactor: make requested changes --- src/models/asyncapi.ts | 3 -- src/models/server.ts | 10 +++-- src/models/v2/asyncapi.ts | 9 ---- src/models/v2/index.ts | 3 +- src/models/v2/server.ts | 16 ++++++- src/models/v3/asyncapi.ts | 9 ---- src/models/v3/server.ts | 12 +++++ test/models/v2/server.spec.ts | 85 ++++++++++++++++++++++++++++------- 8 files changed, 104 insertions(+), 43 deletions(-) diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index ccf4a46db..4dd5bbf5a 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -11,10 +11,7 @@ export interface AsyncAPIDocumentInterface extends BaseModel, ExternalDocsMixinI version(): string; info(): InfoInterface; servers(): Record; - hasServers(): boolean; - serverNames(): Array; server(name: string): ServerInterface; - } export function newAsyncAPIDocument(json: Record): AsyncAPIDocumentInterface { diff --git a/src/models/server.ts b/src/models/server.ts index d6ebeb9e8..c88c30f9b 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -1,14 +1,18 @@ import { BaseModel } from "./base"; import { ServerSecurityRequirementInterface } from "./server-security-requirement"; import { ServerVariableInterface } from "./server-variables"; -import {BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface} from './mixins'; +import { BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface } from './mixins'; export interface ServerInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface { url(): string; + hasUrl(): boolean; protocol(): string; - protocolVersion(): string + hasProtocol(): boolean; + protocolVersion(): string | undefined; + hasProtocolVersion(): boolean; variables(): Record; - variable(name: string): ServerVariableInterface; + variables(): Record | undefined; + variable(name: string): ServerVariableInterface | undefined; hasVariables(): boolean; security(): [ServerSecurityRequirementInterface] | undefined; } \ No newline at end of file diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index 7bb91b295..6c1f58f26 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -23,15 +23,6 @@ export class AsyncAPIDocument return createMapOfTypes(this.json('servers'), Server); } - hasServers(): boolean { - return !!this.json('servers'); - } - - serverNames(): string[] { - if(!this.json('servers')) return []; - return Object.keys(this.json('servers')); - } - server(name: string): ServerInterface { return getMapValueOfType(this.json('servers'), name, Server); } diff --git a/src/models/v2/index.ts b/src/models/v2/index.ts index 089742fbe..8a1a22302 100644 --- a/src/models/v2/index.ts +++ b/src/models/v2/index.ts @@ -2,4 +2,5 @@ export { AsyncAPIDocument as AsyncAPIDocumentV2 } from './asyncapi'; export { Contact as ContactV2 } from './contact'; export { Info as InfoV2 } from './info'; export { License as LicenseV2 } from './license'; -export { Server as ServerV2 } from './server'; \ No newline at end of file +export { Server as ServerV2 } from './server'; +export {ServerVariable as ServerVariableV2} from './server-variable'; \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index 793c05392..d71b89457 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -12,15 +12,29 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp return this.json('url'); } + hasUrl(): boolean { + return !!this.json('url'); + } + protocol(): string { return this.json('protocol'); } + hasProtocol(): boolean { + return !!this.json('protocol'); + } + protocolVersion(): string { return this.json('protocolVersion'); } - variables(): Record { + hasProtocolVersion(): boolean { + return !!this.json('protocolVersion'); + } + + variables(): Record; + variables(): Record | undefined { + if(!this.json('variables')) return undefined; return createMapOfTypes(this.json('variables'), ServerVariable) } diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index 2234f6442..2aa3aa0f4 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -23,15 +23,6 @@ export class AsyncAPIDocument return createMapOfTypes(this.json('servers'), Server); } - hasServers(): boolean { - return !!this.json('servers'); - } - - serverNames(): string[] { - if(!this.json('servers')) return []; - return Object.keys(this.json('servers')); - } - server(name: string): ServerInterface { return getMapValueOfType(this.json('servers'), name, Server); } diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index 31ed77a5c..d052094eb 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -13,14 +13,26 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp return this.json('url'); } + hasUrl(): boolean { + return !!this.json('url'); + } + protocol(): string { return this.json('protocol'); } + hasProtocol(): boolean { + return !!this.json('protocol'); + } + protocolVersion(): string { return this.json('protocolVersion'); } + hasProtocolVersion(): boolean { + return !!this.json('protocolVersion'); + } + variables(): Record { return createMapOfTypes(this.json('variables'), ServerVariable); } diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index 433dbc4af..fc2355fda 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -1,30 +1,81 @@ -import { ServerV2 } from '../../../src/models'; +import { ServerV2, ServerVariableV2 } from '../../../src/models'; + +const doc1 = {} +const doc2 = { + "url": "development.gigantic-server.com", + "description": "Development server", + "protocol": "kafka", + "protocolVersion": "1.0.0", + "variables": { + "username": { + "default": "demo", + "description": "This value is assigned by the service provider, in this example `gigantic-server.com`" + }, + "port": { + "enum": [ + "8883", + "8884" + ], + "default": "8883" + }, + "basePath": { + "default": "v2" + } + } +} + +const d1 = new ServerV2(doc1); +const d2 = new ServerV2(doc2); describe('Server model', function () { describe('.url()', function () { it('should return the url', function () { - const doc = { url: "https://github.com/asyncapi" }; - const d = new ServerV2(doc); - expect(d.url()).toMatch(doc.url); + expect(d2.url()).toMatch(doc2.url); + }); + + it('should return undefined when tere is no value', function () { + expect(d1.url()).toBeUndefined(); + }); + }); + + describe('.protocol()', function () { + it('should return the value', function () { + expect(d2.protocol()).toMatch(doc2.protocol); + }); + + it('should return undefined when there is no value', function () { + expect(d1.protocol()).toBeUndefined(); + }); + }); + + describe('.hasProtocol()', function(){ + it('should return true if is there is protocol', function(){ + expect(d2.hasProtocol()).toBeTruthy(); }); - it('should return undefined when tere is no value', function(){ - const doc = {}; - const d = new ServerV2(doc); - expect(d.url()).toBeUndefined(); + it('should return false if there is no protocol', function(){ + expect(d1.hasProtocol()).toBeFalsy(); }); }); - describe('.protocol()', function(){ - it('should return the value', function(){ - const doc = {protocol: 'http'}; - const d = new ServerV2(doc); - expect(d.protocol()).toMatch('http'); + describe('.protocolVersion()', function() { + it('should return the value', function() { + expect(d2.protocolVersion()).toMatch(doc2.protocolVersion); }); + it('should return undefined when there is no value', function(){ - const doc = {}; - const d = new ServerV2(doc); - expect(d.protocol()).toBeUndefined() - }) + expect(d1.protocolVersion()).toBeUndefined(); + }); + }); + + describe('.hasProtocolVersion()', function(){ + it('should return true if there is protocolVersion', function(){ + expect(d2.hasProtocolVersion()).toBeTruthy(); + }); + + it('should return false if there is no protocolVersion', function() { + expect(d1.hasProtocolVersion()).toBeFalsy(); + }); }); + }) \ No newline at end of file From 6523013e8d6031fe317164b9d341013df9cab4c2 Mon Sep 17 00:00:00 2001 From: Souvikns Date: Fri, 18 Mar 2022 16:59:58 +0530 Subject: [PATCH 05/15] refactor: add suggested changes --- src/models/server.ts | 9 ++++----- src/models/v2/server.ts | 6 +++++- src/models/v3/server.ts | 6 +++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/models/server.ts b/src/models/server.ts index c88c30f9b..67023eba3 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -5,14 +5,13 @@ import { BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensi export interface ServerInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface { url(): string; - hasUrl(): boolean; protocol(): string; - hasProtocol(): boolean; protocolVersion(): string | undefined; hasProtocolVersion(): boolean; - variables(): Record; variables(): Record | undefined; - variable(name: string): ServerVariableInterface | undefined; + variables(name: string): ServerVariableInterface | undefined; hasVariables(): boolean; - security(): [ServerSecurityRequirementInterface] | undefined; + hasVariables(name: string): boolean; + security(): ServerSecurityRequirementInterface[] | undefined; + hasSecurity(): boolean; } \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index d71b89457..9f2be483c 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -32,7 +32,7 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp return !!this.json('protocolVersion'); } - variables(): Record; + variables(): undefined; variables(): Record | undefined { if(!this.json('variables')) return undefined; return createMapOfTypes(this.json('variables'), ServerVariable) @@ -50,4 +50,8 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) } + hasSecurity(): boolean { + return !!this.json('security'); + } + } diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index d052094eb..3c09037f6 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -33,7 +33,8 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp return !!this.json('protocolVersion'); } - variables(): Record { + variables(): undefined; + variables(): Record | undefined { return createMapOfTypes(this.json('variables'), ServerVariable); } @@ -48,4 +49,7 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Sp security(): [ServerSecurityRequirementInterface] | undefined { return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) } + hasSecurity(): boolean { + return !!this.json('security'); + } } From 20dea9180c2722286d99c64e776bcfba8996cabe Mon Sep 17 00:00:00 2001 From: Souvikns Date: Fri, 18 Mar 2022 17:08:32 +0530 Subject: [PATCH 06/15] refactor: update asyncapi.ts --- src/models/asyncapi.ts | 3 ++- src/models/v2/asyncapi.ts | 16 +++++++++++----- src/models/v3/asyncapi.ts | 15 +++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index 4dd5bbf5a..13f2db7cd 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -11,7 +11,8 @@ export interface AsyncAPIDocumentInterface extends BaseModel, ExternalDocsMixinI version(): string; info(): InfoInterface; servers(): Record; - server(name: string): ServerInterface; + servers(name: string): ServerInterface; + hasServer(name:string): boolean; } export function newAsyncAPIDocument(json: Record): AsyncAPIDocumentInterface { diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index 6c1f58f26..8ba5604a9 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -7,8 +7,8 @@ import { ServerInterface } from "../server"; import { createMapOfTypes, getMapValueOfType } from "../utils"; import { Server } from "./server"; -export class AsyncAPIDocument - extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) +export class AsyncAPIDocument + extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) implements AsyncAPIDocumentInterface { version(): string { @@ -19,11 +19,17 @@ export class AsyncAPIDocument return new Info(this.json("info")); } - servers(): Record { + servers(): Record; + servers(name: string): ServerInterface; + servers(name?: any): ServerInterface | Record { + if (name) { + return getMapValueOfType(this.json('servers'), name, Server); + } + return createMapOfTypes(this.json('servers'), Server); } - server(name: string): ServerInterface { - return getMapValueOfType(this.json('servers'), name, Server); + hasServer(name: string): boolean { + return Object.keys(this.json('servers')).includes(name) ? true : false; } } diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index 2aa3aa0f4..63c2ce4cc 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -7,10 +7,10 @@ import { ServerInterface } from "../server"; import { createMapOfTypes, getMapValueOfType } from "../utils"; import { Server } from "./server"; -export class AsyncAPIDocument - extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) +export class AsyncAPIDocument + extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) implements AsyncAPIDocumentInterface { - + version(): string { return this.json("asyncapi"); } @@ -19,11 +19,14 @@ export class AsyncAPIDocument return new Info(this.json("info")); } - servers(): Record { + servers(): Record; + servers(name: string): ServerInterface; + servers(name?: any): ServerInterface | Record { + if (name) return getMapValueOfType(this.json('servers'), name, Server); return createMapOfTypes(this.json('servers'), Server); } - server(name: string): ServerInterface { - return getMapValueOfType(this.json('servers'), name, Server); + hasServer(name: string): boolean { + return Object.keys(this.json('servers')).includes(name) ? true : false; } } From d4ed90b9a04d3f21c00b30bb14f0547b5cd9c502 Mon Sep 17 00:00:00 2001 From: souvik Date: Mon, 21 Mar 2022 09:28:12 +0530 Subject: [PATCH 07/15] chore: clean up --- src/models/asyncapi.ts | 3 - src/models/server-security-requirement.ts | 5 -- src/models/server-variables.ts | 10 --- src/models/server.ts | 18 ----- src/models/v2/asyncapi.ts | 10 --- src/models/v2/index.ts | 4 +- src/models/v2/server-security-requirement.ts | 6 -- src/models/v2/server-variable.ts | 29 ------- src/models/v2/server.ts | 53 ------------- src/models/v3/asyncapi.ts | 11 --- src/models/v3/index.ts | 3 +- src/models/v3/server-recurity-requirement.ts | 6 -- src/models/v3/server-variable.ts | 27 ------- src/models/v3/server.ts | 51 ------------ test/models/v2/server.spec.ts | 81 -------------------- 15 files changed, 2 insertions(+), 315 deletions(-) delete mode 100644 src/models/server-security-requirement.ts delete mode 100644 src/models/server-variables.ts delete mode 100644 src/models/server.ts delete mode 100644 src/models/v2/server-security-requirement.ts delete mode 100644 src/models/v2/server-variable.ts delete mode 100644 src/models/v2/server.ts delete mode 100644 src/models/v3/server-recurity-requirement.ts delete mode 100644 src/models/v3/server-variable.ts delete mode 100644 src/models/v3/server.ts delete mode 100644 test/models/v2/server.spec.ts diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index 4dd5bbf5a..e5f95f030 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -5,13 +5,10 @@ import { AsyncAPIDocumentV2 } from "./v2"; import { AsyncAPIDocumentV3 } from "./v3"; import { ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface } from "./mixins"; -import { ServerInterface } from "./server"; export interface AsyncAPIDocumentInterface extends BaseModel, ExternalDocsMixinInterface, SpecificationExtensionsMixinInterface, TagsMixinInterface { version(): string; info(): InfoInterface; - servers(): Record; - server(name: string): ServerInterface; } export function newAsyncAPIDocument(json: Record): AsyncAPIDocumentInterface { diff --git a/src/models/server-security-requirement.ts b/src/models/server-security-requirement.ts deleted file mode 100644 index f12c22bec..000000000 --- a/src/models/server-security-requirement.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { BaseModel } from "./base"; - -export interface ServerSecurityRequirementInterface extends BaseModel { - -} \ No newline at end of file diff --git a/src/models/server-variables.ts b/src/models/server-variables.ts deleted file mode 100644 index cfdc7f38c..000000000 --- a/src/models/server-variables.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { BaseModel } from "./base"; - -export interface ServerVariableInterface extends BaseModel { - allowedValues(): any[]; - allows(name: string): boolean; - hasAllowedValues(): boolean; - defaultValue(): string; - hasDefaultValue(): boolean; - examples(): [string] -} diff --git a/src/models/server.ts b/src/models/server.ts deleted file mode 100644 index c88c30f9b..000000000 --- a/src/models/server.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { BaseModel } from "./base"; -import { ServerSecurityRequirementInterface } from "./server-security-requirement"; -import { ServerVariableInterface } from "./server-variables"; -import { BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface } from './mixins'; - -export interface ServerInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, SpecificationExtensionsMixinInterface { - url(): string; - hasUrl(): boolean; - protocol(): string; - hasProtocol(): boolean; - protocolVersion(): string | undefined; - hasProtocolVersion(): boolean; - variables(): Record; - variables(): Record | undefined; - variable(name: string): ServerVariableInterface | undefined; - hasVariables(): boolean; - security(): [ServerSecurityRequirementInterface] | undefined; -} \ No newline at end of file diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index 6c1f58f26..ebb52c374 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -3,9 +3,7 @@ import { BaseModel } from "../base"; import { Info } from "./info"; import { Mixin, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin } from '../mixins'; -import { ServerInterface } from "../server"; import { createMapOfTypes, getMapValueOfType } from "../utils"; -import { Server } from "./server"; export class AsyncAPIDocument extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) @@ -18,12 +16,4 @@ export class AsyncAPIDocument info(): Info { return new Info(this.json("info")); } - - servers(): Record { - return createMapOfTypes(this.json('servers'), Server); - } - - server(name: string): ServerInterface { - return getMapValueOfType(this.json('servers'), name, Server); - } } diff --git a/src/models/v2/index.ts b/src/models/v2/index.ts index 8a1a22302..a06d7d121 100644 --- a/src/models/v2/index.ts +++ b/src/models/v2/index.ts @@ -1,6 +1,4 @@ export { AsyncAPIDocument as AsyncAPIDocumentV2 } from './asyncapi'; export { Contact as ContactV2 } from './contact'; export { Info as InfoV2 } from './info'; -export { License as LicenseV2 } from './license'; -export { Server as ServerV2 } from './server'; -export {ServerVariable as ServerVariableV2} from './server-variable'; \ No newline at end of file +export { License as LicenseV2 } from './license'; \ No newline at end of file diff --git a/src/models/v2/server-security-requirement.ts b/src/models/v2/server-security-requirement.ts deleted file mode 100644 index e92e9a20c..000000000 --- a/src/models/v2/server-security-requirement.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { BaseModel } from '../base'; -import { ServerSecurityRequirementInterface } from '../server-security-requirement'; - -export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface { - -} \ No newline at end of file diff --git a/src/models/v2/server-variable.ts b/src/models/v2/server-variable.ts deleted file mode 100644 index ec1931015..000000000 --- a/src/models/v2/server-variable.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {ServerVariableInterface} from '../server-variables'; -import {BaseModel} from '../base'; - -export class ServerVariable extends BaseModel implements ServerVariableInterface { - allowedValues(): any[] { - return this.json('enum'); - } - - allows(name: string): boolean { - if(this.json('enum') === undefined) return true; - return this.json('enum').includes(name); - } - - hasAllowedValues(): boolean { - return this.json('enum') !== undefined; - } - - defaultValue(): string { - return this.json('default'); - } - - hasDefaultValue(): boolean { - return this.json('default') !== undefined; - } - - examples(): [string] { - return this.json('examples'); - } -} \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts deleted file mode 100644 index d71b89457..000000000 --- a/src/models/v2/server.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { createMapOfTypes, getMapValueOfType } from '../utils'; -import { ServerVariableInterface } from '../server-variables'; -import { BaseModel } from '../base'; -import { ServerInterface } from "../server"; -import { ServerVariable } from './server-variable'; -import { ServerSecurityRequirementInterface } from '../server-security-requirement'; -import { ServerSecurityRequirement } from './server-security-requirement'; -import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from '../mixins'; - -export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, SpecificationExtensionsMixin) implements ServerInterface { - url(): string { - return this.json('url'); - } - - hasUrl(): boolean { - return !!this.json('url'); - } - - protocol(): string { - return this.json('protocol'); - } - - hasProtocol(): boolean { - return !!this.json('protocol'); - } - - protocolVersion(): string { - return this.json('protocolVersion'); - } - - hasProtocolVersion(): boolean { - return !!this.json('protocolVersion'); - } - - variables(): Record; - variables(): Record | undefined { - if(!this.json('variables')) return undefined; - return createMapOfTypes(this.json('variables'), ServerVariable) - } - - variable(name: string): ServerVariableInterface { - return getMapValueOfType(this.json('variables'), name, ServerVariable); - } - - hasVariables(): boolean { - return !!this.json('variables'); - } - - security(): [ServerSecurityRequirementInterface] { - return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) - } - -} diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index 2aa3aa0f4..113a738fe 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -3,9 +3,6 @@ import { BaseModel } from "../base"; import { Info } from "./info"; import { Mixin, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin } from '../mixins'; -import { ServerInterface } from "../server"; -import { createMapOfTypes, getMapValueOfType } from "../utils"; -import { Server } from "./server"; export class AsyncAPIDocument extends Mixin(BaseModel, ExternalDocsMixin, SpecificationExtensionsMixin, TagsMixin) @@ -18,12 +15,4 @@ export class AsyncAPIDocument info(): Info { return new Info(this.json("info")); } - - servers(): Record { - return createMapOfTypes(this.json('servers'), Server); - } - - server(name: string): ServerInterface { - return getMapValueOfType(this.json('servers'), name, Server); - } } diff --git a/src/models/v3/index.ts b/src/models/v3/index.ts index df1a4dc20..7be2b264d 100644 --- a/src/models/v3/index.ts +++ b/src/models/v3/index.ts @@ -1,5 +1,4 @@ export { AsyncAPIDocument as AsyncAPIDocumentV3 } from './asyncapi'; export { Contact as ContactV3 } from './contact'; export { Info as InfoV3 } from './info'; -export { License as LicenseV3 } from './license'; -export {Server as ServerV3} from './server'; \ No newline at end of file +export { License as LicenseV3 } from './license'; \ No newline at end of file diff --git a/src/models/v3/server-recurity-requirement.ts b/src/models/v3/server-recurity-requirement.ts deleted file mode 100644 index abd7d3af7..000000000 --- a/src/models/v3/server-recurity-requirement.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { BaseModel } from "../base"; -import { ServerSecurityRequirementInterface } from "../server-security-requirement"; - -export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface { - -} \ No newline at end of file diff --git a/src/models/v3/server-variable.ts b/src/models/v3/server-variable.ts deleted file mode 100644 index 37e524682..000000000 --- a/src/models/v3/server-variable.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { BaseModel } from "../base"; -import { ServerVariableInterface } from "../server-variables"; - -export class ServerVariable extends BaseModel implements ServerVariableInterface { - allowedValues(): any[] { - return this.json('enum'); - } - allows(name: string): boolean { - if (this.json('enum') === undefined) return true; - return this.json('enum').includes(name); - } - - hasAllowedValues(): boolean { - return this.json('enum') !== undefined; - } - - defaultValue(): string { - return this.json('default'); - } - - hasDefaultValue(): boolean { - return this.json('default') !== undefined; - } - examples(): [string] { - return this.json('examples'); - } -} \ No newline at end of file diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts deleted file mode 100644 index d052094eb..000000000 --- a/src/models/v3/server.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { BaseModel } from "../base"; -import { BindingsMixin, DescriptionMixin, Mixin, SpecificationExtensionsMixin } from "../mixins"; -import { ServerInterface } from "../server"; -import { ServerSecurityRequirementInterface } from "../server-security-requirement"; -import { ServerVariableInterface } from "../server-variables"; -import { createMapOfTypes, getMapValueOfType } from "../utils"; -import { ServerSecurityRequirement } from "./server-recurity-requirement"; -import { ServerVariable } from "./server-variable"; - - -export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, SpecificationExtensionsMixin) implements ServerInterface { - url(): string { - return this.json('url'); - } - - hasUrl(): boolean { - return !!this.json('url'); - } - - protocol(): string { - return this.json('protocol'); - } - - hasProtocol(): boolean { - return !!this.json('protocol'); - } - - protocolVersion(): string { - return this.json('protocolVersion'); - } - - hasProtocolVersion(): boolean { - return !!this.json('protocolVersion'); - } - - variables(): Record { - return createMapOfTypes(this.json('variables'), ServerVariable); - } - - variable(name: string): ServerVariableInterface { - return getMapValueOfType(this.json('variables'), name, ServerVariable); - } - - hasVariables(): boolean { - return !!this.json('variables'); - } - - security(): [ServerSecurityRequirementInterface] | undefined { - return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) - } -} diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts deleted file mode 100644 index fc2355fda..000000000 --- a/test/models/v2/server.spec.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { ServerV2, ServerVariableV2 } from '../../../src/models'; - -const doc1 = {} -const doc2 = { - "url": "development.gigantic-server.com", - "description": "Development server", - "protocol": "kafka", - "protocolVersion": "1.0.0", - "variables": { - "username": { - "default": "demo", - "description": "This value is assigned by the service provider, in this example `gigantic-server.com`" - }, - "port": { - "enum": [ - "8883", - "8884" - ], - "default": "8883" - }, - "basePath": { - "default": "v2" - } - } -} - -const d1 = new ServerV2(doc1); -const d2 = new ServerV2(doc2); - -describe('Server model', function () { - describe('.url()', function () { - it('should return the url', function () { - expect(d2.url()).toMatch(doc2.url); - }); - - it('should return undefined when tere is no value', function () { - expect(d1.url()).toBeUndefined(); - }); - }); - - describe('.protocol()', function () { - it('should return the value', function () { - expect(d2.protocol()).toMatch(doc2.protocol); - }); - - it('should return undefined when there is no value', function () { - expect(d1.protocol()).toBeUndefined(); - }); - }); - - describe('.hasProtocol()', function(){ - it('should return true if is there is protocol', function(){ - expect(d2.hasProtocol()).toBeTruthy(); - }); - - it('should return false if there is no protocol', function(){ - expect(d1.hasProtocol()).toBeFalsy(); - }); - }); - - describe('.protocolVersion()', function() { - it('should return the value', function() { - expect(d2.protocolVersion()).toMatch(doc2.protocolVersion); - }); - - it('should return undefined when there is no value', function(){ - expect(d1.protocolVersion()).toBeUndefined(); - }); - }); - - describe('.hasProtocolVersion()', function(){ - it('should return true if there is protocolVersion', function(){ - expect(d2.hasProtocolVersion()).toBeTruthy(); - }); - - it('should return false if there is no protocolVersion', function() { - expect(d1.hasProtocolVersion()).toBeFalsy(); - }); - }); - -}) \ No newline at end of file From 798db97cbae726f5ed14edf2a262e7ad4b033b43 Mon Sep 17 00:00:00 2001 From: Souvikns Date: Mon, 21 Mar 2022 18:44:02 +0530 Subject: [PATCH 08/15] feat: add server object based on server-api --- src/models/server.ts | 14 +++++++++++++ src/models/servers.ts | 4 ++++ src/models/v2/server.ts | 43 ++++++++++++++++++++++++++++++++++++++++ src/models/v2/servers.ts | 13 ++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 src/models/server.ts create mode 100644 src/models/servers.ts create mode 100644 src/models/v2/server.ts create mode 100644 src/models/v2/servers.ts diff --git a/src/models/server.ts b/src/models/server.ts new file mode 100644 index 000000000..9b3a80d6e --- /dev/null +++ b/src/models/server.ts @@ -0,0 +1,14 @@ +import { BaseModel } from "./base"; +import { BindingsMixinInterface, DescriptionMixinInterface } from './mixins'; + +export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface { + id(): string; + name(): string | undefined; + hasName(): boolean; + protocol(): string | undefined; + hasProtocol(): boolean; + protocolVersion(): string; + hasProtocolVersion(): boolean; + url(): string; + hasUrl(): boolean; +} \ No newline at end of file diff --git a/src/models/servers.ts b/src/models/servers.ts new file mode 100644 index 000000000..d8360d487 --- /dev/null +++ b/src/models/servers.ts @@ -0,0 +1,4 @@ +import { Collection } from "./collection"; +import {ServerInterface} from "./server"; + +export interface ServersInterface extends Collection {} \ No newline at end of file diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts new file mode 100644 index 000000000..ec92dc113 --- /dev/null +++ b/src/models/v2/server.ts @@ -0,0 +1,43 @@ +import { Mixin } from '../utils'; +import { BaseModel } from '../base'; +import { ServerInterface } from '../server'; +import { DescriptionMixin } from './mixins/description'; +import { BindingsMixin } from './mixins/bindings'; + +export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { + id(): string { + return this.json('id'); + } + + hasName(): boolean { + return !!this.json('name'); + } + + name(): string | undefined { + return this.json('name'); + } + + hasProtocol(): boolean { + return !!this.json('protocol'); + } + + protocol(): string | undefined { + return this.json('protocol'); + } + + hasProtocolVersion(): boolean { + return !!this.json('protocolVersion'); + } + + protocolVersion(): string { + return this.json('protocolVersion'); + } + + hasUrl(): boolean { + return !!this.json('url'); + } + + url(): string { + return this.json('url'); + } +} \ No newline at end of file diff --git a/src/models/v2/servers.ts b/src/models/v2/servers.ts new file mode 100644 index 000000000..fd4c9f9ef --- /dev/null +++ b/src/models/v2/servers.ts @@ -0,0 +1,13 @@ +import { Collection } from 'models/collection'; +import { ServerInterface } from 'models/server'; +import { ServersInterface } from '../servers'; + +export class Servers extends Collection implements ServersInterface { + override get(id: string): ServerInterface | undefined { + return this.collections.find(server => server.id() === id); + } + + override has(id: string): boolean { + return this.collections.some(server => server.id() === id); + } +} \ No newline at end of file From 101cad07df51389d2a9e76df07914eeca4482c1d Mon Sep 17 00:00:00 2001 From: Souvikns Date: Mon, 21 Mar 2022 19:51:54 +0530 Subject: [PATCH 09/15] feat: add tests --- src/models/v2/servers.ts | 4 +- test/models/v2/server.spec.ts | 150 ++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 test/models/v2/server.spec.ts diff --git a/src/models/v2/servers.ts b/src/models/v2/servers.ts index fd4c9f9ef..c8e9fa0dc 100644 --- a/src/models/v2/servers.ts +++ b/src/models/v2/servers.ts @@ -1,5 +1,5 @@ -import { Collection } from 'models/collection'; -import { ServerInterface } from 'models/server'; +import { Collection } from '../collection'; +import { ServerInterface } from '../server'; import { ServersInterface } from '../servers'; export class Servers extends Collection implements ServersInterface { diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts new file mode 100644 index 000000000..f2edac945 --- /dev/null +++ b/test/models/v2/server.spec.ts @@ -0,0 +1,150 @@ +import { Server } from '../../../src/models/v2/server'; +import { Servers } from '../../../src/models/v2/servers'; + +import { + assertBindingsMixinInheritance, + assertDescriptionMixinInheritance, +} from './mixins/inheritance'; + +const doc = { + id: 'id', + name: 'development', + protocol: 'mqtt', + protocolVersion: '1.0.0', + url: 'development.gigantic-server.com' +}; +const docItem = new Server(doc); +const emptyItem = new Server({}); + +describe('Servers model', function () { + describe('.isEmpty()', function () { + it('should return true if collection is empty', function () { + const servers = new Servers([]); + expect(servers.isEmpty()).toBeTruthy(); + }); + + it('should return false if collection is not empty', function () { + const servers = new Servers([docItem]); + expect(servers.isEmpty()).toBeFalsy(); + }); + }) + + describe('.get(id)', function () { + it('should return a specific server Object if it is present', function () { + const servers = new Servers([docItem]); + expect(servers.get('id')).toBeTruthy(); + }); + + it('should return undefined if a server is said Id is missing ', function () { + const servers = new Servers([]); + expect(servers.get('id')).toBeUndefined(); + }); + }) + + describe('.has(id)', function () { + + const servers = new Servers([docItem]); + + it('should return true if the said Id is available', function () { + expect(servers.has('id')).toBeTruthy(); + }) + + it('should return fase id the said id is missing', function () { + expect(servers.has('uid')).toBeFalsy(); + }) + }) +}) + +describe('Server Model', function () { + + describe('.id()', function () { + it('should return id', function () { + expect(docItem.id()).toMatch('id'); + }) + }) + + describe('.hasName()', function () { + it('should return true if name is present', function () { + expect(docItem.hasName()).toBeTruthy(); + }) + + it('should return false if name is not present', function () { + expect(emptyItem.hasName()).toBeFalsy(); + }) + }) + + describe('.name()', function () { + it('should return name if present', function () { + expect(docItem.name()).toMatch(doc.name); + }) + + it('should return undefined if name is not present', function () { + expect(emptyItem.name()).toBeUndefined(); + }) + }) + + describe('.hasProtocol()', function () { + it('should return true if protocol is present', function() { + expect(docItem.hasProtocol()).toBeTruthy(); + }) + + it('should return false if protocol is not present', function(){ + expect(emptyItem.hasProtocol()).toBeFalsy(); + }) + }) + + describe('protocol()', function () { + it('should return protocol ', function() { + expect(docItem.protocol()).toMatch(doc.protocol); + }) + + it('should return undefined when protocol is missing', function() { + expect(emptyItem.protocol()).toBeUndefined(); + }) + }) + + describe('.hasProtocolVersion()', function () { + it('should return true if protocolVersion is not missing', function() { + expect(docItem.hasProtocolVersion()).toBeTruthy(); + }) + + it('should be false when protocolVersion is missing', function() { + expect(emptyItem.hasProtocolVersion()).toBeFalsy(); + }) + }) + + describe('.protocolVersion()', function () { + it('should return protocolVersion', function() { + expect(docItem.protocolVersion()).toMatch(doc.protocolVersion); + }) + + it('should return undefined protocolVersion when protocolVersion is missing', function() { + expect(emptyItem.protocolVersion()).toBeUndefined(); + }) + }) + + describe('.hasUrl()', function () { + it('should return true if url is not missing', function(){ + expect(docItem.hasUrl()).toBeTruthy(); + }) + + it('should return false when url is missing', function() { + expect(emptyItem.hasUrl()).toBeFalsy(); + }) + }) + + describe('.url()', function () { + it('should return url', function(){ + expect(docItem.url()).toMatch(doc.url); + }) + + it('should return undefined when url is missing', function() { + expect(emptyItem.url()).toBeUndefined(); + }) + }) + + describe('mixins inheritance', function () { + assertDescriptionMixinInheritance(Server); + assertBindingsMixinInheritance(Server); + }) +}) \ No newline at end of file From a9fa78c6a8b999946ffb3374dc219b1627b5a78b Mon Sep 17 00:00:00 2001 From: souvik Date: Wed, 23 Mar 2022 20:00:08 +0530 Subject: [PATCH 10/15] refactor: add servers function to asyncapi document --- src/models/asyncapi.ts | 2 ++ src/models/utils.ts | 10 ++++++++ src/models/v2/asyncapi.ts | 8 +++++- src/models/v2/index.ts | 4 ++- src/models/v3/asyncapi.ts | 6 +++++ src/models/v3/index.ts | 4 ++- src/models/v3/server.ts | 43 +++++++++++++++++++++++++++++++++ src/models/v3/servers.ts | 13 ++++++++++ test/models/v2/asyncapi.spec.ts | 14 ++++++++++- 9 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/models/v3/server.ts create mode 100644 src/models/v3/servers.ts diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index 6fb68a1ab..0c20dfcb5 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -4,10 +4,12 @@ import { AsyncAPIDocumentV3 } from "./v3"; import type { InfoInterface } from "./info"; import type { BaseModel } from "./base"; import type { ExtensionsMixinInterface } from "./mixins"; +import { ServersInterface } from "./servers"; export interface AsyncAPIDocumentInterface extends BaseModel, ExtensionsMixinInterface { version(): string; info(): InfoInterface; + servers(): ServersInterface } export function newAsyncAPIDocument(json: Record): AsyncAPIDocumentInterface { diff --git a/src/models/utils.ts b/src/models/utils.ts index 0b73d948a..e2befd7a8 100644 --- a/src/models/utils.ts +++ b/src/models/utils.ts @@ -32,4 +32,14 @@ function mixin(derivedCtor: any, constructors: any[]): typeof BaseModel { }); }); return derivedCtor; +} + +export function createArrayFromMap(json: Record){ + const ArrayObject = []; + for (const [key, value] of Object.entries(json)) { + value['id'] = key; + ArrayObject.push(value); + }; + + return ArrayObject; } \ No newline at end of file diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index e690f3fcb..8a70b6cc8 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -1,10 +1,12 @@ import { BaseModel } from "../base"; import { Info } from "./info"; -import { Mixin } from '../utils'; +import { createArrayFromMap, Mixin } from '../utils'; import { ExtensionsMixin } from './mixins/extensions'; import { AsyncAPIDocumentInterface, InfoInterface } from "../../models"; +import { ServersInterface } from "models/servers"; +import { Servers } from "./servers"; export class AsyncAPIDocument extends Mixin(BaseModel, ExtensionsMixin) @@ -17,4 +19,8 @@ export class AsyncAPIDocument info(): InfoInterface { return new Info(this._json.info); } + + servers(): ServersInterface { + return new Servers(createArrayFromMap(this._json.servers)); + } } diff --git a/src/models/v2/index.ts b/src/models/v2/index.ts index 34c57b438..64a301036 100644 --- a/src/models/v2/index.ts +++ b/src/models/v2/index.ts @@ -5,4 +5,6 @@ export { License as LicenseV2 } from './license'; export { Bindings as BindingsV2, Binding as BindingV2 } from './mixins/bindings'; export { Extensions as ExtensionsV2, Extension as ExtensionV2 } from './mixins/extensions'; export { ExternalDocumentation as ExternalDocumentationV2 } from './mixins/external-docs'; -export { Tags as TagsV2, Tag as TagV2 } from './mixins/tags'; \ No newline at end of file +export { Tags as TagsV2, Tag as TagV2 } from './mixins/tags'; +export { Server as ServerV2 } from './server'; +export { Servers as ServersV2 } from './servers'; \ No newline at end of file diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index c39e2c4f0..ac94c9188 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -4,6 +4,8 @@ import { Info } from "./info"; import { Mixin } from '../utils'; import { ExtensionsMixin } from './mixins/extensions'; +import { ServersInterface } from "models/servers"; +import { Servers } from "./servers"; export class AsyncAPIDocument extends Mixin(BaseModel, ExtensionsMixin) @@ -16,4 +18,8 @@ export class AsyncAPIDocument info(): Info { return new Info(this.json("info")); } + + servers(): ServersInterface { + return new Servers(this._json.servers); + } } diff --git a/src/models/v3/index.ts b/src/models/v3/index.ts index 967ba16c8..c974ffbeb 100644 --- a/src/models/v3/index.ts +++ b/src/models/v3/index.ts @@ -5,4 +5,6 @@ export { License as LicenseV3 } from './license'; export { Bindings as BindingsV3, Binding as BindingV3 } from './mixins/bindings'; export { Extensions as ExtensionsV3, Extension as ExtensionV3 } from './mixins/extensions'; export { ExternalDocumentation as ExternalDocumentationV3 } from './mixins/external-docs'; -export { Tags as TagsV3, Tag as TagV3 } from './mixins/tags'; \ No newline at end of file +export { Tags as TagsV3, Tag as TagV3 } from './mixins/tags'; +export { Server as ServerV3 } from './server'; +export { Servers as ServersV3 } from './servers'; \ No newline at end of file diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts new file mode 100644 index 000000000..ec92dc113 --- /dev/null +++ b/src/models/v3/server.ts @@ -0,0 +1,43 @@ +import { Mixin } from '../utils'; +import { BaseModel } from '../base'; +import { ServerInterface } from '../server'; +import { DescriptionMixin } from './mixins/description'; +import { BindingsMixin } from './mixins/bindings'; + +export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { + id(): string { + return this.json('id'); + } + + hasName(): boolean { + return !!this.json('name'); + } + + name(): string | undefined { + return this.json('name'); + } + + hasProtocol(): boolean { + return !!this.json('protocol'); + } + + protocol(): string | undefined { + return this.json('protocol'); + } + + hasProtocolVersion(): boolean { + return !!this.json('protocolVersion'); + } + + protocolVersion(): string { + return this.json('protocolVersion'); + } + + hasUrl(): boolean { + return !!this.json('url'); + } + + url(): string { + return this.json('url'); + } +} \ No newline at end of file diff --git a/src/models/v3/servers.ts b/src/models/v3/servers.ts new file mode 100644 index 000000000..c8e9fa0dc --- /dev/null +++ b/src/models/v3/servers.ts @@ -0,0 +1,13 @@ +import { Collection } from '../collection'; +import { ServerInterface } from '../server'; +import { ServersInterface } from '../servers'; + +export class Servers extends Collection implements ServersInterface { + override get(id: string): ServerInterface | undefined { + return this.collections.find(server => server.id() === id); + } + + override has(id: string): boolean { + return this.collections.some(server => server.id() === id); + } +} \ No newline at end of file diff --git a/test/models/v2/asyncapi.spec.ts b/test/models/v2/asyncapi.spec.ts index 24c98cbc9..60ee2499c 100644 --- a/test/models/v2/asyncapi.spec.ts +++ b/test/models/v2/asyncapi.spec.ts @@ -1,4 +1,4 @@ -import { newAsyncAPIDocument, AsyncAPIDocumentV2, InfoV2, AsyncAPIDocumentV3 } from '../../../src/models'; +import { newAsyncAPIDocument, AsyncAPIDocumentV2, InfoV2, AsyncAPIDocumentV3, ServersV2 } from '../../../src/models'; import { assertExtensionsMixinInheritance, @@ -27,6 +27,18 @@ describe('AsyncAPIDocument model', function() { }); }); + describe('.servers()', function(){ + it('should return an servers object', function(){ + const doc = {servers: { + development: { + + } + }}; + const d = new AsyncAPIDocumentV2(doc); + expect(d.servers() instanceof ServersV2).toBeTruthy(); + }) + }) + describe('mixins inheritance', function() { assertExtensionsMixinInheritance(AsyncAPIDocumentV2); }); From 5033ba12a61a76db7277444ac2c540b037657eee Mon Sep 17 00:00:00 2001 From: Souvikns Date: Thu, 24 Mar 2022 19:08:20 +0530 Subject: [PATCH 11/15] refactor: requested changes --- src/models/server.ts | 3 -- src/models/v2/asyncapi.ts | 11 +++-- src/models/v2/server.ts | 11 +++-- src/models/v2/servers.ts | 8 ++-- src/models/v3/asyncapi.ts | 7 +++- src/models/v3/server.ts | 19 ++++----- src/models/v3/servers.ts | 8 ++-- test/models/v2/server.spec.ts | 75 ++++++++++++++--------------------- 8 files changed, 65 insertions(+), 77 deletions(-) diff --git a/src/models/server.ts b/src/models/server.ts index 9b3a80d6e..ad750670b 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -2,13 +2,10 @@ import { BaseModel } from "./base"; import { BindingsMixinInterface, DescriptionMixinInterface } from './mixins'; export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface { - id(): string; name(): string | undefined; hasName(): boolean; protocol(): string | undefined; - hasProtocol(): boolean; protocolVersion(): string; hasProtocolVersion(): boolean; url(): string; - hasUrl(): boolean; } \ No newline at end of file diff --git a/src/models/v2/asyncapi.ts b/src/models/v2/asyncapi.ts index 8a70b6cc8..a220e9fa9 100644 --- a/src/models/v2/asyncapi.ts +++ b/src/models/v2/asyncapi.ts @@ -1,15 +1,16 @@ import { BaseModel } from "../base"; import { Info } from "./info"; -import { createArrayFromMap, Mixin } from '../utils'; +import { Mixin } from '../utils'; import { ExtensionsMixin } from './mixins/extensions'; import { AsyncAPIDocumentInterface, InfoInterface } from "../../models"; import { ServersInterface } from "models/servers"; import { Servers } from "./servers"; +import { Server } from "./server"; -export class AsyncAPIDocument - extends Mixin(BaseModel, ExtensionsMixin) +export class AsyncAPIDocument + extends Mixin(BaseModel, ExtensionsMixin) implements AsyncAPIDocumentInterface { version(): string { @@ -21,6 +22,8 @@ export class AsyncAPIDocument } servers(): ServersInterface { - return new Servers(createArrayFromMap(this._json.servers)); + return new Servers( + Object.entries(this._json.servers).map(([serverName, server]) => new Server(serverName, server as Record)) + ); } } diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index ec92dc113..ce4f36659 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -5,16 +5,19 @@ import { DescriptionMixin } from './mixins/description'; import { BindingsMixin } from './mixins/bindings'; export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { - id(): string { - return this.json('id'); + constructor( + private readonly _name: string, + _json: Record + ){ + super(_json); } hasName(): boolean { - return !!this.json('name'); + return !!this._name } name(): string | undefined { - return this.json('name'); + return this._name } hasProtocol(): boolean { diff --git a/src/models/v2/servers.ts b/src/models/v2/servers.ts index c8e9fa0dc..2fab0c29a 100644 --- a/src/models/v2/servers.ts +++ b/src/models/v2/servers.ts @@ -3,11 +3,11 @@ import { ServerInterface } from '../server'; import { ServersInterface } from '../servers'; export class Servers extends Collection implements ServersInterface { - override get(id: string): ServerInterface | undefined { - return this.collections.find(server => server.id() === id); + override get(name: string): ServerInterface | undefined { + return this.collections.find(server => server.name() === name); } - override has(id: string): boolean { - return this.collections.some(server => server.id() === id); + override has(name: string): boolean { + return this.collections.some(server => server.name() === name); } } \ No newline at end of file diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index ac94c9188..5da958d7c 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -6,6 +6,7 @@ import { Mixin } from '../utils'; import { ExtensionsMixin } from './mixins/extensions'; import { ServersInterface } from "models/servers"; import { Servers } from "./servers"; +import { Server } from "./server"; export class AsyncAPIDocument extends Mixin(BaseModel, ExtensionsMixin) @@ -20,6 +21,10 @@ export class AsyncAPIDocument } servers(): ServersInterface { - return new Servers(this._json.servers); + return new Servers( + Object.entries(this._json.servers).map( + ([serverName, server]) => new Server(serverName, server as Record) + ) + ) } } diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index ec92dc113..4bcd6dc4e 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -5,20 +5,19 @@ import { DescriptionMixin } from './mixins/description'; import { BindingsMixin } from './mixins/bindings'; export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { - id(): string { - return this.json('id'); + constructor( + private readonly _name: string, + _json: Record + ){ + super(_json); } hasName(): boolean { - return !!this.json('name'); + return !!this._name; } name(): string | undefined { - return this.json('name'); - } - - hasProtocol(): boolean { - return !!this.json('protocol'); + return this._name } protocol(): string | undefined { @@ -33,10 +32,6 @@ export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) im return this.json('protocolVersion'); } - hasUrl(): boolean { - return !!this.json('url'); - } - url(): string { return this.json('url'); } diff --git a/src/models/v3/servers.ts b/src/models/v3/servers.ts index c8e9fa0dc..2fab0c29a 100644 --- a/src/models/v3/servers.ts +++ b/src/models/v3/servers.ts @@ -3,11 +3,11 @@ import { ServerInterface } from '../server'; import { ServersInterface } from '../servers'; export class Servers extends Collection implements ServersInterface { - override get(id: string): ServerInterface | undefined { - return this.collections.find(server => server.id() === id); + override get(name: string): ServerInterface | undefined { + return this.collections.find(server => server.name() === name); } - override has(id: string): boolean { - return this.collections.some(server => server.id() === id); + override has(name: string): boolean { + return this.collections.some(server => server.name() === name); } } \ No newline at end of file diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index f2edac945..dc61ca904 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -1,20 +1,15 @@ import { Server } from '../../../src/models/v2/server'; import { Servers } from '../../../src/models/v2/servers'; -import { - assertBindingsMixinInheritance, - assertDescriptionMixinInheritance, -} from './mixins/inheritance'; - const doc = { - id: 'id', - name: 'development', - protocol: 'mqtt', - protocolVersion: '1.0.0', - url: 'development.gigantic-server.com' + 'development': { + protocol: 'mqtt', + protocolVersion: '1.0.0', + url: 'development.gigantic-server.com' + } }; -const docItem = new Server(doc); -const emptyItem = new Server({}); +const docItem = new Server('development', doc.development); +const emptyItem = new Server('',{}); describe('Servers model', function () { describe('.isEmpty()', function () { @@ -32,12 +27,12 @@ describe('Servers model', function () { describe('.get(id)', function () { it('should return a specific server Object if it is present', function () { const servers = new Servers([docItem]); - expect(servers.get('id')).toBeTruthy(); + expect(servers.get('development')).toBeTruthy(); }); it('should return undefined if a server is said Id is missing ', function () { const servers = new Servers([]); - expect(servers.get('id')).toBeUndefined(); + expect(servers.get('development')).toBeUndefined(); }); }) @@ -45,23 +40,18 @@ describe('Servers model', function () { const servers = new Servers([docItem]); - it('should return true if the said Id is available', function () { - expect(servers.has('id')).toBeTruthy(); + it('should return true if the said name is available', function () { + expect(servers.has('development')).toBeTruthy(); }) - it('should return fase id the said id is missing', function () { - expect(servers.has('uid')).toBeFalsy(); + it('should return false if the server name is missing', function () { + expect(servers.has('production')).toBeFalsy(); }) }) }) describe('Server Model', function () { - describe('.id()', function () { - it('should return id', function () { - expect(docItem.id()).toMatch('id'); - }) - }) describe('.hasName()', function () { it('should return true if name is present', function () { @@ -75,76 +65,71 @@ describe('Server Model', function () { describe('.name()', function () { it('should return name if present', function () { - expect(docItem.name()).toMatch(doc.name); + expect(docItem.name()).toMatch('development'); }) it('should return undefined if name is not present', function () { - expect(emptyItem.name()).toBeUndefined(); + expect(emptyItem.name()).toMatch(''); }) }) describe('.hasProtocol()', function () { - it('should return true if protocol is present', function() { + it('should return true if protocol is present', function () { expect(docItem.hasProtocol()).toBeTruthy(); }) - it('should return false if protocol is not present', function(){ + it('should return false if protocol is not present', function () { expect(emptyItem.hasProtocol()).toBeFalsy(); }) }) describe('protocol()', function () { - it('should return protocol ', function() { - expect(docItem.protocol()).toMatch(doc.protocol); + it('should return protocol ', function () { + expect(docItem.protocol()).toMatch(doc.development.protocol); }) - it('should return undefined when protocol is missing', function() { + it('should return undefined when protocol is missing', function () { expect(emptyItem.protocol()).toBeUndefined(); }) }) describe('.hasProtocolVersion()', function () { - it('should return true if protocolVersion is not missing', function() { + it('should return true if protocolVersion is not missing', function () { expect(docItem.hasProtocolVersion()).toBeTruthy(); }) - it('should be false when protocolVersion is missing', function() { + it('should be false when protocolVersion is missing', function () { expect(emptyItem.hasProtocolVersion()).toBeFalsy(); }) }) describe('.protocolVersion()', function () { - it('should return protocolVersion', function() { - expect(docItem.protocolVersion()).toMatch(doc.protocolVersion); + it('should return protocolVersion', function () { + expect(docItem.protocolVersion()).toMatch(doc.development.protocolVersion); }) - it('should return undefined protocolVersion when protocolVersion is missing', function() { + it('should return undefined protocolVersion when protocolVersion is missing', function () { expect(emptyItem.protocolVersion()).toBeUndefined(); }) }) describe('.hasUrl()', function () { - it('should return true if url is not missing', function(){ + it('should return true if url is not missing', function () { expect(docItem.hasUrl()).toBeTruthy(); }) - it('should return false when url is missing', function() { + it('should return false when url is missing', function () { expect(emptyItem.hasUrl()).toBeFalsy(); }) }) describe('.url()', function () { - it('should return url', function(){ - expect(docItem.url()).toMatch(doc.url); + it('should return url', function () { + expect(docItem.url()).toMatch(doc.development.url); }) - it('should return undefined when url is missing', function() { + it('should return undefined when url is missing', function () { expect(emptyItem.url()).toBeUndefined(); }) }) - - describe('mixins inheritance', function () { - assertDescriptionMixinInheritance(Server); - assertBindingsMixinInheritance(Server); - }) }) \ No newline at end of file From 23712654e433e3eb3aee5d455c7b8015d62c17b5 Mon Sep 17 00:00:00 2001 From: souvik Date: Fri, 25 Mar 2022 14:23:19 +0530 Subject: [PATCH 12/15] refactor: remove .hasName function as per server-api --- src/models/server.ts | 1 - src/models/v2/server.ts | 4 ---- src/models/v3/server.ts | 4 ---- test/models/v2/server.spec.ts | 11 ----------- 4 files changed, 20 deletions(-) diff --git a/src/models/server.ts b/src/models/server.ts index ad750670b..5ecd934bb 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -3,7 +3,6 @@ import { BindingsMixinInterface, DescriptionMixinInterface } from './mixins'; export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface { name(): string | undefined; - hasName(): boolean; protocol(): string | undefined; protocolVersion(): string; hasProtocolVersion(): boolean; diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index ce4f36659..d771439fb 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -12,10 +12,6 @@ export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) im super(_json); } - hasName(): boolean { - return !!this._name - } - name(): string | undefined { return this._name } diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index 4bcd6dc4e..edb6e7457 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -12,10 +12,6 @@ export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) im super(_json); } - hasName(): boolean { - return !!this._name; - } - name(): string | undefined { return this._name } diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index dc61ca904..d5f771b88 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -52,17 +52,6 @@ describe('Servers model', function () { describe('Server Model', function () { - - describe('.hasName()', function () { - it('should return true if name is present', function () { - expect(docItem.hasName()).toBeTruthy(); - }) - - it('should return false if name is not present', function () { - expect(emptyItem.hasName()).toBeFalsy(); - }) - }) - describe('.name()', function () { it('should return name if present', function () { expect(docItem.name()).toMatch('development'); From 95c130a8788eee565e4ca60f68a3fdcdf7b92c69 Mon Sep 17 00:00:00 2001 From: Souvikns Date: Fri, 25 Mar 2022 16:16:11 +0530 Subject: [PATCH 13/15] refactor: add id field as per server-api --- src/models/server.ts | 2 +- src/models/v2/server.ts | 6 +++--- src/models/v2/servers.ts | 8 ++++---- src/models/v3/server.ts | 6 +++--- src/models/v3/servers.ts | 8 ++++---- test/models/v2/server.spec.ts | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/models/server.ts b/src/models/server.ts index 5ecd934bb..d9526fd3d 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -2,7 +2,7 @@ import { BaseModel } from "./base"; import { BindingsMixinInterface, DescriptionMixinInterface } from './mixins'; export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface { - name(): string | undefined; + id(): string protocol(): string | undefined; protocolVersion(): string; hasProtocolVersion(): boolean; diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index d771439fb..df6d70f6e 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -6,14 +6,14 @@ import { BindingsMixin } from './mixins/bindings'; export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { constructor( - private readonly _name: string, + private readonly _id: string, _json: Record ){ super(_json); } - name(): string | undefined { - return this._name + id(): string { + return this._id; } hasProtocol(): boolean { diff --git a/src/models/v2/servers.ts b/src/models/v2/servers.ts index 2fab0c29a..c8e9fa0dc 100644 --- a/src/models/v2/servers.ts +++ b/src/models/v2/servers.ts @@ -3,11 +3,11 @@ import { ServerInterface } from '../server'; import { ServersInterface } from '../servers'; export class Servers extends Collection implements ServersInterface { - override get(name: string): ServerInterface | undefined { - return this.collections.find(server => server.name() === name); + override get(id: string): ServerInterface | undefined { + return this.collections.find(server => server.id() === id); } - override has(name: string): boolean { - return this.collections.some(server => server.name() === name); + override has(id: string): boolean { + return this.collections.some(server => server.id() === id); } } \ No newline at end of file diff --git a/src/models/v3/server.ts b/src/models/v3/server.ts index edb6e7457..3f52dc724 100644 --- a/src/models/v3/server.ts +++ b/src/models/v3/server.ts @@ -6,14 +6,14 @@ import { BindingsMixin } from './mixins/bindings'; export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { constructor( - private readonly _name: string, + private readonly _id: string, _json: Record ){ super(_json); } - name(): string | undefined { - return this._name + id(): string { + return this._id; } protocol(): string | undefined { diff --git a/src/models/v3/servers.ts b/src/models/v3/servers.ts index 2fab0c29a..c8e9fa0dc 100644 --- a/src/models/v3/servers.ts +++ b/src/models/v3/servers.ts @@ -3,11 +3,11 @@ import { ServerInterface } from '../server'; import { ServersInterface } from '../servers'; export class Servers extends Collection implements ServersInterface { - override get(name: string): ServerInterface | undefined { - return this.collections.find(server => server.name() === name); + override get(id: string): ServerInterface | undefined { + return this.collections.find(server => server.id() === id); } - override has(name: string): boolean { - return this.collections.some(server => server.name() === name); + override has(id: string): boolean { + return this.collections.some(server => server.id() === id); } } \ No newline at end of file diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index d5f771b88..de29716ae 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -52,13 +52,13 @@ describe('Servers model', function () { describe('Server Model', function () { - describe('.name()', function () { + describe('.id()', function () { it('should return name if present', function () { - expect(docItem.name()).toMatch('development'); + expect(docItem.id()).toMatch('development'); }) it('should return undefined if name is not present', function () { - expect(emptyItem.name()).toMatch(''); + expect(emptyItem.id()).toMatch(''); }) }) From 356d7b0ee7bbdfd1b6312446611766d53d9d1223 Mon Sep 17 00:00:00 2001 From: souvik Date: Fri, 25 Mar 2022 16:56:25 +0530 Subject: [PATCH 14/15] refactor: made suggested changes --- src/models/v2/server.ts | 8 ------ test/models/v2/server.spec.ts | 50 +++++++---------------------------- 2 files changed, 9 insertions(+), 49 deletions(-) diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index df6d70f6e..3f52dc724 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -16,10 +16,6 @@ export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) im return this._id; } - hasProtocol(): boolean { - return !!this.json('protocol'); - } - protocol(): string | undefined { return this.json('protocol'); } @@ -32,10 +28,6 @@ export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) im return this.json('protocolVersion'); } - hasUrl(): boolean { - return !!this.json('url'); - } - url(): string { return this.json('url'); } diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index de29716ae..ccb318834 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -55,70 +55,38 @@ describe('Server Model', function () { describe('.id()', function () { it('should return name if present', function () { expect(docItem.id()).toMatch('development'); - }) - - it('should return undefined if name is not present', function () { - expect(emptyItem.id()).toMatch(''); - }) - }) - - describe('.hasProtocol()', function () { - it('should return true if protocol is present', function () { - expect(docItem.hasProtocol()).toBeTruthy(); - }) - - it('should return false if protocol is not present', function () { - expect(emptyItem.hasProtocol()).toBeFalsy(); - }) - }) + }); + }); describe('protocol()', function () { it('should return protocol ', function () { expect(docItem.protocol()).toMatch(doc.development.protocol); - }) - - it('should return undefined when protocol is missing', function () { - expect(emptyItem.protocol()).toBeUndefined(); - }) - }) + }); + }); describe('.hasProtocolVersion()', function () { it('should return true if protocolVersion is not missing', function () { expect(docItem.hasProtocolVersion()).toBeTruthy(); - }) + }); it('should be false when protocolVersion is missing', function () { expect(emptyItem.hasProtocolVersion()).toBeFalsy(); - }) + }); }) describe('.protocolVersion()', function () { it('should return protocolVersion', function () { expect(docItem.protocolVersion()).toMatch(doc.development.protocolVersion); - }) + }); it('should return undefined protocolVersion when protocolVersion is missing', function () { expect(emptyItem.protocolVersion()).toBeUndefined(); }) }) - describe('.hasUrl()', function () { - it('should return true if url is not missing', function () { - expect(docItem.hasUrl()).toBeTruthy(); - }) - - it('should return false when url is missing', function () { - expect(emptyItem.hasUrl()).toBeFalsy(); - }) - }) - describe('.url()', function () { it('should return url', function () { expect(docItem.url()).toMatch(doc.development.url); - }) - - it('should return undefined when url is missing', function () { - expect(emptyItem.url()).toBeUndefined(); - }) - }) + }); + }); }) \ No newline at end of file From d17d21906d6b76cf172e525c66a982708e12b2f0 Mon Sep 17 00:00:00 2001 From: souvik Date: Fri, 25 Mar 2022 17:08:34 +0530 Subject: [PATCH 15/15] feat: add sonarcloud properties --- .sonarcloud.properties | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .sonarcloud.properties diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 000000000..1b41ae5b3 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1,2 @@ +# Disable specific duplicate code since it would introduce more complexity to reduce it. +sonar.cpd.exclusions=src/models/**/*.ts \ No newline at end of file