Mailspring/internal_packages/onboarding/lib/token-auth-api.coffee
Ben Gotow 51524ba38c fix(onboarding): Invitation code system, just in case
commit 64938016f6ffbf366a220e7abd9af6f7a4cb478b
Author: Ben Gotow <bengotow@gmail.com>
Date:   Sun Oct 4 16:42:38 2015 -0700

    Don't allow people to double click the token check button, make requests take at least 400msec

commit e548f3ee449c676a813c5630f1624963872ed6e6
Merge: 3350b91 e4b4933
Author: Ben Gotow <bengotow@gmail.com>
Date:   Sun Oct 4 16:39:56 2015 -0700

    Merge branch 'token-auth' of github.com:nylas/N1 into token-auth

    # Conflicts:
    #	internal_packages/onboarding/lib/token-auth-page.cjsx

commit 3350b917449c29299fa078d59a4a5a9339fdf29b
Author: Ben Gotow <bengotow@gmail.com>
Date:   Sun Oct 4 16:38:52 2015 -0700

    Improve a few error states, adding "checking" state when checking token

commit e4b49334cbf59145d9bdd955d35636f16a7c4924
Author: EthanBlackburn <ethan@nylas.com>
Date:   Sun Oct 4 16:21:39 2015 -0700

    Correct retry behavior

commit 11cd9a75b2a1ca0f4347160df93815743909ccea
Author: EthanBlackburn <ethan@nylas.com>
Date:   Sat Oct 3 18:06:55 2015 -0700

    Removed old auth token variable

commit afe451cd70de528def3443d8b373fd24f4aa5cde
Author: EthanBlackburn <ethan@nylas.com>
Date:   Sat Oct 3 16:08:12 2015 -0700

    Added token auth page
2015-10-04 16:49:41 -07:00

58 lines
1.8 KiB
CoffeeScript

nodeRequest = require 'request'
{Actions, Utils, APIError} = require 'nylas-exports'
class TokenAuthAPI
constructor: ->
atom.config.onDidChange('env', @_onConfigChanged)
@_onConfigChanged()
@
_onConfigChanged: =>
env = atom.config.get('env')
if env is 'development'
@APIRoot = "http://localhost:6001"
else if env in ['experimental', 'staging']
@APIRoot = "https://invite-staging.nylas.com"
else
@APIRoot = "https://invite.nylas.com"
request: (options={}) ->
return if atom.getLoadSettings().isSpec
options.method ?= 'GET'
options.url ?= "#{@APIRoot}#{options.path}" if options.path
options.body ?= {} unless options.formData
options.json = true
options.error ?= @_defaultErrorCallback
# This is to provide functional closure for the variable.
rid = Utils.generateTempId()
[rid].forEach (requestId) ->
options.startTime = Date.now()
Actions.willMakeAPIRequest({
request: options,
requestId: requestId
})
nodeRequest options, (error, response, body) ->
statusCode = response?.statusCode
Actions.didMakeAPIRequest({
request: options,
statusCode: statusCode,
error: error,
requestId: requestId
})
if error? or statusCode > 299
if not statusCode or statusCode in [-123, 500]
body = "Sorry, we could not reach the Nylas API. Please try
again, or email us if you continue to have trouble.
(#{statusCode})"
options.error(new APIError({error:error, response:response, body:body, requestOptions: options}))
else
options.success(body) if options.success
_defaultErrorCallback: (apiError) ->
console.error(apiError)
module.exports = new TokenAuthAPI