0.0.5 • Published 2 years ago

@cua.run/babel v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@cua.run/babel

cua is your favorite async tasks handler to offload your JavaScript API.

@cua.run/babel enable you to seamlessly call cua functions as follows:

import type { NextApiRequest, NextApiResponse } from "next";
import importContacts from "../../cua-functions/importContacts";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const user = { /* ... */ }
  await importContacts([user]); // will be replaced by `cua.push('importContacts', [user])` by our babel plugin
  res.status(200).json({ name: "John Doe" });
}

Install

yarn add -D @cua.run/babel

# or

npm i --save-dev @cua.run/babel

Configuration

Next.js

Create a babelrc.js configuration as follows:

const babelPlugin = require("@cua.run/babel");
module.exports = {
  presets: ["next/babel"],
  plugins: [[babelPlugin]],
};

Write your first cua function

All cua functions must be in a root cua-functions/ folder:

example for a Next.js project:

- pages/
  - _app.ts
  - index.ts
  - api/
    - import.ts
- cua.functions/
  - importContacts.ts
- .babelrc.js
- next.config.js
- package.json

Then, your cua function should be a file having:

  • a function with CuaFunction signature
  • a default export

cua.functions/importContacts.ts

import { CuaFunction } from "@cua.run/client";

interface Contact {
  id: string
  name: string
}

const importContacts: CuaFunction = (contact: Contact[]) => {
  // ...
  // returns a Promise
}

export default importContacts

Then, your cua function can be invoked as follows:

pages/api/import.ts

import type { NextApiRequest, NextApiResponse } from "next";
import importContacts from "../../cua-functions/importContacts";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const user = { /* ... */ }
  // will delay the execution of the function in the background queue
  await importContacts([user]); 
  res.status(200).json({ ok: true });
}