Skip to content

Commit 461ca3c

Browse files
committed
base agent can optionally return raw req object
1 parent bde184c commit 461ca3c

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Agent.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,19 @@ export default class Agent {
5959
* @parma {Object} context the invocation context, describing the tool and project.
6060
* @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body }
6161
*/
62-
request({ uri, method, data = undefined, auth, query = undefined, form = undefined, files = undefined, context = undefined }) {
62+
request({
63+
uri,
64+
method,
65+
data = undefined,
66+
auth,
67+
query = undefined,
68+
form = undefined,
69+
files = undefined,
70+
context = undefined,
71+
raw = false
72+
}) {
6373
const requestFiles = this._sanitizeFiles(files);
64-
return this._request({ uri, method, data, auth, query, form, context, files: requestFiles });
74+
return this._request({ uri, method, data, auth, query, form, context, files: requestFiles, raw });
6575
}
6676

6777
/**
@@ -76,8 +86,12 @@ export default class Agent {
7686
* @param {Object} context the invocation context
7787
* @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body }
7888
*/
79-
_request({ uri, method, data, auth, query, form, files, context }) {
89+
_request({ uri, method, data, auth, query, form, files, context, raw }) {
8090
const req = this._buildRequest({ uri, method, data, auth, query, form, context, files });
91+
92+
if (raw){
93+
return req;
94+
}
8195
return this._promiseResponse(req);
8296
}
8397

@@ -123,7 +137,7 @@ export default class Agent {
123137
});
124138
}
125139

126-
_buildRequest({ uri, method, data, auth, query, form, files, context, makerequest=request }) {
140+
_buildRequest({ uri, method, data, auth, query, form, files, context, makerequest = request }) {
127141
const req = makerequest(method, uri);
128142
if (this.prefix) {
129143
req.use(this.prefix);
@@ -141,7 +155,7 @@ export default class Agent {
141155
let options = {
142156
filepath: file.path
143157
};
144-
if (this._inBrowser(makerequest)) {
158+
if (this.isForBrowser(makerequest)) {
145159
options = file.path;
146160
}
147161
req.attach(name, file.data, options);
@@ -160,7 +174,7 @@ export default class Agent {
160174
return req;
161175
}
162176

163-
_inBrowser(makerequest) {
177+
isForBrowser(makerequest = request) {
164178
// superagent only has the getXHR method in the browser version
165179
return !!makerequest.getXHR;
166180
}

test/Agent.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ describe('Agent', () => {
304304
query: 'all',
305305
form:form,
306306
files: sanitizedFiles,
307-
context: undefined
307+
context: undefined,
308+
raw: false
308309
});
309310
});
310311

@@ -323,7 +324,8 @@ describe('Agent', () => {
323324
files: undefined,
324325
form: undefined,
325326
query: undefined,
326-
context: undefined
327+
context: undefined,
328+
raw: false
327329
});
328330
});
329331

0 commit comments

Comments
 (0)