Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created June 22, 2019 04:30
Show Gist options
  • Save jsmanifest/a31fd7bc3e67449ffe5078915837a08a to your computer and use it in GitHub Desktop.
Save jsmanifest/a31fd7bc3e67449ffe5078915837a08a to your computer and use it in GitHub Desktop.
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