Skip to content

Commit

Permalink
Fixed types and added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Aug 29, 2024
1 parent f36808e commit 005d4c1
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 20 deletions.
209 changes: 209 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Infisical Javascript SDK V2

The Infisical SDK provides a convenient way to interact with the Infisical API.

## Installation

```bash
npm install @infisical/sdk-v2
```

## Getting Started

```typescript

const client = new InfisicalSDK({
siteUrl: "your-infisical-instance.com" // Optional, defaults to https://app.infisical.com
});

// Authenticate with Infisical
await client.auth().universalAuth.login({
clientId: "<machine-identity-client-id>",
clientSecret: "<machine-identity-client-secret>"
});

const allSecrets = await client.secrets().listSecrets({
environment: "dev" // stg, dev, prod, or custom environment slugs
workspaceId: "<"<your-project-id>">"
});

console.log("Fetched secrets", allSecrets)
```

## Core Methods

The SDK methods are organized into the following high-level categories:

1. `auth`: Handles authentication methods.
2. `secrets`: Manages CRUD operations for secrets.

### `auth`

The `Auth` component provides methods for authentication:

#### Universal Auth

```typescript
await client.auth().universalAuth.login({
clientId: "<machine-identity-client-id>",
clientSecret: "<machine-identity-client-secret>"
});
```


### `secrets`

This sub-class handles operations related to secrets:

#### List Secrets

```typescript
const allSecrets = await client.secrets().listSecrets({
environment: "dev",
projectId: "<your-project-id>",
expandSecretReferences: true,
includeImports: false,
recursive: false,
secretPath: "/foo/bar"
});
```

**Parameters:**
- `projectId` (string): The ID of your project.
- `environment` (string): The environment in which to list secrets (e.g., "dev").
- `secretPath` (str): The path to the secrets.
- `expandSecretReferences` (bool): Whether to expand secret references.
- `recursive` (bool): Whether to list secrets recursively.
- `includeImports` (bool): Whether to include imported secrets.
- `tagFilters` (string[]): Tags to filter secrets.

**Returns:**
- `ApiV3SecretsRawGet200Response`: The response containing the list of secrets.

#### Create Secret

```typescript
const newSecret = await client.secrets().createSecret("SECRET_NAME", {
environment: "dev",
projectId: "<your-project-id>",
secretValue: "SECRET_VALUE",
secretComment: "This is a new secret", // Optional
secretPath: "/foo/bar", // Optional
secretReminderNote: "This is a reminder note", // Optional
secretReminderRepeatDays: 7, // Optional
skipMultilineEncoding: false, // Optional
tagIds: ["tagId1", "tagId2"], // Optional
type: "personal" // Optional
});
```

**Parameters:**
- `projectId` (string): The ID of your project.
- `environment` (str): The environment in which to create the secret.
- `secretValue` (str): The value of the secret.
- `secretPath` (string, optional): The path to the secret.
- `secretComment` (str, optional): A comment associated with the secret.
- `skipMultilineEncoding` (bool, optional): Whether to skip encoding for multiline secrets.
- `secretReminderNote` (string, optional): A note for the secret reminder.
- `secretReminderRepeatDays` (number, optional): Number of days after which to repeat secret reminders.
- `tagIds` (string[], optional): Array of tags to assign to the new secret.
- `type` (personal | shared, optional): Which type of secret to create.

**Returns:**
- `ApiV3SecretsRawSecretNamePost200Response`: The response after creating the secret.

#### Update Secret

```typescript
const updatedSecret = await client.secrets().updateSecret("SECRET_TO_UPDATE", {
environment: "dev",
projectId: "<your-project-id>",
secretValue: "UPDATED_SECRET_VALUE",
newSecretName: "NEW_SECRET_NAME2", // Optional
secretComment: "This is an updated secret", // Optional
secretPath: "/foo/bar", // Optional
secretReminderNote: "This is an updated reminder note", // Optional
secretReminderRepeatDays: 14, // Optional
skipMultilineEncoding: false, // Optional
tagIds: ["tagId1", "tagId2"], // Optional
type: "personal", // Optional
metadata: { // Optional
extra: "metadata"
}
});
```

