-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OS Map Layer #758
base: main
Are you sure you want to change the base?
Add OS Map Layer #758
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// copied from https://github.com/digital-land/digital-land.info/blob/main/assets/javascripts/osApiToken.js | ||
|
||
const API_ACCESS_TOKEN_URL = '/api/os/get-access-token' | ||
let apiToken = { | ||
access_token: '', | ||
expires_in: 0, | ||
issued_at: 0 | ||
} | ||
|
||
let makingRequest = false | ||
|
||
export const getApiToken = () => { | ||
const tokenCheckBuffer = 30 * 1000 | ||
const tokenExpires = parseInt(apiToken.expires_in) * 1000 + parseInt(apiToken.issued_at) | ||
if (Date.now() > tokenExpires - tokenCheckBuffer && !makingRequest) { | ||
getFreshApiToken() | ||
} | ||
return apiToken.access_token | ||
} | ||
Comment on lines
+3
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Synchronous vs. asynchronous token refresh |
||
|
||
export const getFreshApiToken = () => { | ||
return new Promise((resolve, reject) => { | ||
makingRequest = true | ||
fetch(API_ACCESS_TOKEN_URL) | ||
.then(res => res.json()) | ||
.then(res => { | ||
apiToken = res | ||
makingRequest = false | ||
resolve(apiToken.access_token) | ||
}) | ||
.catch(err => { | ||
makingRequest = false | ||
reject(err) | ||
}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure the token is fetched by awaiting
getFreshApiToken()
somewhere increateMapFromServerContext()
(or before it's called).It sucks that the request transform function can't be async, so no straightforward way to refresh it here 😔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We call
await getApiToken()
in the initialisation script - with calls bothThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, unfortunately there's a race condition.
getApiToken()
is a synchronous function (which the initialisation codeawait
s; it should't). That means it returns a value immediately, possibly before the promise ofgetFreshApiToken()
completes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see the problem - good catch, will update