@sgrnext/api v0.1.5
@sgrnext/api
@sgrnext/api is a lightweight and developer-friendly library designed to simplify the creation of API handlers in Next.js applications.
See full documentation.
Motivaton
The @sgrnext/api library simplifies API development by abstracting common, repetitive logic involved in handler creation. It provides tools to encapsulate tasks such as request parsing, validation, and the collection of web method statistics, enabling developers to concentrate on core business logic while ensuring a clean, efficient, and maintainable codebase.
The plugin system in @sgrnext/api offers flexible customization of the handler environment, enabling developers to tailor it to their needs. Additionally, it supports the integration of project-specific functionality, enhancing adaptability across diverse use cases.
Getting started
Install
npm install --save @sgrnext/api
Crate handlers factory
// src/server/handlers.ts
import { HandlerFactory } from '@sgrnext/api';
import { parse } from "@sgrnext/api-parse";
export const handlers = new HandlerFactory()
// apply plugins
.plugin({ pool: myDatabaseConnection })
.plugin(parse())
...
;
Create route
// src/app/api/books/route.ts
import { compileSchema } from '@sgrnext/api-parse';
import { z } from "zod";
import { handlers } from '@/server/handlers';
export POST = handlers.create(async (env) => {
const { title, price } = await env.parse(SCHEMA, ZOD_SCHEMA)
const { id } = await env.pool.execute('...', [ title, price ]);
return { id };
});
const SCHEMA = compileSchema({ title: 'body', price: 'body' });
const ZOD_SCHEMA = z.object({ title: z.string(), price: z.number() });