/resonate-sdk-ts

a dead simple programming model for modern applications

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Resonate is in the Design Phase

Our code base is constantly evolving as we are exploring Resonate's programming model. If you are passionate about a dead simple developer experience, join us on this journey of discovery and share your thoughts.

Join our slack



Resonate TypeScript SDK

cicd License

Docs   •   Twitter   •   Slack   •   Issues   •   Roadmap

An SDK for writing simple and elegant durable async await applications.

Why Resonate?

Resonate offers a programming model that allows you to build distributed applications using an intuitive paradigm you already know — async await.

What is Durable Async Await?

Durable Async Await are Functions and Promises that maintain progress in durable storage.

Features

Available now:

  • durable async await
  • volatile async await
  • retries
  • tracing
  • logging

Coming soon:

  • rate limiting
  • metrics

Let us know features you would like Resonate to support.

Install

npm install @resonatehq/sdk

Getting Started

import express from "express";
import { Resonate, Context } from "@resonatehq/sdk";

/* Your async code using Resonate's Context */
type User = {
  id: number;
  name: string;
};

type Song = {
  id: number;
  title: string;
};

// Purchase song event handler
async function purchase(ctx: Context, user: User, song: Song): Promise<{ charge: any; access: any }> {
  const charge = await ctx.run(chargeCreditCard, user, song);
  const access = await ctx.run(unlockUserAccess, user, song);
  return { charge, access };
}

async function chargeCreditCard(ctx: Context, user: User, song: Song): Promise<any> {
  console.log("Charging credit card...");
  return { status: "charged" };
}

async function unlockUserAccess(ctx: Context, user: User, song: Song): Promise<any> {
  console.log("Unlocking user access...");
  return { status: "unlocked" };
}

/* Express Application w/ Resonate Event Handler */

// Create Resonate instance
const resonate = new Resonate();

// Register purchase handler
resonate.register("durablePurchase", purchase);

// Initialize Express app with purchase route
const app = express();

app.post("/purchase", async (req, res) => {
  
  // Dummy user and song data
  const user = { id: 1, name: "John" };
  const song = { id: 1, title: "Song 1" };

  // Create unique ID for purchase execution. This is used to track the execution. 
  // Typically, this would be an external ID from your incoming request.
  const purchaseId = `purchase-${user.id}-${song.id}`

  // Execute durable purchase
  try {
    const result = await resonate.run("durablePurchase", purchaseId, user, song);
    res.send(result);
  } catch (err) {
    res.status(500).send("Unable to purchase song");
  }
});

app.listen(3000, () => {
  console.log("App listening on port 3000");
});

Once the server is running, invoke the purchase endpoint.

curl -X POST localhost:3000/purchase

See our docs for more detailed information.

Development

npm install
npm run lint
npm test

Contributing

See our contribution guidelines.

License

The Resonate TypeScript SDK is available under the Apache 2.0 License.