**Parameters:**
- `environment` (str): The environment in which to update the secret.
- `projectId` (str): The ID of your project.
- `secretValue` (str, optional): The new value of the secret.
- `newSecretName` (str, optional): A new name for the secret.
- `secretComment` (str, optional): An updated comment associated with the secret.
- `secretPath` (str): The path to the secret.
- `secretReminderNote` (str, optional): An updated note for the secret reminder.
- `secretReminderRepeatDays` (number, optional): Updated number of days after which to repeat secret reminders.
- `skipMultilineEncoding` (bool, optional): Whether to skip encoding for multiline secrets.
- `tagIds` (string[], optional): Array of tags to assign to the secret.
- `type` (personal | shared, optional): Which type of secret to create.
- `metadata` (object, optional): Assign additional details to the secret, accessible through the API.

**Returns:**
- `ApiV3SecretsRawSecretNamePost200Response`: The response after updating the secret.

#### Get Secret by Name

```typescript
const singleSecret = await client.secrets().getSecret({
environment: "dev",
projectId: "<your-project-id>",
secretName: "DATABASE_URL",
expandSecretReferences: true, // Optional
includeImports: true, // Optional
secretPath: "/foo/bar", // Optional
type: "shared", // Optional
version: 1 // Optional
});
```

**Parameters:**
- `environment` (str): The environment in which to retrieve the secret.
- `projectId` (str): The ID of your project.
- `secretName` (str): The name of the secret.
- `secretPath` (str, optional): The path to the secret.
- `expandSecretReferences` (bool, optional): Whether to expand secret references.
- `includeImports` (bool): Whether to include imported secrets.
- `version` (str, optional): The version of the secret to retrieve. Fetches the latest by default.
- `type` (personal | shared, optional): The type of secret to fetch.


**Returns:**
- `ApiV3SecretsRawSecretNameGet200Response`: The response containing the secret.

#### Delete Secret by Name

```typescript
const deletedSecret = await client.secrets().deleteSecret("SECRET_TO_DELETE", {
environment: "dev",
projectId: "<your-project-id>",
secretPath: "/foo/bar", // Optional
type: "personal" // Optional
});
```

**Parameters:**
- `projectId` (str): The ID of your project.
- `environment` (str): The environment in which to delete the secret.
- `secret_path` (str, optional): The path to the secret.
- `type` (personal | shared, optional): The type of secret to delete.

**Returns:**
- `ApiV3SecretsRawSecretNamePost200Response`: The response after deleting the secret.

### `rest`

The `rest` component exposes all available API endpoints in a standardized format. This allows for more advanced or custom operations not covered by the high-level methods.

```python
# Example of using a custom API endpoint
custom_response = client.rest.custom_endpoint_method(param1="value1", param2="value2")
```
93 changes: 73 additions & 20 deletions src/custom/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,110 @@
import { Configuration, DefaultApi as InfisicalApi } from "../infisicalapi_client";
import type {
DefaultApiApiV3SecretsRawGetRequest,
DefaultApiApiV3SecretsRawSecretNameDeleteRequest,
DefaultApiApiV3SecretsRawSecretNameGetRequest,
DefaultApiApiV3SecretsRawSecretNamePatchRequest,
DefaultApiApiV3SecretsRawSecretNamePostRequest
} from "../infisicalapi_client";

type SecretType = "shared" | "personal";

type ListSecretsOptions = {
environment: string;
projectId: string;
expandSecretReferences?: boolean;
includeImports?: boolean;
recursive?: boolean;
secretPath?: string;
tagSlugs?: string[];
};

type GetSecretOptions = {
environment: string;
secretName: string;
expandSecretReferences?: boolean;
includeImports?: boolean;
secretPath?: string;
type?: SecretType;
version?: number;
projectId: string;
};

type UpdateSecretOptions = Omit<DefaultApiApiV3SecretsRawSecretNamePatchRequest["apiV3SecretsRawSecretNamePatchRequest"], "workspaceId"> & {
projectId: string;
};

