forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowdin.ts
More file actions
83 lines (71 loc) · 2.33 KB
/
Copy pathcrowdin.ts
File metadata and controls
83 lines (71 loc) · 2.33 KB
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
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { createArrayGuard, createObjectGuard, isInt, isString, TypeGuard } from 'typescript-guard'
import { CROWDIN_PROJECT_ID } from "../util/constant/url"
type ListResponse<T> = {
data: { data: T }[]
}
const createListRespGuard = <T,>(itemGuard: TypeGuard<T>) => createObjectGuard<ListResponse<T>>({
data: createArrayGuard(createObjectGuard({ data: itemGuard }))
})
/**
* Used to obtain translation status
*/
const PUBLIC_TOKEN = '5e0d53accb6e8c490a1af2914c0963f78082221d7bc1dcb0d56b8e3856a875e432a2e353a948688e'
export type TranslationStatusInfo = {
/**
* https://developer.crowdin.com/language-codes/
*/
languageId: string
translationProgress: number
}
const isStatusResp = createListRespGuard(
createObjectGuard<TranslationStatusInfo>({
languageId: isString,
translationProgress: isInt,
})
)
type MemberInfo = {
username: string
joinedAt: string
avatarUrl: string
}
const isMembersResp = createListRespGuard(
createObjectGuard<MemberInfo>({
username: isString,
joinedAt: isString,
avatarUrl: isString,
})
)
export async function getTranslationStatus(): Promise<TranslationStatusInfo[]> {
const limit = 500
const url = `https://api.crowdin.com/api/v2/projects/${CROWDIN_PROJECT_ID}/languages/progress?limit=${limit}`
return await getList(url, isStatusResp)
}
export async function getMembers(): Promise<MemberInfo[]> {
const result: MemberInfo[] = []
const limit = 10
let offset = 0
while (true) {
const url = `https://api.crowdin.com/api/v2/projects/${CROWDIN_PROJECT_ID}/members?limit=${limit}&offset=${offset}`
const newItems = await getList(url, isMembersResp)
result.push(...newItems)
if (newItems.length < limit) break
offset += limit
}
return result
}
async function getList<T>(url: string, respGuard: TypeGuard<ListResponse<T>>): Promise<T[]> {
const resp = await fetch(url, {
method: 'GET',
headers: { "Authorization": `Bearer ${PUBLIC_TOKEN}` },
})
const json = await resp.json()
if (respGuard(json)) return json.data.map(i => i.data)
console.warn('Unexpected response from Crowdin API', json)
return []
}