layer-api
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -13,19 +13,20 @@ 'use strict'; | ||
* | ||
* @param {Object} options Options parameters | ||
* @param {Object} config Configuration values | ||
* @property {String} token Layer Platform API token | ||
* @property {String} appId Layer Application ID | ||
*/ | ||
var LayerAPI = module.exports = function(options) { | ||
options = options || {}; | ||
if (!options.token) throw new Error(utils.i18n.layerapi.token); | ||
options.appId = utils.toUUID(options.appId); | ||
if (!options.appId) throw new Error(utils.i18n.layerapi.appId); | ||
module.exports = function(config) { | ||
config = config || {}; | ||
if (!config.token) throw new Error(utils.i18n.layerapi.token); | ||
config.appId = utils.toUUID(config.appId); | ||
if (!config.appId) throw new Error(utils.i18n.layerapi.appId); | ||
var request = new Request(options); | ||
var request = new Request(config); | ||
RESOURCES.forEach(function(type) { | ||
LayerAPI.prototype[type] = require('./resources/' + type)(request); | ||
}); | ||
this[type] = require('./resources/' + type)(request); | ||
}, this); | ||
process.env.LAYER_API_DEBUG = options.debug || false; | ||
utils.debug('Initialized v' + pkg.version + ' with appId: ' + options.appId); | ||
process.env.LAYER_API_DEBUG = config.debug || false; | ||
utils.debug('Initialized v' + pkg.version + ' with appId: ' + config.appId); | ||
}; |
@@ -16,3 +16,2 @@ 'use strict'; | ||
}; | ||
var TIMEOUT = 10 * 1000; | ||
@@ -22,25 +21,32 @@ /** | ||
* | ||
* @param {Object} options Options | ||
* @param {Object} config Configuration values | ||
* @property {String} appId Application ID | ||
* @property {String} token Platform API token | ||
*/ | ||
var Request = module.exports = function(options) { | ||
this.appId = options.appId; | ||
this.token = options.token; | ||
module.exports = function(config) { | ||
this.token = config.token; | ||
this.appId = config.appId; | ||
// defaults for optional values | ||
this.version = config.version || '1.0'; | ||
this.timeout = config.timeout || 10000; | ||
// expose request methods | ||
API.methods.forEach(function(method) { | ||
this[method] = request.bind(this, method); | ||
}, this); | ||
}; | ||
/** | ||
* Expose supported HTTP methods | ||
* Request implementation | ||
* | ||
* @param {String} method HTTP method type | ||
* @param {Object} params Parameters passed in from the resource | ||
* @param {Function} callback Callback function | ||
*/ | ||
API.methods.forEach(function(method) { | ||
Request.prototype[method] = function(params, callback) { | ||
request(this, method, params, callback); | ||
}; | ||
}); | ||
function request(method, params, callback) { | ||
function request(options, method, params, callback) { | ||
var headers = { | ||
Accept: 'application/vnd.layer+json; version=1.0', | ||
Authorization: 'Bearer ' + options.token | ||
Accept: 'application/vnd.layer+json; version=' + this.version, | ||
Authorization: 'Bearer ' + this.token | ||
}; | ||
@@ -54,3 +60,3 @@ | ||
port: API.port, | ||
path: API.prefix + options.appId + params.path, | ||
path: API.prefix + this.appId + params.path, | ||
method: method.toUpperCase(), | ||
@@ -60,5 +66,5 @@ headers: headers | ||
req.setTimeout(TIMEOUT, timeout.bind(null, req, callback)); | ||
req.on('error', error.bind(null, req, callback)); | ||
req.on('response', response.bind(null, callback)); | ||
req.setTimeout(this.timeout, timeout.bind(this, req, callback)); | ||
req.on('error', error.bind(this, req, callback)); | ||
req.on('response', response.bind(this, callback)); | ||
@@ -103,7 +109,7 @@ req.on('socket', function(socket) { | ||
function timeout(req, callback) { | ||
utils.debug('Request timeout error after ' + TIMEOUT / 1000 + 'seconds'); | ||
utils.debug('Request timeout error after ' + this.timeout / 1000 + 'seconds'); | ||
req._isAborted = true; | ||
req.abort(); | ||
callback(new APIError('Request timeout after ' + TIMEOUT / 1000 + 'seconds.')); | ||
callback(new APIError('Request timeout after ' + this.timeout / 1000 + 'seconds.')); | ||
} |
{ | ||
"name": "layer-api", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Node.js library, which provides a wrapper for the Layer Platform API", | ||
@@ -17,2 +17,3 @@ "main": "lib/index.js", | ||
"license": "Apache-2.0", | ||
"repository": "layerhq/node-layer-api", | ||
"devDependencies": { | ||
@@ -19,0 +20,0 @@ "should": "~7.0.1", |
# 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) | ||
@@ -41,12 +42,17 @@ A Node.js library, which provides a wrapper for the [Layer](https://layer.com) Platform API. | ||
To use this library you need to create a new instance of the `layer-api` module by passing `options` object to a constructor. | ||
To use this library you need to create a new instance of the `layer-api` module by passing `config` object to a constructor. | ||
### new LayerAPI(options) | ||
### new LayerAPI(config) | ||
Layer API constructor is initialized with the following options: | ||
Layer API constructor is initialized with the following configuration values: | ||
- `token` - Layer Platform API token which can be obtained from [Developer Dashboard](https://developer.layer.com/projects/keys) | ||
- `appId` - Layer application ID | ||
- `debug` - *Optional* Enable debugging | ||
*Optional values:* | ||
- `version` - API version to use (default: `1.0`) | ||
- `timeout` - Request timeout in milliseconds (default: `10000` milliseconds) | ||
- `debug` - Enable debugging (default: `false`) | ||
## Conversations | ||
@@ -156,3 +162,3 @@ | ||
}; | ||
layerAPI.messages.sendTexFromUser(cid, payload, function(err, res) { | ||
layerAPI.messages.send(cid, payload, function(err, res) { | ||
if (err) return console.error(err); | ||
@@ -159,0 +165,0 @@ |
@@ -15,3 +15,3 @@ /*globals describe it*/ | ||
describe('Passing options with full appId', function() { | ||
describe('Passing config with full appId', function() { | ||
it('should throw an error', function() { | ||
@@ -23,3 +23,3 @@ var layerApi = new LayerAPI({token: fixtures.token, appId: fixtures.appIdFull}); | ||
describe('Passing options with token and appId', function() { | ||
describe('Passing config with token and appId', function() { | ||
it('should expose API operations', function() { | ||
@@ -44,3 +44,3 @@ var layerApi = new LayerAPI({token: fixtures.token, appId: fixtures.appId}); | ||
describe('Passing no options', function() { | ||
describe('Passing no config', function() { | ||
it('should throw an error', function() { | ||
@@ -57,3 +57,3 @@ try { | ||
describe('Passing options with token only', function() { | ||
describe('Passing config with token only', function() { | ||
it('should throw an error', function() { | ||
@@ -70,3 +70,3 @@ try { | ||
describe('Passing options with invalid appId', function() { | ||
describe('Passing config with invalid appId', function() { | ||
it('should throw an error', function() { | ||
@@ -73,0 +73,0 @@ try { |
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
53003
980
241