type CreateSecretOptions = Omit<DefaultApiApiV3SecretsRawSecretNamePostRequest["apiV3SecretsRawSecretNamePostRequest"], "workspaceId"> & {
projectId: string;
};

type DeleteSecretOptions = Omit<DefaultApiApiV3SecretsRawSecretNameDeleteRequest["apiV3SecretsRawSecretNameDeleteRequest"], "workspaceId"> & {
projectId: string;
};

const convertBool = (value: boolean | undefined) => (value ? "true" : "false");

export default class SecretsClient {
#apiInstance: InfisicalApi;
constructor(private apiInstance: InfisicalApi) {
this.#apiInstance = apiInstance;
}

listSecrets = async (options: DefaultApiApiV3SecretsRawGetRequest) => {
const res = await this.#apiInstance.apiV3SecretsRawGet(options);
listSecrets = async (options: ListSecretsOptions) => {
const res = await this.#apiInstance.apiV3SecretsRawGet({
environment: options.environment,
workspaceId: options.projectId,
expandSecretReferences: convertBool(options.expandSecretReferences),
includeImports: convertBool(options.includeImports),
recursive: convertBool(options.recursive),
secretPath: options.secretPath,
tagSlugs: options.tagSlugs ? options.tagSlugs.join(",") : undefined
});
return res.data;
};

getSecret = async (options: DefaultApiApiV3SecretsRawSecretNameGetRequest) => {
const res = await this.#apiInstance.apiV3SecretsRawSecretNameGet(options);
getSecret = async (options: GetSecretOptions) => {
const res = await this.#apiInstance.apiV3SecretsRawSecretNameGet({
environment: options.environment,
secretName: options.secretName,
workspaceId: options.projectId,
expandSecretReferences: convertBool(options.expandSecretReferences),
includeImports: convertBool(options.includeImports),
secretPath: options.secretPath,
type: options.type,
version: options.version
});
return res.data.secret;
};

updateSecret = async (
secretName: DefaultApiApiV3SecretsRawSecretNamePatchRequest["secretName"],
options: DefaultApiApiV3SecretsRawSecretNamePatchRequest["apiV3SecretsRawSecretNamePatchRequest"]
) => {
updateSecret = async (secretName: DefaultApiApiV3SecretsRawSecretNamePatchRequest["secretName"], options: UpdateSecretOptions) => {
const res = await this.#apiInstance.apiV3SecretsRawSecretNamePatch({
secretName,
apiV3SecretsRawSecretNamePatchRequest: options
apiV3SecretsRawSecretNamePatchRequest: {
...options,
workspaceId: options.projectId
}
});
return res.data;
};

createSecret = async (
secretName: DefaultApiApiV3SecretsRawSecretNamePostRequest["secretName"],
options: DefaultApiApiV3SecretsRawSecretNamePostRequest["apiV3SecretsRawSecretNamePostRequest"]
) => {
createSecret = async (secretName: DefaultApiApiV3SecretsRawSecretNamePostRequest["secretName"], options: CreateSecretOptions) => {
const res = await this.#apiInstance.apiV3SecretsRawSecretNamePost({
secretName,
apiV3SecretsRawSecretNamePostRequest: options
apiV3SecretsRawSecretNamePostRequest: {
...options,
workspaceId: options.projectId
}
});
return res.data;
};

deleteSecret = async (
secretName: DefaultApiApiV3SecretsRawSecretNameDeleteRequest["secretName"],
options: DefaultApiApiV3SecretsRawSecretNameDeleteRequest["apiV3SecretsRawSecretNameDeleteRequest"]
) => {
deleteSecret = async (secretName: DefaultApiApiV3SecretsRawSecretNameDeleteRequest["secretName"], options: DeleteSecretOptions) => {
const res = await this.#apiInstance.apiV3SecretsRawSecretNameDelete({
secretName,
apiV3SecretsRawSecretNameDeleteRequest: options
apiV3SecretsRawSecretNameDeleteRequest: {
...options,
workspaceId: options.projectId
}
});
return res.data;
};
Expand Down

0 comments on commit 005d4c1

Please sign in to comment.