In-House MathSwe-TS-and-Client Libraries | MathSwe Ops Services (2024/09/15)
It provides two new in-house libraries in the MathSwe Ops Services application that will become MathSwe standard libraries for any TypeScript and server projects. While MathSwe-TS leverages the FP-TS library to add extended support for FP, MathSwe-Client will be standard for server applications.
Add in-house libs mathswe-ts, mathswe-client
Sep 15: PR #2 merged into by tobiasbriones
The mathswe-ts/adt
module defines the **general pattern matching for sum types
**. You also must follow manual guides to correctly build a sum type in
TypeScript, even with fp-ts
and mathswe-ts
.
Something the test doesn’t show is that you must create the data constructors
manually to keep up with good practices. That is, to avoid the tag
field in
client code, which is supposed to be an implementation detail.
Notice that, when using pipe
you give context to the match
function, i.e.
SumTypeMap<Shape, string>
, so the pattern matching is exhaustive. If you
call it separately, i.e. match({})(shape)
, it loses the sum type, i.e.
SumTypeMap<SumType, string>
, so the generic type weakens from Shape
to
SumType
.
You must apply the withMatchVariant
higher-order function to pass your branch
function to it. Your branch function, for example, circleArea
, will define
the type of the branch. In that case, you tell your withShapeVariant
partial
function that the type of the branch argument is Circle
since it performs a
cast under the hood, so you must define this type correctly to match
successfully the branch.
As you can see, the branch function circleArea
defines the { radius }:
Circle
param type that the withShapeVariant
partial function will use to
convert the original sum type to the actual variant value.
Finally, the matching is lazy for correctness. If you pass an eager map to
match
, it will execute all the variants automatically. Further, it would fail
at runtime because it’d pass the same argument to all the different branch
functions without discrimination.
Conversely, the mathswe-ts/enum
module allows you to match plain sum type
branches eagerly.
The matchPlain
function of the enum
module is eager, and you must not use
matchPlain
for non-trivial sum types.
Notice that, fp-ts
doesn’t have a general pattern-matching solution, only
ad-hoc matching functions for specific types, like Either
. Therefore, I took
the exciting challenge to solve it, as per the language limitations since it’s
better to have some kind of pattern matching than none.
Finally, the mathswe-ts/string
module defines some type-classes to start
implementing them in TS programs as a standard practice (like the Rust way).
The module mathswe-ts/require
provides a method requireRight
to unsafe
unwrap of Either
values. It throws an Error
if the given Either
is Left
,
or else returns its Right
value. It’s useful for testing when you’re sure a
value is always Right
, or must be Right
to pass the test.
The current MathSwe-TS library supports sum-type general pattern matching, which is lazy, but also for eager matching for plain branches without fields. It further defines some type-classes for Strings and a method for unsafe unwrapping of Either.
The mathswe-client/domain/domain
module defines the ToDomainName
type class
to convert any domain to a string
domain name. It also defines the Allowed
sum type to grant access to those domains.
The mathswe-client/req/http
module defines HTTP abstractions.
The mathswe-client/req/origin
path contains modules with rules defining if a
given request origin has access to the server.
Finally, the mathswe-client/req/client/client-req
module provides a function
to get the Origin
of a Request
. Recall that the existence of an Origin
value means the underlying request is allowed.
The current MathSwe-Client library defines known MathSwe domains with full access and third-party domains, like GitHub, which can only have partial access to some paths. It defines HTTP concepts, like SecureUrl, so that DSL ensures only allowed origins according to domain rules.
The new features of MathSwe-TS complement general and exhausting (when piping) pattern matching for sum types. While it’s not perfect, it works better than expected, complimenting FP-TS, which only has ad-hoc matching for monads like Option and Either.
Further, the new MathSwe-Client supports Origin rules to only accept HTTP requests from accepted domains and paths.
The purpose of these two libraries is to become MathSwe standards for TypeScript projects.