ellipsis-api
Advanced tools
Comparing version 0.1.2 to 0.2.0-0
"use strict"; | ||
const inspect = require('util').inspect; | ||
@@ -11,7 +12,18 @@ module.exports = { | ||
url: options.url, | ||
body: require('util').inspect(body) // just echo back the options for inspection by tests | ||
body: inspect(body) // just echo back the options for inspection by tests | ||
}; | ||
callback(null, response, body); | ||
}), | ||
delete: jest.fn((options, callback) => { | ||
const body = options; | ||
const response = { | ||
statusCode: 200, | ||
statusMessage: "Ok", | ||
url: options.url, | ||
body: inspect(body) | ||
}; | ||
callback(null, response, body); | ||
}) | ||
}; |
@@ -221,55 +221,2 @@ "use strict"; | ||
describe("listen", () => { | ||
const actionName = "some action"; | ||
const messageInputName = "message"; | ||
const args = [ {name: "param", value: "v" }]; | ||
const defaultOptions = { | ||
actionName: actionName, | ||
messageInputName: messageInputName | ||
}; | ||
it("listens in default context", () => { | ||
defaultExpectedForm = { | ||
medium: api.ellipsis.userInfo.messageInfo.medium, | ||
channel: api.ellipsis.userInfo.messageInfo.channel, | ||
token: api.ellipsis.token, | ||
userId: api.ellipsis.userInfo.ellipsisUserId | ||
}; | ||
expect.assertions(2); | ||
const options = Object.assign({}, { args: args }, defaultOptions); | ||
return actionsApi.listen(options).then(body => { | ||
const form = body.form; | ||
const expectedForm = Object.assign({}, defaultExpectedForm, defaultOptions, { | ||
actionName: options.actionName, | ||
messageInputName: messageInputName, | ||
"arguments[0].name": args[0].name, | ||
"arguments[0].value": args[0].value | ||
}); | ||
expect(form).toEqual(expectedForm); | ||
expect(request.post.mock.calls[0][0].url).toEqual(actionsApi.urlFor("v1/add_message_listener")); | ||
}); | ||
}); | ||
it("complains when no actionName", () => { | ||
expect.assertions(1); | ||
delete defaultOptions.actionName; | ||
expect(actionsApi.listen(defaultOptions)).rejects.toHaveProperty('message', errorMessages.ACTION_NAME_MISSING); | ||
}); | ||
it("complains when no messageInputName", () => { | ||
expect.assertions(1); | ||
delete defaultOptions.messageInputName; | ||
expect(actionsApi.listen(defaultOptions)).rejects.toHaveProperty('message', errorMessages.MESSAGE_INPUT_NAME_MISSING); | ||
}); | ||
}); | ||
describe("generateToken", () => { | ||
@@ -300,3 +247,26 @@ | ||
describe("deleteSavedAnswers", () => { | ||
it("sends a delete user saved answer request by default with an inputName", () => { | ||
return actionsApi.deleteSavedAnswers({ | ||
inputName: "inputThatSavesAnswers" | ||
}).then(body => { | ||
expect(request.delete.mock.calls[0][0].url).toEqual(actionsApi.urlFor(`v1/inputs/inputThatSavesAnswers/user_saved_answer/${ellipsis.token}`)); | ||
}); | ||
}); | ||
it('complains when inputName is missing', () => { | ||
expect(actionsApi.deleteSavedAnswers({})).rejects.toHaveProperty('message', errorMessages.INPUT_NAME_MISSING); | ||
}); | ||
it('sends a delete team saved answers request with an inputName and deleteAll', () => { | ||
return actionsApi.deleteSavedAnswers({ | ||
inputName: "inputThatSavesAnswers", | ||
deleteAll: true | ||
}).then(body => { | ||
expect(request.delete.mock.calls[0][0].url).toEqual(actionsApi.urlFor(`v1/inputs/inputThatSavesAnswers/team_saved_answers/${ellipsis.token}`)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -303,0 +273,0 @@ |
class EllipsisApiError extends Error { | ||
constructor(props) { | ||
const bodyString = (typeof props.body === "object") ? JSON.stringify(props.body) : String(props.body); | ||
const errorMessage = `${props.response.statusCode}: ${props.response.statusMessage} | ||
${props.body}`; | ||
${bodyString}`; | ||
super(errorMessage); | ||
@@ -6,0 +7,0 @@ this.response = props.response; |
@@ -12,3 +12,3 @@ module.exports = { | ||
EMAIL_MISSING: "You need to pass an `email` argument containing an email address you want to lookup", | ||
MESSAGE_INPUT_NAME_MISSING: "You need to pass a `messageInputName` argument" | ||
INPUT_NAME_MISSING: "You need to pass an `inputName` argument containing the name of the input you want to reference" | ||
}; |
97
index.js
@@ -23,6 +23,2 @@ const request = require('request'); | ||
mediumFor(options) { | ||
return options.medium ? options.medium : this.ellipsis.userInfo.messageInfo.medium; | ||
} | ||
originalEventType() { | ||
@@ -86,8 +82,12 @@ return this.ellipsis.event ? this.ellipsis.event.originalEventType : null; | ||
mergeOptions(options, resolve, reject) { | ||
return Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
} | ||
run(options) { | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
this.checkActionOptionsIn(mergedOptions); | ||
@@ -111,6 +111,3 @@ const formData = Object.assign({ | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
if (!mergedOptions.message) { | ||
@@ -142,6 +139,3 @@ this.handleError(mergedOptions, errorMessages.MESSAGE_MISSING); | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
this.checkSchedulingOptionsIn(mergedOptions); | ||
@@ -167,6 +161,3 @@ const formData = Object.assign({ | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
this.checkActionOptionsIn(mergedOptions); | ||
@@ -189,41 +180,5 @@ const formData = { | ||
checkListeningOptionsIn(options) { | ||
if (!options.actionName) { | ||
this.handleError(options, errorMessages.ACTION_NAME_MISSING); | ||
} | ||
if (!options.messageInputName) { | ||
this.handleError(options, errorMessages.MESSAGE_INPUT_NAME_MISSING); | ||
} | ||
} | ||
listen(options) { | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
this.checkListeningOptionsIn(options); | ||
const formData = Object.assign({ | ||
actionName: mergedOptions.actionName, | ||
messageInputName: mergedOptions.messageInputName, | ||
medium: this.mediumFor(mergedOptions), | ||
channel: this.channelFor(mergedOptions), | ||
thread: mergedOptions.thread, | ||
userId: this.ellipsis.userInfo.ellipsisUserId, | ||
token: this.token() | ||
}, this.argsFormDataFor(mergedOptions.args)); | ||
request.post({ | ||
url: this.urlFor("v1/add_message_listener"), | ||
form: formData, | ||
json: true | ||
}, (error, response, body) => this.handleResponse(mergedOptions, error, response, body)); | ||
}); | ||
} | ||
generateToken(options) { | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = Object.assign({}, options, { | ||
success: resolve, | ||
error: reject | ||
}); | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
const formData = { | ||
@@ -242,2 +197,29 @@ expirySeconds: mergedOptions.expirySeconds, | ||
checkDeleteSavedAnswerOptions(options) { | ||
if (!options.inputName) { | ||
this.handleError(options, errorMessages.INPUT_NAME_MISSING); | ||
} | ||
} | ||
deleteSavedAnswers(options) { | ||
return new Promise((resolve, reject) => { | ||
const mergedOptions = this.mergeOptions(options, resolve, reject); | ||
this.checkDeleteSavedAnswerOptions(mergedOptions); | ||
const endPoint = options.deleteAll ? "team_saved_answers" : "user_saved_answer"; | ||
request.delete({ | ||
url: this.urlFor(`v1/inputs/${mergedOptions.inputName}/${endPoint}/${this.token()}`) | ||
}, (error, response, body) => this.handleResponse(mergedOptions, error, response, body)); | ||
}); | ||
} | ||
deleteUserSavedAnswer(options) { | ||
return this.deleteSavedAnswers(options); | ||
} | ||
deleteTeamSavedAnswers(options) { | ||
return this.deleteSavedAnswers(Object.assign({}, options, { | ||
deleteAll: true | ||
})); | ||
} | ||
} | ||
@@ -339,3 +321,2 @@ | ||
this.unschedule = this.actions.unschedule.bind(this.actions); | ||
this.listen = this.actions.listen.bind(this.actions); | ||
this.generateToken = this.actions.generateToken.bind(this.actions); | ||
@@ -342,0 +323,0 @@ } |
{ | ||
"name": "ellipsis-api", | ||
"version": "0.1.2", | ||
"version": "0.2.0-0", | ||
"description": "Interact with Ellipsis skills & actions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
25153
625