Supported JavaScript libraries
To assist in streamlining the process of creating mock server definitions, we have included a selection of useful third-party JavaScript libraries within the mock server runtime. These libraries have been pre-installed and are automatically injected into the JavaScript context, eliminating the need for you to manually include them using the require() function. Simply begin utilizing them in your code.
The list of supported libraries are:
- lo-dash v4.17.21 (general utility)
- momentjs v2.30.1 (time and dates)
- faker 5.5.3 (data generator)
- ajv 6.10.0 (JSON schema validation)
- validator 13.12.0 (validation helper)
If you have a suggestion for a library that is not currently available, contact us and suggest it.
mock.define("/example", "GET", function (req, res) {
// faker lib
var myState = faker.lorem.text()
faker.lorem.slug() // 'dolores-illo-est'
faker.lorem.slug(5) // 'delectus-totam-iusto-itaque-placeat'
faker.lorem.slug({ min: 1, max: 3 }) // 'illo-ratione'
// ldash lib
console.log(_.eq("Geeks", "Geeks" ));
// use moment lib
moment().format('MMMM Do YYYY, h:mm:ss a'); // August 26th 2024, 5:35:05 pm
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Aug 26th 24
moment().format('YYYY [escaped] YYYY'); // 2024 escaped 2024
moment().format();
moment("20111031", "YYYYMMDD").fromNow(); // 13 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 12 years ago
moment().startOf('day').fromNow(); // 18 hours ago
moment().endOf('day').fromNow(); // in 6 hours
moment().startOf('hour').fromNow();
// Ajv lib
const ajv = new Ajv()
const schema = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"}
},
required: ["foo"],
additionalProperties: false
}
const data = {foo: 1, bar: "abc"}
const valid = ajv.validate(schema, data)
if (!valid) console.log("invalid")
// validator js
var result = validator.isEmail('foo@bar.com');
console.log(result ? "isEmail":"not email");
return res.json(myState || {})
});