Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add models for server object #497

Merged
merged 17 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ 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<string, ServerInterface>;
server(name: string): ServerInterface;
}

export function newAsyncAPIDocument(json: Record<string, any>): AsyncAPIDocumentInterface {
Expand Down
5 changes: 5 additions & 0 deletions src/models/server-security-requirement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BaseModel } from "./base";

export interface ServerSecurityRequirementInterface extends BaseModel {

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServerSecurityRequirementInterface is unnecessary because it should not be a model, but only type https://github.com/asyncapi/spec/blob/master/spec/asyncapi.md#securityRequirementObject

10 changes: 10 additions & 0 deletions src/models/server-variables.ts
Original file line number Diff line number Diff line change
@@ -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]
}
18 changes: 18 additions & 0 deletions src/models/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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<string, ServerVariableInterface>;
variables(): Record<string, ServerVariableInterface> | undefined;
variable(name: string): ServerVariableInterface | undefined;
hasVariables(): boolean;
security(): [ServerSecurityRequirementInterface] | undefined;
}
22 changes: 22 additions & 0 deletions src/models/utils.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = {};
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);
}
11 changes: 11 additions & 0 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -15,4 +18,12 @@ export class AsyncAPIDocument
info(): Info {
return new Info(this.json("info"));
}

servers(): Record<string, ServerInterface> {
return createMapOfTypes(this.json('servers'), Server);
}

server(name: string): ServerInterface {
return getMapValueOfType(this.json('servers'), name, Server);
}
}
4 changes: 3 additions & 1 deletion src/models/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 { License as LicenseV2 } from './license';
export { Server as ServerV2 } from './server';
export {ServerVariable as ServerVariableV2} from './server-variable';
6 changes: 6 additions & 0 deletions src/models/v2/server-security-requirement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseModel } from '../base';
import { ServerSecurityRequirementInterface } from '../server-security-requirement';

export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface {

}
29 changes: 29 additions & 0 deletions src/models/v2/server-variable.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
53 changes: 53 additions & 0 deletions src/models/v2/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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<string, ServerVariableInterface>;
variables(): Record<string, ServerVariableInterface> | 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))
}

}
11 changes: 11 additions & 0 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -15,4 +18,12 @@ export class AsyncAPIDocument
info(): Info {
return new Info(this.json("info"));
}

servers(): Record<string, ServerInterface> {
return createMapOfTypes(this.json('servers'), Server);
}

server(name: string): ServerInterface {
return getMapValueOfType(this.json('servers'), name, Server);
}
}
3 changes: 2 additions & 1 deletion src/models/v3/index.ts
Original file line number Diff line number Diff line change
@@ -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';
export { License as LicenseV3 } from './license';
export {Server as ServerV3} from './server';
6 changes: 6 additions & 0 deletions src/models/v3/server-recurity-requirement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseModel } from "../base";
import { ServerSecurityRequirementInterface } from "../server-security-requirement";

export class ServerSecurityRequirement extends BaseModel implements ServerSecurityRequirementInterface {

}
27 changes: 27 additions & 0 deletions src/models/v3/server-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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');
}
}
51 changes: 51 additions & 0 deletions src/models/v3/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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<string, ServerVariableInterface> {
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))
}
}
Loading