@superpms/memory-cli 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/commands/login.mjs +4 -4
- package/lib/api-client.mjs +11 -11
- package/package.json +1 -1
package/commands/login.mjs
CHANGED
|
@@ -26,11 +26,11 @@ export default async function login(args) {
|
|
|
26
26
|
async function handleLogin(args) {
|
|
27
27
|
const endpoint = args.find((a) => a.startsWith('--endpoint='))?.split('=')[1];
|
|
28
28
|
|
|
29
|
-
const username = await prompt('
|
|
29
|
+
const username = await prompt('Email: ');
|
|
30
30
|
const password = await prompt('Password: ');
|
|
31
31
|
|
|
32
32
|
try {
|
|
33
|
-
const result = await cloudLogin(username, password);
|
|
33
|
+
const result = await cloudLogin(username, password, endpoint);
|
|
34
34
|
saveCredentials(result.api_key, result.user_id, result.will_id, endpoint);
|
|
35
35
|
console.log('Logged in as: ' + result.display_name + ' (' + result.username + ')');
|
|
36
36
|
console.log('Will ID: ' + result.will_id);
|
|
@@ -43,12 +43,12 @@ async function handleLogin(args) {
|
|
|
43
43
|
async function handleRegister(args) {
|
|
44
44
|
const endpoint = args.find((a) => a.startsWith('--endpoint='))?.split('=')[1];
|
|
45
45
|
|
|
46
|
-
const username = await prompt('
|
|
46
|
+
const username = await prompt('Email: ');
|
|
47
47
|
const password = await prompt('Password: ');
|
|
48
48
|
const displayName = await prompt('Display name (optional): ');
|
|
49
49
|
|
|
50
50
|
try {
|
|
51
|
-
const result = await cloudRegister(username, password, displayName || undefined);
|
|
51
|
+
const result = await cloudRegister(username, password, displayName || undefined, endpoint);
|
|
52
52
|
saveCredentials(result.api_key, result.user_id, result.will_id, endpoint);
|
|
53
53
|
console.log('Registered as: ' + result.display_name + ' (' + result.username + ')');
|
|
54
54
|
console.log('Will ID: ' + result.will_id);
|
package/lib/api-client.mjs
CHANGED
|
@@ -6,8 +6,8 @@ import { readCredentials } from './credentials.mjs';
|
|
|
6
6
|
import { request } from 'node:http';
|
|
7
7
|
import { request as httpsRequest } from 'node:https';
|
|
8
8
|
|
|
9
|
-
function getEndpoint() {
|
|
10
|
-
return readCredentials().api_endpoint || 'http://localhost:3900';
|
|
9
|
+
function getEndpoint(override) {
|
|
10
|
+
return override || readCredentials().api_endpoint || 'http://localhost:3900';
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
function getApiKey() {
|
|
@@ -19,14 +19,14 @@ function isConfigured() {
|
|
|
19
19
|
return !!(creds.api_key && creds.api_endpoint);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function httpFetch(method, path, body = null) {
|
|
22
|
+
function httpFetch(method, path, body = null, options = {}) {
|
|
23
23
|
return new Promise((resolve, reject) => {
|
|
24
|
-
const endpoint = getEndpoint();
|
|
24
|
+
const endpoint = getEndpoint(options.endpoint);
|
|
25
25
|
const url = new URL(path, endpoint);
|
|
26
26
|
const isHttps = url.protocol === 'https:';
|
|
27
27
|
const reqFn = isHttps ? httpsRequest : request;
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const requestOptions = {
|
|
30
30
|
hostname: url.hostname,
|
|
31
31
|
port: url.port || (isHttps ? 443 : 80),
|
|
32
32
|
path: url.pathname + url.search,
|
|
@@ -37,9 +37,9 @@ function httpFetch(method, path, body = null) {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
const apiKey = getApiKey();
|
|
40
|
-
if (apiKey)
|
|
40
|
+
if (apiKey) requestOptions.headers['Authorization'] = 'Bearer ' + apiKey;
|
|
41
41
|
|
|
42
|
-
const req = reqFn(
|
|
42
|
+
const req = reqFn(requestOptions, (res) => {
|
|
43
43
|
const chunks = [];
|
|
44
44
|
res.on('data', (c) => chunks.push(c));
|
|
45
45
|
res.on('end', () => {
|
|
@@ -68,12 +68,12 @@ function httpFetch(method, path, body = null) {
|
|
|
68
68
|
|
|
69
69
|
// --- Auth ---
|
|
70
70
|
|
|
71
|
-
export async function cloudRegister(username, password, displayName) {
|
|
72
|
-
return httpFetch('POST', '/auth/register', { username, password, display_name: displayName });
|
|
71
|
+
export async function cloudRegister(username, password, displayName, endpoint) {
|
|
72
|
+
return httpFetch('POST', '/auth/register', { username, password, display_name: displayName }, { endpoint });
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
export async function cloudLogin(username, password) {
|
|
76
|
-
return httpFetch('POST', '/auth/login', { username, password });
|
|
75
|
+
export async function cloudLogin(username, password, endpoint) {
|
|
76
|
+
return httpFetch('POST', '/auth/login', { username, password }, { endpoint });
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
export async function cloudMe() {
|