# tweek-client

> Tweek client for JavaScript

Latest version **3.2.5** (published 2022-04-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install tweek-client
pnpm add tweek-client
yarn add tweek-client
bun add tweek-client
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.2.5 |
| Published | 2022-04-20 |
| First published | 2017-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 123.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Soluto |
| Maintainers | npmsoluto |

## Links

- npm: https://www.npmjs.com/package/tweek-client
- Repository: https://github.com/Soluto/tweek-clients
- Homepage: https://github.com/Soluto/tweek-clients#readme
- Issues: https://github.com/Soluto/tweek-clients/issues
- npm.io page: https://npm.io/package/tweek-client

## Dependencies (6)

- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.0.1
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) ^4.2.0
- [query-string](https://npm.io/package/query-string.md) ^7.0.0
- [change-emitter](https://npm.io/package/change-emitter.md) ^0.1.6
- [zen-observable](https://npm.io/package/zen-observable.md) ^0.8.13
- [fast-json-patch](https://npm.io/package/fast-json-patch.md) ^3.0.0-1

## Recent versions

- 3.2.5 (latest) — 2022-04-20
- 3.2.4 — 2022-03-29
- 3.2.3 — 2022-03-29
- 3.1.3 — 2021-05-06
- 3.1.2 — 2021-05-05
- 3.1.1 — 2021-05-03
- 3.1.0 — 2021-05-03
- 3.0.0 — 2021-03-15
- 2.2.1-rc1 — 2020-11-22
- 2.2.1-rc0 — 2020-09-09
- 2.2.0 — 2020-09-03
- 2.1.4 — 2020-09-01
- 2.0.4 — 2020-07-20
- 2.0.3 — 2020-05-11
- 2.0.2 — 2020-05-11
- … 33 more at https://npm.io/package/tweek-client/versions

## README

# Tweek Client for JavaScript

## Basic usage

### Install the package

```
npm install --save tweek-client
```

### TweekClient

#### Create a client

`createTweekClient(config: CreateTweekClientConfig): TweekClient`

use this method to create a tweek client

```javascript
const tweekClient = createTweekClient({
  baseServiceUrl: 'https://mydomain',
});
```

the config object accepts these properties:

| Prop                     | Description                                            | Type                                              | Default     |
| ------------------------ | ------------------------------------------------------ | ------------------------------------------------- | ----------- |
| `baseServiceUrl`         | Required - the url for tweek gateway                   | `string`                                          |
| `context`                | the context to add to anu request                      | `object`                                          |
| `useLegacyEndpoint`      | if set to true, will use `v1` version of the api       | `boolean`                                         | `false`     |
| `fetch`                  | a fetch client to use to make the requests             | `(RequestInfo, RequestInit) => Promise<Response>` | cross-fetch |
| `requestTimeoutInMillis` | request timeout in ms                                  | `number`                                          | 8000        |
| `onError`                | callback to be called for request errors               | `(Error) => void`                                 |
| `getAuthenticationToken` | a function that returns a token for jwt authentication | `() => Promise<string> \| string`                 |
| `clientId`               | client id for basic auth authentication                | `string`                                          |
| `clientSecret`           | client secret for basic auth authentication            | `string`                                          |

#### Query your configuration key and get value

`tweekClient.getValues<T>(keyPath: string, config?: GetValuesConfig): Promise<T>`

```javascript
const myConfiguration = await tweekClient.getValues('some_key/path');
```

the config object accepts these properties:

| Prop             | Description                                                                                   | Type       | Default |
| ---------------- | --------------------------------------------------------------------------------------------- | ---------- | ------- |
| `include`        | the keys to include in the request, used to filter scan keys                                  | `string[]` |
| `context`        | override the client context                                                                   | `object`   |
| `flatten`        | if set to true the response will be in the format of a { [keyPath]: value }                   | `boolean`  | `false` |
| `ignoreKeyTypes` | if set to true, all the key types will be ignored and returned as strings                     | `boolean`  | `false` |
| `maxChunkSize`   | if the `include` section has a lot of entries, it will split the request into multiple chunks | `number`   | 100     |

#### Query configuration with key value error details

**only supported on api versions 1.0-rc3 and above**

`tweekClient.getValuesWithDetails<T>(path: string, config?: GetValuesConfig) : Promise<DetailedTweekResult<T>>`

```javascript
const myDetaildConfig = await tweekClient.getValuesWithDetails('some_key/path');
```

the config object has the same properties as `getValues`

### TweekManagementClient

#### Create a client

`createTweekManagementClient(config: CreateTweekManagementClientConfig): TweekManagementClient`

use this method to create a tweek management client

```javascript
const tweekManagementClient = createTweekManagementClient({
  baseServiceUrl: 'https://mydomain',
});
```

the config object accepts these properties:

| Prop                     | Description                                            | Type                                              | Default     |
| ------------------------ | ------------------------------------------------------ | ------------------------------------------------- | ----------- |
| `baseServiceUrl`         | Required - the url for tweek gateway                   | `string`                                          |
| `fetch`                  | a fetch client to use to make the requests             | `(RequestInfo, RequestInit) => Promise<Response>` | cross-fetch |
| `requestTimeoutInMillis` | request timeout in ms                                  | `number`                                          | 8000        |
| `onError`                | callback to be called for request errors               | `(Error) => void`                                 |
| `getAuthenticationToken` | a function that returns a token for jwt authentication | `() => Promise<string> \| string`                 |
| `clientId`               | client id for basic auth authentication                | `string`                                          |
| `clientSecret`           | client secret for basic auth authentication            | `string`                                          |

#### Updating Context

```javascript
const myContext = {
  age: 23,
};

await tweekManagementClient.appendContext('user', '123456', myContext);
```

## Using Authentication

Example:

```javascript
function getAuthenticationToken() {
  return 'jwt token';
}

const tweekClient = createTweekClient({
  baseServiceUrl: 'https://mydomain',
  getAuthenticationToken,
});
```

`getAuthenticationToken` can also return a promise

```javascript
function getAuthenticationToken() {
  return Promise.resolve('jwt token');
}

const tweekClient = createTweekClient({
  baseServiceUrl: 'https://mydomain',
  getAuthenticationToken,
});
```

---
_Source: https://npm.io/package/tweek-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
