Skip to content

Commit

Permalink
Merge branch 'next-major-spec' into next-spec/new-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Apr 19, 2023
2 parents 37cdace + 58b8b85 commit 71681ea
Show file tree
Hide file tree
Showing 45 changed files with 663 additions and 169 deletions.
4 changes: 4 additions & 0 deletions src/models/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ServerVariablesInterface } from './server-variables';
import type { OperationTraitsInterface } from './operation-traits';
import type { OperationRepliesInterface } from './operation-replies';
import type { OperationReplyAddressesInterface } from './operation-reply-addresses';
import type { MessageTraitsInterface } from './message-traits';
import type { SecuritySchemesInterface } from './security-schemes';
import type { CorrelationIdsInterface } from './correlation-ids';
Expand All @@ -25,6 +27,8 @@ export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface
serverVariables(): ServerVariablesInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
replies(): OperationRepliesInterface;
replyAddresses(): OperationReplyAddressesInterface;
correlationIds(): CorrelationIdsInterface;
securitySchemes(): SecuritySchemesInterface;
tags(): TagsInterface;
Expand Down
1 change: 1 addition & 0 deletions src/models/external-documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import type { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixi
export interface ExternalDocumentationInterface
extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {

id(): string | undefined;
url(): string;
}
4 changes: 4 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export * from './message';
export * from './messages';
export * from './oauth-flow';
export * from './oauth-flows';
export * from './operation-replies';
export * from './operation-reply-address';
export * from './operation-reply-addresses';
export * from './operation-reply';
export * from './operation-trait';
export * from './operation-traits';
export * from './operation';
Expand Down
10 changes: 10 additions & 0 deletions src/models/operation-replies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from './collection';
import type { OperationReplyInterface } from './operation-reply';

export type OperationRepliesInterface = Collection<OperationReplyInterface>

export class OperationReplies extends Collection<OperationReplyInterface> implements OperationRepliesInterface {
override get(id: string): OperationReplyInterface | undefined {
return this.collections.find(reply => reply.id() === id);
}
}
7 changes: 7 additions & 0 deletions src/models/operation-reply-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { BaseModel } from './base';
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';

export interface OperationReplyAddressInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string | undefined;
location(): string;
}
10 changes: 10 additions & 0 deletions src/models/operation-reply-addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from './collection';
import type { OperationReplyAddressInterface } from './operation-reply-address';

export type OperationReplyAddressesInterface = Collection<OperationReplyAddressInterface>

export class OperationReplyAddresses extends Collection<OperationReplyAddressInterface> implements OperationReplyAddressesInterface {
override get(id: string): OperationReplyAddressInterface | undefined {
return this.collections.find(reply => reply.id() === id);
}
}
12 changes: 12 additions & 0 deletions src/models/operation-reply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { BaseModel } from './base';
import type { ExtensionsMixinInterface } from './mixins';
import type { ChannelInterface } from './channel';
import type { OperationReplyAddressInterface } from './operation-reply-address';

