UNPKG

imf-data-nodejs-sdk

Version:
197 lines (168 loc) 5.43 kB
/** * Copyright 2015 IBM Corp. All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; /* jshint node: true */ var errs = require('errs'), cloudant = require('cloudant'), request = require('request'), imf = require('imf-oauth-user-sdk'), Errors = require('./Errors.js'), url = require('url'), util = require('util'), follow = require('follow'), _ = require('underscore'), i18n = require('i18next'); // Modify Feed.follow prototype to inject AMA authorization header var oldFollow = follow.Feed.prototype.follow; follow.Feed.prototype.follow = function() { var self = this; imf.getAuthorizationHeader(security).then(function(token) { var request = self.request || {}; request.headers = request.headers || {}; request.headers.Authorization = token; self.request = request; oldFollow.apply(self); }, function(err) { throw err; }).done(); }; var security, protocol, domain, APP_ID, IMFData, nano = {}; function initialize(config, callback) { if (!(config && config.appId)) throw errs.create('imfdata_config'); APP_ID = config.appId; // Set optional values or their defaults protocol = config.protocol || 'https'; domain = config.domain || 'ng.bluemix.net'; // If it's a bluemix domain make sure it hits mobile if (/bluemix/.test(domain.toLowerCase())){ domain = 'mobile.' + domain; } security = config.AdvancedMobileAccess || { cacheSize: 1000 }; // Manipulate security if (!_.isEmpty(security.tenantId)) { security.appId = security.tenantId; delete security.tenantId; } IMFData = cloudant({ url: protocol + '://' + domain, request: IMFDataRequest }); // Set language config.language = config.language || 'en-US'; var resStore = {}; resStore[config.language] = { translation: require('../locales/' + config.language + '/translation.json' ) }; i18n.init({resStore: resStore, fallbackLng: 'en-US'}); // Deprecate Cloudant specific functions IMFData.generate_api_key = util.deprecate(IMFData.generate_api_key, i18n.t('messages.generate_api_key')); IMFData.auth = util.deprecate(IMFData.auth, i18n.t('messages.auth')); nano.use = IMFData.use; IMFData.set_permissions = set_permissions; IMFData.remove_permissions = remove_permissions; IMFData.use = use_db; IMFData.scope = use_db; IMFData.db.use = use_db; IMFData.db.scope = use_db; IMFData.db.follow = follow_db; delete IMFData.config.request_defaults; IMFData.config.url = IMFData.config.url + '/imfdata/api/v1/apps/' + APP_ID + '/'; if (callback) { IMFData.request({db:''},function(err, response) { if (err) { callback(err); } else { callback(null, IMFData, response); } }); } return IMFData; } // Inject/Override functions in db module function use_db(name) { var db = nano.use(name); db.follow = function(params, callback) { return follow_db(db.config.db, params, callback); }; db.spatial = util.deprecate(db.spatial, i18n.t('messages.spatial')); return db; } // Replaces account.db.follow and db.follow function follow_db(name, params, callback) { if (typeof params === 'function') { callback = params; params = {}; } params = params || {}; params.db = protocol + '://' + domain + '/imfdata/api/v1/apps/' + APP_ID + '/' + encodeURIComponent(name); if (typeof callback === 'function') { return follow(params, callback); } else { return new follow.Feed(params); } } /** * Sets user permissions for a database */ function set_permissions(payload, callback) { if (!(payload && payload.identity && payload.database && payload.access)) throw errs.create('imfdata_set_permissions'); IMFData.request({ db: '_set_permissions', content_type: 'application/json', method: 'POST', body: payload }, callback); } function remove_permissions(payload, callback) { if (!(payload && payload.identity && payload.database && payload.access)) throw errs.create('imfdata_remove_permissions'); IMFData.request({ db: '_set_permissions', content_type: 'application/json', method: 'DELETE', body: payload }, callback); } /** * Replaces request module used by nano to use imfdata url and inject Authorization header */ function IMFDataRequest(options, callback) { imf.getAuthorizationHeader(security).then(function(token) { options.headers.Authorization = token; request(options, callback); }, function(err) { callback(err); }); } /** * Permissions constants */ var permissions = { ADMINS: 'admins', MEMBERS: 'members', PUBLIC: 'public' }; exports.initialize = initialize; exports.permissions = permissions;