-
I am using middleware and add it this way: What would be proper typing for middleware from example in the docs?
|
Beta Was this translation helpful? Give feedback.
Answered by
huksley
Dec 3, 2021
Replies: 1 comment 1 reply
-
Answering myself, after much trial and error I figured out the following type signature. Full implementation included. const logger = (useSWRNext: SWRHook) => {
return (key: unknown, fetcher: BareFetcher | null, config: SWRConfiguration) => {
let nextFetcher = fetcher;
if (fetcher) {
nextFetcher = (...args: unknown[]) => {
const started = Date.now();
const label = typeof key === "function" ? key() : Array.isArray(key) ? key.join(", ") : key;
console.info("SWR Request", label);
const response = fetcher(...args);
if (response instanceof Promise) {
return response.then(result => {
console.info("SWR Request complete", label, "elapsed", Date.now() - started, "ms");
return result;
});
} else {
console.info("SWR Request complete", label, "elapsed", Date.now() - started, "ms");
return response;
}
};
}
// Execute the hook with the new fetcher.
return typeof key === "function"
? useSWRNext(key, nextFetcher, config)
: useSWRNext(key as Arguments, nextFetcher, config);
};
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
huksley
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answering myself, after much trial and error I figured out the following type signature. Full implementation included.