UNPKG

hellojs-xiaotian

Version:

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

166 lines (131 loc) 2.92 kB
(function(hello) { hello.init({ joinme: { name: 'join.me', oauth: { version: 2, auth: 'https://secure.join.me/api/public/v1/auth/oauth2', grant: 'https://secure.join.me/api/public/v1/auth/oauth2' }, refresh: false, scope: { basic: 'user_info', user: 'user_info', scheduler: 'scheduler', start: 'start_meeting', email: '', friends: '', share: '', publish: '', photos: '', publish_files: '', files: '', videos: '', offline_access: '' }, scope_delim: ' ', login: function(p) { p.options.popup.width = 400; p.options.popup.height = 700; }, base: 'https://api.join.me/v1/', get: { me: 'user', meetings: 'meetings', 'meetings/info': 'meetings/@{id}' }, post: { 'meetings/start/adhoc': function(p, callback) { callback('meetings/start'); }, 'meetings/start/scheduled': function(p, callback) { var meetingId = p.data.meetingId; p.data = {}; callback('meetings/' + meetingId + '/start'); }, 'meetings/schedule': function(p, callback) { callback('meetings'); } }, patch: { 'meetings/update': function(p, callback) { callback('meetings/' + p.data.meetingId); } }, del: { 'meetings/delete': 'meetings/@{id}' }, wrap: { me: function(o, headers) { formatError(o, headers); if (!o.email) { return o; } o.name = o.fullName; o.first_name = o.name.split(' ')[0]; o.last_name = o.name.split(' ')[1]; o.id = o.email; return o; }, 'default': function(o, headers) { formatError(o, headers); return o; } }, xhr: formatRequest } }); function formatError(o, headers) { var errorCode; var message; var details; if (o && ('Message' in o)) { message = o.Message; delete o.Message; if ('ErrorCode' in o) { errorCode = o.ErrorCode; delete o.ErrorCode; } else { errorCode = getErrorCode(headers); } o.error = { code: errorCode, message: message, details: o }; } return o; } function formatRequest(p, qs) { // Move the access token from the request body to the request header var token = qs.access_token; delete qs.access_token; p.headers.Authorization = 'Bearer ' + token; // Format non-get requests to indicate json body if (p.method !== 'get' && p.data) { p.headers['Content-Type'] = 'application/json'; if (typeof (p.data) === 'object') { p.data = JSON.stringify(p.data); } } if (p.method === 'put') { p.method = 'patch'; } return true; } function getErrorCode(headers) { switch (headers.statusCode) { case 400: return 'invalid_request'; case 403: return 'stale_token'; case 401: return 'invalid_token'; case 500: return 'server_error'; default: return 'server_error'; } } }(hello));