If you run into issues that aren’t covered by this migration guide, please open an issue and we’re happy to help!
Checklist
Stop calling decoders
Decoders are no longer functions - see instructions
Rename decoders
Rewrite patterns/imports for decoders that have been renamed - see instructions
Rewrite guard() usage
All uses of
guard() need to be rewritten - see instructionsUpdate lemons/debrief imports (if applicable)
Flow users: Update $DecoderType
Rewrite use of
$DecoderType - see instructionsStop “calling” decoders
Decoders in 1.x were functions. In 2.x they’re more like classes.Rename some decoders
Some decoders have been renamed. See the table below. You can do a simple “search & replace” command for these:| Replace this v1 pattern | → | …with this v2 API | Notes |
|---|---|---|---|
map(mydecoder, ...) | → | mydecoder.transform(...) | migration instructions |
compose(mydecoder, predicate(...)) | → | mydecoder.refine(...) | migration instructions |
describe(mydecoder, ...) | → | mydecoder.describe(...) | |
either, either3, …, either9 | → | either | |
tuple1, tuple2, … tuple6 | → | tuple | |
dispatch | → | taggedUnion | |
url(...) | → | httpsUrl / url (signature has changed) | migration instructions |
map() is now .transform()
The map() decoder has been renamed to the .transform() decoder method:
compose() + predicate() is now .refine()
The compose() and predicate() decoders from v1 were mostly used in tandem and used to add additional checks to existing decoders. Predicates have been moved to the .refine() decoder method:
Guards are no longer a thing
Instead, all decoders now have a.verify() method which does the exact same thing.
Signature of url has changed
The signature of the old url decoder has changed. Compare old vs new.
Rewriting imports from lemons or debrief
This section only applies if you used the
lemons or debrief library directly to build custom decoders. If not, just ignore this section.lemons and debrief, but this is no longer the case in v2. You no longer have to create Ok/Err results manually when defining your own decoders, nor deal with annotating inputs manually.
Take this example decoder, which defines a Buffer decoder:
ok and err helpers get passed to you by .define(), so you no longer have to import them yourself. Also, notice how you no longer have to manually annotate the blob in simple cases like this. You can simply return a string err message directly.
If you are accessing Result instances directly
The Result<T> type is no longer a class, but a very simple data structure. This plays well with TypeScript as well as helps to tree-shake many unused methods from your bundle.
As such, methods previously available on Result instances no longer exist. Direct access of the ok, value, and error properties are now favored.
Suggested changes:
| Replace this v1 pattern | → | …with this v2 API |
|---|---|---|
result.andThen(f) | → | result.ok ? f(result.value) : result |
result.dispatch(f, g) | → | result.ok ? f(result.value) : g(result.error) |
result.errValue() | → | result.error |
result.expect() | → | removed |
result.isErr() | → | !result.ok |
result.isOk() | → | result.ok |
result.map(f) | → | result.ok ? ok(f(result.value)) : result |
result.mapError(g) | → | result.ok ? result : err(g(result.error)) |
result.toString() | → | removed |
result.unwrap() | → | removed (see below) |
result.value() ?? xxx | → | decoder.value(...) ?? xxx |
result.value() || xxx | → | decoder.value(...) || xxx |
result.withDefault(xxx) | → | decoder.value(...) ?? xxx |
result.unwrap() it’s probably because you’re using it like so:
.verify() exists.
Rewrite use of $DecoderType
Flow users only! The helper type
$DecoderType has now been simplified. The $-sign has been removed from the name, and you no longer have to $Call<> it.For TypeScript users no changes are needed.