Seeking Advice on Data Massaging with Effect in TypeScript

Hey there 👋 ,

I have a data massaging scenario that is pretty common, yet troubles me with how to implement it correctly in Effect. Sample code below

Effect.gen(function*() {

  const placesService = yield* PlacesService
  
  return yield* placesService.searchNearby({ latitude: params.lat, longitude: params.long }, 50).pipe(
    Effect
      .map((places) => places
        .filter((place) => place.photos.length > 0)
        .map((place) => ({  
          ...place,
          photos: Effect.all(place.photos.map((photo) => placesService.getPlacePhotoURL(photo.name)))
        })
    )),
    Effect.flatMap((places) => Http.response.json(places))
  )
}

// Method signatures
searchNearby: (point: Point, radius: number) => Effect.Effect<readonly Place[], UnknownException | ParseError, never>

getPlacePhotoURL: (photoName: string) => Effect.Effect<PhotoMedia, HttpClientError | ParseError, never>


In this scenario, I'm getting an array of Places wrapped in an effect, filtering a few entries, and then augmenting in each Place a computed property that's also wrapped in an Effect. What this ends up serialized as JSON, is an array of objects where the photos property is still an effect.

{
    photos: Effect.Effect<PhotoMedia[], HttpClientError | ParseError, never>;
    id: string;
    displayName: {
        readonly text: string;
        readonly languageCode: string;
    };
    location: {
        ...;
    };
    types: readonly string[];
}[]


Obviously, that won't fly 🙂 Could anyone help on what's the correct way to do this type of data massaging in Effect? Many thanks!
Was this page helpful?