-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: port components model (#548)
- Loading branch information
1 parent
ae4b0ff
commit 7fc5916
Showing
7 changed files
with
406 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
import type { BaseModel } from './base'; | ||
import type { Collection } from './collection'; | ||
import type { ServersInterface } from './servers'; | ||
import type { ChannelsInterface } from './channels'; | ||
import type { OperationsInterface } from './operations'; | ||
import type { OperationTraitsInterface } from './operation-traits'; | ||
import type { MessagesInterface } from './messages'; | ||
import type { MessageTraitsInterface } from './message-traits'; | ||
import type { SchemasInterface } from './schemas'; | ||
import type { ChannelParametersInterface } from './channel-parameters'; | ||
import type { ServerVariablesInterface } from './server-variables'; | ||
import type { ServerInterface } from './server'; | ||
import type { ChannelInterface } from './channel'; | ||
import type { OperationTraitInterface } from './operation-trait'; | ||
import type { MessageInterface } from './message'; | ||
import type { MessageTraitInterface } from './message-trait'; | ||
import type { SchemaInterface } from './schema'; | ||
import type { ChannelParameterInterface } from './channel-parameter'; | ||
import type { ServerVariableInterface } from './server-variable'; | ||
import type { CorrelationIdInterface } from './correlation-id'; | ||
import type { BindingsInterface } from './bindings'; | ||
import type { SecuritySchemesInterface } from './security-schemes'; | ||
import type { SecuritySchemeInterface } from './security-scheme'; | ||
import type { ExtensionsMixinInterface } from './mixins'; | ||
|
||
export interface Components extends BaseModel, ExtensionsMixinInterface { | ||
servers(): ServersInterface; | ||
channels(): ChannelsInterface; | ||
operations(): OperationsInterface; | ||
messages(): MessagesInterface; | ||
schemas(): SchemasInterface; | ||
channelParameters(): ChannelParametersInterface; | ||
serverVariables(): ServerVariablesInterface; | ||
operationTraits(): OperationTraitsInterface; | ||
messageTraits(): MessageTraitsInterface; | ||
correlationIds(): Collection<CorrelationIdInterface>; | ||
securitySchemes(): SecuritySchemesInterface; | ||
serverBindings(): Collection<BindingsInterface>; | ||
channelBindings(): Collection<BindingsInterface>; | ||
operationBindings(): Collection<BindingsInterface>; | ||
messageBindings(): Collection<BindingsInterface>; | ||
export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface { | ||
servers(): Record<string, ServerInterface>; | ||
channels(): Record<string, ChannelInterface>; | ||
messages(): Record<string, MessageInterface>; | ||
schemas(): Record<string, SchemaInterface>; | ||
channelParameters(): Record<string, ChannelParameterInterface>; | ||
serverVariables(): Record<string, ServerVariableInterface>; | ||
operationTraits(): Record<string, OperationTraitInterface>; | ||
messageTraits(): Record<string, MessageTraitInterface>; | ||
correlationIds(): Record<string, CorrelationIdInterface>; | ||
securitySchemes(): Record<string, SecuritySchemeInterface>; | ||
serverBindings(): Record<string, BindingsInterface>; | ||
channelBindings(): Record<string, BindingsInterface>; | ||
operationBindings(): Record<string, BindingsInterface>; | ||
messageBindings(): Record<string, BindingsInterface>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { BaseModel } from "../base"; | ||
import { Channel } from "./channel"; | ||
import { ChannelParameter } from "./channel-parameter"; | ||
import { CorrelationId } from "./correlation-id"; | ||
import { Message } from "./message"; | ||
import { MessageTrait } from "./message-trait"; | ||
import { OperationTrait } from "./operation-trait"; | ||
import { Schema } from "./schema"; | ||
import { SecurityScheme } from "./security-scheme"; | ||
import { Server } from "./server"; | ||
import { ServerVariable } from "./server-variable"; | ||
|
||
import { Mixin } from '../utils'; | ||
import { Bindings, Binding } from "./mixins/bindings"; | ||
import { ExtensionsMixin } from './mixins/extensions'; | ||
|
||
import type { BindingsInterface } from "../bindings"; | ||
import type { ComponentsInterface } from "../components"; | ||
import type { ChannelInterface } from "../channel"; | ||
import type { ChannelParameterInterface } from "../channel-parameter"; | ||
import type { CorrelationIdInterface } from "../correlation-id"; | ||
import type { MessageInterface } from "../message"; | ||
import type { MessageTraitInterface } from "../message-trait"; | ||
import type { OperationTraitInterface } from "../operation-trait"; | ||
import type { SchemaInterface } from "../schema"; | ||
import type { SecuritySchemeInterface } from "../security-scheme"; | ||
import type { ServerInterface } from "../server"; | ||
import type { ServerVariableInterface } from "../server-variable"; | ||
import type { Constructor } from "../utils"; | ||
|
||
export class Components extends Mixin(BaseModel, ExtensionsMixin) implements ComponentsInterface { | ||
servers(): Record<string, ServerInterface> { | ||
return this.createMap('servers', Server); | ||
} | ||
|
||
channels(): Record<string, ChannelInterface> { | ||
return this.createMap('channels', Channel); | ||
} | ||
|
||
messages(): Record<string, MessageInterface> { | ||
return this.createMap('messages', Message); | ||
} | ||
|
||
schemas(): Record<string, SchemaInterface> { | ||
return this.createMap('schemas', Schema); | ||
} | ||
|
||
channelParameters(): Record<string, ChannelParameterInterface> { | ||
return this.createMap('parameters', ChannelParameter); | ||
} | ||
|
||
serverVariables(): Record<string, ServerVariableInterface> { | ||
return this.createMap('serverVariables', ServerVariable); | ||
} | ||
|
||
operationTraits(): Record<string, OperationTraitInterface> { | ||
return this.createMap('operationTraits', OperationTrait); | ||
} | ||
|
||
messageTraits(): Record<string, MessageTraitInterface> { | ||
return this.createMap('messageTraits', MessageTrait); | ||
} | ||
|
||
correlationIds(): Record<string, CorrelationIdInterface> { | ||
return this.createMap('correlationIds', CorrelationId); | ||
} | ||
|
||
securitySchemes(): Record<string, SecuritySchemeInterface> { | ||
return this.createMap('securitySchemes', SecurityScheme); | ||
} | ||
|
||
serverBindings(): Record<string, BindingsInterface> { | ||
return this.createBindings('serverBindings'); | ||
} | ||
|
||
channelBindings(): Record<string, BindingsInterface> { | ||
return this.createBindings('channelBindings'); | ||
} | ||
|
||
operationBindings(): Record<string, BindingsInterface> { | ||
return this.createBindings('operationBindings'); | ||
} | ||
|
||
messageBindings(): Record<string, BindingsInterface> { | ||
return this.createBindings('messageBindings'); | ||
} | ||
|
||
protected createMap<M extends BaseModel>(itemsName: string, model: Constructor<M>): Record<string, M> { | ||
return Object.entries(this._json[itemsName] || {}).reduce((items, [itemName, item]) => { | ||
items[itemName] = this.createModel(model, item, { id: itemName, pointer: `/components/${itemsName}/${itemName}` }) | ||
return items; | ||
}, {} as Record<string, M>); | ||
} | ||
|
||
protected createBindings(itemsName: string): Record<string, BindingsInterface> { | ||
return Object.entries(this._json[itemsName] || {}).reduce((bindings, [name, item]) => { | ||
bindings[name] = new Bindings( | ||
Object.entries(item as any || {}).map(([protocol, binding]) => | ||
this.createModel(Binding, binding, { id: protocol, pointer: `components/${itemsName}/${name}/${protocol}` }) | ||
) | ||
); | ||
return bindings; | ||
}, {} as Record<string, BindingsInterface>); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Collection } from '../collection'; | ||
|
||
import type { SchemasInterface } from '../schemas'; | ||
import type { SchemaInterface } from '../schema'; | ||
|
||
export class Schemas extends Collection<SchemaInterface> implements SchemasInterface { | ||
override get(id: string): SchemaInterface | undefined { | ||
return this.collections.find(schema => schema.uid() === id); | ||
} | ||
|
||
override has(id: string): boolean { | ||
return this.collections.some(schema => schema.uid() === id); | ||
} | ||
} |
Oops, something went wrong.