|
| 1 | +import { BaseModel } from "../base"; |
| 2 | +import { Channel } from "./channel"; |
| 3 | +import { ChannelParameter } from "./channel-parameter"; |
| 4 | +import { CorrelationId } from "./correlation-id"; |
| 5 | +import { Message } from "./message"; |
| 6 | +import { MessageTrait } from "./message-trait"; |
| 7 | +import { OperationTrait } from "./operation-trait"; |
| 8 | +import { Schema } from "./schema"; |
| 9 | +import { SecurityScheme } from "./security-scheme"; |
| 10 | +import { Server } from "./server"; |
| 11 | +import { ServerVariable } from "./server-variable"; |
| 12 | + |
| 13 | +import { Mixin } from '../utils'; |
| 14 | +import { Bindings, Binding } from "./mixins/bindings"; |
| 15 | +import { ExtensionsMixin } from './mixins/extensions'; |
| 16 | + |
| 17 | +import type { BindingsInterface } from "../bindings"; |
| 18 | +import type { ComponentsInterface } from "../components"; |
| 19 | +import type { ChannelInterface } from "../channel"; |
| 20 | +import type { ChannelParameterInterface } from "../channel-parameter"; |
| 21 | +import type { CorrelationIdInterface } from "../correlation-id"; |
| 22 | +import type { MessageInterface } from "../message"; |
| 23 | +import type { MessageTraitInterface } from "../message-trait"; |
| 24 | +import type { OperationTraitInterface } from "../operation-trait"; |
| 25 | +import type { SchemaInterface } from "../schema"; |
| 26 | +import type { SecuritySchemeInterface } from "../security-scheme"; |
| 27 | +import type { ServerInterface } from "../server"; |
| 28 | +import type { ServerVariableInterface } from "../server-variable"; |
| 29 | +import type { Constructor } from "../utils"; |
| 30 | + |
| 31 | +export class Components extends Mixin(BaseModel, ExtensionsMixin) implements ComponentsInterface { |
| 32 | + servers(): Record<string, ServerInterface> { |
| 33 | + return this.createMap('servers', Server); |
| 34 | + } |
| 35 | + |
| 36 | + channels(): Record<string, ChannelInterface> { |
| 37 | + return this.createMap('channels', Channel); |
| 38 | + } |
| 39 | + |
| 40 | + messages(): Record<string, MessageInterface> { |
| 41 | + return this.createMap('messages', Message); |
| 42 | + } |
| 43 | + |
| 44 | + schemas(): Record<string, SchemaInterface> { |
| 45 | + return this.createMap('schemas', Schema); |
| 46 | + } |
| 47 | + |
| 48 | + channelParameters(): Record<string, ChannelParameterInterface> { |
| 49 | + return this.createMap('parameters', ChannelParameter); |
| 50 | + } |
| 51 | + |
| 52 | + serverVariables(): Record<string, ServerVariableInterface> { |
| 53 | + return this.createMap('serverVariables', ServerVariable); |
| 54 | + } |
| 55 | + |
| 56 | + operationTraits(): Record<string, OperationTraitInterface> { |
| 57 | + return this.createMap('operationTraits', OperationTrait); |
| 58 | + } |
| 59 | + |
| 60 | + messageTraits(): Record<string, MessageTraitInterface> { |
| 61 | + return this.createMap('messageTraits', MessageTrait); |
| 62 | + } |
| 63 | + |
| 64 | + correlationIds(): Record<string, CorrelationIdInterface> { |
| 65 | + return this.createMap('correlationIds', CorrelationId); |
| 66 | + } |
| 67 | + |
| 68 | + securitySchemes(): Record<string, SecuritySchemeInterface> { |
| 69 | + return this.createMap('securitySchemes', SecurityScheme); |
| 70 | + } |
| 71 | + |
| 72 | + serverBindings(): Record<string, BindingsInterface> { |
| 73 | + return this.createBindings('serverBindings'); |
| 74 | + } |
| 75 | + |
| 76 | + channelBindings(): Record<string, BindingsInterface> { |
| 77 | + return this.createBindings('channelBindings'); |
| 78 | + } |
| 79 | + |
| 80 | + operationBindings(): Record<string, BindingsInterface> { |
| 81 | + return this.createBindings('operationBindings'); |
| 82 | + } |
| 83 | + |
| 84 | + messageBindings(): Record<string, BindingsInterface> { |
| 85 | + return this.createBindings('messageBindings'); |
| 86 | + } |
| 87 | + |
| 88 | + protected createMap<M extends BaseModel>(itemsName: string, model: Constructor<M>): Record<string, M> { |
| 89 | + return Object.entries(this._json[itemsName] || {}).reduce((items, [itemName, item]) => { |
| 90 | + items[itemName] = this.createModel(model, item, { id: itemName, pointer: `/components/${itemsName}/${itemName}` }) |
| 91 | + return items; |
| 92 | + }, {} as Record<string, M>); |
| 93 | + } |
| 94 | + |
| 95 | + protected createBindings(itemsName: string): Record<string, BindingsInterface> { |
| 96 | + return Object.entries(this._json[itemsName] || {}).reduce((bindings, [name, item]) => { |
| 97 | + bindings[name] = new Bindings( |
| 98 | + Object.entries(item as any || {}).map(([protocol, binding]) => |
| 99 | + this.createModel(Binding, binding, { id: protocol, pointer: `components/${itemsName}/${name}/${protocol}` }) |
| 100 | + ) |
| 101 | + ); |
| 102 | + return bindings; |
| 103 | + }, {} as Record<string, BindingsInterface>); |
| 104 | + } |
| 105 | +} |
0 commit comments