@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 CHANGED
@@ -1,8 +1,10 @@
1
- type TokenWithBaseUrl = {
2
- token: string;
1
+ type BaseUrl = {
3
2
  superblocksBaseUrl: string;
4
3
  };
5
- export declare function saveApiToken(token: string, superblocksBaseUrl: string): Promise<void>;
6
- export declare function getLocalTokenWithUrlIfExists(): Promise<TokenWithBaseUrl | undefined>;
7
- export declare function getLocalTokenWithUrl(): Promise<TokenWithBaseUrl>;
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(token, superblocksBaseUrl) {
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
- await fs.writeJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH), {
13
- token,
14
- superblocksBaseUrl,
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
- return {
35
- token: tokenConfig.token,
36
- superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
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");
@@ -1,6 +1,5 @@
1
1
  export type SuperblocksMetadata = {
2
2
  cliVersion: string;
3
- remote: string;
4
3
  lastUpdated: string;
5
4
  created: string;
6
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/util",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "main": "dist/index.js",
5
5
  "homepage": "https://www.superblocks.com",
6
6
  "scripts": {
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 TokenWithBaseUrl = {
7
- token: string;
6
+ type BaseUrl = {
8
7
  superblocksBaseUrl: string;
9
8
  };
10
9
 
11
- export async function saveApiToken(token: string, superblocksBaseUrl: string) {
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
- await fs.writeJSON(join(homedir(), TOKEN_CONFIG_PATH), {
15
- token,
16
- superblocksBaseUrl,
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<TokenWithBaseUrl> {
44
+ export async function getLocalTokenWithUrl(): Promise<
45
+ TokenWithBaseUrl | BaseUrl
46
+ > {
34
47
  try {
35
- const tokenConfig = await fs.readJSON(join(homedir(), TOKEN_CONFIG_PATH));
36
- return {
37
- token: tokenConfig.token as string,
38
- superblocksBaseUrl: tokenConfig.superblocksBaseUrl as string,
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
  }
@@ -3,7 +3,6 @@ import { RESOURCE_CONFIG_PATH } from "./constants";
3
3
 
4
4
  export type SuperblocksMetadata = {
5
5
  cliVersion: string;
6
- remote: string;
7
6
  lastUpdated: string;
8
7
  created: string;
9
8
  };