diff --git a/README.md b/README.md index b4fef9b..bace41d 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file diff --git a/samples/sample-01.ts b/samples/sample-01.ts new file mode 100644 index 0000000..7514b63 --- /dev/null +++ b/samples/sample-01.ts @@ -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); + } +})();