browse
Version:
Unified Browserbase CLI for browser automation and cloud APIs.
61 lines (60 loc) • 2.55 kB
JavaScript
import { sealSecret } from "./seal.js";
import { requestBrowserbase, requestBrowserbaseJson } from "../cloud/api.js";
export function listSecrets(options, query) {
return requestBrowserbaseJson(options, withQuery("/v1/secrets", query));
}
function withQuery(path, query) {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(query)) {
if (value !== undefined)
params.set(key, String(value));
}
const search = params.toString();
return search ? `${path}?${search}` : path;
}
export function getSecret(options, secretId) {
return requestBrowserbaseJson(options, secretPath(secretId));
}
export async function deleteSecret(options, secretId) {
await requestBrowserbase(options, secretPath(secretId), { method: "DELETE" });
}
function secretPath(id) {
return `/v1/secrets/${encodeURIComponent(id)}`;
}
export async function createSecret(options, secretKey, value) {
const sealed = await encryptValue(options, value);
return requestBrowserbaseJson(options, "/v1/secrets", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
secretKey,
...sealed,
}),
});
}
export async function updateSecret(options, secretId, value) {
const sealed = await encryptValue(options, value);
return requestBrowserbaseJson(options, secretPath(secretId), {
method: "PATCH",
headers: { "content-type": "application/json" },
body: JSON.stringify(sealed),
});
}
async function encryptValue(options, value) {
const keypair = await requestBrowserbaseJson(options, "/v1/secrets/keypair");
const sealedSecretValue = await sealSecret(keypair.publicKey, value);
return { keypairId: keypair.id, sealedSecretValue };
}
export async function attachFunctionSecret(options, functionId, secretId) {
await requestBrowserbase(options, `/v1/functions/${encodeURIComponent(functionId)}/secrets`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ secretId }),
});
}
export async function detachFunctionSecret(options, functionId, secretId) {
await requestBrowserbase(options, `/v1/functions/${encodeURIComponent(functionId)}/secrets/${encodeURIComponent(secretId)}`, { method: "DELETE" });
}
export function listFunctionSecrets(options, functionId, query) {
return requestBrowserbaseJson(options, withQuery(`/v1/functions/${encodeURIComponent(functionId)}/secrets`, query));
}