Skip to content

Commit

Permalink
refactor: port message, operation, correlation-id and security-requir…
Browse files Browse the repository at this point in the history
…ements models (#542)
  • Loading branch information
magicmatatjahu authored and derberg committed Oct 4, 2022
1 parent e444b87 commit 3627d02
Show file tree
Hide file tree
Showing 69 changed files with 1,884 additions and 399 deletions.
4 changes: 2 additions & 2 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export interface AsyncAPIDocumentInterface extends BaseModel, ExtensionsMixinInt
export function newAsyncAPIDocument(asyncapi: DetailedAsyncAPI): AsyncAPIDocumentInterface {
switch (asyncapi.semver.major) {
case 2:
return new AsyncAPIDocumentV2(asyncapi.parsed, { parent: null, asyncapi, pointer: '/' });
return new AsyncAPIDocumentV2(asyncapi.parsed, { asyncapi, pointer: '/' });
case 3:
return new AsyncAPIDocumentV3(asyncapi.parsed, { parent: null, asyncapi, pointer: '/' });
return new AsyncAPIDocumentV3(asyncapi.parsed, { asyncapi, pointer: '/' });
default:
throw new Error(`Unsupported AsyncAPI version: ${asyncapi.semver.version}`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Constructor } from "./utils";
import type { DetailedAsyncAPI } from "../types";

export interface ModelMetadata<P = unknown> {
export interface ModelMetadata {
asyncapi: DetailedAsyncAPI;
pointer: string;
parent: P | null;
[key: string]: any;
}

export abstract class BaseModel {
Expand Down Expand Up @@ -32,8 +32,8 @@ export abstract class BaseModel {
return `${this._meta?.pointer}/${field}`;
}

protected createModel<T extends BaseModel>(Model: Constructor<T>, value: any, { id, parent, pointer }: { id?: string, parent?: any, pointer: string | number }): T {
const meta = { asyncapi: this._meta.asyncapi, parent: parent || this, pointer } as ModelMetadata;
protected createModel<T extends BaseModel>(Model: Constructor<T>, value: any, { id, parent, pointer, ...rest }: { id?: string, pointer: string | number, [key: string]: any }): T {
const meta = { ...rest, asyncapi: this._meta.asyncapi, pointer } as ModelMetadata;
if (id) {
return new Model(id, value, meta);
}
Expand Down
10 changes: 9 additions & 1 deletion src/models/channel-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { BaseModel } from "./base";
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from "./mixins";
import type { SchemaInterface } from "./schema";

export interface ChannelParameterInterface extends BaseModel {}
export interface ChannelParameterInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string;
hasSchema(): boolean;
schema(): SchemaInterface | undefined;
hasLocation(): boolean;
location(): string | undefined;
}
12 changes: 11 additions & 1 deletion src/models/channel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { BaseModel } from "./base";
import type { ChannelParametersInterface } from "./channel-parameters";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface } from "./mixins";
import type { OperationsInterface } from "./operations";
import type { ServersInterface } from "./servers";

export interface ChannelInterface extends BaseModel {}
export interface ChannelInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string;
address(): string;
servers(): ServersInterface;
operations(): OperationsInterface;
parameters(): ChannelParametersInterface;
}
11 changes: 6 additions & 5 deletions src/models/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import type { Collection } from './collection';
import type { ServersInterface } from './servers';
import type { ChannelsInterface } from './channels';
import type { OperationsInterface } from './operations';
import type { OperationsTraitsInterface } from './operation-traits';
import type { OperationTraitsInterface } from './operation-traits';
import type { MessagesInterface } from './messages';
import type { MessagesTraitsInterface } from './message-traits';
import type { MessageTraitsInterface } from './message-traits';
import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ServerVariablesInterface } from './server-variables';
import type { CorrelationIdInterface } from './correlation-id';
import type { BindingsInterface } from './bindings';
import type { SecuritySchemesInterface } from './security-schemes';
import type { ExtensionsMixinInterface } from './mixins';

export interface Components extends BaseModel, ExtensionsMixinInterface {
Expand All @@ -21,10 +22,10 @@ export interface Components extends BaseModel, ExtensionsMixinInterface {
schemas(): SchemasInterface;
channelParameters(): ChannelParametersInterface;
serverVariables(): ServerVariablesInterface;
operationTraits(): OperationsTraitsInterface;
messageTraits(): MessagesTraitsInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
correlationIds(): Collection<CorrelationIdInterface>;
securitySchemes(): any; // TODO: Fix type after merging Souvik PR
securitySchemes(): SecuritySchemesInterface;
serverBindings(): Collection<BindingsInterface>;
channelBindings(): Collection<BindingsInterface>;
operationBindings(): Collection<BindingsInterface>;
Expand Down
6 changes: 5 additions & 1 deletion src/models/correlation-id.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { BaseModel } from "./base";
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from "./mixins";

export interface CorrelationIdInterface extends BaseModel {}
export interface CorrelationIdInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
hasLocation(): boolean;
location(): string | undefined;
}
12 changes: 11 additions & 1 deletion src/models/message-example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { BaseModel } from "./base";
import type { ExtensionsMixinInterface } from './mixins';

export interface MessageExample extends BaseModel {}
export interface MessageExampleInterface extends BaseModel, ExtensionsMixinInterface {
hasName(): boolean;
name(): string;
hasSummary(): boolean;
summary(): string;
hasHeaders(): boolean;
headers(): Record<string, any> | undefined;
hasPayload(): boolean;
payload(): Record<string, any> | undefined;
}
4 changes: 4 additions & 0 deletions src/models/message-examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from "./collection";
import type { MessageExampleInterface } from "./message-example";

export interface MessageExamplesInterface extends Collection<MessageExampleInterface> {}
23 changes: 22 additions & 1 deletion src/models/message-trait.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import type { BaseModel } from "./base";
import type { CorrelationIdInterface } from "./correlation-id";
import type { MessageExamplesInterface } from "./message-examples";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { SchemaInterface } from "./schema";

export interface MessageTraitInterface extends BaseModel {}
export interface MessageTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
hasMessageId(): boolean;
messageId(): string | undefined;
hasCorrelationId(): boolean;
correlationId(): CorrelationIdInterface | undefined;
hasContentType(): boolean;
contentType(): string | undefined;
hasHeaders(): boolean
headers(): SchemaInterface | undefined;
hasName(): boolean;
name(): string | undefined;
hasTitle(): boolean;
title(): string | undefined;
hasSummary(): boolean;
summary(): string | undefined;
examples(): MessageExamplesInterface;
}
2 changes: 1 addition & 1 deletion src/models/message-traits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection } from "./collection";
import type { MessageTraitInterface } from "./message-trait";

export interface MessagesTraitsInterface extends Collection<MessageTraitInterface> {}
export interface MessageTraitsInterface extends Collection<MessageTraitInterface> {}
8 changes: 7 additions & 1 deletion src/models/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { BaseModel } from "./base";
import type { MessageTraitsInterface } from "./message-traits";
import type { MessageTraitInterface } from "./message-trait";
import type { SchemaInterface } from "./schema";

export interface MessageInterface extends BaseModel, MessageTraitInterface {}
export interface MessageInterface extends BaseModel, MessageTraitInterface {
hasPayload(): boolean;
payload(): SchemaInterface | undefined;
traits(): MessageTraitsInterface;
}
12 changes: 6 additions & 6 deletions src/models/oauth-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { BaseModel } from './base';
import { ExtensionsMixinInterface } from './mixins';

export interface OAuthFlowInterface extends BaseModel, ExtensionsMixinInterface {
authorizationUrl(): string | undefined;
hasRefreshUrl(): boolean;
refreshUrl(): string | undefined;
scopes(): Record<string, string> | undefined;
tokenUrl(): string | undefined;
}
authorizationUrl(): string | undefined;
hasRefreshUrl(): boolean;
refreshUrl(): string | undefined;
scopes(): Record<string, string> | undefined;
tokenUrl(): string | undefined;
}
24 changes: 12 additions & 12 deletions src/models/oauth-flows.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { OAuthFlowInterface } from './oauth-flow';
import { BaseModel } from './base';
import {ExtensionsMixinInterface} from './mixins';
import type { BaseModel } from './base';
import type { OAuthFlowInterface } from './oauth-flow';
import type { ExtensionsMixinInterface } from './mixins';

export interface OAuthFlowsInterface extends BaseModel, ExtensionsMixinInterface {
hasAuthorizationCode(): boolean;
authorizationCode(): OAuthFlowInterface | undefined;
hasClientCredentials(): boolean
clientCredentials(): OAuthFlowInterface | undefined;
hasImplicit(): boolean;
implicit(): OAuthFlowInterface | undefined;
hasPassword(): boolean;
password(): OAuthFlowInterface | undefined;
}
hasAuthorizationCode(): boolean;
authorizationCode(): OAuthFlowInterface | undefined;
hasClientCredentials(): boolean
clientCredentials(): OAuthFlowInterface | undefined;
hasImplicit(): boolean;
implicit(): OAuthFlowInterface | undefined;
hasPassword(): boolean;
password(): OAuthFlowInterface | undefined;
}
13 changes: 12 additions & 1 deletion src/models/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import type { BaseModel } from "./base";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { OperationAction } from "./operation";
import type { SecuritySchemeInterface } from "./security-scheme";

