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
-
Upgrade
@monarcode/effetchto 0.1.0-rc.4 or later:npm install @monarcode/effetch@latest -
Commit your changes, the upgrade included, or stash them. The command rewrites files in place. With a clean working tree,
git diffshows exactly what it changed, andgit restore .undoes it. -
Preview the migration. The following command lists the files it would change, and changes none:
npx -p @monarcode/effetch effetch-migrate --dry-run -
Run the migration:
npx -p @monarcode/effetch effetch-migrateIt prints a line for each file it updated, a
Checkline for each place it couldn't rewrite, and a summary such as3 of 4 files importing @monarcode/effetch changed, 1 to check by hand. -
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. -
Run your formatter, since rewritten lines may need reflowing.
-
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"becomesimport { Fetch } from "@monarcode/effetch". Under another name, as inimport * as Api, it becomesimport { 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 }, becomeimport { Fetch }, and each use becomesFetch.HttpErrororFetch.Client. When the file already has something calledFetch, the import is namedEffetch, thenEffetchFetch, thenFetch2,Fetch3and 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 ofFetchitself stays on the main entry.- Types written as
import("@monarcode/effetch").Clientbecomeimport("@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 itsFetchmember, as in(await import("@monarcode/effetch")).Fetch; - the package's name passed to a call, such as
vi.mock("@monarcode/effetch"). The module exports onlyFetch, 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 moduleblock; - 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
.gitignoredoes. - Elsewhere, it skips
node_modules,dist,build,coverageand 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
Fetchexport.