From b99ec2f20ce25af7004d41258764fc448001dd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pyry=20Kr=C3=B6ger?= Date: Thu, 4 Jul 2019 17:29:04 +0300 Subject: [PATCH] Support for io-ts@2.0 (#11) * add support for io-ts@2.0 * fix typo in installPeerDependencies npm script * fix broken link to Either.ts in README.md --- README.md | 2 +- package-lock.json | 6 ------ package.json | 8 ++++---- src/index.ts | 13 ++++++------- test/helpers/chai-fp-ts.ts | 17 ++++++++++++----- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 00717ea..7dd61a6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ It provides the following: ### Promise chain decoding -Decode data from Promise based APIs, without having to worry about how to retrieve data from the [Either](https://github.com/gcanti/fp-ts/blob/master/docs/Either.md) values returned by the `io-ts` types. +Decode data from Promise based APIs, without having to worry about how to retrieve data from the [Either](https://gcanti.github.io/fp-ts/modules/Either.ts.html) values returned by the `io-ts` types. ```typescript import * as t from 'io-ts'; diff --git a/package-lock.json b/package-lock.json index 79518e0..9bd22c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -376,12 +376,6 @@ "is-buffer": "~2.0.3" } }, - "fp-ts": { - "version": "1.19.0", - "resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-1.19.0.tgz", - "integrity": "sha512-8I22IXnAQbNzShKhrk506iHXftsJLDjyLMyaNYJwvY9jWhDUVghQZ0F9QX0XU9wRM1NzQpOCTYN39NBQq/2Gxw==", - "dev": true - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index e4a062a..3df06f5 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "lint:typescript": "tsc --noEmit", "test:js": "mocha -r ts-node/register test/*.ts", "build": "tsc", - "installPeerDepenencies": "npm install --no-save io-ts@1.x", - "prepublish": "npm run installPeerDepenencies && npm run build" + "installPeerDependencies": "npm install --no-save io-ts@2.x fp-ts@2.x", + "prepublish": "npm run installPeerDependencies && npm run build" }, "repository": { "type": "git", @@ -36,7 +36,8 @@ "deep-equal": "^1.0.1" }, "peerDependencies": { - "io-ts": "1.x" + "fp-ts": "2.x", + "io-ts": "2.x" }, "devDependencies": { "@types/chai": "^4.1.7", @@ -45,7 +46,6 @@ "@types/mocha": "^5.2.7", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", - "fp-ts": "^1.19.0", "mocha": "^6.1.4", "prettier": "^1.18.2", "ts-node": "^8.3.0", diff --git a/src/index.ts b/src/index.ts index 5b02214..8003bed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import deepEqual from 'deep-equal'; +import { either, fold } from 'fp-ts/lib/Either'; import * as t from 'io-ts'; import { PathReporter } from 'io-ts/lib/PathReporter'; @@ -36,12 +37,10 @@ export function decode( Promise >(null, type); default: - return type - .decode(value || arguments[2]) - .fold( - errors => Promise.reject(new DecodeError(errors)), - decodedValue => Promise.resolve(decodedValue), - ); + return fold>( + errors => Promise.reject(new DecodeError(errors)), + decodedValue => Promise.resolve(decodedValue), + )(type.decode(value || arguments[2])); } } @@ -156,7 +155,7 @@ export function extendDecoder( value: unknown, context: t.Context, ) => { - return baseDecoder.validate(value, context).chain(chainedValue => { + return either.chain(baseDecoder.validate(value, context), chainedValue => { try { return t.success(decode(chainedValue)); } catch (e) { diff --git a/test/helpers/chai-fp-ts.ts b/test/helpers/chai-fp-ts.ts index 219766f..1184264 100644 --- a/test/helpers/chai-fp-ts.ts +++ b/test/helpers/chai-fp-ts.ts @@ -22,16 +22,23 @@ interface Utils { export default function(chai: Chai, utils: Utils) { const Assertion = chai.Assertion; + function isObject(value: unknown): value is object { + return typeof value === 'object' && value !== null; + } + function isEither(value: unknown): value is Either.Either { - return value instanceof Either.Left || value instanceof Either.Right; + if (!isObject(value)) { + return false; + } + return Either.isLeft(value as any) || Either.isRight(value as any); } Assertion.addProperty('left', function() { const obj = this._obj; let isLeft: boolean = false; - if (isEither(obj) && obj.isLeft()) { - utils.flag(this, 'object', obj.value); + if (isEither(obj) && Either.isLeft(obj)) { + utils.flag(this, 'object', obj.left); isLeft = true; } @@ -46,8 +53,8 @@ export default function(chai: Chai, utils: Utils) { const obj = this._obj; let isRight: boolean = false; - if (isEither(obj) && obj.isRight()) { - utils.flag(this, 'object', obj.value); + if (isEither(obj) && Either.isRight(obj)) { + utils.flag(this, 'object', obj.right); isRight = true; }