0.0.8 • Published 1 year ago

aardvark-js v0.0.8

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
1 year ago

aardvark-js

An isomorphic JavaScript client for Aardvark.

Usage

First of all, you need to install the library:

npm install aardvark-js

Or using Yarn...

yarn add aardvark-js

Then you're able to import the library and create an Aardvark client:

require style

const { createClient } = require("aardvark-js");

import style

import { createClient } from "aardvark-js";

UMD

You can now use plain <script> to import aardvark-js from CDNs, like:

<script src="https://cdn.jsdelivr.net/npm/aardvark-js"></script>

Getting Started

Secure Authentication

!IMPORTANT! Do NOT put your Aardvark Application Secret in frontend code. This is opaque and anyone with access to your application will be able to steal your credentials and authenticate as you.

Our recommended approach is to have an endpoint in a secure runtime (typically an API or serverless function) that authenticates with your application ID and application secret, and returns a token which can be securely used in your frontend to now make Aardvark requests.

Secure Runtime Endpoint (Node/Express API endpoint)

The below example code outlines how you could run a very simple Node API or serverless function which authenticates with Aardvark and returns the secure token to your frontend to initialize and securely interact with Aardvark.

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const port = 3030;
const { createClient } = require("aardvark-js");

const aardvark = createClient(
  "https://api.aardvark.sh",
  {
    applicationId: YOUR_APP_ID,
    applicationSecret: YOUR_APP_SECRET,
  },
  YOUR_COMMUNITY_ID
);

app.post("/aardvark", async (req, res) => {
  const response = await aardvark.authenticate({
    externalId: req.body.externalId,
  });
  res.status(200).send(response);
});

app.listen(port, () => {
  console.log(`Aardvark SDK example app listening on port ${port}`);
});

Frontend client initialization

import { createClient } from "aardvark-js";

// note: the url and port chosen match the secure runtime example above
const { data: authCredentials } = await axios.post(
  "http://localhost:3030/aardvark",
  {
    externalId: MY_USER_ID,
  }
);

const aardvark = createClient(
  "https://api.aardvark.sh",
  authCredentials,
  COMMUNITY_ID
);

Logging in

const aardvarkUser = await aardvark.initialize({
  externalId: MY_SYSTEMS_USERID, // could be a username, guid, etc
});

Listen for messages

Messages from both channels and DMs will trigger the function passed as an argument to the .listen((type, message)) function.

await aardvarkUser.listen((type, message) => {
  console.log(message);
});

Sample output might be

{
  "messageId": "99a74b26-c215-4acc-8a20-9d2e6c7e58fb",
  "conversationId": "b49c646d-6626-44b3-a2a3-69f8a0f0b30d",
  "message": "I'm a test message!",
  "datetime": "2023-01-11T13:15:30Z",
  "encrypted": false,
  "userId": "f2a29045-3076-44e5-8269-e116416d87e8",
  "senderUsername": "AardvarkAustin#0001",
  "senderProfilePicUri": "myuri.com/pfp.jpg",
  "channelId": "99a74b26-c215-4acc-8a20-9d2e6c7e58fb",
  "channelName": "General"
}

Connect a metamask wallet

await aardvarkUser.connectWallet(WalletProvider.METAMASK);

Sending a message to a username

const conversationId = await aardvarkUser.createConversation({
  parties: [
    {
      username: AARDVARK_USERNAME, // ex: 'KidnappedKitten#0001'
    },
  ],
});

await aardvarkUser.sendMessage(conversationId, {
  message: messageContent.value,
});

Sending a message to a wallet address

const conversationId = await aardvarkUser.createConversation({
  parties: [
    {
      walletPublicKey: "0xBfaFb822806E0551071BD86521641DE6fD50439C",
    },
  ],
});

await aardvarkUser.sendMessage(conversationId, {
  message: messageContent.value,
});

Get all channels

const channels = await aardvarkUser.getChannels();

Sample response

{
  "data": [
    {
      "id": "93e811c2-e2f3-4cd2-8e88-307caa96c915",
      "createdAt": "2023-02-02T17:31:48.861634+00:00",
      "name": "General",
      "description": "General chat for all users",
      "communityId": "d327d192-570a-44a0-8f87-833c382d27e9"
    },
    {
      "id": "0def97fa-9106-4b41-9259-e98578c4a591",
      "createdAt": "2023-02-02T17:40:54.431071+00:00",
      "name": "Trading",
      "description": "Trade, swap, barter, profit!",
      "communityId": "d327d192-570a-44a0-8f87-833c382d27e9"
    }
  ],
  "paging": {
    "offset": 0,
    "size": 25,
    "totalCount": 2
  }
}

Add user to channel

await aardvarkUser.addToChannel({ channelId: GENERAL_CHANNEL_ID });

Send message in channel

await aardvarkUser.sendChannelMessage(channelId, {
  message: "I'm a message in a channel!",
});