-
Notifications
You must be signed in to change notification settings - Fork 548
/
github.js
102 lines (79 loc) · 1.91 KB
/
github.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(function(hello) {
hello.init({
github: {
name: 'GitHub',
oauth: {
version: 2,
auth: 'https://github.com/login/oauth/authorize',
grant: 'https://github.com/login/oauth/access_token',
response_type: 'code'
},
scope: {
email: 'user:email'
},
base: 'https://api.github.com/',
get: {
me: 'user',
'me/friends': 'user/following?per_page=@{limit|100}',
'me/following': 'user/following?per_page=@{limit|100}',
'me/followers': 'user/followers?per_page=@{limit|100}',
'me/like': 'user/starred?per_page=@{limit|100}'
},
wrap: {
me: function(o, headers) {
formatError(o, headers);
formatUser(o);
return o;
},
'default': function(o, headers, req) {
formatError(o, headers);
if (Array.isArray(o)) {
o = {data: o};
}
if (o.data) {
paging(o, headers, req);
o.data.forEach(formatUser);
}
return o;
}
},
xhr: function(p) {
if (p.method !== 'get' && p.data) {
// Serialize payload as JSON
p.headers = p.headers || {};
p.headers['Content-Type'] = 'application/json';
if (typeof (p.data) === 'object') {
p.data = JSON.stringify(p.data);
}
}
return true;
}
}
});
function formatError(o, headers) {
var code = headers ? headers.statusCode : (o && 'meta' in o && 'status' in o.meta && o.meta.status);
if ((code === 401 || code === 403)) {
o.error = {
code: 'access_denied',
message: o.message || (o.data ? o.data.message : 'Could not get response')
};
delete o.message;
}
}
function formatUser(o) {
if (o.id) {
o.thumbnail = o.picture = o.avatar_url;
o.name = o.login;
}
}
function paging(res, headers, req) {
if (res.data && res.data.length && headers && headers.Link) {
var next = headers.Link.match(/<(.*?)>;\s*rel=\"next\"/);
if (next) {
res.paging = {
next: next[1]
};
}
}
}
})(hello);