ty-fetch 0.0.2-beta.4 → 0.0.2-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,12 +32,16 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  exports.specCache = exports.KNOWN_SPECS = void 0;
37
40
  exports.registerSpecs = registerSpecs;
38
41
  exports.ensureSpec = ensureSpec;
39
42
  exports.ensureSpecSync = ensureSpecSync;
40
43
  exports.fetchSpecForDomain = fetchSpecForDomain;
44
+ const js_yaml_1 = __importDefault(require("js-yaml"));
41
45
  exports.KNOWN_SPECS = {
42
46
  "api.stripe.com": "https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json",
43
47
  "petstore3.swagger.io": "https://petstore3.swagger.io/api/v3/openapi.json",
@@ -131,12 +135,20 @@ async function fetchSpecForDomain(domain, log) {
131
135
  return entry;
132
136
  }
133
137
  }
138
+ function parseSpec(data, source) {
139
+ const isYaml = /\.ya?ml$/i.test(source) ||
140
+ (!source.endsWith(".json") && !data.trimStart().startsWith("{"));
141
+ if (isYaml) {
142
+ return js_yaml_1.default.load(data);
143
+ }
144
+ return JSON.parse(data);
145
+ }
134
146
  async function fetchSpec(urlOrPath) {
135
147
  // Local file path — read from disk
136
148
  if (!urlOrPath.startsWith("http://") && !urlOrPath.startsWith("https://")) {
137
149
  const fs = require("fs");
138
150
  const data = fs.readFileSync(urlOrPath, "utf-8");
139
- return JSON.parse(data);
151
+ return parseSpec(data, urlOrPath);
140
152
  }
141
153
  // Remote URL — fetch via HTTPS/HTTP
142
154
  const mod = urlOrPath.startsWith("https://") ? await Promise.resolve().then(() => __importStar(require("https"))) : await Promise.resolve().then(() => __importStar(require("http")));
@@ -150,10 +162,10 @@ async function fetchSpec(urlOrPath) {
150
162
  res.on("data", (chunk) => (data += chunk));
151
163
  res.on("end", () => {
152
164
  try {
153
- resolve(JSON.parse(data));
165
+ resolve(parseSpec(data, urlOrPath));
154
166
  }
155
- catch {
156
- reject(new Error("Invalid JSON"));
167
+ catch (err) {
168
+ reject(new Error(`Failed to parse spec: ${err}`));
157
169
  }
158
170
  });
159
171
  res.on("error", reject);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ty-fetch",
3
- "version": "0.0.2-beta.4",
3
+ "version": "0.0.2-beta.5",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "exports": {
@@ -31,7 +31,11 @@
31
31
  "prepublishOnly": "cp base.d.ts index.d.ts && npm run build"
32
32
  },
33
33
  "devDependencies": {
34
+ "@types/js-yaml": "^4.0.9",
34
35
  "@types/node": "^25.5.2",
35
36
  "typescript": "^5.5.0"
37
+ },
38
+ "dependencies": {
39
+ "js-yaml": "^4.1.1"
36
40
  }
37
41
  }