exzact
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

npm version code style: prettier

Exzact

⚡︎ Simple, expressive middleware for NextJS Server Actions using Zact.

Installation

$ pnpm install exzact

Usage

Create simple middleware for zact functions:

import { z } from "zod"
import { exzact, Middleware } from "exzact"

interface Context {
  thing?: string
  something?: string
}

const app = exzact<Context>({
  thing: "things",
})

const logMiddleware: Middleware = async (context, next) => {
  console.log("Log Middleware (Pre): ", context.input)
  await next()
  console.log("Log Middleware (Post): ", context.input)
}

export const hello = app.zact(z.object({ stuff: z.string().min(1) }), {
  something: "stuff",
})(async ({ stuff }, { thing, something }) => {
  console.log(`Hello ${stuff}, you injected ${thing} and ${something}`)
})

app.use(logMiddleware)

hello({ stuff: "world" })

In the above example, the logging middleware will run before the action has executed.

The simple example above will log the following:

Log Middleware (Pre):  { input: { stuff: 'world' }, thing: 'things', something: 'stuff' }
Log Middleware (Post):  { input: { stuff: 'world' }, thing: 'things', something: 'stuff' }
Hello world, you injected things and stuff

Middleware Stages

Middleware can run during any of the following stages:

  • PRE_VALIDATE: Before the Zact function Zod validation runs.
  • PRE_EXECUTE: Before the validated Zact function runs.
  • POST_EXECUTE: After the validated Zact runction runs.

Middleware defaults to PRE_EXECUTE if no stage is manually set.

import { MiddlewareStage } from "exzact";

...

logMiddleware.stage = MiddlewareStage.POST_EXECUTE;

...

app.use(logMiddleware);

hello({ stuff: "world" });

In the above example, the logging middleware will run after the action has executed.

Rate Limiting

Rate-limiting can easily be added to Zact functions via middleware:

import { Ratelimit } from "@upstash/ratelimit"
import { Redis } from "@upstash/redis"
import { Middleware } from "zact"

/* Requires setting the following environment variables:
    * - UPSTASH_REDIS_REST_URL
    * - UPSTASH_REDIS_REST_TOKEN
    
    You can find these in your Upstash console.
*/

const redis = Redis.fromEnv()

// Create a new ratelimiter, that allows 5 requests per 30 seconds
const ratelimit = new Ratelimit({
  redis,
  limiter: Ratelimit.fixedWindow(5, "30 s"),
})

const upstashMiddleware: Middleware = async (_, next) => {
  const { success } = await ratelimit.limit("all")

  if (!success) {
    throw new Error("Unable to process at this time")
  }

  await next()
}

app.use(upstashMiddleware)

For more examples, check out the Code Examples.

Development

Install the necessary libraries.

$ npm install

Run a simple example.

$ npm run example:simple

Run a complete example of the middleware suite (requires Upstash configuration).

$ npm run example:complete

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Add your changes: git add .
  4. Commit your changes: git commit -am 'Add some feature'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request 😎

License

MIT License © Adam King

Readme

Keywords

none

Package Sidebar

Install

npm i exzact

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

100 kB

Total Files

23

Last publish

Collaborators

  • adamjking3