-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
38 lines (35 loc) · 1.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {AllArtRequests, ArtRequests, SearchOptionsRequests} from './types/request';
import {Result} from '@ryandur/sand';
import {GetAllArt, GetArt, matchSource, SearchArt, Source} from './types/resource';
import {AllArt, Art, SearchOptions} from './types/response';
import {Explanation, HTTPError} from '../types';
import {http, unknownSource} from '../http';
import {aic} from './aic';
import {harvard} from './harvard';
import {rijks} from './rijks';
export const artGallery = {
getAllArt: ({page, size, source, search}: GetAllArt): Result.Async<AllArt, Explanation<HTTPError>> =>
matchSource<AllArtRequests>(source, {
[Source.AIC]: () => aic.allArt,
[Source.HARVARD]: () => harvard.allArt,
[Source.RIJKS]: () => rijks.allArt(page),
}).map(({endpoint, validate, toAllArt}) =>
http.get(endpoint({params: {page, size, search}})).mBind(validate).map(toAllArt)
).orElse(unknownSource()),
getArt: ({id, source}: GetArt): Result.Async<Art, Explanation<HTTPError>> =>
matchSource<ArtRequests>(source, {
[Source.AIC]: () => aic.art,
[Source.HARVARD]: () => harvard.art,
[Source.RIJKS]: () => rijks.art,
}).map(({endpoint, validate, toArt}) =>
http.get(endpoint({path: [id]})).mBind(validate).map(toArt)
).orElse(unknownSource()),
searchForArt: ({search, source}: SearchArt): Result.Async<SearchOptions, Explanation<HTTPError>> =>
matchSource<SearchOptionsRequests>(source, {
[Source.AIC]: () => aic.searchOptions,
[Source.HARVARD]: () => harvard.searchOptions,
[Source.RIJKS]: () => rijks.searchOptions,
}).map(({endpoint, validate, toSearchOptions}) =>
http.get(endpoint(search)).mBind(validate).map(toSearchOptions)
).orElse(unknownSource())
};