-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataAPI.server.ts
154 lines (132 loc) · 3.89 KB
/
dataAPI.server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import invariant from "tiny-invariant";
import { BACKEND_CONFIG } from "~/config/backend.server"
import { APIResponse, DataAPI, FetchAPI } from "~/types/mediaAPI";
invariant('SESSION_SECRET', 'SESSION_SECRET not set')
const {
DATA_API: {
PARAMS: {
OFFSET,
WHERE,
SELECT_ITEMS_DEFAULT,
SELECT_ITEMS_NO_IMAGE,
LIMIT,
ORDER_BY_PROP,
SELECT
},
ENDPOINTS: { RATSINFO, RATSINFO_FRAGMENT_GROUPS }
}
} = BACKEND_CONFIG;
/**
* search params for API (helper)
*/
const constructParams = (params: string[]) => {
let paramsFragments = ''
for (let i = 0; i < params.length; i += 1) {
if (i > 0) paramsFragments += '&'
paramsFragments += params[i]
}
return paramsFragments
}
/**
* fetch API (helper)
*/
const fetchAPI = async ({
endpoint,
params,
offset
}: FetchAPI): Promise<APIResponse | null> => {
try {
let path = endpoint + constructParams(params)
if (offset) path += OFFSET + offset
const res = await fetch(new URL(path))
const toJson = await res.json();
if (toJson) return toJson
return null
} catch (error: Error | unknown) {
return null
}
}
/**
* get API data by configuration
*/
export const dataAPI = async ({
...props
}: DataAPI) => {
const {
offset,
endpoint,
limit,
includeImage,
} = props
const select = includeImage ? SELECT_ITEMS_DEFAULT : SELECT_ITEMS_NO_IMAGE
const nextYear = new Date().getUTCFullYear() + 1
const where = WHERE + encodeURIComponent(`published < ${nextYear}`) // exclude wrong entries dated in far future
const res = await fetchAPI({
endpoint: endpoint,
params: [
where,
LIMIT + limit,
ORDER_BY_PROP.replace('{{PROP}}', 'published'),
SELECT + select
],
offset
})
if (res) return res
return res
}
export const dataAPISearch = async ({
...props
}) => {
const {
// offset,
endpoint,
limit,
includeImage,
search_term
} = props
const select = includeImage ? SELECT_ITEMS_DEFAULT : SELECT_ITEMS_NO_IMAGE
const where = WHERE + encodeURIComponent(`title like "${search_term}" OR description like "${search_term}"`) // exclude wrong entries dated in far future
const res = await fetchAPI({
endpoint: endpoint,
params: [
where,
LIMIT + limit,
ORDER_BY_PROP.replace('{{PROP}}', 'published'),
SELECT + select
],
})
if (res) return res
return res
}
export const dataAPIRatstinfoGroups = async (
feed: Record<string, unknown>[],
keys: string[][]) => {
const groupIds: number[] = []
const groupCalls = []
let groups = {}
const collectedIds = []
for (let i = 0; i < feed.length; i += 1) {
for (let j = 0; j < keys.length; j += 1) {
if (keys[j].length === 1) {
collectedIds.push(feed[i][keys[j][0]])
} else if (keys[j].length === 2) {
// @ts-expect-error any type
collectedIds.push(feed[i][keys[j][0]][keys[j][1]])
} else if (keys[j].length === 3) {
// @ts-expect-error any type
collectedIds.push(feed[i][keys[j][0]][keys[j][1]][keys[j][2]])
}
}
}
for (let i = 0; i < collectedIds.length; i += 1) {
if (!groupIds.includes(collectedIds[i])) groupIds.push(collectedIds[i])
}
for (let i = 0; i < groupIds.length; i += 1) {
groupCalls.push(fetch(`${RATSINFO + RATSINFO_FRAGMENT_GROUPS}/${groupIds[i]}`).then((it) => it.json()))
}
const callRes = await Promise.all(groupCalls)
for (let i = 0; i < callRes.length; i += 1) {
groups = { ...groups, ['g_' + callRes[i].id]: callRes[i] }
}
return groups
}