Skip to content

TypeScript support for custom middleware #1689

Answered by huksley
huksley asked this question in Q&A
Discussion options

You must be logged in to vote

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",

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kingllama
Comment options

Answer selected by huksley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants