小编Ars*_*nin的帖子

在 fp-ts 中使用 TaskEither 和 fetch API

我想以某种方式将 Fetch API 包装在 fp-ts 中:

  1. 创建请求
  2. 检查状态
  3. 如果状态正常 - 返回 json
import * as TE from 'fp-ts/lib/TaskEither';
import * as E from 'fp-ts/lib/Either';
import { flow } from 'fp-ts/lib/function';
import { pipe } from 'fp-ts/lib/pipeable';

const safeGet = (url: string): TE.TaskEither<Error, Response> => TE.tryCatch(
  () => fetch(url),
  (reason) => new Error(String(reason))
);

const processResponce = flow(
  (x: Response): E.Either<Error, Response> => {
    return x.status === 200
      ? E.right(x)
      : E.left(Error(x.statusText))
  },
  TE.fromEither
);

export const httpGet = (url: string): …
Run Code Online (Sandbox Code Playgroud)

typescript fp-ts

5
推荐指数
1
解决办法
1648
查看次数

标签 统计

fp-ts ×1

typescript ×1