Skip to content

Commit 9bab868

Browse files
committed
added post and delete commands
1 parent e4c876c commit 9bab868

File tree

5 files changed

+411
-22
lines changed

5 files changed

+411
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ dist
107107
.vscode/
108108

109109
# other stuff
110-
.pasty.json
110+
.pasty.json
111+
.test

lib/api-functions.js

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import inquirer from 'inquirer';
22
import fetch from 'node-fetch';
3-
import { LOGIN_LINK } from './constant.js';
4-
import { rmSync, writeFileSync } from 'fs';
3+
import { API_URLS, FORMAT_CHOICES } from './constant.js';
4+
import { readFileSync, rmSync, writeFileSync } from 'fs';
55
import { homedir } from 'os';
66
import { table } from 'table';
7-
import {
8-
convertToDate,
9-
getListTable,
10-
mapToVisibilityString,
11-
tokenGuard,
12-
} from './util.js';
7+
import { getListTable, mapToVisiblityCode, tokenGuard } from './util.js';
138

149
export async function loginUser(argv, apiToken) {
1510
if (tokenGuard(apiToken)) {
@@ -31,7 +26,7 @@ export async function loginUser(argv, apiToken) {
3126
])
3227
).password;
3328

34-
const response = await fetch(LOGIN_LINK, {
29+
const response = await fetch(API_URLS.login, {
3530
body: `api_dev_key=${apiToken}&api_user_name=${username}&api_user_password=${password}`,
3631
headers: {
3732
'Content-Type': 'application/x-www-form-urlencoded',
@@ -49,7 +44,7 @@ export async function loginUser(argv, apiToken) {
4944
}
5045
}
5146

52-
export async function listPastes(argv, apiToken, userToken) {
47+
export async function listPastes(amount, apiToken, userToken) {
5348
if (tokenGuard(apiToken)) {
5449
console.log(
5550
'Please provide your pastebin.com API token in the ~/.pasty.api file.'
@@ -62,31 +57,77 @@ export async function listPastes(argv, apiToken, userToken) {
6257
process.exit(1);
6358
}
6459

65-
const amount = argv.amount;
66-
67-
const response = await fetch('https://pastebin.com/api/api_post.php', {
60+
const response = await fetch(API_URLS.apiPost, {
6861
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=list&api_results_limit=${amount}`,
6962
headers: {
7063
'Content-Type': 'application/x-www-form-urlencoded',
7164
},
7265
method: 'POST',
7366
});
74-
const data = await response.text();
67+
const text = await response.text();
7568

7669
if (response.status === 200) {
77-
const tableData = getListTable(data);
70+
const tableData = getListTable(text);
7871

7972
console.log(table(tableData));
8073
} else {
81-
console.log('Could not get pastes: ', data);
74+
console.log(`Error! ${text}`);
8275
}
8376
}
8477

8578
export function logout() {
8679
try {
8780
rmSync(`${homedir()}/.pasty.user`);
88-
console.log('Success! Logged you out.');
81+
console.log('Successfully logged you out.');
8982
} catch (e) {
9083
console.log("You're currently not logged in");
9184
}
9285
}
86+
87+
export async function deletePaste(pasteId, apiToken, userToken) {
88+
const response = await fetch(API_URLS.apiPost, {
89+
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=delete&api_paste_key=${pasteId}`,
90+
headers: {
91+
'Content-Type': 'application/x-www-form-urlencoded',
92+
},
93+
method: 'POST',
94+
});
95+
96+
const text = await response.text();
97+
98+
if (response.status === 200) {
99+
console.log(`Success! ${text}`);
100+
} else {
101+
console.log(`Error! ${text}`);
102+
}
103+
}
104+
105+
export async function createPaste(argv, apiToken, userToken) {
106+
const { file, string, format, visibility, expiry, folder, name } = argv;
107+
108+
if (format && !FORMAT_CHOICES.includes(format)) {
109+
console.log(
110+
'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats'
111+
);
112+
process.exit(1);
113+
}
114+
115+
const mappedVisibility = mapToVisiblityCode(visibility);
116+
const pasteText = file ? readFileSync(file, 'utf-8').trim() : string;
117+
118+
const response = await fetch(API_URLS.apiPost, {
119+
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=paste&api_paste_code=${pasteText}&api_paste_name=${name}&api_paste_format=${format}&api_paste_private=${mappedVisibility}`,
120+
headers: {
121+
'Content-Type': 'application/x-www-form-urlencoded',
122+
},
123+
method: 'POST',
124+
});
125+
126+
const text = await response.text();
127+
128+
if (response.status === 200) {
129+
console.log(text);
130+
} else {
131+
console.log(`Error! ${text}`);
132+
}
133+
}

0 commit comments

Comments
 (0)