UNPKG

unmock-core

Version:

[![npm](https://img.shields.io/npm/v/unmock-core.svg)][npmjs] [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codecov.io/gh/unmock/unmock-js/branch/dev/graph/badge.svg)](h

40 lines 1.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Either_1 = require("fp-ts/lib/Either"); const js_yaml_1 = __importDefault(require("js-yaml")); const loas3_1 = __importDefault(require("loas3")); const service_1 = require("./service"); class ServiceParser { parse(serviceDef) { const serviceFiles = serviceDef.serviceFiles; const matchingFiles = serviceFiles.filter((serviceDefFile) => ServiceParser.KNOWN_FILENAMES.test(serviceDefFile.basename)); if (matchingFiles.length === 0) { throw new Error(`Cannot find known service specification from: ${JSON.stringify(serviceFiles)}`); } const serviceFile = matchingFiles[0]; const contents = serviceFile.contents instanceof Buffer ? serviceFile.contents.toString("utf-8") : serviceFile.contents; const schema = loas3_1.default(js_yaml_1.default.safeLoad(contents)); if (Either_1.isLeft(schema)) { throw new Error([ "The following errors occured while parsing your loas3 schema", ...schema.left.map(i => ` ${i.message}`), ].join("\n")); } if (schema === undefined) { throw new Error(`Could not load schema from ${contents}`); } const name = serviceDef.directoryName; return new service_1.Service({ name, schema: schema.right, }); } } ServiceParser.KNOWN_FILENAMES = /^(?:index|spec|openapi)\.ya?ml$/i; exports.ServiceParser = ServiceParser; //# sourceMappingURL=parser.js.map