@elding/sdk 0.1.0

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/api.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function exchangeToken(refreshToken: string): Promise<string>;
2
+ export declare function fetchSecrets(accessToken: string, setId: string): Promise<Record<string, string>>;
package/dist/api.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exchangeToken = exchangeToken;
4
+ exports.fetchSecrets = fetchSecrets;
5
+ const BASE_URL = process.env.ELDING_API_URL ?? "https://app.elding.io";
6
+ async function exchangeToken(refreshToken) {
7
+ const res = await fetch(`${BASE_URL}/api/cli/auth/token`, {
8
+ method: "POST",
9
+ headers: { "Content-Type": "application/json" },
10
+ body: JSON.stringify({ refreshToken }),
11
+ });
12
+ const body = (await res.json());
13
+ if (!body.success || !body.accessToken)
14
+ throw new Error(body.error ?? "Échec d'authentification Elding");
15
+ return body.accessToken;
16
+ }
17
+ async function fetchSecrets(accessToken, setId) {
18
+ const res = await fetch(`${BASE_URL}/api/cli/secrets?setId=${encodeURIComponent(setId)}`, {
19
+ headers: { Authorization: `Bearer ${accessToken}` },
20
+ });
21
+ const body = (await res.json());
22
+ if (!body.success || !body.secrets || typeof body.secrets !== "object")
23
+ throw new Error(body.error ?? `Erreur ${res.status}`);
24
+ return sanitizeSecrets(body.secrets);
25
+ }
26
+ const DANGEROUS_KEYS = new Set(["__proto__", "constructor", "prototype"]);
27
+ // Filtre les paires clé/valeur string sûres — évite injection d'objets ou pollution de prototype
28
+ function sanitizeSecrets(raw) {
29
+ const out = Object.create(null);
30
+ for (const [name, value] of Object.entries(raw)) {
31
+ if (DANGEROUS_KEYS.has(name))
32
+ continue;
33
+ if (typeof value !== "string")
34
+ continue;
35
+ out[name] = value;
36
+ }
37
+ return out;
38
+ }
@@ -0,0 +1,14 @@
1
+ export type ClientOptions = {
2
+ setId?: string;
3
+ refreshToken?: string;
4
+ envFallback?: boolean;
5
+ };
6
+ export declare class EldingClient {
7
+ private secrets;
8
+ private envFallback;
9
+ private constructor();
10
+ static create(options?: ClientOptions): Promise<EldingClient>;
11
+ secret(name: string): string;
12
+ secretOrUndefined(name: string): string | undefined;
13
+ all(): Record<string, string>;
14
+ }
package/dist/client.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EldingClient = void 0;
4
+ const config_js_1 = require("./config.js");
5
+ const api_js_1 = require("./api.js");
6
+ class EldingClient {
7
+ secrets;
8
+ envFallback;
9
+ constructor(secrets, envFallback) {
10
+ this.secrets = secrets;
11
+ this.envFallback = envFallback;
12
+ }
13
+ static async create(options = {}) {
14
+ // 1. Resolve setId
15
+ const setId = options.setId ?? (0, config_js_1.readProjectConfig)()?.setId;
16
+ if (!setId)
17
+ throw new Error("[elding] setId introuvable. Lancez `elding init` ou passez setId en option.");
18
+ // 2. Resolve refresh token
19
+ const refreshToken = options.refreshToken ?? (0, config_js_1.readGlobalConfig)()?.refreshToken;
20
+ if (!refreshToken)
21
+ throw new Error("[elding] Token introuvable. Lancez `elding login` ou passez refreshToken en option.");
22
+ // 3. Fetch secrets
23
+ const accessToken = await (0, api_js_1.exchangeToken)(refreshToken);
24
+ const secrets = await (0, api_js_1.fetchSecrets)(accessToken, setId);
25
+ return new EldingClient(secrets, options.envFallback ?? true);
26
+ }
27
+ secret(name) {
28
+ const value = this.secrets[name];
29
+ if (value !== undefined)
30
+ return value;
31
+ if (this.envFallback) {
32
+ const envValue = process.env[name];
33
+ if (envValue !== undefined)
34
+ return envValue;
35
+ }
36
+ throw new Error(`[elding] Secret "${name}" introuvable dans le set.`);
37
+ }
38
+ // Returns undefined instead of throwing
39
+ secretOrUndefined(name) {
40
+ try {
41
+ return this.secret(name);
42
+ }
43
+ catch {
44
+ return undefined;
45
+ }
46
+ }
47
+ // Returns all secrets as a plain object (e.g. to spread into a config)
48
+ all() {
49
+ if (this.envFallback)
50
+ return { ...process.env, ...this.secrets };
51
+ return { ...this.secrets };
52
+ }
53
+ }
54
+ exports.EldingClient = EldingClient;
@@ -0,0 +1,9 @@
1
+ export type EldingConfig = {
2
+ refreshToken: string;
3
+ };
4
+ export type ProjectConfig = {
5
+ setId: string;
6
+ setName: string;
7
+ };
8
+ export declare function readGlobalConfig(): EldingConfig | null;
9
+ export declare function readProjectConfig(): ProjectConfig | null;
package/dist/config.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.readGlobalConfig = readGlobalConfig;
7
+ exports.readProjectConfig = readProjectConfig;
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const os_1 = __importDefault(require("os"));
10
+ const path_1 = __importDefault(require("path"));
11
+ function readGlobalConfig() {
12
+ try {
13
+ const file = path_1.default.join(os_1.default.homedir(), ".elding", "config.json");
14
+ return JSON.parse(fs_1.default.readFileSync(file, "utf-8"));
15
+ }
16
+ catch {
17
+ return null;
18
+ }
19
+ }
20
+ function readProjectConfig() {
21
+ try {
22
+ return JSON.parse(fs_1.default.readFileSync(".elding.json", "utf-8"));
23
+ }
24
+ catch {
25
+ return null;
26
+ }
27
+ }
@@ -0,0 +1,14 @@
1
+ import { EldingClient, type ClientOptions } from "./client.js";
2
+ export { EldingClient };
3
+ export type { ClientOptions };
4
+ /**
5
+ * Crée un client Elding et charge tous les secrets du set configuré.
6
+ *
7
+ * @example
8
+ * const elding = await client();
9
+ * const key = elding.secret("OPENAI_API_KEY");
10
+ *
11
+ * @example avec options
12
+ * const elding = await client({ setId: "xxx", refreshToken: "eld_rt_..." });
13
+ */
14
+ export declare function client(options?: ClientOptions): Promise<EldingClient>;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EldingClient = void 0;
4
+ exports.client = client;
5
+ const client_js_1 = require("./client.js");
6
+ Object.defineProperty(exports, "EldingClient", { enumerable: true, get: function () { return client_js_1.EldingClient; } });
7
+ /**
8
+ * Crée un client Elding et charge tous les secrets du set configuré.
9
+ *
10
+ * @example
11
+ * const elding = await client();
12
+ * const key = elding.secret("OPENAI_API_KEY");
13
+ *
14
+ * @example avec options
15
+ * const elding = await client({ setId: "xxx", refreshToken: "eld_rt_..." });
16
+ */
17
+ async function client(options) {
18
+ return client_js_1.EldingClient.create(options);
19
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@elding/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Elding SDK — accès aux secrets depuis le code, zéro .env",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "dev": "tsc --watch",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^22.0.0",
20
+ "typescript": "^5.5.0"
21
+ },
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "license": "MIT"
26
+ }