vessels 0.2.3 → 0.2.4

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.
Files changed (2) hide show
  1. package/dist/index.js +27 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -23,16 +23,39 @@ function writeConfig(config) {
23
23
  if (!existsSync(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
24
24
  writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
25
25
  }
26
- function getToken() {
27
- return readConfig().access_token ?? null;
26
+ function isTokenExpired(token) {
27
+ try {
28
+ const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString());
29
+ return payload.exp * 1e3 < Date.now() + 6e4;
30
+ } catch {
31
+ return true;
32
+ }
33
+ }
34
+ async function getFreshToken() {
35
+ const config = readConfig();
36
+ if (!config.access_token) return null;
37
+ if (!isTokenExpired(config.access_token)) return config.access_token;
38
+ if (config.refresh_token) {
39
+ try {
40
+ const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
41
+ const { data } = await supabase.auth.refreshSession({ refresh_token: config.refresh_token });
42
+ if (data.session) {
43
+ writeConfig({ ...config, access_token: data.session.access_token, refresh_token: data.session.refresh_token });
44
+ return data.session.access_token;
45
+ }
46
+ } catch {
47
+ }
48
+ }
49
+ return null;
28
50
  }
29
51
  async function api(path, options = {}) {
30
- const token = getToken();
52
+ const token = await getFreshToken();
53
+ if (!token) throw new Error("Not logged in. Run: vessels login --email <email>");
31
54
  const res = await fetch(`${BASE_URL}${path}`, {
32
55
  ...options,
33
56
  headers: {
34
57
  "Content-Type": "application/json",
35
- ...token ? { Authorization: `Bearer ${token}` } : {},
58
+ Authorization: `Bearer ${token}`,
36
59
  ...options.headers ?? {}
37
60
  }
38
61
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vessels",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Vessels CLI — manage your agent communication layer from the terminal",
5
5
  "type": "module",
6
6
  "bin": {