New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

exzact

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exzact

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-86.67%
Maintainers
1
Weekly downloads
 
Created
Source

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 :sunglasses:

License

MIT License © Adam King

FAQs

Package last updated on 18 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc