Skip to content

Commit

Permalink
Merge branch 'main' into add-types-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisllontop authored Sep 4, 2024
2 parents 62a56ba + f3f2c94 commit 8dd77e1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"useNodejsImportProtocol": "off"
},
"complexity": {
"noThisInStatic": "off"
"noThisInStatic": "off",
"noStaticOnlyClass": "off"
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/parser/src/core/docutopia.ts
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();
}
}
56 changes: 28 additions & 28 deletions packages/parser/src/core/loader.ts
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.");
}
}
}
20 changes: 10 additions & 10 deletions packages/parser/src/core/parser.ts
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}`);
}
}
8 changes: 4 additions & 4 deletions packages/parser/src/parsers/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {OpenAPISpec} from "@/types/openapi";
import type {BaseResolver} from "@/resolvers/base";
import {ResolverFactory} from "@/resolvers/factory";
import type {DocutopiaParserOutput} from "@/types/output";
import type { BaseResolver } from "@/resolvers/base";
import { ResolverFactory } from "@/resolvers/factory";
import type { OpenAPISpec } from "@/types/openapi";
import type { DocutopiaParserOutput } from "@/types/output";

export abstract class BaseParser {
protected spec: OpenAPISpec;
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/parsers/openapi-v3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {DocutopiaParserOutput} from "@/types/output";
import {BaseParser} from "@/parsers/base";
import { BaseParser } from "@/parsers/base";
import type { DocutopiaParserOutput } from "@/types/output";

export class OpenAPIParserV3 extends BaseParser {
private groups: Array<any> = [];
Expand Down
34 changes: 17 additions & 17 deletions packages/parser/src/resolvers/base.ts
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;
}
}
20 changes: 10 additions & 10 deletions packages/parser/src/resolvers/factory.ts
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}`);
}
}
24 changes: 12 additions & 12 deletions packages/parser/src/resolvers/openapi-v3.ts
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`);
}
}

0 comments on commit 8dd77e1

Please sign in to comment.