Created
June 22, 2019 04:30
-
-
Save jsmanifest/a31fd7bc3e67449ffe5078915837a08a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Api = function(params) { | |
const _store_ = { | |
accessToken: null, | |
} | |
return { | |
getAccessToken() { | |
return _store.accessToken | |
}, | |
async login() { | |
try { | |
const response = await axios.post( | |
'https://something.com/v1/auth', | |
params, | |
) | |
return response.data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async refreshToken() { | |
try { | |
const response = await axios.post( | |
'https://something.com/v1/access_token/', | |
params, | |
) | |
const { token } = response.data | |
_store.accessToken = token | |
return token | |
} catch (error) { | |
throw error | |
} | |
}, | |
setAccessToken(token) { | |
if (token === undefined) { | |
throw new Error('token is undefined') | |
} | |
_store.accessToken = token | |
}, | |
// ...other methods | |
} | |
} | |
const api = Api({ | |
username: 'bob', | |
password: 'the_builder123', | |
}) | |
api | |
.refreshToken()) | |
.then((token) => console.log(token)) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment