Skip to content

Commit

Permalink
Refactor SpecsLoader by removing singleton and file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisllontop committed Sep 4, 2024
1 parent 9a55774 commit e60436a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 deletions.
1 change: 0 additions & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@rspack/cli": "catalog:",
"@rspack/core": "catalog:",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.5.1",
"ts-loader": "catalog:",
"typescript": "catalog:"
},
Expand Down
33 changes: 4 additions & 29 deletions packages/parser/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
import { readFileSync } from "node:fs";
import { extname } from "node:path";
import type { OpenAPISpec } from "@/types/openapi";
import * as yaml from "js-yaml";

export class SpecsLoader {
private static instance: SpecsLoader;
private readonly source: string;

private constructor(source: string) {
constructor(source: string) {
this.source = source;
}

public static getInstance(source: string): SpecsLoader {
if (!SpecsLoader.instance || SpecsLoader.instance.source !== source) {
SpecsLoader.instance = new SpecsLoader(source);
}
return SpecsLoader.instance;
}

public async loadSpec(): Promise<OpenAPISpec> {
if (this.isUrl(this.source)) {
return this.loadFromUrl(this.source);
} else {
return this.loadFromFile(this.source);
throw new Error("Invalid URL provided. Please provide a valid URL.");
}
}

private isUrl(source: string): boolean {
return source.startsWith("http://") || source.startsWith("https://");
}

private async loadFromUrl(url: string): Promise<any> {
private 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}`);
Expand All @@ -44,26 +34,11 @@ export class SpecsLoader {
contentType?.includes("application/x-yaml") ||
contentType?.includes("text/yaml")
) {
return yaml.load(text);
return yaml.load(text) as OpenAPISpec;
} else {
throw new Error(
"Unsupported content type. Please provide a valid JSON or YAML URL.",
);
}
}

private loadFromFile(filePath: string): OpenAPISpec {
const fileExtension = extname(filePath);
const fileContent = readFileSync(filePath, "utf-8");

if (fileExtension === ".yaml" || fileExtension === ".yml") {
return <OpenAPISpec>yaml.load(fileContent);
} else if (fileExtension === ".json") {
return JSON.parse(fileContent);
} else {
throw new Error(
"Unsupported file format. Please provide a .json, .yaml, or .yml file.",
);
}
}
}
3 changes: 2 additions & 1 deletion packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { OpenAPIParserV3 } from "@/parsers/v3";

export class DocutopiaParser {
public static async parse(source: string): Promise<OpenAPIParserOutput> {
const spec = await SpecsLoader.getInstance(source).loadSpec();
const specsLoader = new SpecsLoader(source);
const spec = await specsLoader.loadSpec();
const version = spec.openapi;

if (version.startsWith("3.0")) {
Expand Down

0 comments on commit e60436a

Please sign in to comment.