-
Notifications
You must be signed in to change notification settings - Fork 8
/
record-http-mocks.js
executable file
·102 lines (96 loc) · 2.48 KB
/
record-http-mocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
/**
* Inspired by https://github.com/topheman/react-es6-redux/blob/master/bin/nock/call-nock.js
*
* This script records the http requests (requests / response with body and headers)
* so that they could be mocked in development mode or tests.
* This task is automated, this way, you don't have to bother to do it manually ! ;-)
*/
// we are importing ESM modules from the front
require = require("esm")(module); // eslint-disable-line
require("dotenv").config({ path: ".env.production" }); // eslint-disable-line
const path = require("path");
const { recordAll } = require("./lib/record-http-mocks");
const {
TARGET_API_NPM_API,
TARGET_API_NPM_REGISTRY,
TARGET_API_NPMS_IO
} = require("../src/services/apis/constants");
const recordConfig = {
[TARGET_API_NPM_REGISTRY]: {
config: {
baseURL: process.env.REACT_APP_NPM_REGISTRY_API_BASE_URL,
headers: {
origin: "https://github.io"
}
},
requests: [
"/react",
"/@angular%2Fcore",
{
url: "/-/v1/search?text=react",
match: "/-/v1/search\\?text=react(\\.*)"
},
{
url: "/-/v1/search?text=%40angular",
match: "/-/v1/search\\?text=%40angular(\\.*)"
}
],
outputPath: path.join(
__dirname,
"..",
"src",
"services",
"apis",
"mocks",
`${TARGET_API_NPM_REGISTRY}.fixtures.json`
)
},
[TARGET_API_NPM_API]: {
config: {
baseURL: process.env.REACT_APP_NPM_API_BASE_URL
},
requests: [
"/downloads/range/last-year/react",
"/downloads/range/last-year/@angular%2Fcore"
],
outputPath: path.join(
__dirname,
"..",
"src",
"services",
"apis",
"mocks",
`${TARGET_API_NPM_API}.fixtures.json`
)
},
[TARGET_API_NPMS_IO]: {
config: {
baseURL: process.env.REACT_APP_NPMS_IO_API_BASE_URL
},
requests: [
"/v2/search?q=react",
"/v2/search?q=%40angular",
{
url: "/v2/search/suggestions?q=react",
match: "/v2/search/suggestions\\?q=react(\\.*)"
},
{
url: "/v2/search/suggestions?q=%40angular",
match: "/v2/search/suggestions\\?q=%40angular(\\.*)"
}
],
outputPath: path.join(
__dirname,
"..",
"src",
"services",
"apis",
"mocks",
`${TARGET_API_NPMS_IO}.fixtures.json`
)
}
};
recordAll(recordConfig)
.then(() => console.log("Recording success"))
.catch(e => console.log("[ERROR]", e.message));