Skip to content

Commit

Permalink
Finsihed adding jsdoc comment blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthisk committed Oct 26, 2015
1 parent 7870325 commit 10f6456
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 88 deletions.
13 changes: 13 additions & 0 deletions .jsdoc
@@ -0,0 +1,13 @@
{
"source" : {
"include": ["src/"]
},
"plugins": [],
"opts": {
"recurse": true,
"template": "templates/default",
"destination": "./docs/",
"package": "./package.json",
"readme": "./README.md"
}
}
40 changes: 23 additions & 17 deletions src/getstream.js
@@ -1,25 +1,31 @@

// GetStream client library for node and the browser
// Author: Thierry Schellenbach
// BSD License

/**
* @module stream
* @author Thierry Schellenbach
* BSD License
*/
var StreamClient = require('./lib/client');
var errors = require('./lib/errors');
var request = require('request');

function connect(apiKey, apiSecret, appId, options) {
/*
* Usage
* stream.connect(apiKey, apiSecret)
* or if you want to be able to subscribe and listen
* for changes
* stream.connect(apiKey, apiSecret, appId)
* or on heroku
* stream.connect(streamURL)
* where streamURL looks like this
* https://thierry:pass@getstream.io/?app=1
*
*/
/**
* Create StreamClient
* @method connect
* @param {string} apiKey API key
* @param {string} [apiSecret] API secret (only use this on the server)
* @param {string} [appId] Application identifier
* @param {object} [options] Additional options
* @param {string} [options.location] Datacenter location
* @return {StreamClient} StreamClient
* @example <caption>Basic usage</caption>
* stream.connect(apiKey, apiSecret);
* @example <caption>or if you want to be able to subscribe and listen</caption>
* stream.connect(apiKey, apiSecret, appId);
* @example <caption>or on Heroku</caption>
* stream.connect(streamURL);
* @example <caption>where streamURL looks like</caption>
* "https://thierry:pass@gestream.io/?app=1"
*/
if (typeof (process) !== 'undefined' && process.env.STREAM_URL && !apiKey) {
var parts = /https\:\/\/(\w+)\:(\w+)\@([\w-]*).*\?app_id=(\d+)/.exec(process.env.STREAM_URL);
apiKey = parts[1];
Expand Down
93 changes: 61 additions & 32 deletions src/lib/batch_operations.js
Expand Up @@ -4,47 +4,76 @@ var errors = require('./errors');

module.exports = {
addToMany: function(activity, feeds, callback) {
var req = this.makeSignedRequest({
url: 'feed/add_to_many/',
body: {
activity: activity,
feeds: feeds,
},
}, callback);
/**
* Add one activity to many feeds
* @method addToMany
* @memberof StreamClient.prototype
* @since 2.3.0
* @param {object} activity The activity to add
* @param {Array} feeds Array of objects describing the feeds to add to
* @param {requestCallback} callback Callback called on completion
* @return {object} XHR request object
*/
var req = this.makeSignedRequest({
url: 'feed/add_to_many/',
body: {
activity: activity,
feeds: feeds,
},
}, callback);

return req;
},
return req;
},

followMany: function(follows, callback) {
var req = this.makeSignedRequest({
url: 'follow_many/',
body: follows,
}, callback);
/**
* Follow multiple feeds with one API call
* @method followMany
* @memberof StreamClient.prototype
* @since 2.3.0
* @param {Array} follows The follow relations to create
* @param {requestCallback} callback Callback called on completion
* @return {object} XHR request object
*/
var req = this.makeSignedRequest({
url: 'follow_many/',
body: follows,
}, callback);

return req;
},
return req;
},

makeSignedRequest: function(kwargs, cb) {
if (!this.apiSecret) {
throw new errors.SiteError('Missing secret, which is needed to perform signed requests, use var client = stream.connect(key, secret);');
}
/**
* Method to create request to api with application level authentication
* @method makeSignedRequest
* @memberof StreamClient.prototype
* @since 2.3.0
* @access private
* @param {object} kwargs Arguments for the request
* @param {requestCallback} cb Callback to call on completion
* @return {object} XHR request object
*/
if (!this.apiSecret) {
throw new errors.SiteError('Missing secret, which is needed to perform signed requests, use var client = stream.connect(key, secret);');
}

this.send('request', 'post', kwargs, cb);
this.send('request', 'post', kwargs, cb);

kwargs.url = this.enrichUrl(kwargs.url);
kwargs.json = true;
kwargs.method = 'POST';
kwargs.headers = { 'X-Api-Key': this.apiKey };
kwargs.url = this.enrichUrl(kwargs.url);
kwargs.json = true;
kwargs.method = 'POST';
kwargs.headers = { 'X-Api-Key': this.apiKey };

var callback = this.wrapCallback(cb);
var req = request(kwargs, callback);
var callback = this.wrapCallback(cb);
var req = request(kwargs, callback);

httpSignature.sign(req, {
algorithm: 'hmac-sha256',
key: this.apiSecret,
keyId: this.apiKey,
});
httpSignature.sign(req, {
algorithm: 'hmac-sha256',
key: this.apiSecret,
keyId: this.apiKey,
});

return request;
},
return request;
},
};
23 changes: 15 additions & 8 deletions src/lib/client.js
Expand Up @@ -5,11 +5,18 @@ var errors = require('./errors');
var utils = require('./utils');
var BatchOperations = require('./batch_operations');

