Skip to content

Commit

Permalink
feat: add js client (#71)
Browse files Browse the repository at this point in the history
* feat: add js client to send JSON data to server (#60)

* feat: add support for proto serde in js client (#62)

* feat: add ci stages for js client (#68)

* docs: add readme and examples for js client (#70)

* chore:bump version

---------

Co-authored-by: Ravi Suhag <suhag.ravi@gmail.com>
  • Loading branch information
prakharmathur82 and ravisuhag committed Sep 24, 2023
1 parent af275df commit 7546d5c
Show file tree
Hide file tree
Showing 34 changed files with 10,016 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .github/workflows/release-js-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release Client - JS

on:
release:
types: [published]
workflow_dispatch:

jobs:
publish-js-client:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
scope: "@raystack"
- run: npm install
working-directory: clients/js
- run: npm publish --access public
working-directory: clients/js
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
48 changes: 48 additions & 0 deletions .github/workflows/test-js-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test Raccoon JS Client
on:
push:
paths:
- "clients/js/**"
pull_request:
paths:
- "clients/js/**"
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '20.x'
- name: Install dependencies
run: npm ci
working-directory: clients/js
- name: Check formatting
run: npm run format
working-directory: clients/js
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '20.x'
- name: Install dependencies
run: npm ci
working-directory: clients/js
- name: Check linting
run: npm run lint
working-directory: clients/js
test-js-client:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '20.x'
- name: Install dependencies
run: npm ci
working-directory: clients/js
- name: Test
run: npm test
working-directory: clients/js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ coverage
*.idea/
raccoon
.temp
__debug.*
node_modules
__debug.*
1 change: 1 addition & 0 deletions clients/js/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protos
17 changes: 17 additions & 0 deletions clients/js/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
env:
browser: true
commonjs: true
es2021: true
jest: true
extends:
- airbnb-base
- prettier
parserOptions:
ecmaVersion: 12
rules:
quotes: ["error", "single", { "avoidEscape": true }]
no-console: ["error", allow: ["error"]]
semi:
- error
- always
import/extensions: [0, { <js>: "always" }]
2 changes: 2 additions & 0 deletions clients/js/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples
test
1 change: 1 addition & 0 deletions clients/js/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protos
181 changes: 181 additions & 0 deletions clients/js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Raccoon Client for Javascript

A JS client library for [Raccoon](https://github.com/raystack/raccoon), compatible both in browsers as well as nodejs environment.

Features:
* Send JSON/Protobuf encoded messages to raccoon server
* Configurable request wiretype i.e.,JSON/PROTOBUF
* Retry mechanism

**Note:** For encoding protobuf messages, this client utilises [protobufjs](https://github.com/protobufjs/protobuf.js).

## Requirements

- **Node.js**: Version 20.x or above.
- **Browser**: Modern web browser with ES6+ compatibility.

## Install

```bash
npm i @raystack/raccoon --save
```

## Usage

#### Construct a new REST client, then pass the available options on the client.

For example:

```javascript
import { RaccoonClient, SerializationType, WireType } from '@raystack/raccoon';
```

```javascript
const raccoonClient = new RaccoonClient({
serializationType: SerializationType.PROTOBUF,
wireType: WireType.JSON,
timeout: 5000,
url: 'http://localhost:8080/api/v1/events',
headers: {
'X-User-ID': 'user-1'
}
});
```

#### Send messages

In the below example, we are sending protobuf messages.

**Note:** In this case, we only need to send the protobufjs object along with values, the client will take care of encoding the object.

```javascript
const pageEvent = new clickevents.PageEvent();
pageEvent.eventGuid = "123";
pageEvent.eventName = "page open";

const clickEvent = new clickevents.ClickEvent();
clickEvent.eventGuid = "123";
clickEvent.componentIndex = 1;
clickEvent.componentName = "images";

const events = [
{
type: 'test-topic1',
data: clickEvent
},
{
type: 'test-topic2',
data: pageEvent
}
];

raccoonClient.send(events)
.then(result => {
console.log('Result:', result);
})
.catch(error => {
console.error('Error:', error);
});
```

**Note:** Here, the PageEvent and ClickEvent are imported from generated protobufjs code.

See complete examples for sending JSON and protobuf messages: [examples](examples)

## Configurations

#### `SerializationType`

The serialization type to use for the events being sent to Kafka, either 'PROTOBUF' or 'JSON'.

- Type: `Required`
- Example value: `SerializationType.JSON`

#### `WireType`

The wire configuration using which the request payload should be sent to raccoon server

- Type: `Required`
- Example value: `WireType.JSON`

#### `url`

The URL for the raccoon server.

- Type: `Required`
- Example value: `http://localhost:8080/api/v1/events`

#### `headers`

Custom headers to be included in the HTTP requests.

- Type: `Optional`
- Default value: `{}`

#### `timeout`

The request timeout in milliseconds.

- Type: `Optional`
- Default value: `1000`

#### `retryMax`

The maximum number of retry attempts for failed requests.

- Type: `Optional`
- Default value: `3`

#### `retryWait`

The time in milliseconds to wait between retry attempts.

- Type: `Optional`
- Default value: `5000`

#### `logger`

Logger object for logging.

- Type: `Optional`
- Default value: `console`

## Setting up development environment

### Prerequisite Tools

- [Node.js](https://nodejs.org/) (version >= 20.x)
- [Git](https://git-scm.com/)

1. Clone the repo

```sh
$ git clone https://github.com/raystack/raccoon
$ cd raccoon/clients/js
```

2. Install dependencies

```sh
$ npm install
```

3. Run the tests. All of the tests are written with [jest](https://jestjs.io/).

```sh
$ npm test
```
4. Run format.

```sh
$ npm run format
```
4. Run lint.

```sh
$ npm run lint
```

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags](https://www.npmjs.com/package/@raystack/raccoon?activeTab=versions).
37 changes: 37 additions & 0 deletions clients/js/examples/json_messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// eslint-disable-next-line import/no-unresolved
import { RaccoonClient, SerializationType, WireType } from '@raystack/raccoon';

const logger = console;

// create json messages
const jsonEvents = [
{
type: 'test-topic1',
data: { key1: 'value1', key2: ['a', 'b'] }
},
{
type: 'test-topic2',
data: { key1: 'value2', key2: { key3: 'value3', key4: 'value4' } }
}
];

// initialise the raccoon client with required configs
const raccoonClient = new RaccoonClient({
serializationType: SerializationType.JSON,
wireType: WireType.JSON,
timeout: 5000,
url: 'http://localhost:8080/api/v1/events',
headers: {
'X-User-ID': 'user-1'
}
});

// send the request
raccoonClient
.send(jsonEvents)
.then((result) => {
logger.log('Result:', result);
})
.catch((error) => {
logger.error('Error:', error);
});
Loading

0 comments on commit 7546d5c

Please sign in to comment.