From ddaf0f9564f08d8272034f383a781225861a8aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garci=CC=81a?= Date: Fri, 26 Jan 2024 17:34:12 +0100 Subject: [PATCH] Add small JS example --- languages/js/example/index.js | 31 +++++++++++++++++++++++ languages/js/example/package-lock.json | 34 ++++++++++++++++++++++++++ languages/js/example/package.json | 12 +++++++++ languages/js/sdk-client/src/client.ts | 6 +++-- languages/js/sdk-client/tsconfig.json | 2 +- 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 languages/js/example/index.js create mode 100644 languages/js/example/package-lock.json create mode 100644 languages/js/example/package.json diff --git a/languages/js/example/index.js b/languages/js/example/index.js new file mode 100644 index 000000000..ca089ec72 --- /dev/null +++ b/languages/js/example/index.js @@ -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(); \ No newline at end of file diff --git a/languages/js/example/package-lock.json b/languages/js/example/package-lock.json new file mode 100644 index 000000000..f6cab6536 --- /dev/null +++ b/languages/js/example/package-lock.json @@ -0,0 +1,34 @@ +{ + "name": "sdk-example", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "sdk-example", + "dependencies": { + "@bitwarden/sdk-client": "../sdk-client", + "@bitwarden/sdk-wasm": "../wasm" + } + }, + "../sdk-client": { + "name": "@bitwarden/sdk-client", + "devDependencies": { + "@types/node": "^18.15.11", + "rimraf": "^5.0.0", + "typescript": "^5.0.3" + } + }, + "../wasm": { + "name": "@bitwarden/sdk-wasm", + "version": "0.1.0" + }, + "node_modules/@bitwarden/sdk-client": { + "resolved": "../sdk-client", + "link": true + }, + "node_modules/@bitwarden/sdk-wasm": { + "resolved": "../wasm", + "link": true + } + } +} diff --git a/languages/js/example/package.json b/languages/js/example/package.json new file mode 100644 index 000000000..49a0cc2cb --- /dev/null +++ b/languages/js/example/package.json @@ -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" + } + } + \ No newline at end of file diff --git a/languages/js/sdk-client/src/client.ts b/languages/js/sdk-client/src/client.ts index 2f9098596..0f06889c1 100644 --- a/languages/js/sdk-client/src/client.ts +++ b/languages/js/sdk-client/src/client.ts @@ -70,12 +70,13 @@ export class SecretsClient { key: string, value: string, note: string, + projectIds: string[], organizationId: string, ): Promise { const response = await this.client.run_command( Convert.commandToJson({ secrets: { - create: { key, value, note, organizationId }, + create: { key, value, note, projectIds, organizationId }, }, }), ); @@ -100,12 +101,13 @@ export class SecretsClient { key: string, value: string, note: string, + projectIds: string[], organizationId: string, ): Promise { const response = await this.client.run_command( Convert.commandToJson({ secrets: { - update: { id, key, value, note, organizationId }, + update: { id, key, value, note, projectIds, organizationId }, }, }), ); diff --git a/languages/js/sdk-client/tsconfig.json b/languages/js/sdk-client/tsconfig.json index 453623d5f..987e8d679 100644 --- a/languages/js/sdk-client/tsconfig.json +++ b/languages/js/sdk-client/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "outDir": "./dist/", - "module": "esnext", + "module": "commonjs", "target": "es5", "moduleResolution": "node", "sourceMap": true,