# ctvault

> ctvault is a wrapper library around the commercetools JavaScript SDK that aims to reduce boilerplate code and provide consolidated credential management for multiple commercetools projects.

Latest version **2.0.8** (published 2021-03-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install ctvault
pnpm add ctvault
yarn add ctvault
bun add ctvault
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.8 |
| Published | 2021-03-24 |
| First published | 2020-02-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 19 |
| Unpacked size | 72 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | dave lilly |
| Maintainers | dlillyatx |

## Links

- npm: https://www.npmjs.com/package/ctvault
- npm.io page: https://npm.io/package/ctvault

## Dependencies (19)

- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [nconf](https://npm.io/package/nconf.md) ^0.10.0
- [yargs](https://npm.io/package/yargs.md) ^14.2.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [moment](https://npm.io/package/moment.md) ^2.28.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.1.0
- [js-logger](https://npm.io/package/js-logger.md) ^1.6.1
- [pluralize](https://npm.io/package/pluralize.md) ^8.0.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [fast-crc32c](https://npm.io/package/fast-crc32c.md) ^2.0.0
- [line-reader](https://npm.io/package/line-reader.md) ^0.4.0
- [@firebase/app](https://npm.io/package/@firebase/app.md) ^0.5.6
- [firebase-admin](https://npm.io/package/firebase-admin.md) ^9.5.0
- [js-levenshtein](https://npm.io/package/js-levenshtein.md) ^1.1.6
- [single-line-log](https://npm.io/package/single-line-log.md) ^1.1.2
- [@commercetools/sdk-client](https://npm.io/package/@commercetools/sdk-client.md) ^2.1.2
- [@commercetools/api-request-builder](https://npm.io/package/@commercetools/api-request-builder.md) ^5.6.3
- [@commercetools/sdk-middleware-auth](https://npm.io/package/@commercetools/sdk-middleware-auth.md) ^5.1.7
- [@commercetools/sdk-middleware-http](https://npm.io/package/@commercetools/sdk-middleware-http.md) ^5.3.1

## Recent versions

- 2.0.8 (latest) — 2021-03-24
- 2.0.7 — 2021-03-23
- 2.0.6 — 2021-03-23
- 2.0.5 — 2021-02-16
- 2.0.4 — 2021-02-16
- 1.0.39 — 2021-02-16
- 2.0.3 — 2020-12-01
- 2.0.2 — 2020-12-01
- 2.0.1 — 2020-11-24
- 2.0.0 — 2020-11-23
- 1.0.38 — 2020-11-19
- 1.0.37 — 2020-11-19
- 1.0.36 — 2020-11-19
- 1.0.35 — 2020-11-09
- 1.0.34 — 2020-10-15
- … 33 more at https://npm.io/package/ctvault/versions

## README

# ctvault
## Motivation
ctvault is a wrapper library around the commercetools JavaScript SDK that aims to reduce boilerplate code and provide consolidated credential management for multiple commercetools projects.

## Configuration
There are multiple methods for configuring the type of data store that ctvault uses.  Set the `CT_VAULT_CONFIG` environment variable to point to the location of the JSON configuration file.

### commercetools project

This configuration will point ctvault at a commercetools project, specifying a namespace and key for a custom object to store the managed credentials.

| key           | value                                 |
| ------------- | ------------------------------------- |
| `type`        | `"ctp"`                               |
| `namespace`   | the custom object namespace           |
| `key`         | the custom object key                 |
| `credentials` | the commercetools project credentials |

### Google Firebase

You can also use a Google Firebase store to serve as the credentials vault.

| key           | value                                                                                                                 |
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
| `type`        | `"firebase"`                                                                                                          |
| `credentials` | path to a JSON file with GCP service account credentials (see https://cloud.google.com/iam/docs/service-accounts)     |
| `collection`  | name of the firebase collection that holds the managed credentials                                                    |

### Local file

Alternatively you can store them in a file on the local file system.

| key           | value                                         |
| ------------- | --------------------------------------------- |
| `type`        | `"file"`                                      |
| `credentials` | an array of commercetools project credentials |

### commercetools Credentials

Here is the structure for the commercetools credentials objects.  These are both to specify the project (if you are pointing at a commercetools project) and to specify the set of managed credentials.

| key             | value                                   |
| --------------- | --------------------------------------- |
| `oauth_url`     | URL to the commercetools auth server    |
| `api_url`       | URL to the commercetools API gateway    |
| `project`       | project key                             |
| `client_id`     | API client ID                           |
| `client_secret` | API client secret                       |

## Usage

ctvault acts as a simple broker.  You can query it by using two methods:

### `getClient(projectKey)`

This will return a CTP client configured to talk to the project specified in `projectKey` if the credentials exist in the vault.  If they are not, an exception is thrown to that effect.

If `projectKey` is empty, ctvault will attempt to find it in the command line argument `--project`.

### `getClients`

This will return an array of CTP clients for which the vault contains credentials.

Both of these methods are asynchronous, so be sure to use `await`.

## Using a ctvault CTP client (ctclient)

ctclient uses a syntax based on `api-request-builder` (https://commercetools.github.io/nodejs/sdk/api/apiRequestBuilder.html), specifically the Declarative Usage section (https://commercetools.github.io/nodejs/sdk/api/apiRequestBuilder.html#declarative-usage).

Largely syntactic sugar, it provides an interface for you to use standard CRUD operations (`create`, `get`, `update`, `remove`), but also adds the following verbs:

`process`: This will use the `processRequest` method from `api-request-builder` to page through objects.

`ensure`: Given a source object, will try to find an object with the matching `key`.  If it is not found, it will be created with the source object as the template.  Use when building data models.

### Examples

Query for a tax category with a key of `standard`:

```
const argv = require('yargs').argv
let ct = await require('ctvault').getClient(argv.project)

let standardTaxCategory = await ct.taxCategories.get({ key: 'standard' })
```

Make sure order type `foo` is defined:

```
const argv = require('yargs').argv
let ct = await require('ctvault').getClient(argv.project)

let fooOrderType = {
    key: 'FooOrderType',
    name: { 
        en: 'Foo'
    },
    resourceTypeIds: ['order'],
    description: { 
        en: 'Foo'
    },
    fieldDefinitions: [{
        name: 'bar',
        type: { name: 'String' },
        required: false,
        label: { 
            en: 'Bar'
        }
    }]
}

let foo = await ct.types.ensure(fooOrderType)
```

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