export interface OperationTraitInterface extends BaseModel {}
export interface OperationTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
action(): OperationAction;
hasOperationId(): boolean;
operationId(): string | undefined;
hasSummary(): boolean;
summary(): string | undefined;
security(): Array<Record<string, { schema: SecuritySchemeInterface; scopes: string[]; }>>;
}
2 changes: 1 addition & 1 deletion src/models/operation-traits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Collection } from "./collection";
import type { OperationTraitInterface } from "./operation-trait";

export interface OperationsTraitsInterface extends Collection<OperationTraitInterface> {}
export interface OperationTraitsInterface extends Collection<OperationTraitInterface> {}
9 changes: 8 additions & 1 deletion src/models/operation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { BaseModel } from "./base";
import type { MessagesInterface } from "./messages";
import type { OperationTraitsInterface } from "./operation-traits";
import type { OperationTraitInterface } from "./operation-trait";

export interface OperationInterface extends BaseModel, OperationTraitInterface {}
export type OperationAction = 'send' | 'receive' | 'publish' | 'subscribe';

export interface OperationInterface extends BaseModel, OperationTraitInterface {
messages(): MessagesInterface;
traits(): OperationTraitsInterface;
}
3 changes: 2 additions & 1 deletion src/models/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BaseModel } from "./base";
import type { ExtensionsMixinInterface } from "./mixins";

