From c90edc8d7245d403c931671c2357b0924a3d337e Mon Sep 17 00:00:00 2001 From: "William Bernting (whf962)" Date: Fri, 16 Aug 2019 10:52:50 +0200 Subject: [PATCH 1/2] clarify variables available inside the .js files --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 3b5ef9e..e28dd47 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,13 @@ Dynamic values of headers can be filled with valid JS statements such as: X-Subject-Token: #header ${require('uuid/v4')()}; ``` +Because of how the javascript files are evaluated, you can also access the following variables inside the .js file: +* `request` +* `context` +* `value` + +Relative `require()` import statements for relative files will not work. + ## Custom response status You can specify response status (200, 201, 404. etc.) depending on request parameters. To do this, you need to use `#import './code.js';` in first line of your mock file: From 5427059f9a320c8a014b9e54718f0232424a8139 Mon Sep 17 00:00:00 2001 From: William Bernting Date: Fri, 16 Aug 2019 12:40:10 +0200 Subject: [PATCH 2/2] Update README.md Add how to require() with context --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e28dd47..8708cee 100644 --- a/README.md +++ b/README.md @@ -290,7 +290,17 @@ Because of how the javascript files are evaluated, you can also access the follo * `context` * `value` -Relative `require()` import statements for relative files will not work. +Relative `require()` import statements for relative files will not work out-of-the-box, but since `context` contains the path to the directory where your .js file is, you can do the following hack to get them to work. + +```js +/* globals request, context, path */ + +// actually path is already available via the eval'd scope +// var path = require('path'); +var fooFunc = require(path.join(context, 'foo.js')); + +module.exports = fooFunc(request).bar; +``` ## Custom response status