forked from asyncapi/parser-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: interface and implementations for each major spec version (a…
- Loading branch information
1 parent
e3809c0
commit edc8529
Showing
28 changed files
with
274 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { InfoInterface } from "./info"; | ||
import { BaseModel } from "./base"; | ||
import { Info } from "./info"; | ||
import { AsyncAPIDocumentV2 } from "./v2"; | ||
import { AsyncAPIDocumentV3 } from "./v3"; | ||
|
||
export class AsyncAPIDocument extends BaseModel { | ||
version(): string { | ||
return this.json("asyncapi"); | ||
export interface AsyncAPIDocumentInterface extends BaseModel { | ||
version(): string; | ||
info(): InfoInterface | ||
} | ||
|
||
export function newAsyncAPIDocument(json: Record<string, any>): AsyncAPIDocumentInterface { | ||
const version = json['asyncapi']; // Maybe this should be an arg. | ||
if (version == undefined || version == null || version == '') { | ||
throw new Error('Missing AsyncAPI version in document'); | ||
} | ||
|
||
info(): Info { | ||
return new Info(this.json("info")); | ||
const major = version.split(".")[0]; | ||
switch (major) { | ||
case '2': | ||
return new AsyncAPIDocumentV2(json); | ||
case '3': | ||
return new AsyncAPIDocumentV3(json); | ||
default: | ||
throw new Error(`Unsupported version: ${version}`); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,7 @@ | ||
import { BaseModel } from "./base"; | ||
|
||
export class Contact extends BaseModel { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
|
||
email(): string { | ||
return this.json("email"); | ||
} | ||
export interface ContactInterface extends BaseModel { | ||
name(): string; | ||
url(): string; | ||
email(): string; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from './v2'; | ||
export * from './v3'; | ||
export * from './asyncapi'; | ||
export * from './base'; | ||
export * from './contact'; | ||
export * from './info'; | ||
export * from './license'; | ||
export * from './contact'; | ||
export * from './license'; |
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 |
---|---|---|
@@ -1,31 +1,12 @@ | ||
import { ContactInterface } from "./contact"; | ||
import { LicenseInterface } from "./license"; | ||
import { BaseModel } from "./base"; | ||
import { Contact } from "./contact"; | ||
import { License } from "./license"; | ||
|
||
export class Info extends BaseModel { | ||
title(): string { | ||
return this.json("title"); | ||
} | ||
|
||
version(): string { | ||
return this.json("version"); | ||
} | ||
|
||
description(): string { | ||
return this.json("description"); | ||
} | ||
|
||
termsOfService(): string { | ||
return this.json("termsOfService"); | ||
} | ||
|
||
contact(): Contact | undefined { | ||
const doc = this.json("contact"); | ||
return doc && new Contact(doc); | ||
} | ||
|
||
license(): License | undefined { | ||
const doc = this.json("license"); | ||
return doc && new License(doc); | ||
} | ||
export interface InfoInterface extends BaseModel { | ||
title(): string; | ||
version(): string; | ||
description(): string; | ||
termsOfService(): string; | ||
contact(): ContactInterface | undefined; | ||
license(): LicenseInterface | 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 |
---|---|---|
@@ -1,11 +1,6 @@ | ||
import { BaseModel } from "./base"; | ||
|
||
export class License extends BaseModel { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
export interface LicenseInterface extends BaseModel { | ||
name(): string; | ||
url(): string; | ||
} |
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,13 @@ | ||
import { AsyncAPIDocumentInterface } from "../../models"; | ||
import { BaseModel } from "../base"; | ||
import { Info } from "./info"; | ||
|
||
export class AsyncAPIDocument extends BaseModel implements AsyncAPIDocumentInterface { | ||
version(): string { | ||
return this.json("asyncapi"); | ||
} | ||
|
||
info(): Info { | ||
return new Info(this.json("info")); | ||
} | ||
} |
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,16 @@ | ||
import { ContactInterface } from "../../models/contact"; | ||
import { BaseModel } from "../base"; | ||
|
||
export class Contact extends BaseModel implements ContactInterface { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
|
||
email(): string { | ||
return this.json("email"); | ||
} | ||
} |
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,4 @@ | ||
export { AsyncAPIDocument as AsyncAPIDocumentV2 } from './asyncapi'; | ||
export { Contact as ContactV2 } from './contact'; | ||
export { Info as InfoV2 } from './info'; | ||
export { License as LicenseV2 } from './license'; |
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,32 @@ | ||
import { InfoInterface } from "../../models/info"; | ||
import { BaseModel } from "../base"; | ||
import { Contact } from "./contact"; | ||
import { License } from "./license"; | ||
|
||
export class Info extends BaseModel implements InfoInterface { | ||
title(): string { | ||
return this.json("title"); | ||
} | ||
|
||
version(): string { | ||
return this.json("version"); | ||
} | ||
|
||
description(): string { | ||
return this.json("description"); | ||
} | ||
|
||
termsOfService(): string { | ||
return this.json("termsOfService"); | ||
} | ||
|
||
contact(): Contact | undefined { | ||
const doc = this.json("contact"); | ||
return doc && new Contact(doc); | ||
} | ||
|
||
license(): License | undefined { | ||
const doc = this.json("license"); | ||
return doc && new License(doc); | ||
} | ||
} |
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,12 @@ | ||
import { LicenseInterface } from "../../models/license"; | ||
import { BaseModel } from "../base"; | ||
|
||
export class License extends BaseModel implements LicenseInterface { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
} |
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,13 @@ | ||
import { AsyncAPIDocumentInterface } from "../../models/asyncapi"; | ||
import { BaseModel } from "../base"; | ||
import { Info } from "./info"; | ||
|
||
export class AsyncAPIDocument extends BaseModel implements AsyncAPIDocumentInterface { | ||
version(): string { | ||
return this.json("asyncapi"); | ||
} | ||
|
||
info(): Info { | ||
return new Info(this.json("info")); | ||
} | ||
} |
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,16 @@ | ||
import { ContactInterface } from "../../models/contact"; | ||
import { BaseModel } from "../base"; | ||
|
||
export class Contact extends BaseModel implements ContactInterface { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
|
||
email(): string { | ||
return this.json("email"); | ||
} | ||
} |
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,4 @@ | ||
export { AsyncAPIDocument as AsyncAPIDocumentV3 } from './asyncapi'; | ||
export { Contact as ContactV3 } from './contact'; | ||
export { Info as InfoV3 } from './info'; | ||
export { License as LicenseV3 } from './license'; |
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,32 @@ | ||
import { InfoInterface } from "../../models/info"; | ||
import { BaseModel } from "../base"; | ||
import { Contact } from "./contact"; | ||
import { License } from "./license"; | ||
|
||
export class Info extends BaseModel implements InfoInterface { | ||
title(): string { | ||
return this.json("title"); | ||
} | ||
|
||
version(): string { | ||
return this.json("version"); | ||
} | ||
|
||
description(): string { | ||
return this.json("description"); | ||
} | ||
|
||
termsOfService(): string { | ||
return this.json("termsOfService"); | ||
} | ||
|
||
contact(): Contact | undefined { | ||
const doc = this.json("contact"); | ||
return doc && new Contact(doc); | ||
} | ||
|
||
license(): License | undefined { | ||
const doc = this.json("license"); | ||
return doc && new License(doc); | ||
} | ||
} |
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,12 @@ | ||
import { LicenseInterface } from "../../models/license"; | ||
import { BaseModel } from "../base"; | ||
|
||
export class License extends BaseModel implements LicenseInterface { | ||
name(): string { | ||
return this.json("name"); | ||
} | ||
|
||
url(): string { | ||
return this.json("url"); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import type { ISpectralDiagnostic } from '@stoplight/spectral-core'; | ||
import type { AsyncAPIDocument } from './models/asyncapi'; | ||
import type { AsyncAPIDocumentInterface } from './models/asyncapi'; | ||
|
||
export type MaybeAsyncAPI = { asyncapi: unknown } & Record<string, unknown>; | ||
export type ParserInput = string | MaybeAsyncAPI | AsyncAPIDocument; | ||
export type ParserInput = string | MaybeAsyncAPI | AsyncAPIDocumentInterface; | ||
|
||
export type Diagnostic = ISpectralDiagnostic; | ||
|
||
export interface ParserOutput { | ||
source: ParserInput; | ||
parsed: AsyncAPIDocument | undefined; | ||
parsed: AsyncAPIDocumentInterface | undefined; | ||
diagnostics: Diagnostic[]; | ||
} |
Oops, something went wrong.