@tryghost/express-test 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/agent.js +37 -11
- package/package.json +5 -5
package/lib/agent.js
CHANGED
|
@@ -3,14 +3,19 @@ const ExpectRequest = require('./expect-request');
|
|
|
3
3
|
const {RequestOptions} = require('./request');
|
|
4
4
|
const {normalizeURL} = require('./utils');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef AgentOptions
|
|
8
|
+
* @param {string} [baseUrl] - the base URL path for the API query
|
|
9
|
+
* @param {Object} [queryParams] - custom query params to append to each request
|
|
10
|
+
* @param {Object} [headers] - any default headers to append to each request
|
|
11
|
+
* @param {Object} [body] - a default body to append to each request
|
|
12
|
+
* */
|
|
13
|
+
|
|
6
14
|
class Agent {
|
|
7
15
|
/**
|
|
8
16
|
*
|
|
9
17
|
* @param {Object} app instance of Express app
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {string} [defaults.baseUrl]
|
|
12
|
-
* @param {Object} [defaults.headers]
|
|
13
|
-
* @param {Object} [defaults.queryParams] - custom query params to append to each request
|
|
18
|
+
* @param {AgentOptions} [defaults]
|
|
14
19
|
*/
|
|
15
20
|
constructor(app, defaults = {}) {
|
|
16
21
|
this.app = app;
|
|
@@ -19,20 +24,31 @@ class Agent {
|
|
|
19
24
|
this.jar = new CookieJar();
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param {String} url
|
|
30
|
+
* @param {object} [urlOptions]
|
|
31
|
+
* @param {string} [urlOptions.baseUrl] - the base URL path for the API query
|
|
32
|
+
* @param {Object} [urlOptions.queryParams] - custom query params to append to each request
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
_makeUrl(url, urlOptions = {}) {
|
|
23
36
|
let processedURL = url;
|
|
37
|
+
const baseUrl = urlOptions.baseUrl || this.defaults.baseUrl || null;
|
|
24
38
|
|
|
25
|
-
if (
|
|
26
|
-
processedURL = `/${
|
|
39
|
+
if (baseUrl) {
|
|
40
|
+
processedURL = `/${baseUrl}/${processedURL}`.replace(/(^|[^:])\/\/+/g, '$1/');
|
|
27
41
|
}
|
|
28
42
|
|
|
29
43
|
processedURL = normalizeURL(processedURL);
|
|
30
44
|
|
|
31
|
-
|
|
45
|
+
const queryParams = Object.assign({}, this.defaults.queryParams, urlOptions.queryParams);
|
|
46
|
+
|
|
47
|
+
if (Object.keys(queryParams).length > 0) {
|
|
32
48
|
const searchParams = new URLSearchParams();
|
|
33
49
|
|
|
34
|
-
for (const key in
|
|
35
|
-
searchParams.append(key,
|
|
50
|
+
for (const key in queryParams) {
|
|
51
|
+
searchParams.append(key, queryParams[key]);
|
|
36
52
|
}
|
|
37
53
|
|
|
38
54
|
if (processedURL.includes('?')) {
|
|
@@ -45,6 +61,13 @@ class Agent {
|
|
|
45
61
|
return processedURL;
|
|
46
62
|
}
|
|
47
63
|
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param {string}} method - HTTP method
|
|
67
|
+
* @param {string} url - url to request
|
|
68
|
+
* @param {AgentOptions} options
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
48
71
|
_mergeOptions(method, url, options = {}) {
|
|
49
72
|
// It doesn't make sense to call this method without these properties
|
|
50
73
|
if (!method) {
|
|
@@ -55,9 +78,12 @@ class Agent {
|
|
|
55
78
|
throw new Error('_mergeOptions cannot be called without a url'); /* eslint-disable-line no-restricted-syntax */
|
|
56
79
|
}
|
|
57
80
|
|
|
81
|
+
// urlOptions
|
|
82
|
+
const {baseUrl, queryParams} = options;
|
|
83
|
+
|
|
58
84
|
return new RequestOptions({
|
|
59
85
|
method,
|
|
60
|
-
url: this._makeUrl(url),
|
|
86
|
+
url: this._makeUrl(url, Object.assign({}, {baseUrl, queryParams})),
|
|
61
87
|
headers: Object.assign({}, this.defaults.headers, options.headers),
|
|
62
88
|
// Set this to an empty object for ease, as express.json will do this anyway
|
|
63
89
|
body: Object.assign({}, this.defaults.body, options.body)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/express-test",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/framework/tree/main/packages/express-test",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"c8": "7.11.
|
|
23
|
-
"express": "4.
|
|
22
|
+
"c8": "7.11.2",
|
|
23
|
+
"express": "4.18.0",
|
|
24
24
|
"express-session": "1.17.2",
|
|
25
25
|
"mocha": "9.2.2",
|
|
26
26
|
"sinon": "13.0.2"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@tryghost/jest-snapshot": "^0.3.
|
|
29
|
+
"@tryghost/jest-snapshot": "^0.3.13",
|
|
30
30
|
"cookiejar": "^2.1.3",
|
|
31
31
|
"reqresnext": "^1.7.0"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "944b81c690bc66f703c8cf36a310e51ce5393b9f"
|
|
34
34
|
}
|