Typescript: Matching rtk-query actions with status code #3432
-
I would like to match rtk-query actions in I would like to match any rtk-query response action which:
I have written a matcher like so:
However this example matcher does not take into account the status code. Is there a way to get access to Something like this, but type-safe:
|
Beta Was this translation helpful? Give feedback.
Answered by
EskiMojo14
May 10, 2023
Replies: 1 comment
-
is there? yes return isRejected(action)
&& action.payload === 'Unauthorized'
&& 'baseQueryMeta' in action.meta
&& typeof action.meta.baseQueryMeta === "object"
&& !!action.meta.baseQueryMeta
&& 'response' in action.meta.baseQueryMeta
&& action.meta.baseQueryMeta.response instanceof Response
&& action.meta.baseQueryMeta.response.status === 401; i would rather skip a few steps, and use a type guard: const hasRTKQMeta = (action: any): action is {
meta: {
baseQueryMeta: {
request: Request
response?: Response
}
}
} => action?.meta?.baseQueryMeta?.request instanceof Request
const is401UnauthorizedMatch = (action: PayloadAction<string>): action is PayloadAction<string> => {
return isRejected(action)
&& action.payload === 'Unauthorized'
&& hasRTKQMeta(action)
&& action.meta.baseQueryMeta.response?.status === 401;
} it's technically less type safe, but is a pretty safe assertion. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mauriceoc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there? yes
will you want to do it? nope
i would rather skip a few steps, and use a type guard: