@townco/core 0.0.55 → 0.0.57
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/auth.d.ts +12 -0
- package/dist/auth.js +65 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -3
- package/package.json +9 -5
package/dist/auth.d.ts
CHANGED
|
@@ -40,6 +40,11 @@ export declare function clearAuthCredentials(): boolean;
|
|
|
40
40
|
* Get the shed URL from environment or default.
|
|
41
41
|
*/
|
|
42
42
|
export declare function getShedUrl(): string;
|
|
43
|
+
/**
|
|
44
|
+
* Refresh the auth token using the refresh token.
|
|
45
|
+
* Returns the new credentials if successful, throws error otherwise.
|
|
46
|
+
*/
|
|
47
|
+
export declare function refreshAuthToken(credentials: AuthCredentials): Promise<AuthCredentials>;
|
|
43
48
|
/**
|
|
44
49
|
* Get shed authentication (access token and URL).
|
|
45
50
|
* Tries auth file first, then SHED_API_KEY environment variable.
|
|
@@ -49,3 +54,10 @@ export declare function getShedAuth(): {
|
|
|
49
54
|
accessToken: string;
|
|
50
55
|
shedUrl: string;
|
|
51
56
|
} | null;
|
|
57
|
+
/**
|
|
58
|
+
* Ensure the user is authenticated, refreshing the token if necessary.
|
|
59
|
+
*/
|
|
60
|
+
export declare function ensureAuthenticated(): Promise<{
|
|
61
|
+
accessToken: string;
|
|
62
|
+
shedUrl: string;
|
|
63
|
+
} | null>;
|
package/dist/auth.js
CHANGED
|
@@ -85,6 +85,34 @@ export function clearAuthCredentials() {
|
|
|
85
85
|
export function getShedUrl() {
|
|
86
86
|
return process.env.TOWN_SHED_URL || "https://shed-zeta.vercel.app";
|
|
87
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Refresh the auth token using the refresh token.
|
|
90
|
+
* Returns the new credentials if successful, throws error otherwise.
|
|
91
|
+
*/
|
|
92
|
+
export async function refreshAuthToken(credentials) {
|
|
93
|
+
const shedUrl = credentials.shed_url ?? getShedUrl();
|
|
94
|
+
const response = await fetch(`${shedUrl}/api/cli/refresh`, {
|
|
95
|
+
method: "POST",
|
|
96
|
+
headers: {
|
|
97
|
+
"Content-Type": "application/json",
|
|
98
|
+
},
|
|
99
|
+
body: JSON.stringify({ refresh_token: credentials.refresh_token }),
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
const errorData = await response
|
|
103
|
+
.json()
|
|
104
|
+
.catch(() => ({ error: "Refresh failed" }));
|
|
105
|
+
throw new Error(errorData.error || "Refresh failed");
|
|
106
|
+
}
|
|
107
|
+
const data = (await response.json());
|
|
108
|
+
const newCredentials = {
|
|
109
|
+
...credentials,
|
|
110
|
+
access_token: data.access_token,
|
|
111
|
+
refresh_token: data.refresh_token,
|
|
112
|
+
expires_at: data.expires_at,
|
|
113
|
+
};
|
|
114
|
+
return newCredentials;
|
|
115
|
+
}
|
|
88
116
|
/**
|
|
89
117
|
* Get shed authentication (access token and URL).
|
|
90
118
|
* Tries auth file first, then SHED_API_KEY environment variable.
|
|
@@ -109,3 +137,40 @@ export function getShedAuth() {
|
|
|
109
137
|
}
|
|
110
138
|
return null;
|
|
111
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Ensure the user is authenticated, refreshing the token if necessary.
|
|
142
|
+
*/
|
|
143
|
+
export async function ensureAuthenticated() {
|
|
144
|
+
// 1. Check SHED_API_KEY (static, no refresh needed)
|
|
145
|
+
const apiKey = process.env.SHED_API_KEY;
|
|
146
|
+
if (apiKey) {
|
|
147
|
+
return {
|
|
148
|
+
accessToken: apiKey,
|
|
149
|
+
shedUrl: getShedUrl(),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// 2. Check disk credentials
|
|
153
|
+
let credentials = loadAuthCredentialsFromDisk();
|
|
154
|
+
if (!credentials) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
// 3. Check expiry and refresh if needed
|
|
158
|
+
if (isTokenExpired(credentials)) {
|
|
159
|
+
try {
|
|
160
|
+
credentials = await refreshAuthToken(credentials);
|
|
161
|
+
saveAuthCredentials(credentials);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.warn("Failed to refresh token:", error);
|
|
165
|
+
// If refresh fails, we might still try with the old token or just fail?
|
|
166
|
+
// Usually if refresh fails, the session is dead.
|
|
167
|
+
// We'll return null to indicate auth failure (or let the old token fail downstream)
|
|
168
|
+
// But returning null matches "not logged in" semantics.
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
accessToken: credentials.access_token,
|
|
174
|
+
shedUrl: credentials.shed_url ?? getShedUrl(),
|
|
175
|
+
};
|
|
176
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from "./api-routes
|
|
2
|
-
export * from "./logger
|
|
3
|
-
export * from "./path.js";
|
|
1
|
+
export * from "./api-routes";
|
|
2
|
+
export * from "./logger";
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from "./api-routes
|
|
2
|
-
export * from "./logger
|
|
3
|
-
export * from "./path.js";
|
|
1
|
+
export * from "./api-routes";
|
|
2
|
+
export * from "./logger";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@townco/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.57",
|
|
5
5
|
"description": "core",
|
|
6
6
|
"license": "UNLICENSED",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"./auth": {
|
|
19
19
|
"import": "./dist/auth.js",
|
|
20
20
|
"types": "./dist/auth.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./path": {
|
|
23
|
+
"import": "./dist/path.js",
|
|
24
|
+
"types": "./dist/path.d.ts"
|
|
21
25
|
}
|
|
22
26
|
},
|
|
23
27
|
"dependencies": {
|
|
@@ -25,11 +29,11 @@
|
|
|
25
29
|
"@opentelemetry/api-logs": "^0.56.0"
|
|
26
30
|
},
|
|
27
31
|
"devDependencies": {
|
|
28
|
-
"typescript": "^
|
|
29
|
-
"@townco/tsconfig": "0.1.
|
|
32
|
+
"@typescript/native-preview": "^7.0.0-dev.20251207.1",
|
|
33
|
+
"@townco/tsconfig": "0.1.76"
|
|
30
34
|
},
|
|
31
35
|
"scripts": {
|
|
32
|
-
"build": "
|
|
33
|
-
"check": "
|
|
36
|
+
"build": "tsgo",
|
|
37
|
+
"check": "tsgo --noEmit"
|
|
34
38
|
}
|
|
35
39
|
}
|