-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
207 lines (182 loc) · 5.97 KB
/
api.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
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
import { parse } from "https://deno.land/x/[email protected]/mod.ts";
import type {
ClientOptions,
CreateOptions,
DeletePasteOptions,
GetPastesOptions,
GetRawPasteOptions,
Paste,
User,
} from "./types.ts";
export class PasteClient {
private devKey: string;
private rootDomain = "pastebin.com";
constructor(options: string | ClientOptions) {
if (typeof options === "string") {
this.devKey = options;
} else {
this.devKey = options.devKey;
this.rootDomain = options.domain ?? "pastebin.com";
}
}
private async request(
url: string,
options: Record<string, unknown>,
): Promise<string> {
let body = "";
for (const [key, value] of Object.entries(options)) {
if (!value) continue;
body += `&${encodeURIComponent(key)}=${encodeURIComponent(`${value}`)}`;
}
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: body.substring(1),
});
const text = await res.text();
if (!res.ok || text.startsWith("Bad API request")) throw new Error(text);
return text;
}
/**
* Set custom API root domain. Uses `pastebin.com` by default.
* @param domain The domain of your reverse proxy server.
*/
set domain(value: string) {
this.rootDomain = value;
}
/**
* login to get access to more API routes. See [docs](https://pastebin.com/doc_api#9)
* @param name The user's name.
* @param password The user's password.
* @returns The user token to use for other API routes.
*/
async login(name: string, password: string): Promise<string> {
const res = await this.request(
`https://${this.rootDomain}/api/api_login.php`,
{
api_dev_key: this.devKey,
api_user_name: name,
api_user_password: password,
},
);
if (res.startsWith("Bad API request")) throw new Error(res);
return res;
}
/**
* return raw paste by it's key. See [docs](https://pastebin.com/doc_api#14).
* @param options URL or the key of the paste, if it is public or unlisted. If it's an private paste, pass an object with required options.
* @returns The raw paste
*/
async getRaw(options: string | GetRawPasteOptions): Promise<string> {
// Just key or URL to the paste -- only public and unlisted.
if (typeof options === "string") {
let key = options;
const pastebinRegEx = new RegExp(
`^((?:https?:)?\/\/)?\.?((?:pastebin\.com|${this.rootDomain}))\/(.+)`,
);
if (pastebinRegEx.test(key)) key = key.match(pastebinRegEx)![3];
const res = await fetch(`https://pastebin.com/raw/${key}`);
if (!res.ok) throw new Error(res.statusText);
const code = await res.text();
return code;
}
// Support for private pastes.
const code = await this.request(
`https://${this.rootDomain}/api/api_raw.php`,
{
api_dev_key: this.devKey,
api_option: "show_paste",
api_user_key: options.userKey,
api_paste_key: options.pasteKey,
},
);
if (code.startsWith("Bad API request")) throw new Error(code);
return code;
}
async getUser(userKey: string): Promise<User> {
const res = await this.request(
`https://${this.rootDomain}/api/api_post.php`,
{
api_dev_key: this.devKey,
api_option: "userdetails",
api_user_key: userKey,
},
);
const parsed = parse(`<u>${res}</u>`);
const { user } = parsed["u"] as Record<string, User>;
return user;
}
/**
* Get a limit of 1000 pastes from the logged in user. See [docs](https://pastebin.com/doc_api#10).
* @param options User key and limit.
* @returns An array of all the user's pastes
*/
async getPastes(
options: GetPastesOptions,
): Promise<Paste[]> {
if (options.limit) {
if (options.limit < 1 || options.limit > 1000) {
throw TypeError("Limit cannot be lower than 1 or higher than 1000");
}
}
const res = await this.request(
`https://${this.rootDomain}/api/api_post.php`,
{
api_dev_key: this.devKey,
api_option: "list",
api_user_key: options.userKey,
api_results_limit: options.limit,
},
);
if (res === "No pastes found.") return [];
const parsed = parse(`<pastes>${res}</pastes>`);
const { paste } = parsed["pastes"] as Record<string, Paste[]>;
if (Array.isArray(paste)) return paste;
return [paste];
}
/**
* Create a new paste. See [official docs](https://pastebin.com/doc_api#2).
* @param options The options for the paste
* @returns The URL of the created paste
*/
async create(options: CreateOptions): Promise<string> {
if (options.name && options.name.length > 100) {
throw new TypeError("Name of paste cannot be longer than 100 characters");
}
const res = await this.request(
`https://${this.rootDomain}/api/api_post.php`,
{
api_dev_key: this.devKey,
api_option: "paste",
api_paste_name: options.name ?? "Untitled",
api_paste_code: options.code,
api_paste_format: options.format ?? "text",
api_paste_private: options.publicity ?? 0,
api_paste_expire_date: options.expireDate ?? "N",
api_user_key: options.userKey ?? "",
api_folder_key: options.folderKey ?? "",
},
);
if (res.startsWith("Bad API request")) throw new Error(res);
return res;
}
/**
* Delete a paste by it's key. See [docs](https://pastebin.com/doc_api#11).
* @param options User key and paste key.
* @returns Whether it was deleted or not.
*/
async deletePaste(options: DeletePasteOptions): Promise<boolean> {
const res = await this.request(
`https://${this.rootDomain}/api/api_post.php`,
{
api_dev_key: this.devKey,
api_option: "delete",
api_paste_key: options.pasteKey,
api_user_key: options.userKey,
},
);
// paste was successfully removed
if (res === "Paste Removed") return true;
throw new Error(res);
}
}