/**
* @callback requestCallback
* @param {object} [errors]
* @param {object} response
* @param {object} body
*/

var StreamClient = function() {
/**
* Client to connect to Stream api
* @class StreamClient
*/
*/
this.initialize.apply(this, arguments);
};

Expand All @@ -22,7 +29,7 @@ StreamClient.prototype = {
* @method intialize
* @memberof StreamClient.prototype
* @param {string} apiKey - the api key
* @param {string} [apiSecret] - the api secret
* @param {string} [apiSecret] - the api secret
* @param {string} [appId] - id of the app
* @param {object} options - additional options
* @param {string} options.location - which data center to use
Expand Down Expand Up @@ -145,8 +152,8 @@ StreamClient.prototype = {
/**
* Returns a token that allows only read operations
*
* @method getReadOnlyToken
* @memberOf StreamClient.prototype
* @method getReadOnlyToken
* @memberof StreamClient.prototype
* @param {string} feedSlug - The feed slug to get a read only token for
* @param {string} userId - The user identifier
* @return {string} token
Expand All @@ -162,7 +169,7 @@ StreamClient.prototype = {
* Returns a token that allows read and write operations
*
* @method getReadWriteToken
* @memberOf StreamClient.prototype
* @memberof StreamClient.prototype
* @param {string} feedSlug - The feed slug to get a read only token for
* @param {string} userId - The user identifier
* @return {string} token
Expand All @@ -177,14 +184,14 @@ StreamClient.prototype = {
/**
* Returns a feed object for the given feed id and token
* @method feed
* @memberOf StreamClient.prototype
* @memberof StreamClient.prototype
* @param {string} feedSlug - The feed slug
* @param {string} userId - The user identifier
* @param {string} [token] - The token
* @param {string} [siteId] - The site identifier
* @param {object} [options] - Additional function options
* @param {object} [options] - Additional function options
* @param {boolean} [options.readOnly] - A boolean indicating whether to generate a read only token for this feed
* @return {Feed}
* @return {StreamFeed}
* @example
* client.feed('user', '1', 'token2');
*/
Expand Down
20 changes: 19 additions & 1 deletion src/lib/errors.js
Expand Up @@ -3,6 +3,13 @@ var errors = module.exports;
var canCapture = (typeof Error.captureStackTrace === 'function');
var canStack = !!(new Error()).stack;

/**
* Abstract error object
* @class ErrorAbstract
* @access private
* @param {string} [msg] Error message
* @param {function} constructor
*/
function ErrorAbstract(msg, constructor) {
this.message = msg;

Expand All @@ -22,6 +29,10 @@ ErrorAbstract.prototype = new Error();

/**
* FeedError
* @class FeedError
* @access private
* @extends ErrorAbstract
* @memberof Stream.errors
* @param {String} [msg] - An error message that will probably end up in a log.
*/
errors.FeedError = function FeedError(msg) {
Expand All @@ -30,9 +41,16 @@ errors.FeedError = function FeedError(msg) {

errors.FeedError.prototype = new ErrorAbstract();

/**
* SiteError
* @class SiteError
* @access private
* @extends ErrorAbstract
* @memberof Stream.errors
* @param {string} [msg] An error message that will probably end up in a log.
*/
errors.SiteError = function SiteError(msg) {
ErrorAbstract.call(this, msg);
};

errors.SiteError.prototype = new ErrorAbstract();

0 comments on commit 10f6456

Please sign in to comment.