-
Notifications
You must be signed in to change notification settings - Fork 35
/
fetch.js
255 lines (233 loc) · 6.47 KB
/
fetch.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import semver from 'semver'
import { axios, api } from 'boot/axios'
import { camelCaseDeep, unpack } from 'util/util'
import { Notify } from 'quasar'
const defaultAction = {
type: '',
progress: 0,
id: ''
}
async function fetchChannels (target) {
return await axios
.get('https://update.flipperzero.one/firmware/directory.json')
.then(({ data }) => {
const release = data.channels.find((e) => e.id === 'release')
const rc = data.channels.find((e) => e.id === 'release-candidate')
const dev = data.channels.find((e) => e.id === 'development')
const params = new URLSearchParams(location.search)
const customSource = {
url: params.get('url'),
channel: params.get('channel'),
version: params.get('version'),
target: params.get('target')
}
function formatChannel (channel) {
channel.versions.sort((a, b) => {
if (semver.lt(a.version, b.version)) return 1
else return -1
})
const output = {
version: '',
date: '',
url: '',
files: [],
changelog: ''
}
const updater = channel.versions[0].files.find(
(file) => file.target === 'f' + target && file.type === 'update_tgz'
)
if (updater) {
output.url = updater.url
}
output.version = channel.versions[0].version
output.date = new Date(channel.versions[0].timestamp * 1000)
.toISOString()
.slice(0, 10)
output.files = channel.versions[0].files.sort((a, b) => {
if (a.url.match(/[\w.]+$/g)[0] > b.url.match(/[\w.]+$/g)[0]) return 1
else return -1
})
output.changelog = channel.versions[0].changelog
return output
}
const releaseChannel = formatChannel(release)
const rcChannel = formatChannel(rc)
const devChannel = formatChannel(dev)
let customChannel
if (customSource.url) {
customChannel = {
channel: customSource.channel,
version: customSource.version,
date: new Date().toISOString().slice(0, 10),
url: customSource.url,
files: [
{
url: customSource.url,
type: 'update_tgz',
target: customSource.target
}
]
}
}
return {
release: releaseChannel,
rc: rcChannel,
dev: devChannel,
custom: customChannel
}
})
.catch(({ status }) => {
if (status >= 400) {
throw new Error('Failed to fetch firmware channels (' + status + ')')
}
})
}
async function fetchFirmware (url) {
return await axios
.get(url, { responseType: 'arraybuffer' })
.then(async ({ data }) => {
return unpack(data)
})
.catch((error) => {
const decoder = new TextDecoder('utf-8')
const data = JSON.parse(decoder.decode(error.response.data)).detail
if (data.code >= 400) {
throw new Error('Failed to fetch resources (' + data.code + ')')
}
})
}
async function fetchRegions () {
return axios
.get('https://update.flipperzero.one/regions/api/v0/bundle')
.then(({ data }) => {
if (data.error) {
throw new Error(data.error.text)
} else if (data.success) {
return data.success
}
})
.catch(({ status }) => {
if (status >= 400) {
throw new Error('Failed to fetch region (' + status + ')')
}
})
}
async function fetchCategories (params) {
return await api.get('/category', { params }).then(({ data }) => {
return data.map((category) => camelCaseDeep(category))
})
}
async function fetchPostAppsShort (params) {
return await api.post('/1/application', params)
.then(res => res.data.map(app => {
app.action = {
type: '',
progress: 0,
id: app.id
}
return camelCaseDeep(app)
}))
}
let controller = null
async function fetchAppsShort (params) {
if (controller) controller.abort()
controller = new AbortController()
return await api.get('/0/application', {
params,
signal: controller.signal
}).then(({ data }) => {
return data.map((app) => {
app.action = defaultAction
return camelCaseDeep(app)
})
}).catch((error) => {
if (error.code !== 'ERR_CANCELED') {
if (error.response.status >= 400) {
throw new Error('Failed to fetch resources (' + error.response.status + ')')
}
}
})
}
async function fetchAppById (id, params) {
if (!params.target) {
delete params.target
}
if (!params.api) {
delete params.api
}
return await api.get(`/application/${id}`, { params }).then(({ data }) => {
data.action = defaultAction
return camelCaseDeep(data)
}).catch((err) => {
const data = err.response.data
Notify.create({
type: 'negative',
message: data.detail.details
})
return data
})
}
async function fetchAppFap (params) {
return await api
.get(`/application/version/${params.versionId}/build/compatible`, {
params: {
target: params.target,
api: params.api
},
responseType: 'arraybuffer'
})
.then(({ data }) => {
return data
})
.catch((error) => {
const decoder = new TextDecoder('utf-8')
const data = JSON.parse(decoder.decode(error.response.data)).detail
if (data.code >= 400) {
throw new Error('Failed to fetch application build (' + data.code + ')')
}
})
}
async function fetchAppsVersions (uids) {
const allVersions = []
uids = uids.filter(u => u)
if (uids) {
const size = 100
const subUids = []
for (let i = 0; i < Math.ceil(uids.length / size); i++) {
subUids[i] = uids.slice(i * size, i * size + size)
}
for (const sliceUids of subUids) {
await api
.post('/1/application/versions', {
application_versions: sliceUids,
limit: size
})
.then(({ data }) => allVersions.push(...data))
}
} else {
await api
.post('/1/application/versions', {
limit: 500
})
.then(({ data }) => allVersions.push(...data))
}
const versions = allVersions.map((version) => camelCaseDeep(version))
return versions
}
async function submitAppReport (id, report) {
return api.post(`/application/${id}/issue`, {
...report
})
}
export {
fetchChannels,
fetchFirmware,
fetchRegions,
fetchCategories,
fetchPostAppsShort,
fetchAppsShort,
fetchAppById,
fetchAppFap,
fetchAppsVersions,
submitAppReport
}