UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

41 lines (40 loc) 1.64 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 { validateAsyncApi } from './validateAsyncApi.js'; export const getAsyncApiDocumentFromUrl = (url) => __awaiter(void 0, void 0, void 0, function* () { try { new URL(url); } catch (error) { throw new Error(`Invalid URL: ${url}`); } let response; try { response = yield fetch(url); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error) { throw new Error(`Failed to fetch AsyncAPI document from ${url}`); } let data; try { data = yield response.text(); } catch (error) { throw new Error(`Failed to read response from ${url}`); } const { document: asyncApiDocument, errorMessage } = yield validateAsyncApi(data); if (!asyncApiDocument) { throw new Error(`Could not load AsyncAPI document from ${url}:\n ${errorMessage}`); } return asyncApiDocument; });