Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simpler-mocks

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpler-mocks - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

3

package.json
{
"name": "simpler-mocks",
"version": "1.4.0",
"version": "1.5.0",
"description": "REST API mock server made simple. Runs on Node with YAML and JSON mock definitions.",

@@ -44,2 +44,3 @@ "keywords": [

"chance": "^1.0.18",
"chokidar": "^3.0.0",
"globby": "^9.2.0",

@@ -46,0 +47,0 @@ "js-yaml": "^3.13.1",

@@ -61,3 +61,3 @@ # Simpler Mocks

const server = require('simpler-mocks')
server('./samples', 8080)
server('./samples', { port: 8080, watch: true })
```

@@ -72,4 +72,4 @@

directory The directory where mock api definition files are
located. Glob syntax supported. Defaults to the
current working directory.
located. Glob syntax supported. Defaults to the
current working directory.

@@ -80,4 +80,5 @@ Optional arguments:

--port PORT, -p PORT The port to run the server on. Defaults to a random
open port if none is set.
open port if none is set.
--silent, -s Hides http access logs from the terminal.
--watch, -w Watch the base directory for changes
```

@@ -84,0 +85,0 @@

@@ -6,3 +6,3 @@ // expected objects

// request: koa.request,
// fixtures: {
// : {
// file: '/path/to/file',

@@ -20,2 +20,5 @@ // new: true,

module.exports = {}
module.exports = {
fixtures: {},
mocks: []
}

@@ -30,2 +30,7 @@ #!/usr/bin/env node

cli.addArgument(['--watch', '-w'], {
action: 'storeTrue',
help: 'Watch the base directory for changes'
})
cli.addArgument(['directory'], {

@@ -41,2 +46,2 @@ nargs: '?',

const mocksDirectory = path.resolve(process.cwd(), args.directory)
app(mocksDirectory, args.port, args.silent, args.verbose)
app(mocksDirectory, args)
const path = require('path')
const globby = require('globby')
const chokidar = require('chokidar')
const cache = require('./cache')
const server = require('./server')
const util = require('./util')
async function app(directory = './', port = 0, silent = false, verbose = false) {
async function app(directory = './', { port, silent, verbose, watch }) {
const cwd = path.dirname(require.main.filename)

@@ -14,2 +16,10 @@ cache.mocksDirectory = path.resolve(cwd, directory)

if (watch) {
chokidar
.watch('**/*.(yml|yaml|json)', { cwd: cache.mocksDirectory, ignoreInitial: true })
.on('add', onAdd)
.on('change', addFixture)
.on('unlink', onRemove)
}
return server(port, silent)

@@ -19,25 +29,96 @@ }

/**
* Get a list of all the fixtures and saves it to the cache
* Chokidar add event handler
* @param {string} file
*/
async function findFixtures() {
const glob = path.resolve(cache.mocksDirectory, '__fixtures__', '*.(yml|yaml)')
const files = await globby(glob)
function onAdd(file) {
if (file.startsWith('__fixtures__')) {
addFixture(file)
} else {
addMock(file)
}
}
const fixtures = files.reduce((result, file) => {
const [fileName, key] = file.match(/(\w+)\.ya?ml/i)
/**
* Chokidar unlink event handler
* @param {string} file
*/
function onRemove(file) {
if (file.startsWith('__fixtures__')) {
removeFixture(file)
} else {
removeMock(file)
}
}
cache.verbose && console.log('Found Fixture: ', fileName)
result[key] = {
file,
new: true
}
/**
* Adds fixtures to the cache, or marks an existing fixture as new so it can be reloaded
* @param {string} file
*/
function addFixture(file) {
if (!file.startsWith('__fixtures__')) return
return result
}, {})
util.log('Added Fixture: ', file)
// TODO: watch the fixture directory for changes
cache.fixtures = fixtures
const match = file.match(/(\w+)\.ya?ml/i)
const key = match[1]
const filePath = path.resolve(cache.mocksDirectory, file)
cache.fixtures[key] = {
file: filePath,
new: true
}
}
/**
* Removes a fixture from the cache
* @param {string} file
*/
function removeFixture(file) {
util.log('Removed Fixture: ', file)
const match = file.match(/(\w+)\.ya?ml/i)
const key = match[1]
delete cache.fixtures[key]
}
/**
* Adds a mock to the cache
* @param {string} file
*/
function addMock(file) {
util.log('Added Mock: ', file)
const match = file.match(/(.*)\.(ya?ml|json)/i)
const pattern = match[1].replace(/[^\w]_(?=\/|\.)/g, '/*')
const filePath = path.resolve(cache.mocksDirectory, file)
cache.mocks.push({ file: filePath, pattern })
}
/**
* Removes a mock from cache
* @param {string} file
*/
function removeMock(file) {
util.log('Removed Mock: ', file)
const index = cache.mocks.find((mock) => mock.file === file)
if (index > -1) {
cache.mocks.splice(index, 1)
}
}
/**
* Get a list of all the fixtures and saves it to the cache
*/
async function findFixtures() {
const glob = '__fixtures__/*.(yml|yaml)'
const files = await globby(glob, { cwd: cache.mocksDirectory })
files.forEach(addFixture)
}
/**
* Get a list of all the mocks and url patterns and saves it to the cache

@@ -49,17 +130,5 @@ */

const config = mocks.map((mock) => {
const match = mock.match(/(.*)\.(ya?ml|json)/i)
let [file, url] = match
const pattern = url.replace(/[^\w]_(?=\/|\.)/g, '/*')
cache.verbose && console.log('Found Mock: ', file)
file = path.resolve(cache.mocksDirectory, mock)
return { file, pattern }
})
// TODO: watch the mocks directory for changes
cache.mocks = config
mocks.forEach(addMock)
}
module.exports = app

@@ -36,3 +36,3 @@ const fs = require('fs')

if (match) {
cache.verbose && console.log('Matched file: ', match.file)
util.log('Matched file: ', match.file)
return await loadYamlFile(match.file)

@@ -39,0 +39,0 @@ }

@@ -0,1 +1,3 @@

const cache = require('./cache')
/**

@@ -88,3 +90,3 @@ * Parses a string of options into an object or its true type

// the Any class
// the Any class to be used for the custom !any tag
// type = boolean|string|number|array

@@ -97,2 +99,11 @@ function Any(type) {

/**
* Verbose logging utility function
*/
function log() {
if (cache.verbose) {
console.log.apply(console, arguments)
}
}
module.exports = {

@@ -103,3 +114,4 @@ Any,

keysToLower,
log,
parseOptions
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc