Artem Kobzar
02/16/2024, 4:45 PMHildebrandt Tobias
02/16/2024, 4:52 PMHildebrandt Tobias
02/16/2024, 5:02 PMRequired PromiseResult
Found Promise
Despite the inner function also just returning a Promise.
Guess I'll tinker some moreHildebrandt Tobias
02/16/2024, 5:28 PMPromise and now it's not. (UseQueryOptions from TanStackQuery Wrapper)
val queryOptions = jso<UseQueryOptions<Array<User>, Error, Array<User>, QueryKey>> {
queryKey = USERS_QUERY_KEY
queryFn = { _ ->
CoroutineScope(CoroutineName("")).promise {
getUsers(token)
}
}
}
Expected: PromiseResult Found Promise
Adding .then() and .catch() also doesn't help.
CoroutineScope(CoroutineName("")).promise {
getUsers(token)
}
.then { it }
.catch { emptyArray<User>() }
The signature of getUsers(token) is unchanged aside from adding suspend.
suspend fun getUsers(token: AuthJWT): Promise<Array<User>>Artem Kobzar
02/16/2024, 5:33 PMArtem Kobzar
02/16/2024, 5:36 PMsuspend fun getUsers(token: String): Array<User> =
axios.get(…).then { it.json() }.await()Artem Kobzar
02/16/2024, 5:36 PMHildebrandt Tobias
02/16/2024, 5:37 PMHildebrandt Tobias
02/16/2024, 5:42 PMHildebrandt Tobias
02/16/2024, 5:46 PM.asDeffered().asPromise() the compiler is not complaining, I wonder if it works.Artem Kobzar
02/16/2024, 5:48 PMHildebrandt Tobias
02/16/2024, 5:50 PMPromise one is LegacyPromise very confusing.Artem Kobzar
02/16/2024, 5:52 PMHildebrandt Tobias
02/16/2024, 5:53 PMkotlin-tanstack-react-query) and
create an object of UseQueryOptions and try to add a queryFn :
jso<UseQueryOptions<Array<User>, Error, Array<User>, QueryKey>> {
queryKey = USERS_QUERY_KEY
queryFn = { _ ->
CoroutineScope(CoroutineName("")).promise {
getUsers(token)
}
.asDeferred()
.asPromise()
}
}Hildebrandt Tobias
02/16/2024, 5:55 PMCoroutineScope.promise() targets org.jetbrains.kotlin:kotlin-stdlib-js:1.9.22
and the one that asPromise() targets is from org.jetbrains.kotlin-wrappers:kotlin-js-js:1.0.0-pre.690Artem Kobzar
02/16/2024, 5:58 PMturansky
02/16/2024, 6:02 PMjs.promise.Promise expected in typingsturansky
02/16/2024, 6:27 PMpromise extensionturansky
02/16/2024, 6:33 PMHildebrandt Tobias
02/16/2024, 6:36 PMHildebrandt Tobias
02/16/2024, 6:40 PMturansky
02/16/2024, 6:40 PMpromise extension :)Hildebrandt Tobias
02/16/2024, 6:43 PMHildebrandt Tobias
02/16/2024, 6:46 PMturansky
02/16/2024, 6:47 PMHildebrandt Tobias
02/16/2024, 6:47 PMturansky
02/16/2024, 6:48 PMturansky
02/16/2024, 6:50 PMHildebrandt Tobias
02/16/2024, 6:50 PMval abortController = jso<AbortController>()
and in the interceptor
abortController.abort()
but that was me with abort is not a function
And I was unsure how to implement this JS part:
return {
...config,
signal: controller.signal
};turansky
02/16/2024, 6:51 PMHildebrandt Tobias
02/16/2024, 6:51 PMturansky
02/16/2024, 6:52 PMturansky
02/16/2024, 6:53 PMHildebrandt Tobias
02/16/2024, 6:56 PMturansky
02/16/2024, 6:57 PMturansky
02/16/2024, 6:58 PMturansky
02/16/2024, 7:00 PMturansky
02/16/2024, 7:00 PMHildebrandt Tobias
02/16/2024, 7:01 PMHildebrandt Tobias
02/16/2024, 7:06 PMturansky
02/16/2024, 7:10 PMHildebrandt Tobias
02/16/2024, 7:12 PMHildebrandt Tobias
02/16/2024, 7:13 PMHildebrandt Tobias
02/16/2024, 7:20 PMrefreshToken is just another axios request:
AxiosInterceptor.AxiosRequestInterceptor(
{ config ->
val token: String? = config.headers["Authorization"] as? String
println("Security Interceptor Token: $token")
refreshToken(token)
.then {
config.headers["Authorization"] = it
}.catch {
println(it)
}
config
}, { error ->
println("interceptor 1 request error: $error")
Promise.reject(error as JsError)
}
)
But since refreshToken doesn't block the unaltered request goes through. That's the core of my current problem.turansky
02/16/2024, 7:26 PMval tokenQuery = useQuery(jso {
queryFn = {
if (headers["Authorization"] == null) {
refreshToken(token)
} else {
Promise.resolve()
}
})
val dataQuery = useQuery(jso {
enabled = tokenQuery.loaded
...
})Hildebrandt Tobias
02/16/2024, 7:31 PMuseUsers() function in the wrapper example?Hildebrandt Tobias
02/16/2024, 7:34 PMturansky
02/16/2024, 7:38 PMAnd that would be inside theIn simple case - yesfunction in the wrapper example?useUsers()
turansky
02/16/2024, 7:38 PMHildebrandt Tobias
02/16/2024, 7:40 PMturansky
02/16/2024, 7:49 PMHildebrandt Tobias
02/16/2024, 7:53 PMprintln(config.headers["Authorization"])
it's always null. I can see the header in the outgoing request, so it's there.Hildebrandt Tobias
02/16/2024, 7:54 PMReadonlyRecord<String,String>turansky
02/16/2024, 11:32 PMturansky
02/17/2024, 1:00 PMturansky
02/17/2024, 1:01 PM