export interface SchemaInterface extends BaseModel {}
export interface SchemaInterface extends BaseModel, ExtensionsMixinInterface {}
27 changes: 13 additions & 14 deletions src/models/security-scheme.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { BaseModel } from './base';
import { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
import { OAuthFlowsInterface } from './oauth-flows';
import type { BaseModel } from './base';
import type { OAuthFlowsInterface } from './oauth-flows';
import type { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';

export type SecuritySchemaType = 'userPassword' | 'apiKey' | 'X509' | 'symmetricEncryption' | 'asymmetricEncryption' | 'httpApiKey' | 'http' | 'oauth2' | 'openIdConnect' | 'plain' | 'scramSha256' | 'scramSha512' | 'gssapi';


export interface SecuritySchemeInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string
hasBearerFormat(): boolean;
bearerFormat(): string | undefined;
openIdConnectUrl(): string;
scheme(): string | undefined;
flows(): OAuthFlowsInterface | undefined;
scopes(): string[];
type(): SecuritySchemaType;
in(): string | undefined;
}
id(): string
hasBearerFormat(): boolean;
bearerFormat(): string | undefined;
openIdConnectUrl(): string;
scheme(): string | undefined;
flows(): OAuthFlowsInterface | undefined;
scopes(): string[];
type(): SecuritySchemaType;
in(): string | undefined;
}
6 changes: 3 additions & 3 deletions src/models/security-schemes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Collection} from './collection';
import { SecuritySchemeInterface } from './security-scheme';
import type { Collection} from './collection';
import type { SecuritySchemeInterface } from './security-scheme';

export interface SecuritySchemesInterface extends Collection<SecuritySchemeInterface> {}
export interface SecuritySchemesInterface extends Collection<SecuritySchemeInterface> {}
4 changes: 2 additions & 2 deletions src/models/server-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface ServerVariableInterface extends BaseModel, DescriptionMixinInte
id(): string;
hasDefaultValue(): boolean;
defaultValue(): string | undefined;
hasAllowedValue(): boolean;
allowedValue(): Array<string>;
hasAllowedValues(): boolean;
allowedValues(): Array<string>;
examples(): Array<string>;
}
8 changes: 5 additions & 3 deletions src/models/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { BaseModel } from "./base";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
import type { ServerVariablesInterface } from "./server-variables";
import type { SecuritySchemeInterface } from "./security-scheme";

export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface, ExtensionsMixinInterface {
id(): string
url(): string;
protocol(): string | undefined;
protocol(): string;
protocolVersion(): string;
hasProtocolVersion(): boolean;
variables(): ServerVariablesInterface
}
variables(): ServerVariablesInterface;
security(): Array<Record<string, { schema: SecuritySchemeInterface; scopes: string[]; }>>;
}
5 changes: 1 addition & 4 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { ExtensionsMixin } from './mixins/extensions';
import type { AsyncAPIDocumentInterface, InfoInterface } from "../../models";
import type { ServersInterface } from "models/servers";

export class AsyncAPIDocument
extends Mixin(BaseModel, ExtensionsMixin)
implements AsyncAPIDocumentInterface {

export class AsyncAPIDocument extends Mixin(BaseModel, ExtensionsMixin) implements AsyncAPIDocumentInterface {
version(): string {
return this._json.asyncapi;
}
Expand Down
17 changes: 17 additions & 0 deletions src/models/v2/correlation-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseModel } from "../base";

import { Mixin } from '../utils';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';

import type { CorrelationIdInterface } from "../correlation-id";

export class CorrelationId extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements CorrelationIdInterface {
hasLocation(): boolean {
return !!this._json.location;
}

location(): string | undefined {
return this._json.location;
}
}
4 changes: 2 additions & 2 deletions src/models/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export { SecurityScheme as SecuritySchemeV2 } from './security-scheme';
export { SecuritySchemes as SecuritySchemesV2 } from './security-schemes';
export { ServerVariable as ServerVariableV2 } from './server-variable';
export { ServerVariables as ServerVariablesV2 } from './server-variables';
export {OAuthFlow as OAuthFlowV2} from './oauth-flow';
export {OAuthFlows as OAuthFlowsV2} from './oauth-flows';
export { OAuthFlow as OAuthFlowV2 } from './oauth-flow';
export { OAuthFlows as OAuthFlowsV2 } from './oauth-flows';
Loading

0 comments on commit 3627d02

Please sign in to comment.