Skip to content

Commit

Permalink
refactor(hasNotFoundError.ts): Simplify the implementation of the has…
Browse files Browse the repository at this point in the history
…NotFoundError function using fp-ts Option and ReadonlyArray functions

The hasNotFoundError function has been refactored to use fp-ts Option and ReadonlyArray functions for a more concise and functional implementation. The code now uses fp-ts functions such as OfromNullable, ObindTo, Olet, OgetOrElse, Omap, and ROAexists to handle optional values and array operations. This simplifies the code and improves readability.
  • Loading branch information
ktun95 committed Feb 8, 2024
1 parent 2e8b496 commit 65eb533
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions apps/dicty-frontpage/src/common/utils/hasNotFoundError.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { type GraphQLErrors, type ApolloError } from "@apollo/client/errors"
import { type ApolloError } from "@apollo/client/errors"
import { pipe } from "fp-ts/function"
import { exists as ROAexists } from "fp-ts/lib/ReadonlyArray"
import {
fromArray,
filter as readonlyArrayFilter,
isNonEmpty,
} from "fp-ts/lib/ReadonlyArray"
import { getOrElse, fromNullable } from "fp-ts/Option"
bindTo as ObindTo,
let as Olet,
getOrElse as OgetOrElse,
fromNullable as OfromNullable,
map as Omap,
} from "fp-ts/Option"

const hasNotFoundError = (apolloError: ApolloError | undefined) =>
pipe(
apolloError,
fromNullable,
getOrElse(() => ({
graphQLErrors: fromArray([]) as GraphQLErrors,
})),
({ graphQLErrors }) => graphQLErrors,
readonlyArrayFilter((gqlError) => gqlError.extensions?.code === "NotFound"),
isNonEmpty,
OfromNullable,
ObindTo("apolloError"),
Olet(
"graphQLErrors",
({ apolloError: { graphQLErrors } }) => graphQLErrors,
),
Olet("notFound", ({ graphQLErrors }) =>
pipe(
graphQLErrors,
ROAexists((gqlError) => gqlError.extensions?.code === "NotFound"),
),
),
Omap(({ notFound }) => notFound),
OgetOrElse(() => false),
)

export { hasNotFoundError }

0 comments on commit 65eb533

Please sign in to comment.