npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eai-nodejs

v0.0.4

Published

A reusable library for RabbitMQ message processing, API adapters, and data transformation.

Downloads

12

Readme

EAI-NODEJS SDK Documentation

Overview

This SDK provides a RabbitMQ message processor, API adapter for fetching data with authentication, and a transformer for applying dynamic transformations to data using JSONata expressions.

It enables easy integration with external APIs, processes RabbitMQ messages, and transforms data dynamically.


Table of Contents

  1. Modules
  2. Installation
  3. API Reference

Modules

1. Adapter Module (/lib/adapter/apiAdapter.js)

Handles authentication and data fetching from an API.

2. Queue Processor (/lib/queue/processMessage.js)

Processes the RabbitMQ messages by fetching data, transforming it, and further processing it.

3. RabbitMQ Listener (/lib/queue/rabbitmqListener.js)

Listens to RabbitMQ queues and triggers the message processing.

4. Data Transformer (/lib/transformer/dataTransformer.js)

Transforms data using dynamic JSONata expressions.


Installation

To install this package, run:

npm install eai-sdk

API Reference

getAuthToken(apiUrl, apiKey)

Fetches an authentication token from the specified API URL using the provided API key.

Parameters:

  • apiUrl: String - The URL of the API to authenticate against.
  • apiKey: String - The API key used for authentication.

Returns:

  • Object - The response object containing the token.

Example:

const token = await getAuthToken('https://api.example.com/auth', 'your-api-key');

fetchData(apiUrl, token)

Fetches data from the specified API URL using the provided token.

Parameters:

  • apiUrl: String - The URL of the API to fetch data from.
  • token: String - The authentication token.

Returns:

  • Object - The data fetched from the API.

Example:

const data = await fetchData('https://api.example.com/data', token);

transformData(data, expression)

Transforms data using a JSONata expression.

Parameters:

  • data: Object - The data to be transformed.
  • expression: String - A JSONata expression used to transform the data.

Returns:

  • Object - The transformed data.

Example:

const transformedData = transformData(data, '$.name');

processMessage(message)

Processes a RabbitMQ message by fetching data using the API adapter, transforming it, and logging the transformed data.

Parameters:

  • message: Object - The message received from RabbitMQ containing the token, apiUrl, and transformationExpression.

Example:

const message = {
  token: 'your-token-key',
  apiUrl: 'https://api.example.com/data',
  transformationExpression: '$.name'
};
await processMessage(message);

startListener(rabbitMQUrl, queueName, handleMessage)

Starts a RabbitMQ listener that processes messages from the specified queue.

Parameters:

  • rabbitMQUrl: String - The RabbitMQ server URL.
  • queueName: String - The name of the queue to listen to.
  • handleMessage: Function - A callback function to process each message.

Example:

startListener('amqp://localhost', 'my_queue', processMessage);

sendMessage(msg, config)

Sends a message to a specified RabbitMQ queue.

Parameters:

  • msg: Object - The message to send.
  • config: Object - Configuration object containing url (RabbitMQ server URL) and outputQueue (the queue name).

Returns:

  • Promise - Resolves when the message is successfully sent.

Example:

const config = {
  url: 'amqp://localhost',
  outputQueue: 'output_queue'
};

const message = { key: 'value' };

await sendMessage(message, config);

Error Handling

Each module includes basic error handling, ensuring that errors encountered during API requests, message processing, or message sending are caught and logged.

Example:

try {
    const token = await getAuthToken(apiUrl, apiKey);
} catch (error) {
    console.error('Failed to fetch auth token:', error);
}

This documentation outlines how to use each component of the library, with examples for setting up RabbitMQ listeners, fetching API data, transforming it, and pushing messages back to RabbitMQ.