Skip to Content

Upgrade from 0.1.0-rc.3

Move a project's imports to the Fetch namespace with the effetch-migrate command.

Up to 0.1.0-rc.3, Effetch's main entry exported each API by name, and you imported it with import * as Fetch or name by name. From 0.1.0-rc.4, everything in the main entry is under one export, Fetch, so code that imports it the old way doesn't compile. The package comes with a command, effetch-migrate, that rewrites those imports for you. This page walks you through the upgrade, from installing 0.1.0-rc.4 or later to a project that type-checks again.

What changes in your code

The import and export lines change, and so does each use of a name you imported on its own, which becomes a member of Fetch. Uses such as Fetch.endpoint stay as they are. Here's a file before and after the migration:

-import * as Fetch from "@monarcode/effetch";
-import { HttpError, type Client } from "@monarcode/effetch";
+import { Fetch } from "@monarcode/effetch";

 export const getTodo = Fetch.endpoint("GET /todos/:id");

-export const describe = (api: Client, error: HttpError) =>
+export const describe = (api: Fetch.Client, error: Fetch.HttpError) =>
   `${error.status}`;

-export { DecodeError } from "@monarcode/effetch";
+export { DecodeError } from "@monarcode/effetch/Fetch";

The /plugins and /test entries didn't change, and the command leaves their imports alone.

Before you begin

Check the following before you start:

  • TypeScript is installed in the project. The command reads your code with the project's own TypeScript. Without it, the command stops with an error.
  • You know which directory to run the commands in: the directory of the package that depends on Effetch. In a monorepo, that's the package's own directory, not the repository's root.

The commands on this page use npm. With pnpm, run pnpm add in place of npm install, and pnpm exec effetch-migrate in place of npx -p @monarcode/effetch effetch-migrate.

Migrate the project

  1. Upgrade @monarcode/effetch to 0.1.0-rc.4 or later:

    npm install @monarcode/effetch@latest
  2. Commit your changes, the upgrade included, or stash them. The command rewrites files in place. With a clean working tree, git diff shows exactly what it changed, and git restore . undoes it.

  3. Preview the migration. The following command lists the files it would change, and changes none:

    npx -p @monarcode/effetch effetch-migrate --dry-run
  4. Run the migration:

    npx -p @monarcode/effetch effetch-migrate

    It prints a line for each file it updated, a Check line for each place it couldn't rewrite, and a summary such as 3 of 4 files importing @monarcode/effetch changed, 1 to check by hand.

  5. Change by hand each place it listed with Check. The file and line are in the message, and What you change by hand says what to do with each kind.

  6. Run your formatter, since rewritten lines may need reflowing.

  7. Run your type checker, such as npx tsc --noEmit. When it passes, the migration is done.

What the command rewrites

The command reads only files that name the main entry, @monarcode/effetch, in a string, such as an import. It makes the following changes in them:

  • import * as Fetch from "@monarcode/effetch" becomes import { Fetch } from "@monarcode/effetch". Under another name, as in import * as Api, it becomes import { Fetch as Api }. A type-only import stays type-only, unless the file also imports a value from Effetch, which joins the same import.
  • Named imports, such as import { HttpError, type Client }, become import { Fetch }, and each use becomes Fetch.HttpError or Fetch.Client. When the file already has something called Fetch, the import is named Effetch, then EffetchFetch, then Fetch2, Fetch3 and so on, whichever is free.
  • A name that the file also re-exports, as in export { HttpError }, keeps its name, and is imported from @monarcode/effetch/Fetch.
  • export … from "@monarcode/effetch" re-exports from @monarcode/effetch/Fetch, which has the main entry's old exports. A re-export of Fetch itself stays on the main entry.
  • Types written as import("@monarcode/effetch").Client become import("@monarcode/effetch").Fetch.Client.

Code that already imports Fetch is left as it is.

What you change by hand

The command lists what it can't rewrite, each with its file and line:

  • a dynamic import() of the package. Use its Fetch member, as in (await import("@monarcode/effetch")).Fetch;
  • the package's name passed to a call, such as vi.mock("@monarcode/effetch"). The module exports only Fetch, so a mock of it needs to as well;
  • the package's name anywhere else in the code, as a string;
  • an import or export inside a declare module block;
  • a default import of the package, which has none;
  • a /** … */ comment that mentions the package;
  • in a JavaScript file, a JSDoc comment that uses a name the command rewrote, such as {HttpError};
  • comments inside an import or export it rewrote, which it dropped;
  • a file that TypeScript couldn't read, which it left unchanged.

The command doesn't read Markdown, so update code in your docs by hand.

Choose the files it reads

The command reads .ts, .tsx, .mts, .cts, .js, .jsx, .mjs and .cjs files. Which of them it reads depends on where it runs:

  • In a git repository, it reads the files git sees, so it skips what .gitignore does.
  • Elsewhere, it skips node_modules, dist, build, coverage and hidden directories, and lists the build output it skipped.

To choose what it reads, pass files or directories, as in effetch-migrate src test. With no path, it reads the current directory.

Other changes

The command covers the imports, which is the change that stops code from compiling. 0.1.0-rc.4 changed one more thing: a content-type header set by a call, an endpoint or the client is removed for a FormData body, so the platform's multipart boundary is kept. Earlier releases changed other things. The changelog lists every release's changes, and what to change in your code for each. Read the entries from your version up.

What's next

  • Installation: see Effetch's entry points, and how to import from each one.
  • Getting started: write a complete program with the Fetch export.

On this page