# diameter-avp-object

> Syntactical sugar for manipulating node-diameter AVP arrays

Latest version **1.1.0** (published 2018-02-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install diameter-avp-object
pnpm add diameter-avp-object
yarn add diameter-avp-object
bun add diameter-avp-object
```

## 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 | 1.1.0 |
| Published | 2018-02-27 |
| First published | 2016-06-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Ates Goral |
| Maintainers | atesgoral, rkovacevic |

## Links

- npm: https://www.npmjs.com/package/diameter-avp-object
- Repository: https://github.com/atesgoral/diameter-avp-object
- Homepage: https://github.com/atesgoral/diameter-avp-object#readme
- Issues: https://github.com/atesgoral/diameter-avp-object/issues
- npm.io page: https://npm.io/package/diameter-avp-object

## Recent versions

- 1.1.0 (latest) — 2018-02-27
- 1.0.7 — 2017-11-22
- 1.0.6 — 2017-11-08
- 1.0.5 — 2017-07-28
- 1.0.4 — 2017-05-27
- 1.0.3 — 2017-04-19
- 1.0.2 — 2017-04-19
- 1.0.1 — 2016-06-13
- 1.0.0 — 2016-06-13
- 0.0.2 — 2016-06-13
- 0.0.1 — 2016-06-13

## README

[![Travis CI](https://img.shields.io/travis/node-diameter/node-diameter-avp-object.svg)](https://travis-ci.org/node-diameter/node-diameter-avp-object)
[![Coverage Status](https://img.shields.io/coveralls/node-diameter/node-diameter-avp-object.svg)](https://coveralls.io/github/node-diameter/node-diameter-avp-object?branch=master)
[![NPM Package](https://img.shields.io/npm/v/diameter-avp-object.svg)](https://www.npmjs.com/package/diameter-avp-object)

# node-diameter-avp-object

Syntactical sugar for manipulating [node-diameter](https://github.com/node-diameter/node-diameter) AVP arrays.

node-diameter's `diameterMessage` event returns a nested array of AVPs in the `event.message.body` property.

Accessing AVPs in this form may look syntactically crowded. This library converts AVP arrays into nested objects that are more syntactically friendly. AVPs are converted into camelCase properties. Multiple occurrences of the same AVP are rolled into an array.

It also converts such objects back to the nested array form for Diameter answers.

For example, the following AVP array:

```
[
  [ 'Subscription-Id', [
    [ 'Subscription-Id-Type', 'END_USER_IMSI' ],
    [ 'Subscription-Id-Data', '1234' ]
  ]],
  [ 'Subscription-Id', [
    [ 'Subscription-Id-Type', 'END_USER_E164' ],
    [ 'Subscription-Id-Data', '4321' ]
  ]],
  [ 'Multiple-Services-Credit-Control', [
    [ 'Used-Service-Unit', [
      [ 'CC-Total-Octets', 1000 ]
    ]],
    [ 'Requested-Service-Unit', [
      [ 'CC-Total-Octets', 2000 ]
    ]]
  ]]
]
```

Gets converted to:

```
{
  subscriptionId: [{
    subscriptionIdType: 'END_USER_IMSI',
    subscriptionIdData: '1234'
  }, {
    subscriptionIdType: 'END_USER_E164',
    subscriptionIdData: '4321'
  }],
  multipleServicesCreditControl: {
    usedServiceUnit: {
      ccTotalOctets: 1000
    },
    requestedServiceUnit: {
      ccTotalOctets: 2000
    }
  }
}
```

## Usage

```
npm install --save diameter-avp-object
```

Then:

```
const avp = require('diameter-avp-object');

// A structure returned by node-diameter
const avpList = [
  [ 'Subscription-Id', [
    [ 'Subscription-Id-Type', 'END_USER_IMSI' ],
    [ 'Subscription-Id-Data', '1234' ]
  ]],
  [ 'Subscription-Id', [
    [ 'Subscription-Id-Type', 'END_USER_E164' ],
    [ 'Subscription-Id-Data', '4321' ]
  ]],
  [ 'Multiple-Services-Credit-Control', [
    [ 'Used-Service-Unit', [
      [ 'CC-Total-Octets', 1000 ]
    ]],
    [ 'Requested-Service-Unit', [
      [ 'CC-Total-Octets', 2000 ]
    ]]
  ]]
];

// Get consolidated object
const avpObj = avp.toObject(avpList);

// Multiple occurences of the same AVP are rolled into an array
const imsi = avpObj.subscriptionId
  .find(sId => sId.subscriptionIdType === 'END_USER_IMSI')
  .subscriptionIdData;

// Single instances are not
const cc = avpObj.multipleServicesCreditControl;

if (cc.ccRequestType === 'UPDATE_REQUEST') {
  const grantedUnits = handleUpdateRequest(
    imsi,
    cc.serviceIdentifier,
    cc.usedServiceUnit.ccTotalOctets,
    cc.requestedServiceUnit.ccTotalOctets
  );

  const avpList = avp.fromObject({
    ccRequestType: cc.ccRequestType,
    ccRequestNumber: avpObj.ccRequestNumber,
    multipleServicesCreditControl: {
      serviceIdentifier: cc.serviceIdentifier,
      ratingGroup: cc.serviceIdentifier,
      resultCode: 'DIAMETER_SUCCESS',
      grantedServiceUnit: {
        ccTotalOctets: grantedUnits
      }
    }
  });

  // Use avpList in the Diameter answer
  // ...
}
```

## Testing

Run tests:

```
npm test
```

For coverage:

```
npm run coverage
```

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