Repository navigation
Expand file tree
/
Copy pathAPI.ts
More file actions
75 lines (71 loc) · 2.76 KB
/
Copy pathAPI.ts
File metadata and controls
75 lines (71 loc) · 2.76 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
import axios from "axios"
const apiURL = "https://www.googleapis.com/youtube/v3/"
export default class API {
public constructor(private readonly apiKey: string) {}
public get = async (type: string, params?: any) => {
if (!params) params = {}
params.key = this.apiKey
let endpoint: string
switch (type) {
case "channel":
endpoint = "channels"
if (!params.part) params.part = "id, snippet, contentDetails, statistics, brandingSettings"
break
case "video":
endpoint = "videos"
if (!params.part) params.part = "id, player, snippet, contentDetails, status, statistics"
break
case "playlist":
endpoint = "playlists"
if (!params.part) params.part = "id, player, snippet, status, contentDetails"
break
case "search":
endpoint = "search"
params.part = "snippet"
if (!params.maxResults) params.maxResults = 50
break
case "comment":
endpoint = "comments"
params.part = "id, snippet"
break
case "commentThreads":
endpoint = "commentThreads"
params.part = "id, snippet, replies"
break
case "subscriptions":
endpoint = "subscriptions"
params.part = "id, snippet, contentDetails, subscriberSnippet"
break
case "playlistItems":
endpoint = "playlistItems"
params.part = "id, snippet, status, contentDetails"
break
case "channelSections":
endpoint = "channelSections"
params.part = "id, snippet, contentDetails"
break
case "guideCategories":
endpoint = "guideCategories"
params.part = "snippet"
break
case "videoCategories":
endpoint = "videoCategories"
params.part = "snippet"
break
default:
}
endpoint = apiURL + endpoint
const response = await axios.get(endpoint, {params}).then((r) => r.data)
if (!response.items[0]) return Promise.reject("Nothing was found.")
return response
}
public part = async (endpoint: string, part: string, params?: any) => {
if (!params) params = {}
params.key = this.apiKey
params.part = part
if (endpoint.startsWith("/")) endpoint = endpoint.slice(1)
endpoint = apiURL + endpoint
const response = await axios.get(endpoint, {params}).then((r) => r.data)
return response
}
}