# promoted-nodejs-client

> Logging utility for browser-side Typescript that logs Promoted events to our Metrics Snowplow-compatible API

Latest version **1.0.1** (published 2021-04-07) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install promoted-nodejs-client
pnpm add promoted-nodejs-client
yarn add promoted-nodejs-client
bun add promoted-nodejs-client
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-04-07 |
| First published | 2021-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 59.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dan Hill |
| Maintainers | prm-deployer |
| Keywords | library, promoted, delivery, client, nodejs |

## Links

- npm: https://www.npmjs.com/package/promoted-nodejs-client
- Repository: https://github.com/promotedai/promoted-nodejs-client
- Homepage: https://github.com/promotedai/promoted-nodejs-client#readme
- Issues: https://github.com/promotedia/promoted-nodejs-client/issues
- npm.io page: https://npm.io/package/promoted-nodejs-client

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-04-07
- 1.0.0 — 2021-04-02

## README

# promoted-nodejs-client

A Node.js client for Promoted APIs.

## Creating a PromotedClient

We recommend creating a PromotedClient in a separate file so (1) it can be shared between files and (2) hides some of the configuration.

PromotedClient avoids having direct dependencies to certain implementation details so we can support customization and keep the binary small.

promotedClient.js
```
import { logOnError, newPromotedClient, throwOnError } from 'promoted-nodejs-client';
import { v5 as uuid } from 'uuid';
import axios from 'axios';

const axiosApiClient = <Req, Res>(url: string, apiKey: string, timeout: number) => (request: Req): Promise<Res> =>
  axios.post(
    url,
    request,
    {
      headers: {
        "x-api-key": apiKey,
      },
      timeout: 3000,
    });

// These values will vary depending on dev vs prod.
const deliveryApi = 'https://....com/...';
const deliveryApiKey = 'AbCSomeRLongString1';
const deliveryTimeoutMillis = 250;
const metricsApi = 'https://....com/...';
const metricsApiKey = 'AbCSomeRLongString2';
const metricsTimeoutMillis = 3000;

// NextJS example.  Throw in dev.  Console log in prod.
const throwError =
  process?.env?.NODE_ENV !== 'production' ||
  (typeof location !== "undefined" && location?.hostname === "localhost");

export const promotedClient = newPromotedClient({
  handleError: throwError ? throwOnError : logOnError;
  deliveryClient: axiosApiClient(deliveryApi, deliveryApiKey, deliveryTimeoutMillis),
  metricsClient: axiosApiClient(metricsApi, metricsApiKey, metricsTimeoutMillis),
  uuid,
  deliveryTimeoutMillis,
  metricsTimeoutMillis,
});
```

## Calling our Delivery API

Let's say the previous code looks like this:
```
static async getProducts(req: any, res: Response) {
  const products = ...; // Logic to get products from DB, apply filtering, etc.
  sendSuccessToClient(res, { products });
}
```

We would modify to something like this:
```
static async getProducts(req: any, res: Response) {
  const products = ...;
  const response = await promotedClient.deliver({
    request: {
      userInfo: {
        logUserId: req.logUserId,
      },
      useCase: 'FEED',
      sessionId: req.sessionId,
      viewId: req.viewId,
      insertion: products.map(product => ({
        contentId: product.id,
        properties: {
          struct: {
            product,
          },
        },
      })),
    },
  });
  // Change the result Product list to use the values in the returned Insertions.
  sendSuccessToClient(res, {
    products: response.insertion.map(insertion => insertion.properties.struct),
  });
  await response.finishAfterResponse();
}
```

There are a bunch of additional options.

# When modifying this library.

## Features

Uses
- [TypeScript](https://www.typescriptlang.org/) support
- [React](https://reactjs.org/) support
- [ESLint](https://eslint.org/) (with [React](https://reactjs.org/) and [Prettier](https://prettier.io/))
- Unit tests ([Jest](https://jestjs.io/) and [Testing Library](https://testing-library.com/))
- Minified output with [Terser](https://terser.org/)
- Bundle size validation with [size-limit](https://github.com/ai/size-limit)
- Flexible builds with [Rollup](https://www.rollupjs.org/)
- [CHANGELOG.md](https://keepachangelog.com/en/1.0.0/) template

## Scripts

- Run most commands: `npm run finish`
- Build the project: `npm run build`
  - Validate output bundle size with `npm run size`
- Lint the project: `npm run lint`
- Run unit tests: `npm test` or `npm test`

## When developing locally

If you want to test local changes in an actual deployment, use `npm link`.

1. Run `npm run updateLink`.
4. Go to client directory and run `npm link promoted-nodejs-client`.

When you update `promoted-nodejs-client`, run `npm run updateLink`.

When you want to undo, use `npm unlink` in `promoted-nodejs-client/dist` and `npm unlink promoted-nodejs-client` in the client directory.

## Deploy

We use a GitHub action that runs semantic-release to determine how to update versions.  Just do a normal code review and this should work.  Depending on the message prefixes (e.g. `feat: `, `fix: `, `clean: `, `docs: `), it'll update the version appropriately.

# Resources

The base of this repository is a combination of the following repos:
- https://github.com/DenysVuika/react-lib
- https://github.com/Alexandrshy/como-north and https://dev.to/alexandrshy/creating-a-template-repository-in-github-1d05

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