Skip to content

โšก๏ธ hyper-connect โšก๏ธ

Test

hyper-connect is an HTTP client for The hyper Service Framework with an intuitive API, built on top of fetch. With hyper-connect, you can access all of the hyper Services on your HTTP-based hyper Server.

Install โ€‹

NodeJS โ€‹

sh
npm install hyper-connect

hyper-connect constructs a Request Object and sends it to the hyper server using fetch. hyper-connect wraps your hyper app's REST API, generating short-lived JWTs using the provided connection string.

Getting Started โ€‹

You will need to construct a Connection String to your hyper Server. It is common to set this as the HYPER environment variable in your application:

js
import { connect } from 'hyper-connect'

/**
 * Your connection string informs hyper-connect where your hyper Server is located,
 * which hyper Domain to interact with, and how to authenticate with your hyper Server
 */
process.env.HYPER = `https://<sub>:<secret>@<host>/<domain>`
const HYPER = process.env['HYPER'] as string

/**
 * hyper-connect will automatically generate a short-lived JWT, whose sub is "user"
 * and is signed using "password" and the HS256 algorithm.
 */
const { data, cache, storage, queue, search } = connect(HYPER)

NodeJS (TypeScript) โ€‹

ts
import { connect } from "hyper-connect";

const HYPER = process.env["HYPER"] as string;

const { data, cache, storage, queue, search } = connect(HYPER);

await data.add({ id: "game-1", type: "game", name: "Donkey Kong" });
await data.add({ id: "game-2", type: "game", name: "Pac Man" });
await data.add({ id: "game-3", type: "game", name: "Galaga" });

const results = await data.query({ type: "game" });

NodeJS (ESM) โ€‹

js
import { connect } from "hyper-connect";

const HYPER = process.env["HYPER"];

const { data, cache, storage, queue, search } = connect(HYPER);

await data.add({ id: "game-1", type: "game", name: "Donkey Kong" });
await data.add({ id: "game-2", type: "game", name: "Pac Man" });
await data.add({ id: "game-3", type: "game", name: "Galaga" });

const results = await data.query({ type: "game" });

NodeJS (CJS) โ€‹

js
const { connect } = require("hyper-connect");

const HYPER = process.env["HYPER"];

const { data, cache, storage, queue, search } = connect(HYPER);

await data.add({ id: "game-1", type: "game", name: "Donkey Kong" });
await data.add({ id: "game-2", type: "game", name: "Pac Man" });
await data.add({ id: "game-3", type: "game", name: "Galaga" });

const results = await data.query({ type: "game" });

Node 18 and localhost โ€‹

Starting with Node 17, Node has changed how it resolves localhost, when using global fetch and fetch from libraries like undici. This may cause requests to localhost not to resolve correctly and fail. To get around this, you can use 127.0.0.1 or 0.0.0.0, in lieu of localhost. For more info, See this issue

Deno โ€‹

js
import { connect } from "https://x.nest.land/hyper-connect@VERSION/deno/mod.ts";

const HYPER = Deno.env.get("HYPER");

const { data, cache, storage, queue, search } = connect(HYPER);

await data.add({ id: "game-1", type: "game", name: "Donkey Kong" });
await data.add({ id: "game-2", type: "game", name: "Pac Man" });
await data.add({ id: "game-3", type: "game", name: "Galaga" });

const results = await data.query({ type: "game" });

Examples โ€‹

How to add a document to hyper Data? โ€‹

js
const doc = {
  id: "movie-1",
  type: "movie",
  title: "Dune",
  year: "2021",
};

const result = await data.add(doc);
console.log(result); // {ok: true, id: "movie-1"}

How to get all the documents of type 'movie'? โ€‹

js
const result = await data.query({ type: "movie" });
console.log(result); // {ok: true, docs: [...]}

How to add a Cache key/value pair to hyper cache? โ€‹

js
const result = await cache.add("key", { counter: 1 });
console.log(result); // {ok: true}

Documentation โ€‹

hyper is a suite of service apis, with hyper connect you can specify the api you want to connect with and the action you want to perform. [service].[action] - with each service there are a different set of actions to call. This table breaks down the service and action with description of the action.

data โ€‹

ServiceActionDescription
datacreatecreate a hyper Data Service
datadestroydestroy a hyper Data Service
dataaddcreates a json document in a hyper Data Service
datalistlists the documents given a start, stop, limit range from a hyper Data Service
datagetretrieves a document by id from a hyper Data Service
dataupdateupdates a given document by id from a hyper Data Service
dataremoveremoves a document from a hyper Data Service
dataqueryqueries for a set of documents based on selector criteria from a hyper Data Service
dataindexcreates an index in a hyper Data Service
databulkinserts, updates, and removes documents from a hyper Data Service

cache โ€‹

ServiceActionDescription
cachecreatecreate a hyper Cache Service
cachedestroydestroy a hyper Cache Service
cacheaddcreates a json document at the key in the hyper Cache Service
cachegetretrieves a document by key from a hyper Cache Service
cachesetsets a given document by key from a hyper Cache Service
cacheremoveremoves a document by key from a hyper Cache Service
cachequeryqueries for a set of documents based on a pattern matcher from a hyper Cache Service
ServiceActionDescription
searchcreatecreate a hyper Search Service
searchdestroydestroy a hyper Search Service
searchaddindexes a json document in a hyper Search Service
searchgetretrieves a document from a hyper Search Service
searchremoveremoves a document from the a hyper Search Service
searchqueryfuzzy searches a hyper Search Service
searchloadloads a batch of documents into a hyper Search Service

storage โ€‹

ServiceActionDescription
storagecreatecreate a hyper Storage Service
storagedestroydestroy a hyper Storage Service
storageuploadadds object/file to a hyper Storage Service
storagedownloadretrieves a object/file from a hyper Storage Service
storageremoveremoves a object/file from a hyper Storage Service

queue โ€‹

ServiceActionDescription
queuecreateWIP create a hyper Queue
queuedestroyWIP destroy a hyper Queue
queueenqueueposts object to a hyper Queue
queueerrorsgets list of error jobs that occurred on a hyper Queue
queuequeuedgets list of objects that are queued and ready to be sent

Verify hyper Queue Service Request Signatures โ€‹

A hyper Queue Service targets your specified web hook worker to receive jobs. In order to secure that endpoint to only receive jobs from a hyper Queue service, you can provide a secret when creating the hyper Queue. This Queue will then sign requests it sends to your webhook worker using a sha256 nounce timestamp and a signature in the X-HYPER-SIGNATURE header.

createHyperVerify on hyper-connect makes it easier to implement your own middleware to validate these incoming jobs, in a secure way.

js
import { createHyperVerify } from 'hyper-connect'

const signatureVerify = createHyperVerify(process.env.QUEUE_SECRET, '1m')

export const validateSignature(req, res, next) {
  const result = signatureVerify(req.headers.get('x-hyper-signature'), req.body))
  if (!result.ok) {
    return res.setStatus(result.status).send({msg: result.msg})
  }
  next()
}