UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

35 lines (34 loc) 1.57 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Parser } from '@asyncapi/parser'; import yaml from 'js-yaml'; export const getAsyncApiDocumentFromUrl = (url) => __awaiter(void 0, void 0, void 0, function* () { try { new URL(url); } catch (error) { throw new Error(`Invalid URL: ${url}`); } const response = yield fetch(url); const data = yield response.text(); const asyncApiDocument = yaml.load(data); if (!asyncApiDocument) { throw new Error(`Could not load AsyncAPI document from ${url}`); } const parser = new Parser(); const diagnostic = yield parser.validate(asyncApiDocument); if (diagnostic.length > 0) { const firstDiagnosis = diagnostic[0]; if (firstDiagnosis) { throw new Error(`This AsyncAPI document is invalid, ${firstDiagnosis.path}, ${firstDiagnosis.code}`); } } return asyncApiDocument; });