1
1
import inquirer from 'inquirer' ;
2
2
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' ;
5
5
import { homedir } from 'os' ;
6
6
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' ;
13
8
14
9
export async function loginUser ( argv , apiToken ) {
15
10
if ( tokenGuard ( apiToken ) ) {
@@ -31,7 +26,7 @@ export async function loginUser(argv, apiToken) {
31
26
] )
32
27
) . password ;
33
28
34
- const response = await fetch ( LOGIN_LINK , {
29
+ const response = await fetch ( API_URLS . login , {
35
30
body : `api_dev_key=${ apiToken } &api_user_name=${ username } &api_user_password=${ password } ` ,
36
31
headers : {
37
32
'Content-Type' : 'application/x-www-form-urlencoded' ,
@@ -49,7 +44,7 @@ export async function loginUser(argv, apiToken) {
49
44
}
50
45
}
51
46
52
- export async function listPastes ( argv , apiToken , userToken ) {
47
+ export async function listPastes ( amount , apiToken , userToken ) {
53
48
if ( tokenGuard ( apiToken ) ) {
54
49
console . log (
55
50
'Please provide your pastebin.com API token in the ~/.pasty.api file.'
@@ -62,31 +57,77 @@ export async function listPastes(argv, apiToken, userToken) {
62
57
process . exit ( 1 ) ;
63
58
}
64
59
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 , {
68
61
body : `api_dev_key=${ apiToken } &api_user_key=${ userToken } &api_option=list&api_results_limit=${ amount } ` ,
69
62
headers : {
70
63
'Content-Type' : 'application/x-www-form-urlencoded' ,
71
64
} ,
72
65
method : 'POST' ,
73
66
} ) ;
74
- const data = await response . text ( ) ;
67
+ const text = await response . text ( ) ;
75
68
76
69
if ( response . status === 200 ) {
77
- const tableData = getListTable ( data ) ;
70
+ const tableData = getListTable ( text ) ;
78
71
79
72
console . log ( table ( tableData ) ) ;
80
73
} else {
81
- console . log ( 'Could not get pastes: ' , data ) ;
74
+ console . log ( `Error! ${ text } ` ) ;
82
75
}
83
76
}
84
77
85
78
export function logout ( ) {
86
79
try {
87
80
rmSync ( `${ homedir ( ) } /.pasty.user` ) ;
88
- console . log ( 'Success! Logged you out.' ) ;
81
+ console . log ( 'Successfully logged you out.' ) ;
89
82
} catch ( e ) {
90
83
console . log ( "You're currently not logged in" ) ;
91
84
}
92
85
}
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