-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
fetchPersonalised.ts
executable file
·48 lines (41 loc) · 1.34 KB
/
fetchPersonalised.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
39
40
41
42
43
44
45
46
47
48
import { normalize, schema } from 'normalizr';
import { NormalizedEntities, NormalizedPersonalizedItem, SoundCloud } from '../../types';
import { playlistSchema } from '../schemas';
import fetchToJson from './helpers/fetchToJson';
interface JsonResponse {
collection: Array<PersonalisedCollectionItem>;
next_href?: string;
query_urn: string;
}
export interface PersonalisedCollectionItem {
urn: string;
query_urn: string;
title: string;
description: string;
tracking_feature_name: string;
last_updated: string;
style: string;
social_proof: SoundCloud.CompactUser;
system_playlists: Array<SoundCloud.SystemPlaylist>;
}
export default async function fetchPersonalised(url: string): Promise<{
json: JsonResponse,
normalized: {
entities: NormalizedEntities;
result: Array<NormalizedPersonalizedItem>;
}
}> {
try {
const json = await fetchToJson<JsonResponse>(url);
const collection = json.collection.filter((t) => t.urn.indexOf('chart') === -1);
return ({
normalized: normalize(collection, new schema.Array(new schema.Object({
system_playlists: new schema.Array(playlistSchema),
playlists: new schema.Array(playlistSchema)
}))),
json
});
} catch (err) {
throw err;
}
}