Skip to content

Commit

Permalink
Merge pull request #174 from ubarsaiyan/custom-request
Browse files Browse the repository at this point in the history
Add custom request API
  • Loading branch information
perry-mitchell authored Oct 12, 2019
2 parents c24cdb6 + 6aae754 commit 39c39f4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
23 changes: 23 additions & 0 deletions source/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { merge } = require("./merge.js");
const directoryContents = require("./interface/directoryContents.js");
const createDir = require("./interface/createDirectory.js");
const createStream = require("./interface/createStream.js");
const custom = require("./interface/custom.js");
const deletion = require("./interface/delete.js");
const getFile = require("./interface/getFile.js");
const quota = require("./interface/quota.js");
Expand Down Expand Up @@ -179,6 +180,28 @@ function createClient(remoteURL, opts = {}) {
return createStream.createWriteStream(remoteFilename, createOptions);
},

/**
* Send a custom request
* @param {String} remotePath The remote path
* @param {RequestOptions=} requestOptions the request options
* @param {Options=} options Options for the request
* @memberof ClientInterface
* @returns {Promise<Any>} A promise that resolves with response of the request
* @example
* const contents = await client.customRequest("/alrighty.jpg", {
* method: "PROPFIND",
* headers: {
* Accept: "text/plain",
* Depth: 0
* },
* responseType: "text"
* });
*/
customRequest: function customRequest(remotePath, requestOptions, options) {
const customOptions = merge(runtimeOptions, options || {});
return custom.customRequest(remotePath, requestOptions, customOptions);
},

/**
* Delete a remote file
* @param {String} remotePath The remote path to delete
Expand Down
15 changes: 15 additions & 0 deletions source/interface/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const joinURL = require("url-join");
const { handleResponseCode } = require("../response.js");
const { encodePath, prepareRequestOptions, request } = require("../request.js");

function customRequest(remotePath, requestOptions, options) {
if (!requestOptions.url) {
requestOptions.url = joinURL(options.remoteURL, encodePath(remotePath), "/");
}
prepareRequestOptions(requestOptions, options);
return request(requestOptions).then(handleResponseCode);
}

module.exports = {
customRequest
};
3 changes: 2 additions & 1 deletion source/interface/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ function parseStat(result, filename, isDetailed = false) {
}

module.exports = {
getStat
getStat,
parseStat
};
6 changes: 5 additions & 1 deletion source/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function encodePath(path) {
* @property {Object=} httpsAgent - HTTPS agent instance
* @property {Object=} headers - Set additional request headers
* @property {Boolean=} withCredentials - Set whether or not credentials should
* @property {Object|String|*=} data - Set additional body
* be included with the request. Defaults to value used by axios.
*/

Expand All @@ -41,6 +42,9 @@ function prepareRequestOptions(requestOptions, methodOptions) {
if (methodOptions.httpsAgent) {
requestOptions.httpsAgent = methodOptions.httpsAgent;
}
if (methodOptions.data) {
requestOptions.data = methodOptions.data;
}
if (methodOptions.headers && typeof methodOptions.headers === "object") {
requestOptions.headers = merge(requestOptions.headers || {}, methodOptions.headers);
}
Expand All @@ -66,7 +70,7 @@ function prepareRequestOptions(requestOptions, methodOptions) {
* @property {Object=} headers - Headers to set on the request
* @property {Object=} httpAgent - A HTTP agent instance
* @property {Object=} httpsAgent - A HTTPS agent interface
* @property {Object|String|*=} body - Body data for the request
* @property {Object|String|*=} data - Body data for the request
*/

/**
Expand Down
48 changes: 48 additions & 0 deletions test/specs/customRequest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { parseXML } = require("../../source/interface/dav.js");
const { parseStat } = require("../../source/interface/stat.js");
const { processResponsePayload } = require("../../source/response.js");

describe("custom", function() {
beforeEach(function() {
this.client = createWebDAVClient("http://localhost:9988/webdav/server", {
username: createWebDAVServer.test.username,
password: createWebDAVServer.test.password
});
clean();
this.server = createWebDAVServer();
return this.server.start();
});

afterEach(function() {
return this.server.stop();
});

it("send and parse stat custom request", function() {
let response = null;
return this.client
.customRequest("/alrighty.jpg", {
method: "PROPFIND",
headers: {
Accept: "text/plain",
Depth: 0
},
responseType: "text"
})
.then(res => {
response = res;
return res.data;
})
.then(parseXML)
.then(xml => parseStat(xml, "alrighty.jpg"))
.then(result => processResponsePayload(response, result))
.then(function(stat) {
expect(stat).to.be.an("object");
expect(stat).to.have.property("filename", "/alrighty.jpg");
expect(stat).to.have.property("basename", "alrighty.jpg");
expect(stat).to.have.property("lastmod").that.is.a.string;
expect(stat).to.have.property("type", "file");
expect(stat).to.have.property("size", 52130);
expect(stat).to.have.property("mime", "image/jpeg");
});
});
});

0 comments on commit 39c39f4

Please sign in to comment.