orval: SWR: Attempted import error: 'swr' does not contain a default export (imported as 'useSwr').

What are the steps to reproduce this issue?

  • Run codegen with swr generator
  • Import one of the helper functions into an RSC component
  • Terminal shows this error

Nothing breaks, it’s just a warning, but annoying

More info: https://github.com/vercel/swr/issues/2694

What happens?

image

What were you expecting to happen?

The codegen could add “use client” to parts that actually call useSWR and separate keys from this code as the keys are used in server components.

Any other comments?

Obviously we don’t want to call useSWR in server components, however there are parts of the generated code that are useful in server components. One of these, which I make use of in Storyden’s codebase is the generated keys. This means I can use fetch on the server but still use the generated types and paths.

export const getThreadListKey = (params?: ThreadListParams) =>
  [`/v1/threads`, ...(params ? [params] : [])] as const;

These generated key functions are useful to keep everything type safe - even while Orval doesn’t generate server code, I can use these to ensure my code is at least somewhat type safe for server components:

import { server } from "src/api/client";
import { ThreadListOKResponse } from "src/api/openapi/schemas"; // schema imports are fine - no useEffect dependents here
import { getThreadListKey } from "src/api/openapi/threads"; // this causes the error, even though I'm not actually using useEffect dependents (useSWR) - the runtime still warns me because getThreadListKey is in the same file as a useSWR import+call

import { Client } from "./Client";

type Props = {
  category: string;
};

export async function FeedScreen(props: Props) {
  const key = getThreadListKey({ categories: [props.category] })[0]; // imported from Orval

  const data = await server<ThreadListOKResponse>(key);

  return <Client category={props.category} threads={data.threads} />;
}

What versions are you using?

Package Version: “orval”: “^6.17.0”,

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Comments: 16

Most upvoted comments

I understand. It’s true that if you’re using both, it’s confusing and difficult to use. I change the label to enhancement.

@soartec-lab Because the models will be generated by each of the generators, it will create a mess with imports since 1 and the same API. And swr client already uses axios and exports the necessary functions.

I removed bug and will close for now unless the OP can confirm.