-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
168 lines (135 loc) · 4.17 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const PRODUCTION_BASE_URL = 'https://api.tenor.com/v1';
export type ContentFilter = 'off' | 'low' | 'medium' | 'high';
export type GifFormat =
| 'gif'
| 'mediumgif'
| 'tinygif'
| 'nanogif'
| 'mp4'
| 'loopedmp4'
| 'tinymp4'
| 'nanomp4'
| 'webm'
| 'tinywebm'
| 'nanowebm';
export type CategoryType = 'featured' | 'emoji' | 'trending';
export interface SearchResult {
next: string;
results: Gif[];
}
export type TrendingResult = SearchResult;
export interface CategoryResult {
tags: Tag[];
}
export interface Media {
preview: string;
url: string;
dims: number[];
size: number;
}
export interface Gif {
created: number;
hasaudio: boolean;
id: string;
// https://github.com/microsoft/TypeScript/issues/24220
media: { [format in GifFormat]: Media };
tags: string[];
title: string;
itemurl: string;
hascaption: boolean;
url: string;
}
export interface Tag {
searchterm: string;
path: string;
image: string;
name: string;
}
export interface SearchOptions {
locale?: string;
media_filter?: string;
ar_range?: string;
contentfilter?: ContentFilter;
limit?: number;
pos?: string;
anon_id?: string;
}
export type TrendingOptions = SearchOptions;
export interface CategoryOptions {
locale?: string;
type?: CategoryType;
contentfilter?: ContentFilter;
anon_id?: string;
}
export interface SearchSuggestionsOptions {
locale?: string;
limit?: number;
anon_id?: string;
}
export interface SearchSuggestionsResult {
results: string[];
}
export type AutoCompleteOptions = SearchSuggestionsOptions;
export type AutoCompleteResult = SearchSuggestionsResult;
export type TrendingSearchTermsOptions = SearchSuggestionsOptions;
export type TrendingSearchTermsResult = SearchSuggestionsResult;
export interface RegisterShareOptions {
locale?: string;
q?: string;
anon_id?: string;
}
export interface RegisterShareResult {
status: string;
}
export interface GifsOptions {
media_filter?: string;
limit?: number;
pos?: string;
anon_id?: string;
}
export type GifsResult = SearchResult;
export interface AnonymousIDResult {
anon_id: string;
}
export default class TenorFetch {
baseURL: string;
constructor(private apiKey: string) {
this.baseURL = PRODUCTION_BASE_URL;
}
get(endpoint: string, ...params: object[]): Promise<any> {
const searchParams = new URLSearchParams(Object.assign({}, ...params)).toString();
const url = `${this.baseURL}/${endpoint}?${searchParams}`;
return fetch(url).then((resp) => resp.json());
}
search(term: string, options?: SearchOptions): Promise<SearchResult> {
return this.get('search', { q: term }, options || {}) as Promise<SearchResult>;
}
trending(options?: TrendingOptions): Promise<TrendingResult> {
return this.get('trending', options || {}) as Promise<TrendingResult>;
}
categories(options?: CategoryOptions): Promise<CategoryResult> {
return this.get('categories', options || {}) as Promise<CategoryResult>;
}
searchSuggestions(term: string, options?: SearchSuggestionsOptions): Promise<SearchSuggestionsResult> {
return this.get('search_suggestions', { q: term }, options || {}) as Promise<SearchSuggestionsResult>;
}
autoComplete(term: string, options?: AutoCompleteOptions): Promise<AutoCompleteResult> {
return this.get('autocomplete', { q: term }, options || {}) as Promise<AutoCompleteResult>;
}
trendingSearchTerms(options?: TrendingSearchTermsOptions): Promise<TrendingSearchTermsResult> {
return this.get('trending_terms', options || {}) as Promise<TrendingSearchTermsResult>;
}
registerShare(id: string, options?: RegisterShareOptions): Promise<RegisterShareResult> {
return this.get('registershare', options || {}) as Promise<RegisterShareResult>;
}
gifs(ids: string[], options?: GifsOptions): Promise<GifsResult> {
const idsString = ids.join(',');
return this.get('gifs', { ids: idsString }, options || {}) as Promise<GifsResult>;
}
random(term: string, options?: SearchOptions): Promise<SearchResult> {
return this.get('random', { q: term }, options || {}) as Promise<SearchResult>;
}
anonymousID(): Promise<AnonymousIDResult> {
return this.get('anonid') as Promise<AnonymousIDResult>;
}
}