-
-
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.
- Loading branch information
1 parent
3db1d09
commit 303f411
Showing
24 changed files
with
1,416 additions
and
2 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
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,164 @@ | ||
import { SpecificationExtensionsModel, hasExternalDocs, externalDocs, tagsMixins, createMapOfType, getMapValue } from './mixins'; | ||
import { Info } from './info'; | ||
import { Server } from './server'; | ||
import { Channel } from './channel'; | ||
import { Components } from './components'; | ||
import { Message } from './message'; | ||
import { Schema } from './schema'; | ||
|
||
import { xParserCircular } from '../constants'; | ||
import { stringify, unstringify } from '../stringify'; | ||
|
||
import type { v2 } from '../spec-types'; | ||
import type { Operation } from './operation'; | ||
|
||
export class AsyncAPIDocument extends SpecificationExtensionsModel<v2.AsyncAPIObject> { | ||
version() { | ||
return this._json.asyncapi; | ||
} | ||
|
||
info() { | ||
return new Info(this._json.info); | ||
} | ||
|
||
id() { | ||
return this._json.id; | ||
} | ||
|
||
externalDocs() { | ||
return externalDocs(this); | ||
} | ||
|
||
hasExternalDocs() { | ||
return hasExternalDocs(this); | ||
} | ||
|
||
hasTags() { | ||
return tagsMixins.hasTags(this); | ||
} | ||
|
||
tags() { | ||
return tagsMixins.tags(this); | ||
} | ||
|
||
tagNames() { | ||
return tagsMixins.tagNames(this); | ||
} | ||
|
||
hasTag(name: string) { | ||
return tagsMixins.hasTag(this, name); | ||
} | ||
|
||
tag(name: string) { | ||
return tagsMixins.tag(this, name); | ||
} | ||
|
||
hasServers() { | ||
return !!this._json.servers; | ||
} | ||
|
||
servers() { | ||
return createMapOfType(this._json.servers, Server); | ||
} | ||
|
||
serverNames() { | ||
if (!this._json.servers) return []; | ||
return Object.keys(this._json.servers); | ||
} | ||
|
||
server(name: string) { | ||
return getMapValue(this._json.servers, name, Server); | ||
} | ||
|
||
hasDefaultContentType() { | ||
return !!this._json.defaultContentType; | ||
} | ||
|
||
defaultContentType() { | ||
return this._json.defaultContentType || null; | ||
} | ||
|
||
hasChannels() { | ||
return !!this._json.channels; | ||
} | ||
|
||
channels() { | ||
return createMapOfType(this._json.channels, Channel); | ||
} | ||
|
||
channelNames() { | ||
if (!this._json.channels) return []; | ||
return Object.keys(this._json.channels); | ||
} | ||
|
||
channel(name: string) { | ||
return getMapValue(this._json.channels, name, Channel); | ||
} | ||
|
||
hasComponents() { | ||
return !!this._json.components; | ||
} | ||
|
||
components() { | ||
if (!this._json.components) return null; | ||
return new Components(this._json.components); | ||
} | ||
|
||
hasMessages() { | ||
return !!this.allMessages().size; | ||
} | ||
|
||
allMessages(): Map<string, Message> { | ||
const messages = new Map<string, Message>(); | ||
|
||
if (this.hasChannels()) { | ||
this.channelNames().forEach(channelName => { | ||
const channel = this.channel(channelName); | ||
if (channel) { | ||
if (channel.hasPublish()) { | ||
(channel.publish() as Operation).messages().forEach(m => { | ||
messages.set(m.uid(), m); | ||
}); | ||
} | ||
if (channel.hasSubscribe()) { | ||
(channel.subscribe() as Operation).messages().forEach(m => { | ||
messages.set(m.uid(), m); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
if (this.hasComponents()) { | ||
Object.values((this.components() as Components).messages()).forEach(m => { | ||
messages.set(m.uid(), m); | ||
}); | ||
} | ||
|
||
return messages; | ||
} | ||
|
||
// TODO: Retrieve all schemas | ||
allSchemas(): Map<string, Schema> { | ||
const schemas = new Map<string, Schema>(); | ||
|
||
return schemas; | ||
} | ||
|
||
hasCircular() { | ||
return !!this._json[xParserCircular]; | ||
} | ||
|
||
// TODO: Make traversing for old API and enable that function | ||
// traverseSchemas(callback, schemaTypesToIterate) { | ||
// traverseAsyncApiDocument(this, callback, schemaTypesToIterate); | ||
// } | ||
|
||
static stringify(doc: AsyncAPIDocument, space: number): string | undefined { | ||
return stringify(doc, { space }); | ||
} | ||
|
||
static parse(doc: string): AsyncAPIDocument | undefined { | ||
const possibleDocument = unstringify(doc); | ||
return possibleDocument ? new AsyncAPIDocument(possibleDocument.json()) : undefined; | ||
} | ||
} |
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 @@ | ||
export abstract class Base<J = any, M extends Record<string, any> = Record<string, any>> { | ||
constructor( | ||
protected readonly _json: J, // TODO: Add error here like in original codebase | ||
protected readonly _meta: M = {} as M, | ||
) {} | ||
|
||
json<T = J>(): T; | ||
json<K extends keyof J>(key: K): J[K]; | ||
json(key?: keyof J) { | ||
if (key === undefined || typeof this._json !== 'object') return this._json; | ||
if (!this._json) return; | ||
return this._json[key]; | ||
} | ||
} |
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,23 @@ | ||
import { SpecificationExtensionsModel, description, hasDescription } from './mixins'; | ||
import { Schema } from './schema'; | ||
|
||
import type { v2 } from '../spec-types'; | ||
|
||
export class ChannelParameter extends SpecificationExtensionsModel<v2.ParameterObject> { | ||
description() { | ||
return description(this); | ||
} | ||
|
||
hasDescription() { | ||
return hasDescription(this); | ||
} | ||
|
||
schema() { | ||
if (!this._json.schema) return null; | ||
return new Schema(this._json.schema as any); | ||
} | ||
|
||
location() { | ||
return this._json.location; | ||
} | ||
} |
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,81 @@ | ||
import { SpecificationExtensionsModel, hasDescription, description, createMapOfType, bindingsMixins, getMapValue } from './mixins'; | ||
import { ChannelParameter } from './channel-parameter'; | ||
import { Operation } from './operation'; | ||
|
||
import type { v2 } from '../spec-types'; | ||
|
||
export class Channel extends SpecificationExtensionsModel<v2.ChannelObject> { | ||
description() { | ||
return description(this); | ||
} | ||
|
||
hasDescription() { | ||
return hasDescription(this); | ||
} | ||
|
||
hasParameters() { | ||
return !!this._json.parameters; | ||
} | ||
|
||
parameters() { | ||
return createMapOfType(this._json.parameters as Record<string, v2.ParameterObject>, ChannelParameter); | ||
} | ||
|
||
parameter(name: string) { | ||
return getMapValue(this._json.parameters as Record<string, v2.ParameterObject>, name, ChannelParameter); | ||
} | ||
|
||
hasServers() { | ||
return !!this._json.servers; | ||
} | ||
|
||
servers() { | ||
if (!this._json.servers) return []; | ||
return this._json.servers; | ||
} | ||
|
||
server(index: number | string) { | ||
if (!this._json.servers) return null; | ||
if (typeof index !== 'number') return null; | ||
if (index > this._json.servers.length - 1) return null; | ||
return this._json.servers[+index]; | ||
} | ||
|
||
publish() { | ||
if (!this._json.publish) return null; | ||
return new Operation(this._json.publish, { kind: 'publish' }); | ||
} | ||
|
||
subscribe() { | ||
if (!this._json.subscribe) return null; | ||
return new Operation(this._json.subscribe, { kind: 'subscribe' }); | ||
} | ||
|
||
hasPublish() { | ||
return !!this._json.publish; | ||
} | ||
|
||
hasSubscribe() { | ||
return !!this._json.subscribe; | ||
} | ||
|
||
hasBindings() { | ||
return bindingsMixins.hasBindings(this as any); | ||
} | ||
|
||
bindings() { | ||
return bindingsMixins.bindings(this as any); | ||
} | ||
|
||
bindingProtocols() { | ||
return bindingsMixins.bindingProtocols(this as any); | ||
} | ||
|
||
hasBinding(name: string): boolean { | ||
return bindingsMixins.hasBinding(this as any, name); | ||
} | ||
|
||
binding(name: string) { | ||
return bindingsMixins.binding(this as any, name); | ||
} | ||
} |
Oops, something went wrong.