Effect service for FormData

Effect services are not limited to contain only functions. A service can wrap any web interface. This can be useful to abstract APIs as services, and take advantage of the benefits of layers and composition.

Let's see an example using FormData.

Effect service for FormData

FormData is a web API that defines utility methods to work with values inside forms.

We can create a service that wraps FormData using Context.Tag:

src/services/FormData.ts
import { Context } from "effect";

export class FormData extends Context.Tag("FormData")<
  FormData,
  globalThis.FormData
>() {}

globalThis allows distinguishing between the FormData service and the FormData web API.

Notice how the value of the service is globalThis.FormData. Using this service inside any Effect gives us access to the globalThis.FormData API.