export interface OperationReplyInterface extends BaseModel, ExtensionsMixinInterface {
id(): string | undefined;
hasAddress(): boolean;
address(): OperationReplyAddressInterface | undefined;
hasChannel(): boolean;
channel(): ChannelInterface | undefined;
}
2 changes: 2 additions & 0 deletions src/models/operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BaseModel } from './base';
import type { OperationTraitsInterface } from './operation-traits';
import type { OperationTraitInterface } from './operation-trait';
import type { OperationReplyInterface } from './operation-reply';
import type { ChannelsInterface } from './channels';
import type { ServersInterface } from './servers';
import type { MessagesInterface } from './messages';
Expand All @@ -14,5 +15,6 @@ export interface OperationInterface extends BaseModel, OperationTraitInterface {
servers(): ServersInterface;
channels(): ChannelsInterface
messages(): MessagesInterface;
reply(): OperationReplyInterface | undefined;
traits(): OperationTraitsInterface;
}
1 change: 1 addition & 0 deletions src/models/security-scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface SecuritySchemeInterface extends BaseModel, DescriptionMixinInte
openIdConnectUrl(): string | undefined;
scheme(): string | undefined;
flows(): OAuthFlowsInterface | undefined;
scopes(): string[] | undefined;
type(): string;
in(): string | undefined;
}
3 changes: 3 additions & 0 deletions src/models/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { SecurityRequirementsInterface } from './security-requirements';
export interface ServerInterface extends BaseModel, CoreMixinInterface {
id(): string
url(): string;
host(): string;
protocol(): string;
hasPathname(): boolean;
pathname(): string | undefined;
protocolVersion(): string | undefined;
hasProtocolVersion(): boolean;
channels(): ChannelsInterface;
Expand Down
1 change: 1 addition & 0 deletions src/models/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import type { DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocum
export interface TagInterface
extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface {

id(): string | undefined;
name(): string;
}
12 changes: 12 additions & 0 deletions src/models/v2/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { ChannelParameters } from '../channel-parameters';
import { ServerVariables } from '../server-variables';
import { OperationTraits } from '../operation-traits';
import { MessageTraits } from '../message-traits';
import { OperationReplies } from '../operation-replies';
import { OperationReplyAddresses } from '../operation-reply-addresses';
import { SecuritySchemes } from '../security-schemes';
import { CorrelationIds } from '../correlation-ids';
import { Operations } from '../operations';
Expand All @@ -43,6 +45,8 @@ import type { ServerVariablesInterface } from '../server-variables';
import type { OperationTraitsInterface } from '../operation-traits';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { MessageTraitsInterface } from '../message-traits';
import type { OperationRepliesInterface } from '../operation-replies';
import type { OperationReplyAddressesInterface } from '../operation-reply-addresses';
import type { OperationsInterface } from '../operations';
import type { ExternalDocumentationsInterface } from '../external-documentations';
import type { TagsInterface } from '../tags';
Expand Down Expand Up @@ -86,6 +90,14 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
return this.createCollection('messageTraits', MessageTraits, MessageTrait);
}

replies(): OperationRepliesInterface {
return new OperationReplies([]);
}

replyAddresses(): OperationReplyAddressesInterface {
return new OperationReplyAddresses([]);
}

correlationIds(): CorrelationIds {
return this.createCollection('correlationIds', CorrelationIds, CorrelationId);
}
Expand Down
6 changes: 5 additions & 1 deletion src/models/v2/external-documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type { ExtensionsInterface } from '../extensions';

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

export class ExternalDocumentation extends BaseModel<v2.ExternalDocumentationObject, { id?: string }> implements ExternalDocumentationInterface {
export class ExternalDocumentation extends BaseModel<v2.ExternalDocumentationObject> implements ExternalDocumentationInterface {
id(): string | undefined {
return;
}

url(): string {
return this._json.url;
}
Expand Down
5 changes: 5 additions & 0 deletions src/models/v2/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { ChannelsInterface } from '../channels';
import type { ChannelInterface } from '../channel';
import type { MessagesInterface } from '../messages';
import type { OperationAction, OperationInterface } from '../operation';
import type { OperationReplyInterface } from '../operation-reply';
import type { OperationTraitsInterface } from '../operation-traits';
import type { ServersInterface } from '../servers';
import type { ServerInterface } from '../server';
Expand Down Expand Up @@ -76,6 +77,10 @@ export class Operation extends OperationTrait<v2.OperationObject> implements Ope
);
}

reply(): OperationReplyInterface | undefined {
return undefined;
}

traits(): OperationTraitsInterface {
return new OperationTraits(
(this._json.traits || []).map((trait: any, index: number) => {
Expand Down
4 changes: 4 additions & 0 deletions src/models/v2/security-scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class SecurityScheme extends BaseModel<v2.SecuritySchemeObject, { id: str
return new OAuthFlows(this._json.flows);
}

scopes(): string[] | undefined {
return undefined;
}

type(): v2.SecuritySchemeType {
return this._json.type;
}
Expand Down
14 changes: 13 additions & 1 deletion src/models/v2/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SecurityRequirements } from '../security-requirements';
import { SecurityRequirement } from './security-requirement';

import { CoreModel } from './mixins';
import { tilde } from '../../utils';
import { tilde, resolveServerUrl } from '../../utils';

import type { ChannelsInterface } from '../channels';
import type { ChannelInterface } from '../channel';
Expand All @@ -31,10 +31,22 @@ export class Server extends CoreModel<v2.ServerObject, { id: string }> implement
return this._json.url;
}

host(): string {
return resolveServerUrl(this._json.url).host;
}

protocol(): string {
return this._json.protocol;
}

hasPathname(): boolean {
return !!this.pathname();
}

pathname(): string | undefined {
return resolveServerUrl(this._json.url).pathname;
}

hasProtocolVersion(): boolean {
return !!this._json.protocolVersion;
}
Expand Down
6 changes: 5 additions & 1 deletion src/models/v2/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { TagInterface } from '../tag';

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

export class Tag extends BaseModel<v2.TagObject, { id?: string }> implements TagInterface {
export class Tag extends BaseModel<v2.TagObject> implements TagInterface {
id(): string | undefined {
return;
}

name(): string {
return this._json.name;
}
Expand Down
11 changes: 6 additions & 5 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
servers(): ServersInterface {
return new Servers(
Object.entries(this._json.servers || {}).map(([serverName, server]) =>
this.createModel(Server, server, { id: serverName, pointer: `/servers/${tilde(serverName)}` })
this.createModel(Server, server as v3.ServerObject, { id: serverName, pointer: `/servers/${tilde(serverName)}` })
)
);
}

channels(): ChannelsInterface {
return new Channels(
Object.entries(this._json.channels || {}).map(([channelId, channel]) =>
this.createModel(Channel, channel, { id: channelId, pointer: `/channels/${tilde(channelId)}` })
this.createModel(Channel, channel as v3.ChannelObject, { id: channelId, pointer: `/channels/${tilde(channelId)}` })
)
);
}

operations(): OperationsInterface {
return new Operations(
Object.entries(this._json.operations || {}).map(([operationId, operation]) =>
this.createModel(Operation, operation, { id: operationId, pointer: `/operations/${tilde(operationId)}` })
this.createModel(Operation, operation as v3.OperationObject, { id: operationId, pointer: `/operations/${tilde(operationId)}` })
)
);
}
Expand All @@ -73,8 +73,9 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
const messagesData: any[] = [];
this.channels().forEach(channel => {
channel.messages().forEach(message => {
if (!messagesData.includes(message.json())) {
messagesData.push(message.json());
const messageData = message.json();
if (!messagesData.includes(messageData)) {
messagesData.push(messageData);
messages.push(message);
}
});
Expand Down
17 changes: 6 additions & 11 deletions src/models/v3/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ export class Channel extends CoreModel<v3.ChannelObject, { id: string }> impleme
servers(): ServersInterface {
const servers: ServerInterface[] = [];
const allowedServers = this._json.servers || [];
Object.entries(this._meta.asyncapi?.parsed?.servers || {}).forEach(([serverName, server]) => {
if (allowedServers.length === 0) {
Object.entries(this._meta.asyncapi?.parsed.servers || {}).forEach(([serverName, server]) => {
if (allowedServers.length === 0 || allowedServers.includes(server)) {
servers.push(this.createModel(Server, server, { id: serverName, pointer: `/servers/${serverName}` }));
} else {
const index = allowedServers.indexOf(server);
if (index !== -1) {
servers.push(this.createModel(Server, server, { id: serverName, pointer: this.jsonPath(`servers/${index}`) }));
}
}
});
return new Servers(servers);
Expand All @@ -47,9 +42,9 @@ export class Channel extends CoreModel<v3.ChannelObject, { id: string }> impleme
operations(): OperationsInterface {
const operations: OperationInterface[] = [];
Object.entries(((this._meta.asyncapi?.parsed as v3.AsyncAPIObject)?.operations || {})).forEach(([operationId, operation]) => {
if (operation.channel === this._json) {
if ((operation as v3.OperationObject).channel === this._json) {
operations.push(
this.createModel(Operation, operation, { id: operationId, pointer: `/operations/${operationId}` }),
this.createModel(Operation, operation as v3.OperationObject, { id: operationId, pointer: `/operations/${operationId}` }),
);
}
});
Expand All @@ -59,7 +54,7 @@ export class Channel extends CoreModel<v3.ChannelObject, { id: string }> impleme
messages(): MessagesInterface {
return new Messages(
Object.entries(this._json.messages || {}).map(([messageName, message]) => {
return this.createModel(Message, message, { id: messageName, pointer: this.jsonPath(`messages/${messageName}`) });
return this.createModel(Message, message as v3.MessageObject, { id: messageName, pointer: this.jsonPath(`messages/${messageName}`) });
})
);
}
Expand All @@ -69,7 +64,7 @@ export class Channel extends CoreModel<v3.ChannelObject, { id: string }> impleme
Object.entries(this._json.parameters || {}).map(([channelParameterName, channelParameter]) => {
return this.createModel(ChannelParameter, channelParameter as v3.ParameterObject, {
id: channelParameterName,
pointer: `${this._meta.pointer}/parameters/${channelParameterName}`
pointer: this.jsonPath(`parameters/${channelParameterName}`),
});
})
);
Expand Down
14 changes: 14 additions & 0 deletions src/models/v3/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { ChannelParameter } from './channel-parameter';
import { CorrelationId } from './correlation-id';
import { MessageTrait } from './message-trait';
import { OperationTrait } from './operation-trait';
import { OperationReply } from './operation-reply';
import { OperationReplyAddress } from './operation-reply-address';
import { Schema } from './schema';
import { SecurityScheme } from './security-scheme';
import { Server } from './server';
Expand All @@ -21,6 +23,8 @@ import { ChannelParameters } from '../channel-parameters';
import { ServerVariables } from '../server-variables';
import { OperationTraits } from '../operation-traits';
import { MessageTraits } from '../message-traits';
import { OperationReplies } from '../operation-replies';
import { OperationReplyAddresses } from '../operation-reply-addresses';
import { SecuritySchemes } from '../security-schemes';
import { CorrelationIds } from '../correlation-ids';
import { Operations } from '../operations';
Expand All @@ -46,6 +50,8 @@ import type { ServerVariablesInterface } from '../server-variables';
import type { OperationTraitsInterface } from '../operation-traits';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { MessageTraitsInterface } from '../message-traits';
import type { OperationRepliesInterface } from '../operation-replies';
import type { OperationReplyAddressesInterface } from '../operation-reply-addresses';
import type { OperationsInterface } from '../operations';
import type { ExternalDocumentationsInterface } from '../external-documentations';
import type { TagsInterface } from '../tags';
Expand Down Expand Up @@ -89,6 +95,14 @@ export class Components extends BaseModel<v3.ComponentsObject> implements Compon
return this.createCollection('messageTraits', MessageTraits, MessageTrait);
}

replies(): OperationRepliesInterface {
return this.createCollection('replies', OperationReplies, OperationReply);
}

replyAddresses(): OperationReplyAddressesInterface {
return this.createCollection('replyAddresses', OperationReplyAddresses, OperationReplyAddress);
}

correlationIds(): CorrelationIds {
return this.createCollection('correlationIds', CorrelationIds, CorrelationId);
}
Expand Down
6 changes: 5 additions & 1 deletion src/models/v3/external-documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type { ExtensionsInterface } from '../extensions';

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

export class ExternalDocumentation extends BaseModel<v3.ExternalDocumentationObject> implements ExternalDocumentationInterface {
export class ExternalDocumentation extends BaseModel<v3.ExternalDocumentationObject, { id?: string }> implements ExternalDocumentationInterface {
id(): string | undefined {
return this._meta.id;
}

url(): string {
return this._json.url;
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export { OAuthFlow as OAuthFlowV3 } from './oauth-flow';
export { OAuthFlows as OAuthFlowsV3 } from './oauth-flows';
export { OperationTrait as OperationTraitV3 } from './operation-trait';
export { OperationTraits as OperationTraitsV3 } from '../operation-traits';
export { OperationReplies as OperationRepliesV3 } from '../operation-replies';
export { OperationReplyAddress as OperationReplyAddressV3 } from './operation-reply-address';
export { OperationReply as OperationReplyV3 } from './operation-reply';
export { Operation as OperationV3 } from './operation';
export { Operations as OperationsV3 } from '../operations';
export { Schema as SchemaV3 } from './schema';
Expand Down
Loading

0 comments on commit 71681ea

Please sign in to comment.