fix(nylas-api): Send API and options as object

This commit is contained in:
Jackie Luo 2016-11-29 14:44:34 -08:00
parent 5eb401d5ed
commit 623b6a7c7c
6 changed files with 61 additions and 43 deletions

View file

@ -52,6 +52,7 @@ export function makeGmailOAuthRequest(sessionKey, callback) {
settings: { settings: {
xoauth2: remoteJSON.resolved_settings.xoauth2, xoauth2: remoteJSON.resolved_settings.xoauth2,
}, },
success: callback,
}, },
error: callback, error: callback,
success: (localJSON) => { success: (localJSON) => {

View file

@ -58,9 +58,9 @@ export default class OAuthSignInPage extends React.Component {
poll = () => { poll = () => {
this.props.makeRequest(this.props.sessionKey, (err, json) => { this.props.makeRequest(this.props.sessionKey, (err, json) => {
clearTimeout(this._pollTimer); clearTimeout(this._pollTimer);
if (json) { if (json && json.body) {
ipcRenderer.removeListener('browser-window-focus', onWindowFocused); ipcRenderer.removeListener('browser-window-focus', onWindowFocused);
this.props.onSuccess(json); this.props.onSuccess(json.body);
} else { } else {
delay = Math.min(delay * 1.2, 10000); delay = Math.min(delay * 1.2, 10000);
this._pollTimer = setTimeout(poll, delay); this._pollTimer = setTimeout(poll, delay);

View file

@ -48,7 +48,10 @@ class EdgehillAPI {
}; };
} }
const req = new NylasAPIRequest(this, options); const req = new NylasAPIRequest({
api: this,
options,
});
return req.run(); return req.run();
} }
} }

View file

@ -10,7 +10,7 @@ import IdentityStore from './stores/identity-store'
import NylasAPI from './nylas-api' import NylasAPI from './nylas-api'
export default class NylasAPIRequest { export default class NylasAPIRequest {
constructor(api, options) { constructor({api, options}) {
const defaults = { const defaults = {
url: `${options.APIRoot || api.APIRoot}${options.path}`, url: `${options.APIRoot || api.APIRoot}${options.path}`,
method: 'GET', method: 'GET',

View file

@ -1,6 +1,7 @@
_ = require 'underscore' _ = require 'underscore'
{remote, shell} = require 'electron' {remote, shell} = require 'electron'
NylasLongConnection = require('./nylas-long-connection').default NylasLongConnection = require('./nylas-long-connection').default
NylasAPIRequest = require('./nylas-api-request').default
Utils = require './models/utils' Utils = require './models/utils'
Account = require('./models/account').default Account = require('./models/account').default
Message = require('./models/message').default Message = require('./models/message').default
@ -20,7 +21,6 @@ SampleTemporaryErrorCode = 504
# This is lazy-loaded # This is lazy-loaded
AccountStore = null AccountStore = null
NylasAPIRequest = null
class NylasAPIChangeLockTracker class NylasAPIChangeLockTracker
constructor: -> constructor: ->
@ -255,26 +255,32 @@ class NylasAPI
getCollection: (accountId, collection, params={}, requestOptions={}) -> getCollection: (accountId, collection, params={}, requestOptions={}) ->
throw (new Error "getCollection requires accountId") unless accountId throw (new Error "getCollection requires accountId") unless accountId
requestSuccess = requestOptions.success requestSuccess = requestOptions.success
@makeRequest _.extend requestOptions, new NylasAPIRequest({
path: "/#{collection}" api: @
accountId: accountId options: _.extend requestOptions,
qs: params path: "/#{collection}"
returnsModel: false accountId: accountId
success: (jsons) => qs: params
@_attachMetadataToResponse(jsons, requestOptions.metadataToAttach) returnsModel: false
@_handleModelResponse(jsons) success: (jsons) =>
if requestSuccess @_attachMetadataToResponse(jsons, requestOptions.metadataToAttach)
requestSuccess(jsons) @_handleModelResponse(jsons)
if requestSuccess
requestSuccess(jsons)
}).run()
makeDraftDeletionRequest: (draft) -> makeDraftDeletionRequest: (draft) ->
return unless draft.serverId return unless draft.serverId
@incrementRemoteChangeLock(Message, draft.serverId) @incrementRemoteChangeLock(Message, draft.serverId)
@makeRequest new NylasAPIRequest({
path: "/drafts/#{draft.serverId}" api: @
accountId: draft.accountId options:
method: "DELETE" path: "/drafts/#{draft.serverId}"
body: {version: draft.version} accountId: draft.accountId
returnsModel: false method: "DELETE"
body: {version: draft.version}
returnsModel: false
}).run()
return return
incrementRemoteChangeLock: (klass, id) -> incrementRemoteChangeLock: (klass, id) ->
@ -341,13 +347,14 @@ class NylasAPI
if NylasEnv.config.get(cacheKey) if NylasEnv.config.get(cacheKey)
return Promise.resolve() return Promise.resolve()
return @makeRequest({ return new NylasAPIRequest({
returnsModel: false, api: @
method: "GET", options:
accountId: account.id, returnsModel: false,
path: "/auth/plugin?client_id=#{pluginId}" method: "GET",
accountId: account.id,
}).then (result) => path: "/auth/plugin?client_id=#{pluginId}"
}).run().then (result) =>
if result.authed if result.authed
NylasEnv.config.set(cacheKey, Date.now()) NylasEnv.config.set(cacheKey, Date.now())
return Promise.resolve() return Promise.resolve()
@ -357,14 +364,16 @@ class NylasAPI
# #
# return @_requestPluginAuth(pluginName, account).then => # return @_requestPluginAuth(pluginName, account).then =>
return @makeRequest({ return new NylasAPIRequest({
returnsModel: false, api: @
method: "POST", options:
accountId: account.id, returnsModel: false,
path: "/auth/plugin", method: "POST",
body: {client_id: pluginId}, accountId: account.id,
json: true path: "/auth/plugin",
}).then => body: {client_id: pluginId},
json: true
}).run().then =>
NylasEnv.config.set(cacheKey, Date.now()) NylasEnv.config.set(cacheKey, Date.now())
return Promise.resolve() return Promise.resolve()
@ -387,11 +396,13 @@ You can review and revoke Offline Access for plugins at any time from Preference
) )
unauthPlugin: (pluginId, accountId) -> unauthPlugin: (pluginId, accountId) ->
return @makeRequest({ return new NylasAPIRequest({
returnsModel: false, api: @
method: "DELETE", options:
accountId: accountId, returnsModel: false,
path: "/auth/plugin?client_id=#{pluginId}" method: "DELETE",
}) accountId: accountId,
path: "/auth/plugin?client_id=#{pluginId}"
}).run()
module.exports = new NylasAPI() module.exports = new NylasAPI()

View file

@ -24,7 +24,10 @@ class N1CloudAPI {
pass: '', pass: '',
} }
const req = new NylasAPIRequest(this, options); const req = new NylasAPIRequest({
api: this,
options,
});
return req.run(); return req.run();
} }
} }