simpler-mocks
Advanced tools
Comparing version 1.10.0 to 1.11.0
{ | ||
"name": "simpler-mocks", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "REST API mock server made simple. Runs on Node with YAML and JSON mock definitions.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -106,11 +106,12 @@ const fs = require('fs') | ||
// save some data for later use with regexp (not implemented) | ||
// format: !save [key1, key2, regexp] | ||
// const Save__array = new yaml.Type('!save', { | ||
// kind: 'sequence', | ||
// resolve: (data) => data.length > 2, // can only have one key | ||
// construct: (data) => { | ||
// const value = data.pop() | ||
// return new types.Save(data, value) | ||
// } | ||
// }) | ||
// format: !save [key1, key2, matcher] | ||
const Save__array = new yaml.Type('!save', { | ||
kind: 'sequence', | ||
resolve: (data) => data && data.length > 1, // need a key and a matcher | ||
construct: (data) => { | ||
const value = data.pop() | ||
const key = data.length == 1 ? data[0] : data | ||
return new types.Save(key, value) | ||
} | ||
}) | ||
@@ -159,2 +160,3 @@ // retrieve persisted data from storage | ||
Request, | ||
Save__array, | ||
Save__string, | ||
@@ -161,0 +163,0 @@ Save__object |
@@ -78,4 +78,17 @@ const cache = require('./cache') | ||
const matches = this.pattern.exec(value) | ||
const result = !!matches | ||
// if we found a match, save as the value of this instance | ||
if (result) { | ||
if (matches.length > 1) { | ||
// If there were groups in the regexp, store it. | ||
// if only one group, save that as single value, otherwise save just the group results | ||
this.data = matches.length === 2 ? matches[1] : matches.slice(1) | ||
} else { | ||
// when there's no grouping, just save wheter the regexp was successful | ||
this.data = result | ||
} | ||
} | ||
// if we found a match, save as the value of this instance | ||
if (matches && matches.length > 1) { | ||
@@ -90,7 +103,7 @@ this.data = matches.length === 2 ? matches[1] : matches.slice(1) | ||
class Save extends CustomType { | ||
constructor(key, value) { | ||
constructor(keys, value) { | ||
value = value || new Any() | ||
super(value) | ||
this.key = key | ||
this.keys = keys | ||
} | ||
@@ -104,4 +117,12 @@ | ||
// the server code is responsible for storing it to a permenent location | ||
const isCustom = this.data instanceof CustomType | ||
cache._storage[this.key] = isCustom ? this.data : result | ||
const isCustomType = this.data instanceof CustomType | ||
const dataToStore = isCustomType ? this.data : result | ||
if (Array.isArray(this.keys)) { | ||
this.keys.forEach((key, index) => { | ||
cache._storage[key] = new StoredIndexItem(index, dataToStore) | ||
}) | ||
} else { | ||
cache._storage[this.keys] = dataToStore | ||
} | ||
} | ||
@@ -113,2 +134,34 @@ | ||
/** | ||
* Custom data type used in conjuction with save, in order to | ||
* point data to a specifc index. | ||
*/ | ||
class StoredIndexItem extends CustomType { | ||
/** | ||
* | ||
* @param {*} index - the index in the array where the value we want is stored. | ||
* @param {*} data - the customtype item we want to store | ||
*/ | ||
constructor(index, data) { | ||
/* istanbul ignore next */ | ||
if (!data || isNaN(index)) { | ||
throw new Error('Stored index items need an index and a value') | ||
} | ||
super(data) | ||
this.index = index | ||
} | ||
toJSON() { | ||
const isCustomType = this.data instanceof CustomType | ||
const data = isCustomType ? this.data.toJSON() : this.data | ||
if (Array.isArray(data)) { | ||
return data[this.index] | ||
} | ||
console.warn(`Could not access index '${this.index} of non array.`) | ||
return data | ||
} | ||
} | ||
class Get extends CustomType { | ||
@@ -124,3 +177,3 @@ constructor(key, defaultVal) { | ||
// temp storage (current session) first, then the persisted storage | ||
let item = cache._storage[this.key] || cache.storage[this.key] || this.default || null | ||
let item = cache._storage[this.key] || cache.storage[this.key] | ||
@@ -130,3 +183,3 @@ if (item && item instanceof CustomType) { | ||
} | ||
return item | ||
return item || this.default || null | ||
} | ||
@@ -133,0 +186,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36093
876