# @n2/common

> The common module for n2

Latest version **0.1.2** (published 2018-10-09) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install @n2/common
pnpm add @n2/common
yarn add @n2/common
bun add @n2/common
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2018-10-09 |
| First published | 2018-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 78.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tcrane |
| Maintainers | thomas-crane |
| Keywords | typescript, rotmg, n2 |

## Links

- npm: https://www.npmjs.com/package/@n2/common
- npm.io page: https://npm.io/package/@n2/common

## Dependencies (1)

- [chalk](https://npm.io/package/chalk.md) ^2.4.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2018-10-09
- 0.1.1 — 2018-10-08
- 0.1.0 — 2018-09-27

## README

# n2/common

This module is home to interfaces and classes which are used throughout the other n2 modules.
Most of the interfaces represent entities from RotMG, however some are used to represent data structures used by the classes in this module.

## Install

[![npm version](https://badge.fury.io/js/%40n2%2Fcommon.svg)](https://badge.fury.io/js/%40n2%2Fcommon)

```bash
npm install @n2/common
```

## Use

There are three main "submodules" provided by the common modules.

+ Http
+ Logging
+ Models

### Http

The exported `HttpClient` class provides a basic promise-based way of making web requests. The http client will include standard request headers with any request, and will automatically unzip any gzipped responses.

```javascript
const { HttpClient, Endpoints } = require('@n2/common');

// query parameters can be included in the request options.
const requestOptions = {
  query: { guid: 'example@email.com', password: '123456' }
};

HttpClient.get(Endpoints.CHAR_LIST, requestOptions).then((data) => {
  console.log('Fetched account info.');
  const accInfoXML = data.toString();
});
```

The http client will normally return the response as a `Buffer` object. A writable stream can optionally be provided as part of the `requestOptions`, and if one is provided, the response will be piped to the stream. In this case, the promise will be resolved with `undefined` instead of the data buffer.

This means that

```javascript
const { HttpClient, Endpoints } = require('@n2/common');
const fs = require('fs');

const writeStream = fs.createWriteStream('./ground-types.json');

HttpClient.get(Endpoints.STATIC_DRIPS + '/current/json/GroundTypes.json').then((data) => {
  writeStream.write(data);
  writeStream.end();
});
```

is the same as

```javascript
const { HttpClient, Endpoints } = require('@n2/common');
const fs = require('fs');

const writeStream = fs.createWriteStream('./ground-types.json');

HttpClient.get(Endpoints.STATIC_DRIPS + '/current/json/GroundTypes.json', {
  stream: writeStream
});
```

### Logging

The `Logger` class provides a logging mechanism in the form of a logger chain. Custom loggers can be added to the chain as long as they adhere to the `LogProvider` interface.

In TypeScript, custom loggers can simply inherit from `LogProvider`, however in JavaScript care will have to be taken to ensure the custom loggers have the required methods.

```typescript
import { LogProvider, LogLevel } from '@n2/common';

class MyCustomLogger implements LogProvider {

  // inherited method from LogProvider.
  log(sender: string, message: string, level: LogLevel): void {
    console.log(`[${sender}] ${message} (${LogLevel[level] || 'no level'})`);
  }
}
```

```typescript
import { Logger, LogLevel } from '@n2/common';
import { MyCustomLogger } from './my-custom-logger';

Logger.addLogger(new MyCustomLogger());

Logger.log('Test', 'Hello, World!', LogLevel.Debug);
// [Test] Hello, World! (Debug)
```

More loggers can be added to the chain. Calls to `Logger.log` will call the `log` method of each logger in the chain in the order that they were added.

```typescript
Logger.addLogger(new MyCustomLogger());
Logger.addLogger(new MyCustomLogger());
Logger.addLogger(new MyCustomLogger());

Logger.log('Test', 'Hello 3 times.', LogLevel.Message);
// [Test] Hello 3 times. (Message)
// [Test] Hello 3 times. (Message)
// [Test] Hello 3 times. (Message)
```

### Models

The common module exports a handful of interfaces and enums which represent data structures and values used throughout clientless applications for RotMG. Some notable examples include the `PlayerData` interface which represents a player entity, and the `Endpoints` enum which contains commonly used HTTP endpoints.

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