Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
⚡︎ Simple, expressive middleware for NextJS Server Actions using Zact.
$ pnpm install exzact
Create simple middleware for zact functions:
import { z } from "zod";
import { zapp, Middleware } from "exzact";
const app = zapp();
const logMiddleware: Middleware = async (context, next) => {
console.log("Log Middleware (Pre): ", context.input);
await next();
console.log("Log Middleware (Post): ", context.input);
};
const hello = app.zact(z.object({ stuff: z.string().min(1) }))(
async ({ stuff }: { stuff: string }) => {
console.log(`Hello ${stuff}`);
}
);
app.use(logMiddleware);
hello({ stuff: "world" });
In the above example, the logging middleware will run before the action has executed.
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 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);
Install the necessary libraries.
$ npm install
Run a complete example of the middleware suite.
$ npm run example:complete
git checkout -b my-new-feature
git add .
git commit -am 'Add some feature'
git push origin my-new-feature
MIT License © Andrea SonnY
FAQs
Unknown package
The npm package exzact receives a total of 2 weekly downloads. As such, exzact popularity was classified as not popular.
We found that exzact demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.