Type error

This commit is contained in:
Daniel Hougaard
2024-08-30 19:57:47 +04:00
parent 0ba7b73780
commit d4131c048d
2 changed files with 84 additions and 37 deletions

View File

@@ -2,17 +2,14 @@ import { Configuration, DefaultApi as InfisicalApi } from "./infisicalapi_client
import { DefaultApiApiV1DynamicSecretsLeasesPostRequest } from "./infisicalapi_client"; import { DefaultApiApiV1DynamicSecretsLeasesPostRequest } from "./infisicalapi_client";
import SecretsClient from "./custom/secrets"; import SecretsClient from "./custom/secrets";
import AuthClient from "./custom/auth"; import AuthClient from "./custom/auth";
import { RawAxiosRequestConfig } from "axios";
import DynamicSecretsClient from "./custom/dynamic-secrets";
const buildRestClient = (apiClient: InfisicalApi, accessToken: string) => { const buildRestClient = (apiClient: InfisicalApi, requestOptions?: RawAxiosRequestConfig) => {
const defaultOptions = {
headers: {
Authorization: `Bearer ${accessToken}`
}
};
return { return {
// Add more as we go
apiV1DynamicSecretsLeasesPost: (options: DefaultApiApiV1DynamicSecretsLeasesPostRequest) => apiV1DynamicSecretsLeasesPost: (options: DefaultApiApiV1DynamicSecretsLeasesPostRequest) =>
apiClient.apiV1DynamicSecretsLeasesPost(options, defaultOptions) apiClient.apiV1DynamicSecretsLeasesPost(options, requestOptions)
}; };
}; };
@@ -23,16 +20,15 @@ type InfisicalSDKOptions = {
class InfisicalSDK { class InfisicalSDK {
#apiInstance: InfisicalApi; #apiInstance: InfisicalApi;
// #accessToken: string; // No need to store the auth token here
#requestOptions: RawAxiosRequestConfig | undefined;
#secretsClient: SecretsClient; #secretsClient: SecretsClient;
#dynamicSecretsClient: DynamicSecretsClient;
#authClient: AuthClient; #authClient: AuthClient;
#basePath: string; #basePath: string;
#accessToken: string;
constructor(options?: InfisicalSDKOptions) { constructor(options?: InfisicalSDKOptions) {
this.#basePath = options?.siteUrl || "https://app.infisical.com"; this.#basePath = options?.siteUrl || "https://app.infisical.com";
this.#accessToken = "";
this.#apiInstance = new InfisicalApi( this.#apiInstance = new InfisicalApi(
new Configuration({ new Configuration({
@@ -41,8 +37,9 @@ class InfisicalSDK {
); );
this.#authClient = new AuthClient(this.authenticate.bind(this), this.#apiInstance); this.#authClient = new AuthClient(this.authenticate.bind(this), this.#apiInstance);
this.#secretsClient = new SecretsClient(this.#apiInstance); this.#dynamicSecretsClient = new DynamicSecretsClient(this.#apiInstance, this.#requestOptions);
this.rest = () => buildRestClient(this.#apiInstance, this.#accessToken); this.#secretsClient = new SecretsClient(this.#apiInstance, this.#requestOptions);
this.rest = () => buildRestClient(this.#apiInstance, this.#requestOptions);
} }
private authenticate(accessToken: string) { private authenticate(accessToken: string) {
@@ -53,17 +50,24 @@ class InfisicalSDK {
}) })
); );
this.#accessToken = accessToken; this.#requestOptions = {
this.#secretsClient = new SecretsClient(this.#apiInstance); headers: {
Authorization: `Bearer ${accessToken}`
}
};
this.rest = () => buildRestClient(this.#apiInstance, this.#requestOptions);
this.#secretsClient = new SecretsClient(this.#apiInstance, this.#requestOptions);
this.#dynamicSecretsClient = new DynamicSecretsClient(this.#apiInstance, this.#requestOptions);
this.#authClient = new AuthClient(this.authenticate.bind(this), this.#apiInstance); this.#authClient = new AuthClient(this.authenticate.bind(this), this.#apiInstance);
this.rest = () => buildRestClient(this.#apiInstance, this.#accessToken);
return this; return this;
} }
secrets = () => this.#secretsClient; secrets = () => this.#secretsClient;
dynamicSecrets = () => this.#dynamicSecretsClient;
auth = () => this.#authClient; auth = () => this.#authClient;
rest = () => buildRestClient(this.#apiInstance, this.#accessToken); rest = () => buildRestClient(this.#apiInstance, this.#requestOptions);
} }
export { InfisicalSDK }; export { InfisicalSDK };

View File

@@ -1,42 +1,85 @@
import { AxiosError } from "axios";
import { InfisicalSDK } from "../src"; import { InfisicalSDK } from "../src";
const PROJECT_ID = "PROJECT_ID";
(async () => { (async () => {
const client = new InfisicalSDK(); const client = new InfisicalSDK({
siteUrl: "http://localhost:8080" // Optional, defaults to https://app.infisical.com
});
await client.auth().universalAuth.login({ await client.auth().universalAuth.login({
// For localhost
clientId: "CLIENT_ID", clientId: "CLIENT_ID",
clientSecret: "CLIENT_SECRET" clientSecret: "CLIENT_SECRET"
}); });
const dynamicSecret = await client
.dynamicSecrets()
.leases.create({
dynamicSecretName: "test-redis",
projectSlug: "11-w-hfo",
environmentSlug: "dev"
})
.catch(err => {
if (err instanceof AxiosError) {
console.log(err.response?.data);
}
throw new Error("oops");
});
console.log(dynamicSecret);
// // process.exit(0);
const allSecrets = await client.secrets().listSecrets({ const allSecrets = await client.secrets().listSecrets({
environment: "dev", environment: "dev",
workspaceId: "PROJECT_ID" projectId: PROJECT_ID,
expandSecretReferences: true,
includeImports: false,
recursive: false
}); });
console.log(allSecrets.secrets); console.log(allSecrets.secrets);
const singleSecret = await client.secrets().getSecret({ const singleSecret = await client.secrets().getSecret({
secretName: "SECRET_NAME",
environment: "dev"
});
console.log(`Fetched single secret, ${singleSecret.secretKey}=${singleSecret.secretValue}`);
const newSecret = await client.secrets().createSecret("NEW_SECRET_NAME", {
environment: "dev", environment: "dev",
workspaceId: "PROJECT_ID", projectId: PROJECT_ID,
secretValue: "INITIAL SECRET VALUE!" secretName: "TEST1",
}); expandSecretReferences: true, // Optional
console.log(`You created a new secret: ${newSecret.secret}`); includeImports: true, // Optional
const updatedSecret = await client.secrets().updateSecret("NEW_SECRET_NAME", { type: "shared", // Optional
environment: "dev", version: 1 // Optional
workspaceId: "PROJECT_ID",
secretValue: "NEW SECRET VALUE!"
}); });
console.log(`You updated the secret: ${updatedSecret.secret}`); console.log(`Fetched single secret, ${singleSecret}=${singleSecret.secretValue}`);
const deletedSecret = await client.secrets().deleteSecret("NEW_SECRET_NAME", { const newSecret = await client.secrets().createSecret("NEW_SECRET_NAME22423423", {
environment: "dev", environment: "dev",
workspaceId: "PROJECT_ID" projectId: PROJECT_ID,
secretValue: "SECRET_VALUE"
}); });
console.log(`You deleted the secret: ${deletedSecret.secret}`); console.log(`You created a new secret: ${newSecret.secret.secretKey}`);
const updatedSecret = await client.secrets().updateSecret("NEW_SECRET_NAME22423423", {
environment: "dev",
projectId: PROJECT_ID,
secretValue: "UPDATED_SECRET_VALUE",
newSecretName: "NEW_SECRET_NAME22222", // Optional
secretComment: "This is an updated secret", // Optional
secretReminderNote: "This is an updated reminder note", // Optional
secretReminderRepeatDays: 14, // Optional
skipMultilineEncoding: false, // Optional
metadata: {
// Optional
extra: "metadata"
}
});
console.log(`You updated the secret: ${updatedSecret.secret.secretKey}`);
const deletedSecret = await client.secrets().deleteSecret("NEW_SECRET_NAME22222", {
environment: "dev",
projectId: PROJECT_ID
});
console.log(`You deleted the secret: ${deletedSecret.secret.secretKey}`);
})(); })();