Update node SDK docs, positioning of examples

This commit is contained in:
Tuan Dang
2023-04-23 09:49:21 +03:00
parent 3817831577
commit a7484f8be5
2 changed files with 69 additions and 64 deletions

View File

@@ -2,7 +2,7 @@
title: "Node"
---
If you're working with Node.js, the official [infisical-node](https://github.com/Infisical/infisical-node) package is the easiest way to fetch secrets for your application.
If you're working with Node.js, the official [infisical-node](https://github.com/Infisical/infisical-node) package is the easiest way to fetch and work with ecrets for your application.
## Installation
@@ -12,14 +12,42 @@ Run `npm` to add `infisical-node` to your project.
npm install infisical-node --save
```
## Initialization
## Configuration
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.
Import the SDK and 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.
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)
<Tabs>
<Tab title="ES6">
```js
import infisical from "infisical-node";
infisical.connect({
token: "your_infisical_token",
});
// your app logic
```
</Tab>
<Tab title="ES5">
```js
const infisical = require("infisical-node");
infisical.connect({
token: "your_infisical_token"
});
// your app logic
````
</Tab>
</Tabs>
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">
@@ -37,7 +65,7 @@ Updates the global instance of the Infisical client with a connection to an Infi
`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.
Time-to-live (in seconds) for refreshing cached secrets. Default: `300`.
</ResponseField>
<ResponseField name="debug" type="boolean" default="false">
Whether or not debug mode is on
@@ -74,41 +102,21 @@ This method is useful if you wish to connect to two or more Infisical projects w
</Expandable>
</ResponseField>
<Tabs>
<Tab title="ES6">
```js
import infisical from "infisical-node";
infisical.connect({
token: "your_infisical_token",
});
// your app logic
```
</Tab>
<Tab title="ES5">
```js
const infisical = require("infisical-node");
infisical.connect({
token: "your_infisical_token"
});
// your app logic
````
</Tab>
</Tabs>
## Usage
### infisical.getSecret(secretName, options)
```js
const secret = await infisical.getSecret("API_KEY");
const value = secret.secretValue; // get its value
```
Retrieve a secret from Infisical.
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="secretName" type="string" required>
The key of the secret to retrieve
</ResponseField>
@@ -120,13 +128,12 @@ By default, `getSecret()` returns a personal secret. If not found, it returns a
</Expandable>
</ResponseField>
```js
const secret = await infisical.getSecret("API_KEY");
const value = secret.secretValue; // get its value
```
### infisical.createSecret(secretName, secretValue, options)
```js
const newApiKey = await infisical.createSecret("API_KEY", "FOO");
```
Create a new secret in Infisical.
<ResponseField name="secretName" type="string" required>
@@ -143,12 +150,12 @@ Create a new secret in Infisical.
</Expandable>
</ResponseField>
```js
const newApiKey = await infisical.createSecret("API_KEY", "FOO");
```
### infisical.updateSecret(secretName, secretValue, options)
```js
const updatedApiKey = await infisical.updateSecret("API_KEY", "BAR");
```
Update an existing secret in Infisical.
<ResponseField name="secretName" type="string" required>
@@ -165,12 +172,12 @@ Update an existing secret in Infisical.
</Expandable>
</ResponseField>
```js
const updatedApiKey = await infisical.updateSecret("API_KEY", "BAR");
```
### infisical.deleteSecret(secretName, options)
```js
const deletedSecret = await infisical.deleteSecret("API_KEY");
```
Delete a secret in Infisical.
<ResponseField name="secretName" type="string" required>
@@ -184,10 +191,6 @@ Delete a secret in Infisical.
</Expandable>
</ResponseField>
```js
const deletedSecret = await infisical.deleteSecret("API_KEY");
```
## Example with Express
```js