-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-types-parser
- Loading branch information
Showing
9 changed files
with
93 additions
and
92 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,11 +1,11 @@ | ||
import {SpecLoader} from "@/core/loader"; | ||
import {ParserFactory} from "@/core/parser"; | ||
import type {DocutopiaParserOutput} from "@/types/output"; | ||
import { SpecLoader } from "@/core/loader"; | ||
import { ParserFactory } from "@/core/parser"; | ||
import type { DocutopiaParserOutput } from "@/types/output"; | ||
|
||
export class DocutopiaParser { | ||
public static async parse(source: string): Promise<DocutopiaParserOutput> { | ||
const spec = await SpecLoader.load(source); | ||
const parser = ParserFactory.createParser(spec); | ||
return parser.parse(); | ||
} | ||
public static async parse(source: string): Promise<DocutopiaParserOutput> { | ||
const spec = await SpecLoader.load(source); | ||
const parser = ParserFactory.createParser(spec); | ||
return parser.parse(); | ||
} | ||
} |
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,35 +1,35 @@ | ||
import type { OpenAPISpec } from "@/types/openapi"; | ||
import * as yaml from "js-yaml"; | ||
import type {OpenAPISpec} from "@/types/openapi"; | ||
|
||
export class SpecLoader { | ||
static async load(source: string): Promise<OpenAPISpec> { | ||
if (SpecLoader.isUrl(source)) { | ||
return SpecLoader.loadFromUrl(source); | ||
} | ||
throw new Error("Invalid source provided. Please provide a valid URL."); | ||
} | ||
static async load(source: string): Promise<OpenAPISpec> { | ||
if (SpecLoader.isUrl(source)) { | ||
return SpecLoader.loadFromUrl(source); | ||
} | ||
throw new Error("Invalid source provided. Please provide a valid URL."); | ||
} | ||
|
||
private static isUrl(source: string): boolean { | ||
return /^https?:\/\//.test(source); | ||
} | ||
private static isUrl(source: string): boolean { | ||
return /^https?:\/\//.test(source); | ||
} | ||
|
||
private static async loadFromUrl(url: string): Promise<OpenAPISpec> { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch OpenAPI spec from URL: ${url}`); | ||
} | ||
const contentType = response.headers.get("content-type"); | ||
const text = await response.text(); | ||
private static async loadFromUrl(url: string): Promise<OpenAPISpec> { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch OpenAPI spec from URL: ${url}`); | ||
} | ||
const contentType = response.headers.get("content-type"); | ||
const text = await response.text(); | ||
|
||
if (contentType?.includes("application/json")) { | ||
return <OpenAPISpec>JSON.parse(text); | ||
} else if ( | ||
contentType?.includes("application/x-yaml") || | ||
contentType?.includes("text/yaml") | ||
) { | ||
return <OpenAPISpec>yaml.load(text); | ||
} else { | ||
throw new Error("Unsupported content type. Please provide JSON or YAML."); | ||
} | ||
} | ||
if (contentType?.includes("application/json")) { | ||
return <OpenAPISpec>JSON.parse(text); | ||
} else if ( | ||
contentType?.includes("application/x-yaml") || | ||
contentType?.includes("text/yaml") | ||
) { | ||
return <OpenAPISpec>yaml.load(text); | ||
} else { | ||
throw new Error("Unsupported content type. Please provide JSON or YAML."); | ||
} | ||
} | ||
} |
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 {OpenAPISpec} from "@/types/openapi"; | ||
import type {BaseParser} from "@/parsers/base"; | ||
import {OpenAPIParserV3} from "@/parsers/openapi-v3"; | ||
import type { OpenAPISpec } from "@/types/openapi"; | ||
import type { BaseParser } from "@/parsers/base"; | ||
import { OpenAPIParserV3 } from "@/parsers/openapi-v3"; | ||
|
||
export class ParserFactory { | ||
static createParser(spec: OpenAPISpec): BaseParser { | ||
const version = spec.openapi; | ||
if (version.startsWith("3.0")) { | ||
return new OpenAPIParserV3(spec); | ||
} | ||
throw new Error(`Unsupported OpenAPI version: ${version}`); | ||
} | ||
static createParser(spec: OpenAPISpec): BaseParser { | ||
const version = spec.openapi; | ||
if (version.startsWith("3.0")) { | ||
return new OpenAPIParserV3(spec); | ||
} | ||
throw new Error(`Unsupported OpenAPI 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
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,23 +1,23 @@ | ||
import type {OpenAPISpec} from "@/types/openapi"; | ||
import type { OpenAPISpec } from "@/types/openapi"; | ||
|
||
export abstract class BaseResolver { | ||
protected spec: OpenAPISpec; | ||
protected spec: OpenAPISpec; | ||
|
||
constructor(spec: OpenAPISpec) { | ||
this.spec = spec; | ||
} | ||
constructor(spec: OpenAPISpec) { | ||
this.spec = spec; | ||
} | ||
|
||
public abstract resolveRef(ref: string): any; | ||
public abstract resolveRef(ref: string): any; | ||
|
||
public replaceRefs(obj: any): any { | ||
if (typeof obj === "object" && obj !== null) { | ||
if (obj.$ref) { | ||
return this.resolveRef(obj.$ref); | ||
} | ||
for (const key in obj) { | ||
obj[key] = this.replaceRefs(obj[key]); | ||
} | ||
} | ||
return obj; | ||
} | ||
public replaceRefs(obj: any): any { | ||
if (typeof obj === "object" && obj !== null) { | ||
if (obj.$ref) { | ||
return this.resolveRef(obj.$ref); | ||
} | ||
for (const key in obj) { | ||
obj[key] = this.replaceRefs(obj[key]); | ||
} | ||
} | ||
return obj; | ||
} | ||
} |
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 {OpenAPISpec} from "@/types/openapi"; | ||
import type {BaseResolver} from "@/resolvers/base"; | ||
import {OpenAPIResolverV3} from "@/resolvers/openapi-v3"; | ||
import type { BaseResolver } from "@/resolvers/base"; | ||
import { OpenAPIResolverV3 } from "@/resolvers/openapi-v3"; | ||
import type { OpenAPISpec } from "@/types/openapi"; | ||
|
||
export class ResolverFactory { | ||
public static createResolver(spec: OpenAPISpec): BaseResolver { | ||
const version = spec.openapi; | ||
if (version.startsWith("3.0")) { | ||
return new OpenAPIResolverV3(spec); | ||
} | ||
throw new Error(`Unsupported OpenAPI version: ${version}`); | ||
} | ||
public static createResolver(spec: OpenAPISpec): BaseResolver { | ||
const version = spec.openapi; | ||
if (version.startsWith("3.0")) { | ||
return new OpenAPIResolverV3(spec); | ||
} | ||
throw new Error(`Unsupported OpenAPI 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,15 @@ | ||
import {BaseResolver} from "@/resolvers/base"; | ||
import { BaseResolver } from "@/resolvers/base"; | ||
|
||
export class OpenAPIResolverV3 extends BaseResolver { | ||
public resolveRef(ref: string): any { | ||
const [_, type, name] = ref.split("/"); | ||
if ( | ||
type === "components" && | ||
this.spec.components && | ||
this.spec.components.schemas | ||
) { | ||
return this.spec.components.schemas[name]; | ||
} | ||
throw new Error(`Reference ${ref} not found`); | ||
} | ||
public resolveRef(ref: string): any { | ||
const [_, type, name] = ref.split("/"); | ||
if ( | ||
type === "components" && | ||
this.spec.components && | ||
this.spec.components.schemas | ||
) { | ||
return this.spec.components.schemas[name]; | ||
} | ||
throw new Error(`Reference ${ref} not found`); | ||
} | ||
} |