
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
next-better-api
Advanced tools
Opinionated helpers for building NextJS APIs, powered by Zod.
import { z } from 'zod';
import { endpoint, asHandler } from 'next-better-api';
const getUsers = endpoint(
{
method: 'get',
responseSchema: z.object({
users: z.array(
z.object({
id: z.string(),
name: z.string(),
email: z.string(),
active: z.boolean(),
})
),
}),
},
async () => {
const users = await getAllUsers();
return {
status: 200,
body: {
users,
},
};
}
);
export asHandler([getUsers]);
next-better-api
requires zod
for schema validation. You can install both libraries with yarn or npm on your existing NextJS project:
$ yarn add zod next-better-api
// OR
$ npm i -S zod next-better-api
And import it from your API definitions:
import { endpoint, asHandler } from 'next-better-api';
// import { z } from 'zod'; // If you are defining schemas for your endpoints
Remember you always need to use asHandler
to convert your next-better-api
endpoints to NextJS-compatible handlers:
export default asHandler([getUsers, createUser, updateUser]);
const getUsers = endpoint({
method: 'get',
// ...
});
const deleteUser = endpoint({
method: 'delete',
// ...
});
See Zod for all available schema validation options.
const createUser = endpoint(
{
method: 'post',
bodySchema: z.object({
// Name must be at least 3 characters long
name: z.string().min(3),
}),
responseSchema: z.object({
userId: z.string(),
}),
},
async ({ req }) => {
// Type for `name` is automatically inferred based on `bodySchema`
const { name } = req.body;
const newUser = await createUser(req.body);
return {
status: 201,
// Response body is also annotated based on `responseSchema`
body: {
userId: newUser.id,
},
};
}
);
Different utility types are available for handling endpoint type information.
import type { InferEndpointType } from 'next-better-api';
import type { getUsers } from '../api/users.ts';
type GetUsersResponseBody = InferEndpointType<typeof getUsers>['Response'];
// Can be combined with API calls, react-query, etc to provide end-to-end type annotation from the endpoint definition:
const response = await fetch('/api/users' /* ... */);
const newUser = (await response.json()) as GetUsersResponseBody;
newUser.userId; // #=> string
Additional helpers are available:
import type {
InferEndpointType,
InferQueryType,
InferResponseBodyType,
InferRequestBodyType,
} from 'next-better-api';
See license information under LICENSE.md
.
Contributions are super welcome - in the form of bug reports, suggestions, or better yet, pull requests!
FAQs
Utilities for safer, easier APIs with NextJS
The npm package next-better-api receives a total of 3 weekly downloads. As such, next-better-api popularity was classified as not popular.
We found that next-better-api 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.