node-freshdesk-api
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,1 +0,3 @@ | ||
module.exports = require('./lib/client'); | ||
"use strict"; | ||
module.exports = require('./lib/client'); |
/** | ||
* Main Client File | ||
* Created by arjun on 03/05/16. | ||
* @module Index of API client | ||
* @author Arjun Komath <arjunkomath@gmail.com> | ||
*/ | ||
var request = require('request'); | ||
"use strict"; | ||
var Freshdesk = function (domain, apikey) { | ||
this.domain = domain; | ||
this.apikey = apikey; | ||
} | ||
const request = require('request'); | ||
const debug = require('debug')('freshdesk-api'); | ||
Freshdesk.prototype.response = function (cb) { | ||
return function (error, response, body) { | ||
// console.log(response.statusCode); | ||
if (!error && response.statusCode == 200) { | ||
try { | ||
var data = JSON.parse(body); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
return cb(null, data); | ||
} else if (!error && body) { | ||
try { | ||
var data = JSON.parse(body); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
return cb(data); | ||
} else if (!error && response.statusCode == 204) { | ||
return cb(null, { | ||
message: 'The server has successfully fulfilled the request and that there is no additional content to send in the response payload body' | ||
}); | ||
} else return cb(error); | ||
} | ||
} | ||
Freshdesk.prototype.req = function (method, url, qs, data, cb) { | ||
authKey = new Buffer(this.apikey + ":X").toString('base64'); | ||
var options = { | ||
method: method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': "Basic " + authKey | ||
}, | ||
url: url, | ||
qs: qs | ||
} | ||
if(data) | ||
options.body = JSON.stringify(data); | ||
request(options, this.response(cb)) | ||
} | ||
const createResponseHandler = function (cb) { | ||
return function (error, response, body) { | ||
if (error) { | ||
debug(`Unexpected status [${error}], req path [${response.request.path}] raw body: ${response.request.body}`); | ||
return cb(error); | ||
} | ||
Freshdesk.prototype.listAllTickets = function (params, cb) { | ||
this.req('GET', '{domain}/api/v2/tickets'.replace('{domain}', this.domain), params, null, cb) | ||
} | ||
let data = null; | ||
Freshdesk.prototype.listAllTicketFields = function (cb) { | ||
this.req('GET', '{domain}/api/v2/ticket_fields'.replace('{domain}', this.domain), null, null, cb) | ||
} | ||
switch(response.statusCode) { | ||
// SUCCESS | ||
// https://httpstatuses.com/200 OK | ||
// https://httpstatuses.com/201 Created | ||
case 200: | ||
case 201: | ||
try { | ||
data = JSON.parse(body); | ||
} catch (err) { | ||
// TODO: catch only JSON-parse errors!!! | ||
debug(`Not a JSON resp with valid status [${response.statusCode}], req path [${response.request.path}] raw body: ${response.request.body}`); | ||
return cb(new FreshdeskError('Not a JSON response from API', data)); | ||
} | ||
return cb(null, data); | ||
Freshdesk.prototype.createTicket = function (data, cb) { | ||
this.req('POST', '{domain}/api/v2/tickets'.replace('{domain}', this.domain), null, data, cb) | ||
} | ||
// SUCCESS for DELETE operations | ||
// https://httpstatuses.com/204 No Content | ||
case 204: | ||
return cb(null, null); | ||
Freshdesk.prototype.getTicket = function (id, cb) { | ||
this.req('GET', '{domain}/api/v2/tickets/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
// https://httpstatuses.com/409 Conflict - NOT UNIQUE, where unique required | ||
case 409: | ||
default: | ||
try { | ||
data = JSON.parse(body); | ||
} catch (err) { | ||
// TODO: catch only JSON-parse errors!!! | ||
Freshdesk.prototype.updateTicket = function (id, data, cb) { | ||
this.req('PUT', '{domain}/api/v2/tickets/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, data, cb) | ||
} | ||
// probably, data is not a JSON, so lets return it "AS IS" | ||
debug(`Not a JSON resp for unexpected status [${response.statusCode}], req path [${response.request.path}] raw body: ${response.request.body}`); | ||
return cb(new FreshdeskError('Not a JSON response from API', data)); | ||
} | ||
Freshdesk.prototype.deleteTicket = function (id, cb) { | ||
this.req('DELETE', '{domain}/api/v2/tickets/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
debug(`Unexpected/Error status [${response.statusCode}], req path [${response.request.path}] raw body: ${response.request.body}`); | ||
return cb(new FreshdeskError(data.description, data)); | ||
} | ||
} | ||
} | ||
Freshdesk.prototype.restoreTicket = function (id, cb) { | ||
this.req('PUT', '{domain}/api/v2/tickets/{id}/restore'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
const makeRequest = function (method, auth, url, qs, data, cb) { | ||
var options = { | ||
method: method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': auth | ||
}, | ||
url: url, // for debugging set to: "https://httpbin.org/get" | ||
qs: qs | ||
} | ||
Freshdesk.prototype.listAllConversations = function (id, cb) { | ||
this.req('GET', '{domain}/api/v2/tickets/{id}/conversations'.replace('{domain}', this.domain).replace('{id}', id), null, cb) | ||
} | ||
if(data) { | ||
options.body = JSON.stringify(data); | ||
} | ||
Freshdesk.prototype.listAllTimeEntries = function (id, cb) { | ||
this.req('GET', '{domain}/api/v2/tickets/{id}/time_entries'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
request(options, createResponseHandler(cb)) | ||
} | ||
//Conversations | ||
Freshdesk.prototype.createReply = function (id, data, cb) { | ||
this.req('POST', '{domain}/api/v2/tickets/{id}/reply'.replace('{domain}', this.domain).replace('{id}', id), null, data, cb) | ||
} | ||
/** | ||
* Freshdesk APIv2 client | ||
* @class | ||
* @param {string} baseUrl Base URL for the API calls, for example `https://demo.freshdesk.com` | ||
* @param {string} apiKey API key | ||
* @public | ||
*/ | ||
const Freshdesk = function (baseUrl, apiKey) { | ||
this.baseUrl = baseUrl; | ||
Freshdesk.prototype.createNote = function (id, data, cb) { | ||
this.req('POST', '{domain}/api/v2/tickets/{id}/notes'.replace('{domain}', this.domain).replace('{id}', id), null, data, cb) | ||
this._auth = 'Basic ' + new Buffer(apiKey + ":X").toString('base64'); | ||
} | ||
Freshdesk.prototype.updateConversation = function (id, data, cb) { | ||
this.req('PUT', '{domain}/api/v2/conversations/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, data, cb) | ||
} | ||
Freshdesk.prototype = Object.create({ | ||
/** | ||
* This callback is called on API call ended. | ||
* @name Freshdesk.requestCallback | ||
* @callback Freshdesk.requestCallback | ||
* @param {*} error Indicates, that error occured during call | ||
* @param {Object} [data] Contains actual data | ||
*/ | ||
Freshdesk.prototype.deleteConversation = function (id, cb) { | ||
this.req('DELETE', '{domain}/api/v2/conversations/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
/** | ||
* Filter-settings for {@link listAllTickets} | ||
* @name Freshdesk.TicketsFilter | ||
* @typedef {Object} Freshdesk.TicketsFilter | ||
* @property {string} [filter] Predefined filters, one of `new_and_my_open`, `watching`, `spam`, `deleted` | ||
* @property {string} [requester_id] Requester | ||
* @property {string} [email] Requester | ||
* @property {string} [company_id] Company ID | ||
* @property {string} [updated_since] Updated since. | ||
*/ | ||
//Contacts | ||
/** | ||
* listAllTickets API method | ||
* {@link http://developers.freshdesk.com/api/#list_all_tickets} | ||
* @param {Freshdesk.TicketsFilter} params Dictionary with request parameters for the API method, for example : `{"company_id": "YOUR_ID"}` | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
* @return {Array.<Freshdesk.Tickets>} The array of tickets found | ||
*/ | ||
listAllTickets (params, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/tickets`, params, null, cb) | ||
}, | ||
Freshdesk.prototype.createContact = function (data, cb) { | ||
this.req('POST', '{domain}/api/v2/contacts'.replace('{domain}', this.domain), null, data, cb) | ||
} | ||
listAllTicketFields(cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/ticket_fields`, null, null, cb) | ||
}, | ||
Freshdesk.prototype.getContact = function (id, cb) { | ||
this.req('GET', '{domain}/api/v2/contacts/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
createTicket(data, cb) { | ||
makeRequest('POST', this._auth, `${this.baseUrl}/api/v2/tickets`, null, data, cb) | ||
}, | ||
Freshdesk.prototype.listAllContacts = function (params, cb) { | ||
this.req('GET', '{domain}/api/v2/contacts'.replace('{domain}', this.domain), params, null, cb) | ||
} | ||
getTicket(id, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/tickets/${id}`, null, null, cb) | ||
}, | ||
Freshdesk.prototype.updateContact = function (id, data, cb) { | ||
this.req('PUT', '{domain}/api/v2/contacts/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, data, cb) | ||
} | ||
updateTicket(id, data, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/tickets/${id}`, null, data, cb) | ||
}, | ||
Freshdesk.prototype.deleteContact = function (id, cb) { | ||
this.req('DELETE', '{domain}/api/v2/contacts/{id}'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
deleteTicket(id, cb) { | ||
makeRequest('DELETE', this._auth, `${this.baseUrl}/api/v2/tickets/${id}`, null, null, cb) | ||
}, | ||
Freshdesk.prototype.makeAgent = function (id, cb) { | ||
this.req('PUT', '{domain}/api/v2/contacts/{id}/make_agent'.replace('{domain}', this.domain).replace('{id}', id), null, null, cb) | ||
} | ||
restoreTicket(id, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/tickets/${id}/restore`, null, null, cb) | ||
}, | ||
Freshdesk.prototype.listAllContactFields = function (cb) { | ||
this.req('GET', '{domain}/api/v2/contact_fields'.replace('{domain}', this.domain), null, null, cb) | ||
listAllConversations(id, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/tickets/${id}/conversations`, null, cb) | ||
}, | ||
listAllTimeEntries(id, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/tickets/${id}/time_entries`, null, null, cb) | ||
}, | ||
//Conversations | ||
createReply(id, data, cb) { | ||
makeRequest('POST', this._auth, `${this.baseUrl}/api/v2/tickets/${id}/reply`, null, data, cb) | ||
}, | ||
createNote(id, data, cb) { | ||
makeRequest('POST', this._auth, `${this.baseUrl}/api/v2/tickets/${id}/notes`, null, data, cb) | ||
}, | ||
updateConversation(id, data, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/conversations/${id}`, null, data, cb) | ||
}, | ||
deleteConversation(id, cb) { | ||
makeRequest('DELETE', this._auth, `${this.baseUrl}/api/v2/conversations/${id}`, null, null, cb) | ||
}, | ||
//Contacts | ||
createContact(data, cb) { | ||
makeRequest('POST', this._auth, `${this.baseUrl}/api/v2/contacts`, null, data, cb) | ||
}, | ||
getContact(id, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/contacts/${id}`, null, null, cb) | ||
}, | ||
listAllContacts(params, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/contacts`, params, null, cb) | ||
}, | ||
updateContact(id, data, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/contacts/${id}`, null, data, cb) | ||
}, | ||
deleteContact(id, cb) { | ||
makeRequest('DELETE', this._auth, `${this.baseUrl}/api/v2/contacts/${id}`, null, null, cb) | ||
}, | ||
makeAgent(id, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/contacts/${id}/make_agent`, null, null, cb) | ||
}, | ||
listAllContactFields(cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/contact_fields`, null, null, cb) | ||
}, | ||
// Companies | ||
/** | ||
* Company settings, for {@link updateCompany} and {@link createCompany} | ||
* | ||
* @category Companies | ||
* | ||
* @name Freshdesk.CompanyData | ||
* @typedef {Object} | ||
* @property {Object.<string, Object>} [custom_fields] Key value pairs containing the names and values of custom fields. Only dates in the format YYYY-MM-DD are accepted as input for custom date fields. Read more here | ||
* @property {string} [description] Description of the company | ||
* @property {Array.<string>} [domains] Domains of the company. Email addresses of contacts that contain this domain will be associated with that company automatically. | ||
* @property {string} [name] **UNIQUE** Name of the company | ||
* @property {string} [note] Any specific note about the company | ||
*/ | ||
/** | ||
* Company settings, returned by view-methods | ||
* | ||
* @category Companies | ||
* | ||
* @name Freshdesk.CompanyViewData | ||
* @typedef {Object} | ||
* @property {string} name **UNIQUE** Name of the company | ||
* @property {string} description Description of the company | ||
* @property {Array.<string>} domains Domains of the company. Email addresses of contacts that contain this domain will be associated with that company automatically. | ||
* @property {string} note Any specific note about the company | ||
* @property {Date} created_at Creation date | ||
* @property {Date} updated_at Last update date | ||
* @property {Object.<string, Object>} custom_fields Key value pairs containing the names and values of custom fields. Only dates in the format YYYY-MM-DD are accepted as input for custom date fields. Read more here | ||
*/ | ||
/** | ||
* Creates company | ||
* | ||
* {@link http://developer.freshdesk.com/api/#create_company} | ||
* | ||
* @category Companies | ||
* | ||
* @param {Object} data [description] | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
* @return {void} `void` | ||
*/ | ||
createCompany(data, cb) { | ||
makeRequest('POST', this._auth, `${this.baseUrl}/api/v2/companies`, null, data, cb); | ||
}, | ||
/** | ||
* View a Company | ||
* | ||
* {@link http://developer.freshdesk.com/api/#view_company} | ||
* | ||
* @category Companies | ||
* | ||
* @param {integer} id Company ID | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
* @return {Freshdesk.CompanyViewData} Company information | ||
*/ | ||
getCompany(id, cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/companies/${id}`, null, null, cb) | ||
}, | ||
/** | ||
* List All Companies | ||
* | ||
* {@link http://developer.freshdesk.com/api/#list_all_companies} | ||
* @category Companies | ||
* | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
* @return {Array.<Freshdesk.CompanyViewData>} Company information | ||
*/ | ||
listAllCompanies(cb) { | ||
makeRequest('GET', this._auth, `${this.baseUrl}/api/v2/companies`, null, null, cb) | ||
}, | ||
/** | ||
* Updates company | ||
* | ||
* {@link http://developer.freshdesk.com/api/#update_company} | ||
* | ||
* @category Companies | ||
* | ||
* @param {integer} id Company ID | ||
* @param {Freshdesk.UpdateCompanyData} data New settings for company | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
* @return {void} `void` | ||
*/ | ||
updateCompany(id, data, cb) { | ||
makeRequest('PUT', this._auth, `${this.baseUrl}/api/v2/companies/${id}`, null, data, cb); | ||
}, | ||
/** | ||
* Delete a Company | ||
* | ||
* Note: | ||
* 1. Deleting a company does not delete the contacts that are associated with it. However the association will be removed. | ||
* 2. Once deleted, a company cannot be restored. | ||
* | ||
* {@link http://developer.freshdesk.com/api/#delete_company} | ||
* | ||
* @category Companies | ||
* | ||
* @param {integer} id Company ID | ||
* @param {Freshdesk.requestCallback} cb Callback function {@link Freshdesk.requestCallback} | ||
*/ | ||
deleteCompany(id, data, cb) { | ||
makeRequest('DELETE', this._auth, `${this.baseUrl}/api/v2/companies/${id}`, null, null, cb); | ||
}, | ||
}); | ||
/** | ||
* Freshdesk's API protocol violations | ||
* | ||
* @param {[type]} message [description] | ||
* @param {[type]} data [description] | ||
*/ | ||
function FreshdeskError(message, data) { | ||
this.name = 'FreshdeskError'; | ||
this.message = message || 'Error in Freshdesk\'s client API'; | ||
this.stack = (new Error()).stack; | ||
this.data = data; | ||
} | ||
FreshdeskError.prototype = Object.create(Error.prototype); | ||
FreshdeskError.prototype.constructor = FreshdeskError; | ||
/* EXPORT SECTION */ | ||
module.exports = Freshdesk; | ||
module.exports = Freshdesk; | ||
module.exports.FreshdeskError = FreshdeskError; |
{ | ||
"name": "node-freshdesk-api", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Node wrapper for Freshdesk v2 API", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "`npm bin`/mocha", | ||
"lint": "`npm bin`/eslint ./", | ||
"coverage": "`npm bin`/istanbul cover `npm bin`/_mocha -- --reporter min", | ||
"docs": "`npm bin`/doxdox ./ ./lib/client.js -o docs/index.html --layout Bootstrap" | ||
}, | ||
@@ -21,3 +24,26 @@ "keywords": [ | ||
"author": "Arjun Komath <arjunkomath@gmail.com>", | ||
"license": "MIT" | ||
"contributors": [ | ||
{ | ||
"name": "Koryukov Maksim", | ||
"email": "maxkoryukov@gmail.com", | ||
"url": "https://github.com/maxkoryukov" | ||
} | ||
], | ||
"engines": { | ||
"node": ">=4.0.0", | ||
"npm": ">=3.0.0" | ||
}, | ||
"homepage": "https://github.com/arjunkomath/node-freshdesk-api", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint": "^3.12.2", | ||
"eslint-config-volebo": "^2.0.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.2.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.5.1", | ||
"request": "^2.79.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
21178
15
355
1
2
5
1
+ Addeddebug@^2.5.1
+ Addedrequest@^2.79.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.0(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.0(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.9.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)