Skip to content

Commit

Permalink
Add small JS example
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Jan 26, 2024
1 parent 840b01f commit ddaf0f9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
31 changes: 31 additions & 0 deletions languages/js/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

const { BitwardenClient: BitwardenClientWasm, LogLevel } = require('@bitwarden/sdk-wasm');
const sdk = require('@bitwarden/sdk-client');


async function main() {
const settings = {
apiUrl: process.env.API_URL,
identityUrl: process.env.IDENTITY_URL
};

const client = new sdk.BitwardenClient(new BitwardenClientWasm(JSON.stringify(settings), LogLevel.Debug));

const organization_id = process.env.ORGANIZATION_ID;
await client.accessTokenLogin(process.env.ACCESS_TOKEN);


const project = await client.projects().create('test', organization_id);
const projects = await client.projects().list(organization_id);
console.log(projects.data);

const secret = await client.secrets().create('test-secret', "My secret!", "This is my secret", [project.id], organization_id);
const secrets = await client.secrets().list(organization_id);
console.log(secrets.data);

console.log(project, secret);

await client.projects().delete([project.id]);
await client.secrets().delete([secret.id]);
}
main();
34 changes: 34 additions & 0 deletions languages/js/example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions languages/js/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "sdk-example",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@bitwarden/sdk-client": "../sdk-client",
"@bitwarden/sdk-wasm": "../wasm"
}
}

6 changes: 4 additions & 2 deletions languages/js/sdk-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ export class SecretsClient {
key: string,
value: string,
note: string,
projectIds: string[],
organizationId: string,
): Promise<SecretResponse> {
const response = await this.client.run_command(
Convert.commandToJson({
secrets: {
create: { key, value, note, organizationId },
create: { key, value, note, projectIds, organizationId },
},
}),
);
Expand All @@ -100,12 +101,13 @@ export class SecretsClient {
key: string,
value: string,
note: string,
projectIds: string[],
organizationId: string,
): Promise<SecretResponse> {
const response = await this.client.run_command(
Convert.commandToJson({
secrets: {
update: { id, key, value, note, organizationId },
update: { id, key, value, note, projectIds, organizationId },
},
}),
);
Expand Down
2 changes: 1 addition & 1 deletion languages/js/sdk-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist/",
"module": "esnext",
"module": "commonjs",
"target": "es5",
"moduleResolution": "node",
"sourceMap": true,
Expand Down

0 comments on commit ddaf0f9

Please sign in to comment.