@scalar/oas-utils
Advanced tools
Comparing version 0.0.3 to 0.0.4
# @scalar/oas-utils | ||
## 0.0.4 | ||
### Patch Changes | ||
- cecf074: Migrate to oas utils for basic spec operations | ||
## 0.0.3 | ||
@@ -4,0 +10,0 @@ |
/** Fetches an OAS spec file from a given URL. */ | ||
export declare const fetchSpecFromUrl: (url: string, proxy?: string, parseObject?: boolean) => Promise<string | import("./types").AnyObject>; | ||
export declare function fetchSpecFromUrl(url: string, proxy?: string): Promise<string>; | ||
//# sourceMappingURL=fetch-spec.d.ts.map |
@@ -1,4 +0,4 @@ | ||
export * from './fetch-spec'; | ||
export { fetchSpecFromUrl } from './fetch-spec'; | ||
export * from './parse'; | ||
export * from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
import { parse, stringify } from 'yaml'; | ||
const yaml = { | ||
parse, | ||
/** Parse and throw if the return value is not an object */ | ||
parse: (val) => { | ||
const yamlObject = parse(val); | ||
if (typeof yamlObject !== "object") | ||
throw Error("Invalid YAML object"); | ||
return yamlObject; | ||
}, | ||
/** Parse and return a fallback on failure */ | ||
parseSafe(val, fallback) { | ||
try { | ||
return yaml.parse(val); | ||
} catch (err) { | ||
return typeof fallback === "function" ? fallback(err) : fallback; | ||
} | ||
}, | ||
stringify | ||
}; | ||
const loadJsonOrYaml = (value) => { | ||
if (typeof value === "string") { | ||
const json = { | ||
/** Parse and throw if the return value is not an object */ | ||
parse: (val) => { | ||
const jsonObject = JSON.parse(val); | ||
if (typeof jsonObject !== "object") | ||
throw Error("Invalid JSON object"); | ||
return jsonObject; | ||
}, | ||
/** Parse and return a fallback on failure */ | ||
parseSafe(val, fallback) { | ||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
if (value.length > 0 && ["{", "["].includes(value[0])) { | ||
throw error; | ||
} | ||
return yaml.parse(value); | ||
return json.parse(val); | ||
} catch (err) { | ||
return typeof fallback === "function" ? fallback(err) : fallback; | ||
} | ||
} | ||
return value; | ||
}, | ||
stringify: (val) => JSON.stringify(val) | ||
}; | ||
function loadJsonOrYamlString(value) { | ||
const isJsonString = (value) => { | ||
if (typeof value !== "string") | ||
return false; | ||
return !!json.parseSafe(value, false); | ||
}; | ||
const transformToJson = (value) => { | ||
return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value))); | ||
}; | ||
function formatJsonOrYamlString(value) { | ||
const trimmed = value.trim(); | ||
@@ -30,4 +57,17 @@ if (trimmed[0] !== "{") | ||
} | ||
const parseJsonOrYaml = (value) => { | ||
if (typeof value !== "string") | ||
return value; | ||
const jsonObject = json.parseSafe(value, null); | ||
if (jsonObject) | ||
return jsonObject; | ||
if (value.length > 0 && ["{", "["].includes(value[0])) { | ||
throw Error("Invalid JSON or YAML"); | ||
} | ||
return yaml.parseSafe(value, (err) => { | ||
throw Error(err); | ||
}); | ||
}; | ||
const fetchSpecFromUrl = async (url, proxy, parseObject = false) => { | ||
async function fetchSpecFromUrl(url, proxy) { | ||
const response = proxy ? await fetch(proxy, { | ||
@@ -50,5 +90,5 @@ method: "POST", | ||
const payload = proxy ? String((await response.json()).data) : await response.text(); | ||
return parseObject ? loadJsonOrYaml(payload) : loadJsonOrYamlString(payload); | ||
}; | ||
return formatJsonOrYamlString(payload); | ||
} | ||
export { fetchSpecFromUrl, loadJsonOrYaml, loadJsonOrYamlString, yaml }; | ||
export { fetchSpecFromUrl, formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml }; |
@@ -1,11 +0,33 @@ | ||
import { parse, stringify } from 'yaml'; | ||
import { stringify } from 'yaml'; | ||
import { type AnyObject } from './types'; | ||
type PrimitiveOrObject = object | string | null | number | boolean | undefined; | ||
/** Yaml handling with optional safeparse */ | ||
export declare const yaml: { | ||
parse: typeof parse; | ||
/** Parse and throw if the return value is not an object */ | ||
parse: (val: string) => AnyObject; | ||
/** Parse and return a fallback on failure */ | ||
parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): AnyObject | T; | ||
stringify: typeof stringify; | ||
}; | ||
/** Parses a JSON or Yaml object or string into an object */ | ||
export declare const loadJsonOrYaml: (value: string | AnyObject) => AnyObject; | ||
/** JSON handling with optional safeparse */ | ||
export declare const json: { | ||
/** Parse and throw if the return value is not an object */ | ||
parse: (val: string) => AnyObject; | ||
/** Parse and return a fallback on failure */ | ||
parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): AnyObject | T; | ||
stringify: (val: object) => string; | ||
}; | ||
/** | ||
* Check if value is a valid JSON string | ||
*/ | ||
export declare const isJsonString: (value?: any) => boolean; | ||
/** | ||
* This helper is used to transform the content of the swagger file to JSON, even it was YAML. | ||
*/ | ||
export declare const transformToJson: (value: string) => string; | ||
/** Validates a JSON string if provided. Otherwise returns the raw Yaml */ | ||
export declare function loadJsonOrYamlString(value: string): string; | ||
export declare function formatJsonOrYamlString(value: string): string; | ||
/** Parse JSON or YAML into an object */ | ||
export declare const parseJsonOrYaml: (value: string | AnyObject) => AnyObject; | ||
export {}; | ||
//# sourceMappingURL=parse.d.ts.map |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"engines": { | ||
@@ -52,3 +52,3 @@ "node": ">=18" | ||
"vitest": "^1.2.2", | ||
"@scalar/build-tooling": "0.0.3" | ||
"@scalar/build-tooling": "0.0.4" | ||
}, | ||
@@ -55,0 +55,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8967
128