Update docs for upcoming Node SDK update

This commit is contained in:
Tuan Dang
2023-04-22 14:34:05 +03:00
parent 3846c42c00
commit 3817831577

View File

@@ -14,13 +14,13 @@ npm install infisical-node --save
## Initialization
Set up the Infisical client asynchronously as early as possible in your application by importing and initializing the global instance with `infisical.connect(options)`.
Call `connect()` with your Infisical token as early as possible in the main entry module of your application. This initializes the global instance of the SDK, which can be accessed anywhere in your application.
This methods fetches back all the secrets in the project and environment accessible by the token passed in `options`.
For multiple Infisical projects or creating multiple SDK instances, use `createConnection()` instead. This returns a local SDK instance, independent of the global instance.
### infisical.connect(options)
Updates the global instance of the Infisical client with a connection to an Infisical project and fetches back secrets if supplied with an [Infisical Token](/getting-started/dashboard/token).
Updates the global instance of the Infisical client with a connection to an Infisical project with the [Infisical Token](/getting-started/dashboard/token).
<ResponseField name="options" type="object">
<Expandable title="properties">
@@ -36,18 +36,18 @@ Updates the global instance of the Infisical client with a connection to an Infi
Your self-hosted absolute site URL including the protocol (e.g.
`https://app.infisical.com`)
</ResponseField>
<ResponseField name="cacheTTL" type="number" default="300">
Time-to-live (in seconds) for cached secrets. If set to 0, data is cached indefinitely.
</ResponseField>
<ResponseField name="debug" type="boolean" default="false">
Whether or not debug mode is on
</ResponseField>
<ResponseField name="attachToProcessEnv" type="boolean" default="false">
Whether or not to attach fetched secrets to `process.env`
</ResponseField>
</Expandable>
</ResponseField>
### infisical.createConnection(options)
Returns a local instance of the Infisical client with a connection to an Infisical project and fetches back secrets if supplied with an [Infisical Token](/getting-started/dashboard/token).
Returns a local instance of the Infisical client with a connection to an Infisical project with an [Infisical Token](/getting-started/dashboard/token).
This method is useful if you wish to connect to two or more Infisical projects within your app.
@@ -65,6 +65,9 @@ This method is useful if you wish to connect to two or more Infisical projects w
Your self-hosted absolute site URL including the protocol (e.g.
`https://app.infisical.com`)
</ResponseField>
<ResponseField name="cacheTTL" type="number" default="300">
Time-to-live (in seconds) for cached secrets. If set to 0, data is cached indefinitely.
</ResponseField>
<ResponseField name="debug" type="boolean" default="false">
Whether or not debug mode is on
</ResponseField>
@@ -76,15 +79,11 @@ This method is useful if you wish to connect to two or more Infisical projects w
```js
import infisical from "infisical-node";
const main = async () => {
await infisical.connect({
token: "your_infisical_token",
});
infisical.connect({
token: "your_infisical_token",
});
// your app logic
}
main();
// your app logic
```
</Tab>
@@ -93,14 +92,10 @@ This method is useful if you wish to connect to two or more Infisical projects w
const infisical = require("infisical-node");
infisical.connect({
token: "your_infisical_token"
})
.then(() => {
// your application logic
})
.catch(err => {
console.error('Error: ', err);
})
token: "your_infisical_token"
});
// your app logic
````
</Tab>
@@ -108,45 +103,117 @@ This method is useful if you wish to connect to two or more Infisical projects w
## Usage
To get the value of a secret, use `infisical.get(key)`.
### infisical.getSecret(secretName, options)
### infisical.get(key)
Retrieve a secret from Infisical.
Return the value of the secret with the specified `key`. Note that the Infisical client falls back to `process.env` if `token` is `undefined` during the
initialization step or if a value for the secret is not found in the fetched secrets.
By default, `getSecret()` returns a personal secret. If not found, it returns a shared secret, or tries to retrieve the value from `process.env`.
<ResponseField name="key" type="string" required>
The key of the secret
<ResponseField name="secretName" type="string" required>
The key of the secret to retrieve
</ResponseField>
<ResponseField name="options" type="object">
<Expandable title="properties">
<ResponseField name="type" type="string">
"personal" (default) or "shared".
</ResponseField>
</Expandable>
</ResponseField>
```js
const value = infisical.get("SOME_KEY");
const secret = await infisical.getSecret("API_KEY");
const value = secret.secretValue; // get its value
```
### infisical.createSecret(secretName, secretValue, options)
Create a new secret in Infisical.
<ResponseField name="secretName" type="string" required>
The key of the secret to create
</ResponseField>
<ResponseField name="secretName" type="string" required>
The value of the secret to create
</ResponseField>
<ResponseField name="options" type="object">
<Expandable title="properties">
<ResponseField name="type" type="string">
"shared" (default) or "personal". A personal secret can only be created if a shared secret with the same name exists.
</ResponseField>
</Expandable>
</ResponseField>
```js
const newApiKey = await infisical.createSecret("API_KEY", "FOO");
```
### infisical.updateSecret(secretName, secretValue, options)
Update an existing secret in Infisical.
<ResponseField name="secretName" type="string" required>
The key of the secret to update
</ResponseField>
<ResponseField name="secretName" type="string" required>
The new value of the secret
</ResponseField>
<ResponseField name="options" type="object">
<Expandable title="properties">
<ResponseField name="type" type="string">
"shared" (default) or "personal".
</ResponseField>
</Expandable>
</ResponseField>
```js
const updatedApiKey = await infisical.updateSecret("API_KEY", "BAR");
```
### infisical.deleteSecret(secretName, options)
Delete a secret in Infisical.
<ResponseField name="secretName" type="string" required>
The key of the secret to delete
</ResponseField>
<ResponseField name="options" type="object">
<Expandable title="properties">
<ResponseField name="type" type="string">
"shared" (default) or "personal". Note that deleting a shared secret also deletes all associated personal secrets.
</ResponseField>
</Expandable>
</ResponseField>
```js
const deletedSecret = await infisical.deleteSecret("API_KEY");
```
## Example with Express
```js
const express = require("express");
const port = 3000;
const infisical = require("infisical-node");
import infisical from "infisical-node";
import express from "express";
const app = express();
const PORT = 3000;
const main = async () => {
await infisical.connect({
token: "st.xxx.xxx",
});
infisical.connect({
token: "YOUR_INFISICAL_TOKEN"
});
// your application logic
app.get("/", async (req, res) => {
// access value
const name = await infisical.getSecret("NAME");
res.send(`Hello! My name is: ${name.secretValue}`);
});
app.get("/", (req, res) => {
res.send(`Howdy, ${infisical.get("NAME")}!`);
});
app.listen(port, async () => {
console.log(`App listening on port ${port}`);
});
};
app.listen(PORT, async () => {
// initialize client
console.log(`App listening on port ${port}`);
});
```
This example demonstrates how to use the Infisical SDK with an Express application. The application retrieves a secret named "NAME" and responds to requests with a greeting that includes the secret value.
<Warning>
We do not recommend hardcoding your [Infisical
Token](/getting-started/dashboard/token). Setting it as an environment