Skip to content

Commit

Permalink
0tdoAI1C: Added support for cancelling requests.
Browse files Browse the repository at this point in the history
Inspired by (and somewhat copy-pasted from)
#4
  • Loading branch information
Joel W Kall committed Jan 16, 2019
1 parent 9d902d3 commit d7c0f1b
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 402 deletions.
22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -78,12 +78,30 @@ var callback = function(response){
client.search("R2 droids", options, callback);
```

The `options` and `callback` parameters are optional. As seen above, if the
`callback` is omitted, `client.search` will return a Promise.
The `options` and `callback` parameters are optional. As seen above,
`client.search` will return a Promise.

All API operations work the same way with regards to `options` and `callback`,
except createEvent and createEvents which do not take `options`.

__Cancelling a request__

All requests can be cancelled, which is useful when the user types fast.

Using promise:
```
const request = client.search("R2 droids", {}); //create request
request.then(response => if(!response.cancelled) console.log('done')); //attach continuation
request.cancel(); // cancel the request
```

Using callback:
```
var callback = function(response) { if(!response.cancelled) console.log('done'); } //create callback
const request = client.search("R2 droids", {}, callback); //create request
request.cancel(); // cancel the request
```

__Create events example__

```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"author": "Loop54",
"dependencies": {
"@babel/polyfill": "^7.0.0",
"axios": "^0.7.0"
"axios": "^0.18.0"
},
"devDependencies": {
"@babel/core": "^7.1.2",
Expand Down
38 changes: 8 additions & 30 deletions src/client.js
Expand Up @@ -50,10 +50,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand Down Expand Up @@ -91,10 +88,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
events: [event]
},null,callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand All @@ -117,10 +111,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
events: events
},null,callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand Down Expand Up @@ -148,10 +139,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand Down Expand Up @@ -183,10 +171,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand All @@ -207,10 +192,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) {
// if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand Down Expand Up @@ -247,9 +229,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) { // if callback is missing, return a promise
return req;
}
return req;
},

/**
Expand All @@ -269,9 +249,7 @@ function getLoop54Client (endpoint, userId, apiKey) {
customData:options.customData
}, null, callback, userId, apiKey);

if (!callback) { // if callback is missing, return a promise
return req;
}
return req;
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/core.js
Expand Up @@ -45,12 +45,14 @@ let core = {
if(apiKey)
headers["Loop54-key"] = apiKey;

var source = axios.CancelToken.source();
var request = axios({
method: method ? method : "post",
url: url,
headers: headers,
responseType: "json",
data: body
data: body,
cancelToken: source.token
})
.then(function (response) {

Expand All @@ -62,6 +64,10 @@ let core = {
})
.catch(function (response) {

if (axios.isCancel(response)) {
return { cancelled:true };
}

var ret = response;

//if there is no data, that means something went wrong before we got a response
Expand All @@ -77,14 +83,16 @@ let core = {
}
return Promise.reject(ret);
});


request.cancel = () => source.cancel();

if (callback) {
request.then(callback).catch(function(response){
callback(response);
});
} else {
return request;
}

return request;
},

ensureProtocol: function (url) {
Expand Down
106 changes: 53 additions & 53 deletions test/autoComplete.js
@@ -1,54 +1,54 @@
import autoCompleteResponse from "./mocks/autocomplete-response-ok";
import nock from "nock";
import chai, {
assert,
expect
} from "chai";
import common from "./common";

module.exports = function () {

let client = Loop54.getClient(common.endpoint);

//mock all calls to the /autoComplete endpoint
beforeEach(() => {
nock(common.endpoint).post("/autoComplete").reply(200, autoCompleteResponse);
});

var okFunc = function(response) {
expect(response.status).to.equal(200);
expect(response.data.queries.count).to.equal(1);
}

it("Returns 200 OK and a valid response, without callback", function () {
return client.autoComplete("m").then(okFunc);
});

it("Returns 200 OK and a valid response, with callback", function (done) {
return client.autoComplete("m", response => common.testCallBack(response,okFunc,done));
});

it("Accepts options as second argument, without a callback", function () {
return client.autoComplete("m", {}).then(okFunc);
});

it("Accepts options as second argument, with a callback", function (done) {
return client.autoComplete("m", {}, response => common.testCallBack(response,okFunc,done));
});

it("Returns error if it has too few arguments", function () {
return client.autoComplete().catch(common.includesError);
});

it("Returns error if it has too many arguments", function () {
return client.autoComplete("m",{},"asdasd","asasd","g42om4").catch(common.includesError);
});

it("Returns error if invalid search query, without callback", function () {
return client.autoComplete("").catch(common.includesError);
});

it("Returns error if invalid search query, with callback", function (done) {
return client.autoComplete("",response => common.testCallBack(response,common.includesError,done));
});
import autoCompleteResponse from "./mocks/autocomplete-response-ok";
import nock from "nock";
import chai, {
assert,
expect
} from "chai";
import common from "./common";

module.exports = function () {

let client = Loop54.getClient(common.endpoint);

//mock all calls to the /autoComplete endpoint
beforeEach(() => {
nock(common.endpoint).post("/autoComplete").reply(200, autoCompleteResponse);
});

var okFunc = function(response) {
expect(response.status).to.equal(200);
expect(response.data.queries.count).to.equal(1);
}

it("Returns 200 OK and a valid response, without callback", function () {
return client.autoComplete("m").then(okFunc);
});

it("Returns 200 OK and a valid response, with callback", function (done) {
client.autoComplete("m", response => common.testCallBack(response,okFunc,done));
});

it("Accepts options as second argument, without a callback", function () {
return client.autoComplete("m", {}).then(okFunc);
});

it("Accepts options as second argument, with a callback", function (done) {
client.autoComplete("m", {}, response => common.testCallBack(response,okFunc,done));
});

it("Returns error if it has too few arguments", function () {
return client.autoComplete().catch(common.includesError);
});

it("Returns error if it has too many arguments", function () {
return client.autoComplete("m",{},"asdasd","asasd","g42om4").catch(common.includesError);
});

it("Returns error if invalid search query, without callback", function () {
return client.autoComplete("").catch(common.includesError);
});

it("Returns error if invalid search query, with callback", function (done) {
client.autoComplete("",response => common.testCallBack(response,common.includesError,done));
});
}

0 comments on commit d7c0f1b

Please sign in to comment.