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

39 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = require("debug"); const Either_1 = require("fp-ts/lib/Either"); const jsYaml = require("js-yaml"); const loas3_1 = require("loas3"); const serviceCore_1 = require("./service/serviceCore"); const debugLog = debug_1.default("unmock:service-parser"); class ServiceParser { static 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)}`); } debugLog(`Found ${matchingFiles.length} service specifications for folder ${serviceDef.directoryName}`); const serviceFile = matchingFiles[0]; debugLog(`Parsing service specification ${serviceFile.basename}`); const contents = serviceFile.contents instanceof Buffer ? serviceFile.contents.toString("utf-8") : serviceFile.contents; const schema = loas3_1.default(jsYaml.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")); } const name = serviceDef.directoryName; return new serviceCore_1.ServiceCore({ absPath: serviceDef.absolutePath, name, schema: schema.right, }); } } exports.ServiceParser = ServiceParser; ServiceParser.KNOWN_FILENAMES = /^(?:index|spec|openapi)\.ya?ml$/i; //# sourceMappingURL=parser.js.map