Skip to content

Commit 059046d

Browse files
committed
More tests and small refactoring
1 parent 5db8dcc commit 059046d

File tree

13 files changed

+733
-448
lines changed

13 files changed

+733
-448
lines changed

lib/api-functions.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import inquirer from 'inquirer';
2-
import fetch from 'node-fetch';
3-
import { API_URLS, FORMAT_CHOICES } from './constant.js';
4-
import { readFileSync, rmSync, writeFileSync } from 'fs';
5-
import { homedir } from 'os';
6-
import { table } from 'table';
7-
import { getListTable, mapToVisiblityCode, tokenGuard } from './util.js';
8-
9-
export async function loginUser(argv, apiToken) {
1+
const inquirer = require('inquirer');
2+
const fetch = require('node-fetch');
3+
const { API_URLS, FORMAT_CHOICES } = require('./constant.js');
4+
const { readFileSync, rmSync, writeFileSync, existsSync } = require('fs');
5+
const { homedir } = require('os');
6+
const { table } = require('table');
7+
const { getListTable, mapToVisiblityCode, tokenGuard } = require('./util.js');
8+
9+
module.exports = {
10+
loginUser,
11+
listPastes,
12+
logout,
13+
deletePaste,
14+
createPaste,
15+
};
16+
17+
async function loginUser(argv, apiToken) {
1018
if (tokenGuard(apiToken)) {
1119
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
1220
}
@@ -42,7 +50,7 @@ export async function loginUser(argv, apiToken) {
4250
}
4351
}
4452

45-
export async function listPastes(amount, apiToken, userToken) {
53+
async function listPastes(amount, apiToken, userToken) {
4654
if (tokenGuard(apiToken)) {
4755
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
4856
}
@@ -69,16 +77,23 @@ export async function listPastes(amount, apiToken, userToken) {
6977
}
7078
}
7179

72-
export function logout() {
73-
try {
80+
function logout() {
81+
if (existsSync(`${homedir}/.pasty.user`)) {
7482
rmSync(`${homedir()}/.pasty.user`);
7583
return 'Successfully logged you out.';
76-
} catch (e) {
77-
return "You're currently not logged in";
7884
}
85+
return "You're currently not logged in (could not find ~/.pasty.user)";
7986
}
8087

81-
export async function deletePaste(pasteId, apiToken, userToken) {
88+
async function deletePaste(pasteId, apiToken, userToken) {
89+
if (tokenGuard(apiToken)) {
90+
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
91+
}
92+
93+
if (tokenGuard(userToken)) {
94+
return 'Please login first via pasty login <username>';
95+
}
96+
8297
const response = await fetch(API_URLS.apiPost, {
8398
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=delete&api_paste_key=${pasteId}`,
8499
headers: {
@@ -92,16 +107,26 @@ export async function deletePaste(pasteId, apiToken, userToken) {
92107
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
93108
}
94109

95-
export async function createPaste(argv, apiToken, userToken) {
110+
async function createPaste(argv, apiToken, userToken) {
111+
if (tokenGuard(apiToken)) {
112+
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
113+
}
114+
115+
if (tokenGuard(userToken)) {
116+
return 'Please login first via pasty login <username>';
117+
}
118+
96119
const { file, string, format, visibility, expiry, folder, name } = argv;
97120

98-
if (format && !FORMAT_CHOICES.includes(format)) {
121+
if (!FORMAT_CHOICES.includes(format)) {
99122
return 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats';
100123
}
101124

102125
const mappedVisibility = mapToVisiblityCode(visibility);
103126
const pasteText = file ? readFileSync(file, 'utf-8').trim() : string;
104127

128+
console.log(name, pasteText);
129+
105130
const response = await fetch(API_URLS.apiPost, {
106131
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}`,
107132
headers: {
@@ -112,5 +137,5 @@ export async function createPaste(argv, apiToken, userToken) {
112137

113138
const text = await response.text();
114139

115-
return response.status === 200 ? `Succes! ${text}` : `Error! ${text}`;
140+
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
116141
}

0 commit comments

Comments
 (0)