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

chore: init models for v3 spec #638

Merged
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
4 changes: 2 additions & 2 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type { SchemasInterface } from './schemas';
import type { SecuritySchemesInterface } from './security-schemes';
import type { ServersInterface } from './servers';

import type { v2 } from '../spec-types';
import type { AsyncAPIObject } from '../types';

export interface AsyncAPIDocumentInterface extends BaseModel<v2.AsyncAPIObject>, ExtensionsMixinInterface {
export interface AsyncAPIDocumentInterface extends BaseModel<AsyncAPIObject>, ExtensionsMixinInterface {
version(): string;
defaultContentType(): string | undefined;
hasDefaultContentType(): boolean;
Expand Down
50 changes: 49 additions & 1 deletion src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
import { BaseModel } from '../base';

export class AsyncAPIDocument extends BaseModel {
import type { AsyncAPIDocumentInterface } from '../asyncapi';

import type { v3 } from '../../spec-types';

export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements AsyncAPIDocumentInterface {
version(): string {
return this._json.asyncapi;
}

defaultContentType(): string | undefined {
return this._json.defaultContentType;
}

hasDefaultContentType(): boolean {
return !!this._json.defaultContentType;
}

info() {
return null as any;
}

servers() {
return null as any;
}

channels() {
return null as any;
}

operations() {
return null as any;
}

messages() {
return null as any;
}

schemas() {
return null as any;
}

securitySchemes() {
return null as any;
}

components() {
return null as any;
}

extensions() {
return null as any;
}
}
28 changes: 28 additions & 0 deletions src/models/v3/binding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BaseModel } from '../base';

import { extensions } from './mixins';

import type { BindingInterface } from '../binding';
import type { ExtensionsInterface } from '../extensions';

import type { v3 } from '../../spec-types';

export class Binding<T extends Record<string, any> = Record<string, any>> extends BaseModel<v3.Binding & T, { protocol: string }> implements BindingInterface<T> {
protocol(): string {
return this._meta.protocol;
}

version(): string {
return this._json.bindingVersion || 'latest';
}

value<V = T>(): V {
const value = { ...this._json };
delete (value as any).bindingVersion;
return value as unknown as V;
}

extensions(): ExtensionsInterface {
return extensions(this);
}
}
31 changes: 31 additions & 0 deletions src/models/v3/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Collection } from '../collection';
import { Extensions } from './extensions';
import { Extension } from './extension';

import { createModel } from '../utils';
import { EXTENSION_REGEX } from '../../constants';

import type { BindingsInterface } from '../bindings';
import type { BindingInterface } from '../binding';
import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';

import type { v3 } from '../../spec-types';

export class Bindings extends Collection<BindingInterface> implements BindingsInterface {
override get<T extends Record<string, any> = Record<string, any>>(name: string): BindingInterface<T> | undefined {
return this.collections.find(binding => binding.protocol() === name);
}

extensions(): ExtensionsInterface {
const extensions: ExtensionInterface[] = [];
Object.entries(this._meta.originalData as v3.SpecificationExtensions || {}).forEach(([id, value]) => {
if (EXTENSION_REGEX.test(id)) {
extensions.push(
createModel(Extension, value, { id, pointer: `${this._meta.pointer}/${id}`, asyncapi: this._meta.asyncapi }) as Extension
);
}
});
return new Extensions(extensions);
}
}
19 changes: 19 additions & 0 deletions src/models/v3/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseModel } from '../base';

import type { ExtensionInterface } from '../extension';

import type { v3 } from '../../spec-types';

export class Extension<T = any> extends BaseModel<v3.SpecificationExtension<T>, { id: string }> implements ExtensionInterface<T> {
id(): string {
return this._meta.id;
}

version(): string {
return 'to implement';
}

value<V = T>(): V {
return this._json as unknown as V;
}
}
11 changes: 11 additions & 0 deletions src/models/v3/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Collection } from '../collection';

import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';

export class Extensions extends Collection<ExtensionInterface> implements ExtensionsInterface {
override get<T = any>(id: string): ExtensionInterface<T> | undefined {
id = id.startsWith('x-') ? id : `x-${id}`;
return this.collections.find(ext => ext.id() === id);
}
}
26 changes: 26 additions & 0 deletions src/models/v3/external-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BaseModel } from '../base';

