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 old api #589

Merged
merged 4 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const xParserOriginalPayload = 'x-parser-original-payload';
export const xParserOriginalTraits = 'x-parser-original-traits';

export const xParserCircular = 'x-parser-circular';
export const xParserCircularProps = 'x-parser-circular-props';

export const EXTENSION_REGEX = /^x-[\w\d\.\-\_]+$/;

Expand Down
4 changes: 2 additions & 2 deletions src/models/v2/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export function hasExternalDocs(model: BaseModel<{ externalDocs?: v2.ExternalDoc
return Object.keys(model.json('externalDocs') || {}).length > 0;
};

export function externalDocs(model: BaseModel): ExternalDocumentationInterface | undefined {
export function externalDocs(model: BaseModel<{ externalDocs?: v2.ExternalDocumentationObject }>): ExternalDocumentationInterface | undefined {
if (hasExternalDocs(model)) {
return new ExternalDocumentation(model.json('externalDocs'));
return new ExternalDocumentation(model.json('externalDocs') as v2.ExternalDocumentationObject);
}
return;
};
Expand Down
164 changes: 164 additions & 0 deletions src/old-api/asyncapi.ts
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;
}
}
14 changes: 14 additions & 0 deletions src/old-api/base.ts
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];
}
}
23 changes: 23 additions & 0 deletions src/old-api/channel-parameter.ts
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;
}
}
81 changes: 81 additions & 0 deletions src/old-api/channel.ts
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);
}
}
Loading