-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
93 lines (79 loc) · 2.26 KB
/
mod.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
export interface AutocompleteExtraData {
results: AutocompleteExtraPreview[];
}
export interface AutocompleteExtraPreview {
preview: string;
term: string;
}
export interface UncacheableEntry {
defid: number;
up: number;
down: number;
current: string;
}
export interface UncacheableEntryList {
thumbs: UncacheableEntry[];
}
export interface Word {
definition: string;
permalink: string;
thumbs_up: number;
sound_urls?: string[];
author: string;
word: string;
defid: number;
current_vote: string;
written_on: string;
example: string;
thumbs_down: number;
}
export interface WordList {
list: Word[];
}
export const BASE_URL = "https://api.urbandictionary.com/v0";
export const request = async <T>(path: string): Promise<T> => {
const response = await fetch(BASE_URL + path);
if (response.ok) {
return response.json();
}
throw new Error(await response.text());
};
/**
* Get the autocompletions for a term.
* @param term The term to get autocompletions for.
*/
export const autocomplete = (term: string) =>
request<string[]>(`/autocomplete?term=${term}`);
/**
* Get the autocompletions for a term with extra preview definitions.
* @param term The term to get autocompletion extras for.
*/
export const autocompleteExtra = (term: string) =>
request<AutocompleteExtraData>(`/autocomplete-extra?term=${term}`);
/**
* Get the definition of a word.
* @param term The term to define.
* @param page The page to start searching from.
*/
export const define = (term: string, page = 1) =>
request<WordList>(`/define?term=${term}&page=${page}`);
/**
* Look up the definition of a word by searching with it's ID.
* @param defid The definition's ID.
*/
export const defineDefid = async (defid: number) =>
(await request<WordList>(`/define?defid=${defid}`)).list[0];
/** Get a random list of definitions. */
export const random = () => request<WordList>("/random");
/**
* I have no idea what this does.
* @param ids A list of definition IDs.
*/
export const uncacheable = (ids: number[]) =>
request<UncacheableEntryList>(`/uncacheable?ids=${ids.join(",")}`);
/**
* Get the words of the day.
* @param page The page to start searching from.
*/
export const wordsOfTheDay = (page = 1) =>
request<WordList>(`/words_of_the_day?page=${page}`);