I'm using RTK Query to call an API in my getServerSideProps function. I can get the result within the function using the code below, but it feels kind of awkward. Also, the result is not typed, which would be preferable.
export const getServerSideProps: GetServerSideProps = wrapper.getServerSideProps(
(store) => async (context: GetServerSidePropsContext) => {
await store.dispatch(apiCall.initiate({ ...someParams }))
await Promise.all(api.util.getRunningOperationPromises())
const mutations = store.getState().api.mutations;
const mutation = Object.values(mutations).find((m) => m?.endpointName === 'apiCall')
// |________| -> type is `unknown`
if (mutation?.error) {
const error = mutation.error as {
status: number
data: {
error: string
}
}
}
return {
props: {},
}
},
)
Is there a better way of doing this?