GithubHelp home page GithubHelp logo

gomah / spera Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 4.92 MB

Run & schedule your code in the background with a fully typed client

License: MIT License

JavaScript 8.31% TypeScript 91.69%
nextjs background-jobs qstash background redis serverless queue spera

spera's Introduction

Spera

Run & schedule your code in the background with a fully typed client.

Preview

⚠️ Very early project – I'm currently using a similar version of this code in production but I don't think you should 🤷

How it works ?

Spera is (for now), just a small typed client for the code you want to schedule in the background, it uses QStash and runs locally when running NODE_ENV is set to development.

All you need is to pass a functions object (key being the name of your event and the function to run as the value), e.g:

const functions = {
  'app/account.created': accountCreated.Handler
}
import qStashProvider from '@spera/plugin-upstash';
import { Spera } from '@spera/core';
import * as accountCreated from './account.created';

export const functions = {
  'app/account.created': accountCreated.handler,
};

export const spera = new Spera({
  url: `${getBaseUrl()}/api/spera`,
  functions,
  provider: qStashProvider({ token: process.env.QSTASH_TOKEN as string }),
});

I plan to support different providers, frameworks & improve the project – there's a fair bit of boilerplate for now.

Quickstart (With Next.js)

Install dependencies

yarn add @spera/core @spera/nextjs @spera/plugin-upstash

Notes:

This first version depends on QStash as a provider, please make sure you have the following environment variables setup for your project:

  • QSTASH_URL
  • QSTASH_TOKEN
  • QSTASH_CURRENT_SIGNING_KEY
  • QSTASH_NEXT_SIGNING_KEY

Define your "functions" / "jobs" you want to run in the background

See apps/next/.spera as an example.

.
├── .spera/                     # Your folder containing your functions to run in the background
│   ├── account.created.ts
│   └── index.ts
├── pages/
│   ├── api/                    # Next.js API folder
│   │   ├── spera.ts            # The Spera API handler
└── ...
// .spera/index.ts
import * as accountCreated from './account.created';
import { Spera } from '@spera/core';
import qStashProvider from '@spera/plugin-upstash';

export const functions = {
  [accountCreated.name]: accountCreated.handler,
};

export const getBaseUrl = () => {
  if (typeof window !== 'undefined') return ''; // browser should use relative url
  if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // SSR should use vercel url
  return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost
};

export const spera = new Spera({
  url: `${getBaseUrl()}/api/spera`,
  functions,
  provider: qStashProvider({ token: process.env.QSTASH_TOKEN as string }),
});
// .spera/account.created.ts
export const name = 'app/account.created';

export interface AccountCreatedPayload {
  id: string;
}

export async function handler(payload: AccountCreatedPayload) {
  const { id } = payload;
  console.info(`Account created: ${id}`);
  return id;
}
// pages/api/spera.ts
import { withSpera } from '@spera/nextjs';
import { verifySignature } from '@spera/plugin-upstash/nextjs';
import type { NextApiRequest, NextApiResponse } from 'next';
import { spera } from '../../.spera';

export const config = {
  api: {
    bodyParser: false,
  },
};

async function handler(req: NextApiRequest, res: NextApiResponse) {
  // This should be displayed after the background function is processed :)
  console.log('Hey there!');
  return res.status(200).end();
}

export default withSpera(handler, spera, verifySignature);

Project "roadmap"

  • Abstract QStash as a "Provider" plugin.
  • Next.js helpers (Spera to extract "use" hooks to verify signatures based on X provider)
  • Dynamic Next.js helpers (based on provider)
  • Add zod
  • Client API Design
  • Cloudflare Queues as a "Provider" plugin.
  • Docs
  • Cleanup code / repo
  • Framework agnostic

spera's People

Contributors

github-actions[bot] avatar gomah avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.