1.10.1 • Published 4 years ago

packet-processor-api-client v1.10.1

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
4 years ago

PacketProcessorApiClient

CircleCI

Installation

Install the library using the following command:

npm install --save @consumertrack/packet-processor-api-client

Usage

const session = 'The user session';
const campaign = '123456';
const url = 'https://stage-t.gofreecredit.com';

const fail = err => console.log('Packet failed');

// Pass all global data settings as the fourth parameter. This is
// used for values that do not change during a user's session
// that come from the original request - like subid.
const papi = new PacketProcessorApiClient(url, session, campaign, {
  subid: 'subid value'
});

// Enable console logging
papi.enableLogger('debug');

// Send a smarturl packet via http request
papi
  .smarturl()
  .validate()
  .then(papi.send)
  .catch(fail);

// If rendering is handled by another system - like a pixel
papi
  .smarturl()
  .validate()
  .then(papi.getUrl)
  .then(url => console.log(url))
  .catch(fail);

// Send a form enter packet for a particular state id via http request
papi
  .enter(41)
  .validate()
  .then(papi.send)
  .catch(fail);

// To append data to any request
papi
  .smarturl()
  .add({ additional_param: 'param' })
  .validate()
  .then(_ => console.log('Passed validation'))
  .catch(fail);

// For more control over flow
papi
  .enter(41)
  .validate()
  .then(packet => {
    if (shouldSend) {
      return packet.send();
    }
  })
  .catch(fail);

Usage with custom agent

Passing in a custom http.Agent can be useful in Node.js applications to utilize keep-alive.

const https = require('https');

const papi = new PacketProcessorApiClient(url, session, campaign, {
  subid: 'subid value'
});

const agent = new https.Agent({
  keepAlive: true,
  maxSockets: 50,
  rejectUnauthorized: true
});

papi.setAgent(agent);

In Node.js Lambdas, the agent should be initialized in the root of the module to ensure it can be reused.

Authorization

Services may require authentication. This client supports OAuth2 bearer tokens for this purpose.

const session = UUID.genV4().hexNoDelim;

const oauth2 = require('simple-oauth2').create({
  client: {
    id: process.env.OIDC_CLIENT_ID,
    secret: process.env.OIDC_CLIENT_SECRET
  },
  auth: {
    tokenHost: process.env.OIDC_TOKEN_HOST,
    tokenPath: '/token'
  }
});

const result = await oauth2.ownerPassword.getToken({ session_id: session });
const tokenResult = oauth2.accessToken.create(result);
const token = tokenResult.token;

const papi = new PacketProcessorApiClient(
  url,
  session,
  campaign,
  {
    subid: 'subid value'
  },
  token.access_token
);

// Or, the token can be set on the instance;
papi.setAccessToken(token.access_token);

Parallel execution

You can execute packets in parallel using the collect/execute methods. An example is below:

const smarturl = papi
  .smarturl()
  .validate()
  // To collect all URLs instead of sending, use papi.getUrl here
  .then(papi.send)
  .catch(err => console.log('Smarturl packet failed'));

const enter = papi
  .enter(41)
  .add({ s: 'asd' }) // Purposeful error
  .validate()
  .then(papi.send)
  .catch(err => console.log('Enter packet failed'));

papi
  .collect(smarturl, enter)
  .execute()
  .then(values => console.log('Done', values))
  .catch(err => console.error('Error', err)); // Should not be hit but here for fallback

CTI Internal Packet Types

smarturl()

Adds a smarturl enter packet.

enter(stateName:string)

Adds a form enter packet for the given state id.

errorEnter(stateName:string)

Adds a form error enter packet for the given state id.

submit(stateName:string)

Adds a form submit packet for the given state id.

errorSubmit(stateName:string)

Adds a form error submit packet for the given state id.

offerImpression(stateName:string, clickTarget:string, clickEvent:string)

Adds an offer impression packet for the given state id. The clickTarget and clickEvent are parameters used in reporting and usually signify the tab click.

displayImpression(displayAd:string, originalQuery:string)

Adds a display impression packet for display ads. The originalQuery varilable is optional.

displayClick(displayAd:string, originalQuery:string)

Adds a display click packet for display ads. The originalQuery varilable is optional.

processing(stateName:string)

Adds a processing enter packet for the given state id.

errorProcessing(stateName:string)

Adds a processing error enter packet for the given state id.

redirect(stateName:string)

Adds a redirect packet for the given state id.

Code Style

This project follows the Airbnb JavaScript Style Guide. A few exceptions exist:

  • use single quotes
  • print width set to 120 characters

Testing and Linting

This project uses Jest, ESLint and Prettier for testing and linting.

Testing

Tests are executed with npm test.

Code Coverage reports

Coverage reports are generated with the npm run test:coverage command. An HTML report will be available in the coverage/lcov-report/index.html file.

Linting

Lint checks and automatic fixes use the popular ESLint and Prettier packages.

Fixing errors automatically

Linting problems can, in most cases, be automatically fixed with the npm run lint:fix command. In certain cases, a developer will need to manually fix issues based on feedback from the linter.

Checking for lint problems

Linting issues are discoverd with the npm run lint:check command.

Workflow

When committing code, lint-staged will monitor staged files and run lint:fix on them as needed. If linting fails, commits will be aborted.

Test suites will run before pushes. If tests fail, the push will abort.