Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify variables available inside the .js files in README.md #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ 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 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

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:
Expand Down