Skip to content

Commit 2dd0327

Browse files
committed
Expose filenaming mechanism
1 parent b624927 commit 2dd0327

6 files changed

Lines changed: 1242 additions & 1195 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
/test/_fixtures/https%3A__openstax.org_*_GET_*_body.raw
44
/test/_fixtures/https%3A__openstax.org_*_GET_*_options.json
55

6+
/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.raw
7+
/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.json
8+
69
/browser-bundle.js
710
/browser-bundle.js.map

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ fetch('https://weedmaps.com/sitemap') // <-- This will be ignored from vcr
8484
})
8585
```
8686

87+
## How do I define custom fixture filenames?
88+
89+
There are 3 find/replace variables exposed `[url]`, `[method]`, and `[hash]`.
90+
91+
```js
92+
// import fetch from 'fetch';
93+
import fetch from 'fetch-vcr';
94+
95+
fetch.configure({
96+
fixtureName: '[url]_[method]_[hash]', // <-- This is the default value
97+
})
98+
```
99+
87100
## Jest Setup
88101

89102
Just add the following to `package.json`:

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var CONFIGURATION = {
2121
mode: VCR_MODE,
2222
fixturePath: './_fixtures',
2323
headerBlacklist: ['authorization', 'user-agent'], // These need to be lowercase
24-
ignoreUrls: [] // regex of urls to ignore
24+
ignoreUrls: [], // regex of urls to ignore
25+
fixtureName: '[url]_[method]_[hash]'
2526
}
2627

2728
function debug(url, message) {
@@ -91,13 +92,19 @@ function buildHash(url, args) {
9192
}
9293

9394
function buildFilenamePrefix(url, args, hash) {
94-
args = args || { }
9595
var [baseUrl, query] = url.split('?')
9696
url = escape(baseUrl).replace(/\//g, '_')
97-
var method = args.method || 'GET'
97+
url += query ? '_' + hashCode(query) : ''
98+
99+
var method = (args || { }).method || 'GET'
98100
method = method.toUpperCase()
99-
var paramsHash = query ? '_' + hashCode(query) : ''
100-
return url + paramsHash + '_' + method + '_' + hash
101+
102+
var fixtureName = CONFIGURATION.fixtureName
103+
104+
return fixtureName
105+
.replace(/\[url\]/g, url)
106+
.replace(/\[method\]/g, method)
107+
.replace(/\[hash\]/g, hash)
101108
}
102109

103110
function buildOptionsFilename(url, args, hash) {
@@ -244,6 +251,7 @@ fetchVCR.configure = function (config) {
244251
CONFIGURATION.mode = VCR_MODE || config.mode
245252
CONFIGURATION.fixturePath = config.fixturePath || CONFIGURATION.fixturePath
246253
CONFIGURATION.ignoreUrls = config.ignoreUrls || CONFIGURATION.ignoreUrls
254+
CONFIGURATION.fixtureName = config.fixtureName || CONFIGURATION.fixtureName
247255
if (config.headerBlacklist) {
248256
CONFIGURATION.headerBlacklist = []
249257
config.headerBlacklist.forEach(function (key) {

0 commit comments

Comments
 (0)