From 40d16fa9964879ab65692ba90957233d875c1d03 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:10:30 +0400 Subject: [PATCH] Updated Node.js docs --- docs/sdks/languages/node.mdx | 302 +++++++++++++++++++++++------------ 1 file changed, 199 insertions(+), 103 deletions(-) diff --git a/docs/sdks/languages/node.mdx b/docs/sdks/languages/node.mdx index d28357c4e..9c3c427c4 100644 --- a/docs/sdks/languages/node.mdx +++ b/docs/sdks/languages/node.mdx @@ -9,39 +9,52 @@ If you're working with Node.js, the official [infisical-node](https://github.com ```js import express from "express"; -import InfisicalClient from "infisical-node"; + +import { InfisicalClient, LogLevel } from "@infisical/sdk"; + const app = express(); + const PORT = 3000; const client = new InfisicalClient({ - token: "YOUR_INFISICAL_TOKEN" + clientId: "YOUR_CLIENT_ID", + clientSecret: "YOUR_CLIENT_SECRET", + logLevel: LogLevel.Error }); app.get("/", async (req, res) => { - // access value - const name = await client.getSecret("NAME"); - res.send(`Hello! My name is: ${name.secretValue}`); + // access value + + const name = await client.getSecret({ + environment: "dev", + projectId: "656dba7f979ebd6652586669", + path: "/", + type: "shared", + secretName: "NAME" + }); + + res.send(`Hello! My name is: ${name.secretValue}`); }); app.listen(PORT, async () => { - console.log(`App listening on port ${PORT}`); + // initialize client + + console.log(`App listening on port ${port}`); }); ``` This example demonstrates how to use the Infisical Node SDK with an Express application. The application retrieves a secret named "NAME" and responds to requests with a greeting that includes the secret value. - We do not recommend hardcoding your [Infisical - Token](/documentation/platform/token). Setting it as an environment - variable would be best. + We do not recommend hardcoding your [Machine Identity Tokens](/platform/identities/overview). Setting it as an environment variable would be best. ## Installation -Run `npm` to add `infisical-node` to your project. +Run `npm` to add `@infisical/sdk` to your project. ```console -$ npm install infisical-node --save +$ npm install @infisical/sdk ``` ## Configuration @@ -51,22 +64,27 @@ Import the SDK and create a client instance with your [Infisical Token](/documen ```js - import InfisicalClient from "infisical-node"; - + import { InfisicalClient, LogLevel } from "@infisical/sdk"; + const client = new InfisicalClient({ - token: "your_infisical_token" + clientId: "YOUR_CLIENT_ID", + clientSecret: "YOUR_CLIENT_SECRET", + logLevel: LogLevel.Error }); ``` ```js - const InfisicalClient = require("infisical-node"); - + const { InfisicalClient, LogLevel } = require("@infisical/sdk"); + const client = new InfisicalClient({ - token: "your_infisical_token" + clientId: "YOUR_CLIENT_ID", + clientSecret: "YOUR_CLIENT_SECRET", + logLevel: LogLevel.Error }); ```` + @@ -74,134 +92,212 @@ Import the SDK and create a client instance with your [Infisical Token](/documen ### Parameters - - - An [Infisical Token](/documentation/platform/token) scoped to a project - and environment - - - Your self-hosted absolute site URL including the protocol (e.g. - `https://app.infisical.com`) - - - Time-to-live (in seconds) for refreshing cached secrets. Default: `300`. - - - Whether or not debug mode is on - - + + + Your machine identity client ID. + + + Your machine identity client secret. + + + + An access token obtained from the machine identity login endpoint. + + + + Your self-hosted absolute site URL including the protocol (e.g. `https://app.infisical.com`) + + + The level of logs you wish to log The logs are derived from Rust, as we have written our base SDK in Rust. + + + - -## Caching - -The SDK caches every secret and updates it periodically based on the provided `cacheTTL`. For example, if `cacheTTL` of `300` is provided, then a secret will be refetched 5 minutes after the first fetch; if the fetch fails, the cached secret is returned. - - - For optimal performance, we recommend creating a single instance of the Infisical client and exporting it to be used across your entire app to take advantage of caching benefits. - - ## Working with Secrets -### client.getAllSecrets() +### client.listSecrets(options) ```js -const secrets = await client.getAllSecrets(); +const secrets = await client.listSecrets({ + environment: "dev", + projectId: "PROJECT_ID", + path: "/foo/bar/", + includeImports: false +}); ``` Retrieve all secrets within the Infisical project and environment that client is connected to -### client.getSecret(secretName, options) +### Parameters + + + + + The slug name (dev, prod, etc) of the environment from where secrets should be fetched from. + + + The project ID where the secret lives in. + + + + The path from where secrets should be fetched from. + + + + Whether or not to include imported secrets from the current path. Read about [secret import](/platform/secret-reference) + + + + + +### client.getSecret(options) ```js -const secret = await client.getSecret("API_KEY"); -const value = secret.secretValue; // get its value +const secret = await client.getSecret({ + environment: "dev", + projectId: "PROJECT_ID", + secretName: "API_KEY", + path: "/", + type: "shared" +}); ``` Retrieve a secret from Infisical. -By default, `getSecret()` fetches and returns a personal secret. If not found, it returns a shared secret, or tries to retrieve the value from `process.env`. If a secret is fetched, `getSecret()` caches it to reduce excessive calls and re-fetches periodically based on the `cacheTTL` option (default is `300` seconds) when initializing the client — for more information, see the caching section. +By default, `getSecret()` fetches and returns a shared secret. ### Parameters - - The key of the secret to retrieve - - - - - The type of the secret. Valid options are "shared" or "personal" - - + + + + The key of the secret to retrieve. + + + The project ID where the secret lives in. + + + The slug name (dev, prod, etc) of the environment from where secrets should be fetched from. + + + The path from where secret should be fetched from. + + + The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared". + + -### client.createSecret(secretName, secretValue, options) +### client.createSecret(options) ```js -const newApiKey = await client.createSecret("API_KEY", "FOO"); +const newApiKey = await client.createSecret({ + projectId: "PROJECT_ID", + environment: "dev", + secretName: "API_KEY", + secretValue: "SECRET VALUE", + path: "/", + type: "shared" +}); ``` Create a new secret in Infisical. - - The key of the secret to create - - - The value of the secret to create - - - - - The type of the secret. Valid options are "shared" or "personal". A personal secret can only be created if a shared secret with the same name exists. - - + + + + The key of the secret to create. + + + The value of the secret. + + + The project ID where the secret lives in. + + + The slug name (dev, prod, etc) of the environment from where secrets should be fetched from. + + + The path from where secret should be created. + + + The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared". + + -### client.updateSecret(secretName, secretValue, options) +### client.updateSecret(options) ```js -const updatedApiKey = await client.updateSecret("API_KEY", "BAR"); +const updatedApiKey = await client.updateSecret({ + secretName: "API_KEY", + secretValue: "NEW SECRET VALUE", + projectId: "PROJECT_ID", + environment: "dev", + path: "/", + type: "shared" +}); ``` Update an existing secret in Infisical. ### Parameters - - The key of the secret to update - - - The new value of the secret - - - - - The type of the secret. Valid options are "shared" or "personal" - - + + + + The key of the secret to update. + + + The new value of the secret. + + + The project ID where the secret lives in. + + + The slug name (dev, prod, etc) of the environment from where secrets should be fetched from. + + + The path from where secret should be updated. + + + The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared". + + -### client.deleteSecret(secretName, options) +### client.deleteSecret(options) ```js -const deletedSecret = await client.deleteSecret("API_KEY"); +const deletedSecret = await client.deleteSecret({ + secretName: "API_KEY", + + environment: "dev", + projectId: "PROJECT_ID", + path: "/", + + type: "shared" +}); ``` Delete a secret in Infisical. - - The key of the secret to delete + + + + The key of the secret to update. + + + The project ID where the secret lives in. + + + The slug name (dev, prod, etc) of the environment from where secrets should be fetched from. + + + The path from where secret should be deleted. + + + The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared". + + - - - - The type of the secret. Valid options are "shared" or "personal". Note that deleting a shared secret also deletes all associated personal secrets. - - - - -