-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
Changes from 1 commit
763da46
6a98d2a
2099076
ecaa181
6523013
20dea91
d4ed90b
68b0982
2330aba
798db97
101cad0
a9fa78c
5033ba1
2371265
95c130a
356d7b0
d17d219
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { BaseModel } from "./base"; | ||
|
||
export interface ServerSecurityRequirementInterface extends BaseModel { | ||
|
||
} | ||
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] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, ServerVariableInterface>; | ||
variable(name: string): ServerVariableInterface; | ||
hasVariables(): boolean; | ||
security(): [ServerSecurityRequirementInterface] | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also I opt to have methods like: hasVariables(): boolean;
hasVariables(name: string): boolean;
variables(): Record<string, ServerVariableInterface> | undefined;
variables(name: string): ServerVariableInterface | undefined; cc @smoya also type like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also read #497 (comment). Intermediate models are the preferred solution (unless better alternative). |
||
} |
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); | ||
} |
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 { | ||
|
||
} |
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'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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] { | ||
return this.json('security').map((sec: any) => new ServerSecurityRequirement(sec)) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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