forked from caged/octo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocto.min.js
5 lines (5 loc) · 2.68 KB
/
octo.min.js
1
2
3
4
5
/*!
* octo.js
* Copyright (c) 2012 Justin Palmer <[email protected]>
* MIT Licensed
*/(function(){typeof superagent=="undefined"&&require&&(superagent=require("superagent"),btoa=require("btoa"));var octo={};octo.api=function(){function api(){}function pager(method,path,params){function pager(){request()}var page=1,perpage=30,hasnext=!1,hasprev=!1,headers={},callbacks={},request=function(){var req=superagent[method](api.host()+path),complete=function(res){limit=~~res.header["x-ratelimit-limit"],remaining=~~res.header["x-ratelimit-remaining"];var link=res.header.link;hasnext=/rel=\"next\"/i.test(link),hasprev=/rel=\"next\"/.test(link),pager.trigger("end",res),res.ok&&pager.trigger("success",res),res.error&&pager.trigger("error",res)};token&&req.set("Authorization","token "+token),!token&&username&&password&&req.set("Authorization","Basic "+btoa(username+":"+password)),req.set(headers).query({page:page,per_page:perpage}).send(params).end(complete)};return pager.page=function(v){return arguments.length?(page=v,pager):page},pager.perpage=function(v){return arguments.length?(perpage=v,pager):perpage},pager.next=function(){return page+=1,request(),pager},pager.prev=function(){return page-=1,request(),pager},pager.hasnext=function(){return hasnext},pager.hasprev=function(){return hasprev},pager.on=function(event,callback){return typeof callbacks[event]=="undefined"&&(callbacks[event]=[]),callbacks[event].push(callback),pager},pager.off=function(event,callback){if(callbacks[event]instanceof Array){var cbacks=callbacks[event],i=0;for(i;i<cbacks.length;i++)if(cbacks[i]===callback){cbacks.splice(i,1);break}}return pager},pager.trigger=function(event,data){return callbacks[event]instanceof Array&&callbacks[event].forEach(function(callback){callback.call(pager,data)}),pager},pager.set=function(key,val){return headers[key]=val,pager},pager}var host="https://api.github.com",agent=superagent,limit,remaining,username,password,token;return api.host=function(val){return arguments.length?(host=val,api):host},api.get=function(path,params){return new pager("get",path)},api.post=function(path,params){return new pager("post",path,params)},api.patch=function(path,params){return new pager("patch",path,params)},api.put=function(path,params){return new pager("put",path,params)},api.delete=function(path,params){return new pager("delete",path,params)},api.limit=function(){return limit},api.remaining=function(){return remaining},api.username=function(v){return arguments.length?(username=v,api):username},api.password=function(v){return arguments.length?(password=v,api):password},api.token=function(v){return arguments.length?(token=v,api):token},api},"undefined"!=typeof exports?module.exports=octo:window.octo=octo})();