WIP. A little custom music server
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: cleanup bullshit from previous commit

+34 -72
+34 -72
backend/src/api.ts
··· 1 - import { Console, Data, Effect, Layer, ManagedRuntime, Schema } from "effect"; 1 + import { Console, Data, Effect, Layer, ManagedRuntime, ParseResult, Schema } from "effect"; 2 2 import { Elysia, status, StatusMap, t, type HTTPHeaders } from "elysia"; 3 3 import { DatabaseLive } from "./db"; 4 4 import { BunContext } from "@effect/platform-bun"; ··· 119 119 ), 120 120 ); 121 121 122 - const artistDecoder = Schema.decodeUnknown(Artist); 123 - 124 - const artists = yield* Effect.gen(function* () { 125 - const albumArtists = album.artists; 126 - 127 - if (albumArtists && Object.hasOwn(albumArtists[0], "artist")) { 128 - const decodeArtists = albumArtists?.map((relation) => { 129 - const rel = relation as Extract<typeof relation, { artist: { name: string } }>; 130 - const artist = rel.artist; 131 - 132 - return artistDecoder(artist); 133 - }); 134 - 135 - const artists = yield* Effect.all(decodeArtists, { concurrency: "unbounded" }); 136 - return artists; 137 - } 138 - return undefined; 122 + const DatabaseSchema = Schema.Struct({ 123 + ...Album.fields, 124 + artists: Schema.Struct({ 125 + artist: Artist, 126 + }).pipe(Schema.Array, Schema.optional), 127 + songs: Schema.Struct({ 128 + ...Song.fields, 129 + artists: Schema.Struct({ 130 + artist: Artist, 131 + }).pipe(Schema.Array, Schema.optional), 132 + }).pipe(Schema.Array, Schema.optional), 139 133 }); 140 134 141 - const songs = yield* Effect.gen(function* () { 142 - if (!album.songs) return undefined; 143 - 144 - const decodeSongs = album.songs.map((song) => 145 - Effect.gen(function* () { 146 - const artists = yield* Effect.gen(function* () { 147 - if (!Object.hasOwn(song, "artists")) return undefined; 148 - const songWithArtists = song as Extract<typeof song, { artists: { songId: string }[] }>; 149 - 150 - const decodeArtists = songWithArtists.artists 151 - .map((relation) => { 152 - if (!Object.hasOwn(relation, "artist")) return undefined; 153 - 154 - const { artist } = relation as Extract< 155 - typeof relation, 156 - { artist: { name: string } } 157 - >; 158 - 159 - return artistDecoder(artist); 160 - }) 161 - .filter(Boolean) 162 - .map((x) => x as typeof x & {}); 163 - 164 - yield* Effect.log(decodeArtists); 165 - 166 - return yield* Effect.all(decodeArtists); 167 - }); 168 - 169 - const songDecoder = Schema.decodeUnknown( 170 - Schema.Struct({ 171 - ...Song.fields, 172 - artists: Schema.optional(Schema.Array(Artist)), 173 - }), 174 - ); 135 + const ResultSchema = Schema.Struct({ 136 + ...Album.fields, 137 + artists: Artist.pipe(Schema.Array, Schema.optional), 138 + songs: Schema.Struct({ 139 + ...Song.fields, 140 + artists: Artist.pipe(Schema.Array, Schema.optional), 141 + }).pipe(Schema.Array, Schema.optional), 142 + }); 175 143 176 - return yield* songDecoder({ 144 + const transformer = Schema.transformOrFail(DatabaseSchema, ResultSchema, { 145 + strict: true, 146 + decode: (input, _, ast) => 147 + ParseResult.succeed({ 148 + ...input, 149 + artists: input.artists?.map(({ artist }) => artist), 150 + songs: input.songs?.map((song) => ({ 177 151 ...song, 178 - artists, 179 - }); 152 + artists: song.artists?.map(({ artist }) => artist), 153 + })), 180 154 }), 181 - ); 182 155 183 - return yield* Effect.all(decodeSongs, { concurrency: "unbounded" }); 156 + encode: (x, _, ast) => ParseResult.fail(new ParseResult.Type(ast, x, "Unimplemented")), 184 157 }); 185 158 186 - const result = yield* Schema.decodeUnknown( 187 - Schema.Struct({ 188 - ...Album.fields, 189 - songs: Schema.Struct({ 190 - ...Song.fields, 191 - artists: Artist.pipe(Schema.Array, Schema.optional), 192 - }).pipe(Schema.Array, Schema.optional), 193 - artists: Artist.pipe(Schema.Array, Schema.optional), 194 - }), 195 - )({ 196 - id: album.id, 197 - title: album.title, 198 - artists: artists, 199 - songs: songs, 200 - }); 159 + const dbDecoder = Schema.decodeUnknown(DatabaseSchema); 160 + const dbData = yield* dbDecoder(album); 161 + 162 + const result = yield* Schema.decode(transformer)(dbData); 201 163 202 164 return result; 203 165 });