FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
commandcode-api-proxy/src/auth.ts at main · thaolaptrinh/commandcode-api-proxy · GitHub
thaolaptrinh
/
commandcode-api-proxy
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
18
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
commandcode-api-proxy
/
src
/
auth.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
79 lines (71 loc) · 2.16 KB
Breadcrumbs
commandcode-api-proxy
/
src
/
auth.ts
Copy path
File metadata and controls
79 lines (71 loc) · 2.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
fs
from
"node:fs"
;
import
path
from
"node:path"
;
import
readline
from
"node:readline"
;
import
tty
from
"node:tty"
;
export
function
getAuthDir
(
)
:
string
{
const
home
=
process
.
env
.
HOME
||
process
.
env
.
USERPROFILE
;
if
(
!
home
)
throw
new
Error
(
"Cannot determine home directory"
)
;
return
path
.
join
(
home
,
".config"
,
"commandcode-api-proxy"
)
;
}
export
function
getAuthPath
(
)
:
string
{
return
path
.
join
(
getAuthDir
(
)
,
"auth.json"
)
;
}
export
function
readAuthKey
(
)
:
string
|
null
{
try
{
const
raw
=
fs
.
readFileSync
(
getAuthPath
(
)
,
"utf-8"
)
;
const
parsed
=
JSON
.
parse
(
raw
)
;
return
parsed
.
apiKey
||
parsed
.
accessToken
||
parsed
.
token
||
null
;
}
catch
{
return
null
;
}
}
export
function
saveApiKey
(
key
:
string
)
:
void
{
const
dir
=
getAuthDir
(
)
;
fs
.
mkdirSync
(
dir
,
{
recursive
:
true
}
)
;
fs
.
writeFileSync
(
getAuthPath
(
)
,
JSON
.
stringify
(
{
apiKey
:
key
}
,
null
,
2
)
+
"\n"
)
;
}
export
function
deleteAuth
(
)
:
void
{
try
{
fs
.
unlinkSync
(
getAuthPath
(
)
)
;
}
catch
{
// Ignore if file doesn't exist
}
}
export
async
function
promptForApiKey
(
)
:
Promise
<
string
>
{
const
stdin
=
process
.
stdin
as
tty
.
ReadStream
;
if
(
!
stdin
.
isTTY
)
{
const
rl
=
readline
.
createInterface
(
{
input
:
process
.
stdin
,
output
:
process
.
stdout
}
)
;
return
new
Promise
(
(
resolve
)
=>
{
rl
.
question
(
"\n Enter your Command Code API key: "
,
(
a
)
=>
{
rl
.
close
(
)
;
resolve
(
a
.
trim
(
)
)
;
}
)
;
}
)
;
}
return
new
Promise
(
(
resolve
)
=>
{
stdin
.
setRawMode
(
true
)
;
stdin
.
resume
(
)
;
let
input
=
""
;
process
.
stdout
.
write
(
"\n Enter your Command Code API key: "
)
;
const
onData
=
(
chunk
:
Buffer
)
=>
{
const
char
=
chunk
.
toString
(
)
;
if
(
char
===
"\r"
||
char
===
"\n"
)
{
stdin
.
setRawMode
(
false
)
;
stdin
.
pause
(
)
;
stdin
.
removeListener
(
"data"
,
onData
)
;
process
.
stdout
.
write
(
"\n"
)
;
resolve
(
input
)
;
}
else
if
(
char
===
"\x7f"
||
char
===
"\b"
)
{
if
(
input
.
length
>
0
)
{
input
=
input
.
slice
(
0
,
-
1
)
;
}
}
else
if
(
char
===
"\x03"
)
{
stdin
.
setRawMode
(
false
)
;
process
.
exit
(
0
)
;
}
else
{
input
+=
char
;
}
}
;
stdin
.
on
(
"data"
,
onData
)
;
}
)
;
}
Back
|
FazBrowse Home
|
New Git URL