日本製BaaSのappiaries(アピアリーズ)をJavaScriptで利用するためのライブラリ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('appiary.1.0.1.js'); | |
/****** | |
for | |
JavaScript appiaries appli | |
constructor | |
Appiary(datastore_id,app_id,app_token,client_id,scope) | |
[propaties] app, app_token, datastore_url, oauth_url, profile_url, store_token, uset_token | |
[methods] collection(collection_id,type), getUser(settings) | |
collection Object | |
[propaties] same as Appiary | |
[methods] x(settings) | |
class.method | |
Appiary.x(url,settings) | |
Appiary.create(tagName,attrs,parent,next) | |
*******/ | |
Appiary = function(datastore,app_id,app_token,client_id,scope){ | |
this.datastore_url = 'https://api-datastore.appiaries.com/v1/'; | |
var oauth_url = 'https://api-oauth.appiaries.com/v1/auth?response_type=token&client_id=' + client_id; | |
if(scope)oauth_url += '&scope='+scope; | |
this.oauth_url = oauth_url; | |
this.app = datastore+'/'+app_id; | |
this.profile_url = 'https://api-profiles.appiaries.com/v1/'+this.app; | |
var access_token = ''; | |
var store_token = ''; | |
if(location.hash) { | |
console.log('[location.hash] ' + location.hash); // Debug | |
if(location.hash.match(/[#&\?]access_token=([a-zA-Z0-9]+)/)){ | |
access_token = RegExp.$1; | |
console.log('[access_token] ' + access_token);//Debug | |
} | |
if(location.hash.match(/[#&\?]store_token=([a-zA-Z0-9]+)/)){ | |
store_token = RegExp.$1; | |
console.log('[store_token] ' + store_token);//Debug | |
} | |
} | |
this.app_token = app_token; | |
this.store_token = store_token; | |
this.user_token = access_token; | |
return this; | |
} | |
Appiary.prototype.getUser = function(settings){ | |
if(this.user_token&&settings){ | |
settings.data = settings.data||{}; | |
settings.data.access_token = this.user_token; | |
Appiary.x(this.profile_url,settings); | |
} | |
} | |
Appiary.prototype.collection=function(collection_id,type){ | |
var obj={}; | |
for(var k in this){ | |
obj[k] = this[k]; | |
} | |
obj.type=type||'dat'; | |
obj.collection=collection_id; | |
obj.x=function(settings){ | |
settings.requestHeader={"Content-Type":"application/json","X-Appiaries-Token":this.store_token||this.app_token}; | |
var url = this.datastore_url+this.type+'/'+this.app+'/'+collection_id+ '/'; | |
Appiary.x(url,settings); | |
} | |
return obj; | |
} | |
Appiary.x = function(url,settings){ | |
var x = new XMLHttpRequest(); | |
var async = true,data=null,method = 'GET'; | |
for(var s in settings){ | |
switch(s){ | |
case 'async': | |
async = settings[s];break; | |
case 'data': | |
data = settings[s];break; | |
case 'method': | |
method = settings[s];break; | |
case 'path': | |
url += settings[s];break; | |
case 'requestHeader': | |
break; | |
default: | |
x[s] = settings[s]; | |
} | |
} | |
if(method=='GET'&&data){ | |
if(url.indexOf('?')==-1)url+='?'; | |
var pairs = []; | |
for(var k in data){ | |
pairs.push(k+'='+encodeURIComponent(data[k])); | |
} | |
url+=pairs.join('&'); | |
data = null; | |
} | |
x.open(method,url,async); | |
console.log('x.open( '+method+','+url+','+async+' )'); | |
for(var k in settings['requestHeader']){ | |
console.log('requestHeader.'+k+':'+settings['requestHeader'][k]); | |
x.setRequestHeader(k,settings['requestHeader'][k]); | |
} | |
data = JSON.stringify(data); | |
console.log('x.send( '+data+ ')'); | |
x.send(data); | |
} | |
Appiary.create = function(tagName,attrs,parent,next){ | |
var elm = null; | |
if(tagName=='text'&&typeof attrs=='string'){ | |
elm = document.createTextNode(attrs); | |
}else{ | |
elm=document.createElement(tagName); | |
for(var k in attrs){ | |
if(typeof attrs[k]!='object'){ | |
elm.setAttribute(k,attrs[k]); | |
} | |
} | |
} | |
if(parent)parent.insertBefore(elm,next||null); | |
return elm; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment