API Docs for:
Show:

File: lib/ncmb.js

"use strict";

var localStorage = null;
if(typeof window !== "undefined"){
  if(window.localStorage){
    localStorage = window.localStorage;
  }else{
    localStorage = require("localforage");
  }
}else{
  localStorage = new require("node-localstorage").LocalStorage("./scratch");
}

/**
* すべてのNCMBクラスおよびメソッドを定義します。
*
* @class NCMB
* @param {string} apikey アプリケーションキー。必須
* @param {string} clientkey クライアントキー。必須
* @param {Object} [config] 通信設定。省略可能
*/
var NCMB = module.exports = (function(){
  var modifiables = [
    "collections", "version", "scriptVersion", "fqdn", "scriptFqdn", "port", "protocol", "proxy", "stub",
    "signatureMethod", "signatureVersion", "apikey", "clientkey"];
  var isModifiable = function(key){
    if(modifiables.indexOf(key) === -1) return false;
    return true;
  };

  function NCMB (apikey, clientkey, config){
    if(!apikey || !clientkey){
      throw new Error("apikey and clientkey required");
    }

    this.apikey           = apikey;
    this.clientkey        = clientkey;

    if(!config) config = {};
    this.collections      = config.collections || {};
    this.version          = config.version || "2013-09-01";
    this.scriptVersion    = config.scriptVersion || "2015-09-01";
    this.fqdn             = config.fqdn || "mbaas.api.nifcloud.com";
    this.scriptFqdn       = config.scriptFqdn || "script.mbaas.api.nifcloud.com";
    this.port             = config.port || 443;
    this.protocol         = config.protocol || "https:";
    this.signatureMethod  = config.signatureMethod || "HmacSHA256";
    this.signatureVersion = config.signatureVersion || 2;
    this.stub             = config.stub || false;

    /** @member {NCMB.UserConstructor} NCMB#User */
    /** @member {NCMB.RoleConstructor} NCMB#Role */
    /** @member {NCMB.File} NCMB#File */
    /** @member {NCMB.PushConstructor} NCMB#Push */
    /** @member {NCMB.InstallationConstructor} NCMB#Installation */
    /** @member {NCMB.ScriptConstructor} NCMB#Script */
    /** @member {NCMB.AclConstructor} NCMB#Acl */
    /** @member {NCMB.GeoPointConstructor} NCMB#GeoPoint */
    /** @member {NCMB.RelationConstructor} NCMB#Relation */

    var tempPrototype          = Object.getPrototypeOf(this);
    tempPrototype.User         = require("./user")(this);
    tempPrototype.Role         = require("./role")(this);
    tempPrototype.File         = require("./file")(this);
    tempPrototype.Push         = require("./push")(this);
    tempPrototype.Installation = require("./installation")(this);
    tempPrototype.Script       = require("./script")(this);
    tempPrototype.Acl          = require("./acl")(this);
    tempPrototype.GeoPoint     = require("./geopoint")(this);
    tempPrototype.Relation     = require("./relation")(this);
    tempPrototype.Errors       = require("./errors");
    Object.setPrototypeOf(this,tempPrototype);
  }

  /**
  * 指定したキーに値を設定します。
  *
  * @method NCMB#set
  * @param {string} key 値を設定したいキー
  * @param value キーに設定する値
  * @return {this}
  */
  NCMB.prototype.set = function(key, val){
    if(!isModifiable(key))
      throw new this.Errors.UnmodifiableVariableError(key + " cannot be set, it is reserved.");
    if(typeof val !== "undefined") this[key] = val;
    return this;
  };

  /**
  * 指定したキー設定されている値を取得します。
  *
  * @method NCMB#get
  * @param {string} key 値を取得したいキー
  * @return {any} this[key] keyに対応する値
  */
  NCMB.prototype.get = function(key){
    return this[key];
  };

  /**
  * レスポンスシグネチャーをチェック許可を設定します。
  *
  * @method NCMB#enableResponseValidation
  * @param {boolean} value true/falseでチェック可否設定する値
  */
  NCMB.prototype.enableResponseValidation = function(value){
    if (typeof value === "boolean") {
      localStorage.setItem('sResponseValidation', value);
    }
  };

  /**
  * レスポンスシグネチャーをチェック許可設定を取得します
  *
  * @method NCMB#getResponseValidation
  * @return {object} sResponseValidation レスポンスシグネチャーをチェック可否設定する値
  */
  NCMB.prototype.getResponseValidation = function(){
    var sResponseValidation = localStorage.getItem('sResponseValidation');
    return JSON.parse(sResponseValidation);
  };

  NCMB.prototype.createSignature = require("./signature").create;

  NCMB.prototype.request = require("./request");
  NCMB.prototype.requestScript = require("./request_script");

  /** 
   * @method NCMB#DataStore 
   * @param {string} name
   * @return {NCMB.DataStoreConstructor} 
  */
  NCMB.prototype.DataStore = require("./datastore");
  return NCMB;
})();

if (typeof define === 'function' && define.amd) {
  define([], NCMB);
}
if(typeof window !== "undefined"){
  window.NCMB = NCMB;
}