Skip to content

Instantly share code, notes, and snippets.

@darioriverat
Last active March 8, 2022 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darioriverat/fddf9be57d7a7f61ce5c50c34c4ec56d to your computer and use it in GitHub Desktop.
Save darioriverat/fddf9be57d7a7f61ce5c50c34c4ec56d to your computer and use it in GitHub Desktop.
Some class to get user Config from API in a browser
class Config {
endpoint = 'https://somewebsite.com/config';
constructor(userId) {
this.userId = userId;
}
async getValueFor(key) {
if (this.data) {
// getting from cache
return this.data[key];
}
return await fetch(`${this.endpoint}/${this.userId}`)
.then((response) => response.json())
.then((data) => {
this.data = data;
return this.data[key];
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment