@superblocksteam/util 0.0.16 → 0.0.17
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/dist/login.d.ts +7 -5
- package/dist/login.js +27 -9
- package/dist/resource-configs.d.ts +0 -1
- package/package.json +1 -1
- package/src/login.ts +37 -14
- package/src/resource-configs.ts +0 -1
package/dist/login.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
type
|
|
2
|
-
token: string;
|
|
1
|
+
type BaseUrl = {
|
|
3
2
|
superblocksBaseUrl: string;
|
|
4
3
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
type TokenWithBaseUrl = BaseUrl & {
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function saveApiToken(superblocksBaseUrl: string, token?: string): Promise<void>;
|
|
8
|
+
export declare function getLocalTokenWithUrlIfExists(): Promise<TokenWithBaseUrl | BaseUrl | undefined>;
|
|
9
|
+
export declare function getLocalTokenWithUrl(): Promise<TokenWithBaseUrl | BaseUrl>;
|
|
8
10
|
export {};
|
package/dist/login.js
CHANGED
|
@@ -6,13 +6,22 @@ const node_os_1 = require("node:os");
|
|
|
6
6
|
const node_path_1 = require("node:path");
|
|
7
7
|
const fs = tslib_1.__importStar(require("fs-extra"));
|
|
8
8
|
const constants_1 = require("./constants");
|
|
9
|
-
async function saveApiToken(
|
|
9
|
+
async function saveApiToken(superblocksBaseUrl, token) {
|
|
10
10
|
try {
|
|
11
11
|
await fs.ensureDir((0, node_path_1.join)((0, node_os_1.homedir)(), ".superblocks"));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
if (token) {
|
|
13
|
+
const tokenConfig = {
|
|
14
|
+
superblocksBaseUrl,
|
|
15
|
+
token,
|
|
16
|
+
};
|
|
17
|
+
await fs.writeJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH), tokenConfig);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const tokenConfig = {
|
|
21
|
+
superblocksBaseUrl,
|
|
22
|
+
};
|
|
23
|
+
await fs.writeJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH), tokenConfig);
|
|
24
|
+
}
|
|
16
25
|
}
|
|
17
26
|
catch {
|
|
18
27
|
throw new constants_1.FileAccessError("Could not save token");
|
|
@@ -31,10 +40,19 @@ exports.getLocalTokenWithUrlIfExists = getLocalTokenWithUrlIfExists;
|
|
|
31
40
|
async function getLocalTokenWithUrl() {
|
|
32
41
|
try {
|
|
33
42
|
const tokenConfig = await fs.readJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH));
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
if (tokenConfig.token) {
|
|
44
|
+
// user logged in
|
|
45
|
+
return {
|
|
46
|
+
token: tokenConfig.token,
|
|
47
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// user not logged in, but set domain
|
|
52
|
+
return {
|
|
53
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
38
56
|
}
|
|
39
57
|
catch {
|
|
40
58
|
throw new Error("No local API key found");
|
package/package.json
CHANGED
package/src/login.ts
CHANGED
|
@@ -3,25 +3,36 @@ import { join } from "node:path";
|
|
|
3
3
|
import * as fs from "fs-extra";
|
|
4
4
|
import { FileAccessError, TOKEN_CONFIG_PATH } from "./constants";
|
|
5
5
|
|
|
6
|
-
type
|
|
7
|
-
token: string;
|
|
6
|
+
type BaseUrl = {
|
|
8
7
|
superblocksBaseUrl: string;
|
|
9
8
|
};
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
type TokenWithBaseUrl = BaseUrl & {
|
|
11
|
+
token: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function saveApiToken(superblocksBaseUrl: string, token?: string) {
|
|
12
15
|
try {
|
|
13
16
|
await fs.ensureDir(join(homedir(), ".superblocks"));
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
if (token) {
|
|
18
|
+
const tokenConfig: TokenWithBaseUrl = {
|
|
19
|
+
superblocksBaseUrl,
|
|
20
|
+
token,
|
|
21
|
+
};
|
|
22
|
+
await fs.writeJSON(join(homedir(), TOKEN_CONFIG_PATH), tokenConfig);
|
|
23
|
+
} else {
|
|
24
|
+
const tokenConfig: BaseUrl = {
|
|
25
|
+
superblocksBaseUrl,
|
|
26
|
+
};
|
|
27
|
+
await fs.writeJSON(join(homedir(), TOKEN_CONFIG_PATH), tokenConfig);
|
|
28
|
+
}
|
|
18
29
|
} catch {
|
|
19
30
|
throw new FileAccessError("Could not save token");
|
|
20
31
|
}
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
export async function getLocalTokenWithUrlIfExists(): Promise<
|
|
24
|
-
TokenWithBaseUrl | undefined
|
|
35
|
+
TokenWithBaseUrl | BaseUrl | undefined
|
|
25
36
|
> {
|
|
26
37
|
try {
|
|
27
38
|
return await getLocalTokenWithUrl();
|
|
@@ -30,13 +41,25 @@ export async function getLocalTokenWithUrlIfExists(): Promise<
|
|
|
30
41
|
}
|
|
31
42
|
}
|
|
32
43
|
|
|
33
|
-
export async function getLocalTokenWithUrl(): Promise<
|
|
44
|
+
export async function getLocalTokenWithUrl(): Promise<
|
|
45
|
+
TokenWithBaseUrl | BaseUrl
|
|
46
|
+
> {
|
|
34
47
|
try {
|
|
35
|
-
const tokenConfig = await fs.readJSON(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
const tokenConfig: TokenWithBaseUrl = await fs.readJSON(
|
|
49
|
+
join(homedir(), TOKEN_CONFIG_PATH)
|
|
50
|
+
);
|
|
51
|
+
if (tokenConfig.token) {
|
|
52
|
+
// user logged in
|
|
53
|
+
return {
|
|
54
|
+
token: tokenConfig.token,
|
|
55
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
56
|
+
};
|
|
57
|
+
} else {
|
|
58
|
+
// user not logged in, but set domain
|
|
59
|
+
return {
|
|
60
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
40
63
|
} catch {
|
|
41
64
|
throw new Error("No local API key found");
|
|
42
65
|
}
|