@trackunit/serverside-utils 0.5.238-alpha-275bba7d3e1.0 → 0.5.239
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/index.cjs.js +13 -3
- package/index.esm.js +13 -3
- package/package.json +2 -2
- package/src/get-secret.d.ts +5 -2
package/index.cjs.js
CHANGED
|
@@ -241,6 +241,13 @@ const getAccountId = ({ context }) => {
|
|
|
241
241
|
const SecretsResponseSchema = zod.z.object({
|
|
242
242
|
secrets: zod.z.record(zod.z.string(), zod.z.string()),
|
|
243
243
|
});
|
|
244
|
+
const getAccountScope = ({ context }) => {
|
|
245
|
+
const userToken = getUserToken({ context });
|
|
246
|
+
if (!userToken) {
|
|
247
|
+
throw new Error("Missing user token");
|
|
248
|
+
}
|
|
249
|
+
return { accountId: getAccountId({ context }).accountId, userToken };
|
|
250
|
+
};
|
|
244
251
|
/**
|
|
245
252
|
* Fetches a secret from the SDK Server.
|
|
246
253
|
*
|
|
@@ -253,7 +260,7 @@ const SecretsResponseSchema = zod.z.object({
|
|
|
253
260
|
* @param options - The options object
|
|
254
261
|
* @param options.context - Hono request context
|
|
255
262
|
* @param options.secretKey - Key name of the secret
|
|
256
|
-
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId)
|
|
263
|
+
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId, which is sent to the SDK Server so it can verify the account)
|
|
257
264
|
* @returns {Promise<string>} The secret value
|
|
258
265
|
* @throws Error if required headers or env vars are missing, or secret not found
|
|
259
266
|
* @example
|
|
@@ -284,10 +291,13 @@ const getSecret = async ({ context, secretKey, accountScoped = false, }) => {
|
|
|
284
291
|
if (!sdkServerUrl) {
|
|
285
292
|
throw new Error("Missing required environment variable: SDK_SERVER_URL");
|
|
286
293
|
}
|
|
287
|
-
const
|
|
294
|
+
const accountScope = accountScoped ? getAccountScope({ context }) : undefined;
|
|
295
|
+
const secretsPath = accountScope ? `/secrets/${accountScope.accountId}` : "/secrets";
|
|
288
296
|
const secretsUrl = new URL(secretsPath, sdkServerUrl).href;
|
|
289
297
|
const res = await fetch(secretsUrl, {
|
|
290
|
-
headers:
|
|
298
|
+
headers: accountScope
|
|
299
|
+
? { Authorization: adminToken, [FORWARDED_AUTHORIZATION_HEADER]: `Bearer ${accountScope.userToken}` }
|
|
300
|
+
: { Authorization: adminToken },
|
|
291
301
|
});
|
|
292
302
|
if (!res.ok) {
|
|
293
303
|
let errorBody = "";
|
package/index.esm.js
CHANGED
|
@@ -239,6 +239,13 @@ const getAccountId = ({ context }) => {
|
|
|
239
239
|
const SecretsResponseSchema = z.object({
|
|
240
240
|
secrets: z.record(z.string(), z.string()),
|
|
241
241
|
});
|
|
242
|
+
const getAccountScope = ({ context }) => {
|
|
243
|
+
const userToken = getUserToken({ context });
|
|
244
|
+
if (!userToken) {
|
|
245
|
+
throw new Error("Missing user token");
|
|
246
|
+
}
|
|
247
|
+
return { accountId: getAccountId({ context }).accountId, userToken };
|
|
248
|
+
};
|
|
242
249
|
/**
|
|
243
250
|
* Fetches a secret from the SDK Server.
|
|
244
251
|
*
|
|
@@ -251,7 +258,7 @@ const SecretsResponseSchema = z.object({
|
|
|
251
258
|
* @param options - The options object
|
|
252
259
|
* @param options.context - Hono request context
|
|
253
260
|
* @param options.secretKey - Key name of the secret
|
|
254
|
-
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId)
|
|
261
|
+
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId, which is sent to the SDK Server so it can verify the account)
|
|
255
262
|
* @returns {Promise<string>} The secret value
|
|
256
263
|
* @throws Error if required headers or env vars are missing, or secret not found
|
|
257
264
|
* @example
|
|
@@ -282,10 +289,13 @@ const getSecret = async ({ context, secretKey, accountScoped = false, }) => {
|
|
|
282
289
|
if (!sdkServerUrl) {
|
|
283
290
|
throw new Error("Missing required environment variable: SDK_SERVER_URL");
|
|
284
291
|
}
|
|
285
|
-
const
|
|
292
|
+
const accountScope = accountScoped ? getAccountScope({ context }) : undefined;
|
|
293
|
+
const secretsPath = accountScope ? `/secrets/${accountScope.accountId}` : "/secrets";
|
|
286
294
|
const secretsUrl = new URL(secretsPath, sdkServerUrl).href;
|
|
287
295
|
const res = await fetch(secretsUrl, {
|
|
288
|
-
headers:
|
|
296
|
+
headers: accountScope
|
|
297
|
+
? { Authorization: adminToken, [FORWARDED_AUTHORIZATION_HEADER]: `Bearer ${accountScope.userToken}` }
|
|
298
|
+
: { Authorization: adminToken },
|
|
289
299
|
});
|
|
290
300
|
if (!res.ok) {
|
|
291
301
|
let errorBody = "";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/serverside-utils",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.239",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@graphql-typed-document-node/core": "^3.2.0",
|
|
11
|
-
"@trackunit/server-graphql-client": "0.0.
|
|
11
|
+
"@trackunit/server-graphql-client": "0.0.100",
|
|
12
12
|
"jwt-decode": "^3.1.2",
|
|
13
13
|
"zod": "^3.25.76"
|
|
14
14
|
},
|
package/src/get-secret.d.ts
CHANGED
|
@@ -4,7 +4,10 @@ type GetSecretOptions<TSecretKey extends string> = {
|
|
|
4
4
|
context: Context;
|
|
5
5
|
/** Key name of the secret (also used as env var name for local dev) */
|
|
6
6
|
secretKey: TSecretKey;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* When true, fetches account-scoped secrets instead of app-level secrets. Requires a valid user token with an accountId.
|
|
9
|
+
* The user token is sent to the SDK Server so it can verify the account.
|
|
10
|
+
*/
|
|
8
11
|
accountScoped?: boolean;
|
|
9
12
|
};
|
|
10
13
|
/**
|
|
@@ -19,7 +22,7 @@ type GetSecretOptions<TSecretKey extends string> = {
|
|
|
19
22
|
* @param options - The options object
|
|
20
23
|
* @param options.context - Hono request context
|
|
21
24
|
* @param options.secretKey - Key name of the secret
|
|
22
|
-
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId)
|
|
25
|
+
* @param options.accountScoped - When true, fetches account-scoped secrets (requires user token with accountId, which is sent to the SDK Server so it can verify the account)
|
|
23
26
|
* @returns {Promise<string>} The secret value
|
|
24
27
|
* @throws Error if required headers or env vars are missing, or secret not found
|
|
25
28
|
* @example
|