import { hasDescription, description, extensions } from './mixins';

import type { ExternalDocumentationInterface } from '../external-docs';
import type { ExtensionsInterface } from '../extensions';

import type { v3 } from '../../spec-types';

export class ExternalDocumentation extends BaseModel<v3.ExternalDocumentationObject> implements ExternalDocumentationInterface {
url(): string {
return this._json.url;
}

hasDescription(): boolean {
return hasDescription(this);
}

description(): string | undefined {
return description(this);
}

extensions(): ExtensionsInterface {
return extensions(this);
}
}
67 changes: 67 additions & 0 deletions src/models/v3/mixins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Bindings } from './bindings';
import { Binding } from './binding';
import { Extensions } from './extensions';
import { Extension } from './extension';
import { ExternalDocumentation } from './external-docs';
import { Tags } from './tags';
import { Tag } from './tag';

import { createModel } from '../utils';
import { EXTENSION_REGEX } from '../../constants';

import type { BaseModel } from '../base';
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { TagsInterface } from '../tags';

import type { v3 } from '../../spec-types';

export function bindings(model: BaseModel<{ bindings?: Record<string, any> }>): BindingsInterface {
const bindings = model.json('bindings') || {};
return new Bindings(
Object.entries(bindings || {}).map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
{ originalData: bindings, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
);
}

export function hasDescription(model: BaseModel<{ description?: string }>) {
return Boolean(description(model));
}

export function description(model: BaseModel<{ description?: string }>): string | undefined {
return model.json('description');
}

export function extensions(model: BaseModel<v3.SpecificationExtensions>): ExtensionsInterface {
const extensions: ExtensionInterface[] = [];
Object.entries(model.json()).forEach(([id, value]: [string, any]) => {
if (EXTENSION_REGEX.test(id)) {
extensions.push(
createModel(Extension, value, { id, pointer: model.jsonPath(id) } as any, model) as Extension
);
}
});
return new Extensions(extensions);
}

export function hasExternalDocs(model: BaseModel<{ externalDocs?: v3.ExternalDocumentationObject }>): boolean {
return Object.keys(model.json('externalDocs') || {}).length > 0;
}

export function externalDocs(model: BaseModel<{ externalDocs?: v3.ExternalDocumentationObject }>): ExternalDocumentationInterface | undefined {
if (hasExternalDocs(model)) {
return new ExternalDocumentation(model.json('externalDocs') as v3.ExternalDocumentationObject);
}
}

export function tags(model: BaseModel<{ tags?: v3.TagsObject }>): TagsInterface {
return new Tags(
(model.json('tags') || []).map((tag, idx) =>
createModel(Tag, tag, { pointer: model.jsonPath(`tags/${idx}`) }, model)
)
);
}
35 changes: 35 additions & 0 deletions src/models/v3/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BaseModel } from '../base';

import { hasDescription, description, extensions, hasExternalDocs, externalDocs } from './mixins';

import type { ExtensionsInterface } from '../extensions';
import type{ ExternalDocumentationInterface } from '../external-docs';
import type { TagInterface } from '../tag';

import type { v3 } from '../../spec-types';

export class Tag extends BaseModel<v3.TagObject> implements TagInterface {
name(): string {
return this._json.name;
}

hasDescription(): boolean {
return hasDescription(this);
}

description(): string | undefined {
return description(this);
}

extensions(): ExtensionsInterface {
return extensions(this);
}

hasExternalDocs(): boolean {
return hasExternalDocs(this);
}

externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}
}
10 changes: 10 additions & 0 deletions src/models/v3/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from '../collection';

import type { TagsInterface } from '../tags';
import type { TagInterface } from '../tag';

export class Tags extends Collection<TagInterface> implements TagsInterface {
override get(name: string): TagInterface | undefined {
return this.collections.find(tag => tag.name() === name);
}
}
1 change: 1 addition & 0 deletions src/spec-types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * as v2 from './v2';
export * as v3 from './v3';
Loading