Skip to content

Commit

Permalink
docs: update usage example and provide samples folder with 1 sample f…
Browse files Browse the repository at this point in the history
…irst
  • Loading branch information
andreyunugro committed Oct 30, 2022
1 parent f71da8f commit 6b4d47e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
27 changes: 1 addition & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,7 @@ npm install sonarqube-webapis --save
```

## Usage Example
```js
// Example 1: get all components / projects in the organization.
import { AxiosError } from 'axios';
import Sonar from 'sonarqube-webapis';

(async () => {
// Initiate Sonar.
const sonar = new Sonar({
auth: {
username: '',
password: '',
},
// You can use sonarcloud / sonarqube web api url.
baseURL: 'https://sonarcloud.io/api',
});
try {
// {@link https://sonarcloud.io/web_api/api/components/search}
const result = await sonar.components.search('my-org2');
// The result is in: result.data
console.log('Components:', result.data.components);
} catch (error) {
// This is to show error messages from sonar.
console.log('Errors: ',(error as AxiosError).response?.data.errors);
}
})();
```
See samples folder.

## License
The project is available under the [MIT license](https://tldrlegal.com/license/mit-license).
37 changes: 37 additions & 0 deletions samples/sample-01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file
* sample-01.ts
*
* Example 1: makesure your web api credential is correct.
*
* @see https://docs.sonarqube.org/latest/extend/web-api/#header-2
*/

import { AxiosError } from 'axios';
import Sonar from 'sonarqube-webapis';

(async () => {
// Initiate Sonar.
const sonar = new Sonar({
// 1. User token: set your token in the username, set empty string password.
// 2. Basic access: set your standard login username & password.
auth: {
username: 'token / your login username',
password: 'empty string / your login password',
},
// You can use sonarcloud / sonarqube web api url.
baseURL: 'https://sonarcloud.io/api',
// baseURL: 'http://localhost:9000/api',
});
try {
// {@link https://sonarcloud.io/web_api/api/authentication/validate}
// {@link http://localhost:9000/web_api/api/authentication/validate}
const result = await sonar.authentication.validate();
// The result is in: result.data
// Sample: { valid: true }
console.log('Validation result:', result.data);
} catch (error) {
// This is to show error messages from sonar.
console.log('Errors: ', (error as AxiosError).response?.data);
}
})();

0 comments on commit 6b4d47e

Please sign in to comment.