Comparing version 1.0.3 to 1.0.4
'use strict'; | ||
/** | ||
* Custom errors | ||
* @module errors | ||
*/ | ||
@@ -12,3 +16,3 @@ var util = require('util'); | ||
*/ | ||
function ResponseError(status, body) { | ||
exports.ResponseError = function(status, body) { | ||
Error.call(this); | ||
@@ -22,3 +26,3 @@ Error.captureStackTrace(this, this.constructor); | ||
} | ||
util.inherits(ResponseError, Error); | ||
util.inherits(exports.ResponseError, Error); | ||
@@ -32,3 +36,3 @@ /** | ||
*/ | ||
function APIError(message, error, response) { | ||
exports.APIError = function(message, error, response) { | ||
Error.call(this); | ||
@@ -43,5 +47,2 @@ Error.captureStackTrace(this, this.constructor); | ||
} | ||
util.inherits(APIError, Error); | ||
exports.ResponseError = ResponseError; | ||
exports.APIError = APIError; | ||
util.inherits(exports.APIError, Error); |
@@ -10,3 +10,8 @@ 'use strict'; | ||
var RESOURCES = ['conversations', 'messages', 'announcements', 'blocklist']; | ||
var RESOURCES = [ | ||
'conversations', | ||
'messages', | ||
'announcements', | ||
'blocklist' | ||
]; | ||
@@ -16,5 +21,8 @@ /** | ||
* | ||
* @param {Object} config Configuration values | ||
* @property {String} token Layer Platform API token | ||
* @property {String} appId Layer Application ID | ||
* @class | ||
* @param {Object} config Configuration values | ||
* @param {String} config.token Layer Platform API token | ||
* @param {String} config.appId Layer Application ID | ||
* @param {String} [config.version] Platform API version | ||
* @param {Number} [config.timeout] Request timeout in ms | ||
*/ | ||
@@ -21,0 +29,0 @@ module.exports = function(config) { |
'use strict'; | ||
/** | ||
* HTTPS Request implementation | ||
* @module request | ||
*/ | ||
@@ -20,5 +24,4 @@ var https = require('https'); | ||
* | ||
* @class | ||
* @param {Object} config Configuration values | ||
* @property {String} appId Application ID | ||
* @property {String} token Platform API token | ||
*/ | ||
@@ -25,0 +28,0 @@ module.exports = function(config) { |
'use strict'; | ||
/** | ||
* Announcements resource | ||
* @module resources/announcements | ||
*/ | ||
@@ -3,0 +7,0 @@ var utils = require('../utils'); |
'use strict'; | ||
/** | ||
* Block list resource | ||
* @module resources/blocklists | ||
*/ | ||
@@ -3,0 +7,0 @@ var querystring = require('querystring'); |
'use strict'; | ||
/** | ||
* Conversations resource | ||
* @module resources/conversations | ||
*/ | ||
@@ -58,2 +62,18 @@ var utils = require('../utils'); | ||
}, callback || utils.nop); | ||
}, | ||
/** | ||
* Delete a conversation | ||
* | ||
* @param {String} conversationId Conversation UUID | ||
* @param {Function} callback Callback function | ||
*/ | ||
delete: function(conversationId, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
utils.debug('Conversation delete: ' + conversationId); | ||
request.delete({ | ||
path: '/conversations/' + conversationId | ||
}, callback || utils.nop); | ||
} | ||
@@ -60,0 +80,0 @@ }; |
'use strict'; | ||
/** | ||
* Messages resource | ||
* @module resources/messages | ||
*/ | ||
var utils = require('../utils'); | ||
var MIME_TEXT = 'text/plain'; | ||
module.exports = function(request) { | ||
@@ -38,3 +40,3 @@ return { | ||
sendTexFromUser: function(conversationId, userId, text, callback) { | ||
send(null, conversationId, textBody('user_id', userId, text), callback); | ||
send(null, conversationId, utils.messageText('user_id', userId, text), callback); | ||
}, | ||
@@ -51,3 +53,3 @@ | ||
sendTexFromName: function(conversationId, name, text, callback) { | ||
send(null, conversationId, textBody('name', name, text), callback); | ||
send(null, conversationId, utils.messageText('name', name, text), callback); | ||
} | ||
@@ -70,22 +72,1 @@ }; | ||
}; | ||
/** | ||
* Plain text message body | ||
* | ||
* @param {String} type Sender type | ||
* @param {String} sender Sender value | ||
* @param {String} text Message text | ||
*/ | ||
function textBody(type, sender, text) { | ||
var body = { | ||
sender: {}, | ||
parts: [ | ||
{ | ||
body: text, | ||
mime_type: MIME_TEXT | ||
} | ||
] | ||
}; | ||
body.sender[type] = sender; | ||
return body; | ||
} |
'use strict'; | ||
/** | ||
* Utilities | ||
* @module utils | ||
*/ | ||
var util = require('util'); | ||
var UUID = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i; | ||
exports.UUID = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i; | ||
exports.MIME_TEXT = 'text/plain'; | ||
exports.isArray = util.isArray; | ||
exports.nop = function() {}; | ||
@@ -18,6 +24,32 @@ /** | ||
val = val.substr(val.lastIndexOf('/') + 1); // omit the prefix | ||
if (val.match(UUID)) return val; | ||
if (val.match(exports.UUID)) return val; | ||
else return null; | ||
}; | ||
/** | ||
* Return a single plain text message body | ||
* | ||
* @param {String} type Sender type | ||
* @param {String} sender Sender value | ||
* @param {String} text Message text | ||
*/ | ||
exports.messageText = function(type, sender, text) { | ||
var body = { | ||
sender: {}, | ||
parts: [ | ||
{ | ||
body: text, | ||
mime_type: exports.MIME_TEXT | ||
} | ||
] | ||
}; | ||
body.sender[type] = sender; | ||
return body; | ||
}; | ||
/** | ||
* Debug function | ||
* | ||
* @param {String} val Text value to debug | ||
*/ | ||
exports.debug = function(val) { | ||
@@ -28,4 +60,2 @@ if (process.env.LAYER_API_DEBUG !== 'true') return; | ||
exports.nop = function() {}; | ||
/** | ||
@@ -32,0 +62,0 @@ * Error messages |
{ | ||
"name": "layer-api", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Node.js library, which provides a wrapper for the Layer Platform API", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "node_modules/mocha/bin/mocha test/" | ||
"test": "mocha test/" | ||
}, | ||
@@ -9,0 +9,0 @@ "keywords": [ |
# Layer API for node.js | ||
[![Build Status](http://img.shields.io/travis/layerhq/node-layer-api.svg?style=flat)](https://travis-ci.org/layerhq/node-layer-api) | ||
[![Build Status](http://img.shields.io/travis/layerhq/node-layer-api.svg)](https://travis-ci.org/layerhq/node-layer-api) | ||
[![npm version](http://img.shields.io/npm/v/layer-api.svg)](https://npmjs.org/package/layer-api) | ||
@@ -346,2 +347,2 @@ A Node.js library, which provides a wrapper for the [Layer](https://layer.com) Platform API. | ||
[Nil Gradisnik](https://github.com/nilgradisnik) | ||
[Nil Gradisnik](https://github.com/nilgradisnik) |
@@ -133,2 +133,19 @@ /*globals describe it*/ | ||
describe('Delete a conversation by conversation ID', function() { | ||
nock('https://api.layer.com') | ||
.delete('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.delete(fixtures.conversations.uuid, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Editing a conversation by passing invalid operations', function() { | ||
@@ -135,0 +152,0 @@ var operations = 'bla'; |
@@ -95,2 +95,14 @@ /*globals describe it*/ | ||
}); | ||
describe('Delete a conversation by conversation ID', function() { | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.delete(conversationId, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.body).be.eql(null); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -14,3 +14,3 @@ /*globals describe it*/ | ||
describe('Passing config with token and appId', function() { | ||
it('should expose API operations', function() { | ||
it('should expose expected methods', function() { | ||
var layerApi = new LayerAPI({token: fixtures.token, appId: fixtures.appId}); | ||
@@ -30,2 +30,7 @@ should.exist(layerApi); | ||
should(typeof layerApi.announcements.send).be.eql('function'); | ||
should(typeof layerApi.announcements.sendDedupe).be.eql('function'); | ||
should(typeof layerApi.blocklist.get).be.eql('function'); | ||
should(typeof layerApi.blocklist.block).be.eql('function'); | ||
should(typeof layerApi.blocklist.unblock).be.eql('function'); | ||
}); | ||
@@ -32,0 +37,0 @@ }); |
@@ -24,2 +24,38 @@ /*globals describe it*/ | ||
describe('Message text payload function type user_id', function() { | ||
it('should return payload data', function() { | ||
var from = 'abcd'; | ||
var text = 'Hello, World!'; | ||
utils.messageText('user_id', from, text).should.be.eql({ | ||
sender: { | ||
user_id: from | ||
}, | ||
parts: [ | ||
{ | ||
body: text, | ||
mime_type: utils.MIME_TEXT | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
describe('Message text payload function type name', function() { | ||
it('should return payload data', function() { | ||
var from = 'abcd'; | ||
var text = 'Hello, World!'; | ||
utils.messageText('name', from, text).should.be.eql({ | ||
sender: { | ||
name: from | ||
}, | ||
parts: [ | ||
{ | ||
body: text, | ||
mime_type: utils.MIME_TEXT | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
describe('Passing invalid value to toUUID function', function() { | ||
@@ -26,0 +62,0 @@ it('should return null', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72789
1458
348
24