UNPKG

hellojs-xiaotian

Version:

A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)

103 lines (79 loc) 1.96 kB
(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);