Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
vite-plugin-api
Advanced tools
A Vite.js plugin that creates API routes by mapping the directory structure, similar to Next.js API Routes. This plugin enhances the functionality for backend development using Vite.
Project Renamed: New Repository - vite-plugin-api-routes
Please note that this project has been renamed to "vite-plugin-api-routes" and has been moved to a new repository. The latest version and updates can now be found in the vite-plugin-api-routes repository. Kindly update your references and bookmarks accordingly.
Important: This repository (vite-plugin-api) is no longer actively maintained, and all future development will take place in the vite-plugin-api-routes repository.
Enhance API routing in ViteJS based on directory structure for improved visibility and project structure in Node.js and Express.
See the tutorial
> tree src/api/
src/api/:
├───admin
│ ├───auth
│ │ ├───login.js
│ │ └───status.js
│ └───user
│ ├───index.js
│ └───[userId] //Remix Format
│ ├───index.js
│ └───detail.js
├───site
│ ├───article
│ │ ├───$articleId.js //NextJS Format
│ │ └───new.js
│ └───page
│ ├───$pageId.js
│ └───new.js
└───index.js
The directory tree is exported as router rules tree:
GET /api/site/
GET /api/routers
USE /api/admin/user
GET /api/admin/user
GET /api/admin/user/
POST /api/admin/user/
GET /api/admin/auth/login
POST /api/admin/auth/login
GET /api/site/article/new
GET /api/admin/auth/status
POST /api/admin/auth/status
GET /api/site/page/:pageId
GET /api/admin/user/:userId/
PUT /api/admin/user/:userId/
DELETE /api/admin/user/:userId/
GET /api/site/article/:articleId
GET /api/admin/user/:userId/detail
For example, the src/api/admin/user/$userId.js
file exports allowed request methods:
//file:src/api/admin/user/$userId.js
export const DELETE = (req, res, next) => {
res.send("DELETE REQUEST");
};
export const PUT = async (req, res, next) => {
res.send("PUT REQUEST");
};
// Support default, GET, HEAD, POST, PUT, DELETE by default
// For CONNECT, OPTIONS, TRACE, PATCH, and others, you need to add the mapping to the mapper attribute config
// If you need middlewares for a route, simply export an array containing all middlewares as the default
export default [authMiddleware, secondMiddleware /* ... */];
Similarly, the [userId].js
or $userId.js
file name is exported as a request parameter /user/:userId
, following the Next.js/Remix framework.
yarn add vite-plugin-api
In vite.config.ts
:
import { defineConfig } from "vite";
import { pluginAPI } from "vite-plugin-api";
export default defineConfig({
plugins: [
pluginAPI({
// moduleId: "@api", // Old version change to "virtual:vite-plugin-api",
// cacheDir: ".api",
// server: "[cacheDir]/server.js",
// handler: "[cacheDir]/handler.js",
// routeBase: "api",
// dirs: [{ dir: "src/api"; route: "", exclude?: ["*.txt", ".csv", "data/*.*"] }],
// include: ["**/*.js", "**/*.ts"],
// exclude: ["node_modules", ".git"],
// mapper: { default: "use", GET: "get", ... },
}),
],
});
Default Value
mapper: {
//[Export Name]: [Http Verbose]
default: "use",
GET: "get",
POST: "post",
PUT: "put",
PATCH: "patch",
DELETE: "delete",
// Overwrite
...mapper,
};
/vite.config.js
export default defineConfig({
plugins: [
createAPI({
mapper: {
/**
* export const PING = ()=>{...}
* Will be mapped to express method
* app.get('/path/dir', PING)
*/
PING: "get",
/**
* export const OTHER_POST = ()=>{...}
* Will be mapped to posible method
* app.post2('/path/dir', OTHER_POST)
*/
OTHER_POST: "post2",
/**
* export const PATCH = ()=>{...}
* Will not be mapped
*/
PATCH: false,
},
}),
],
});
You can disable a method by setting its value to false. In the example PATCH: false
, the PATCH method is disabled.
/src/api/index.js
export PING = (req, res, next)=>{
res.send({name:"Ping Service"});
}
export OTHER_POST = (req, res, next)=>{
res.send({name:"Ping Service"});
}
export PATCH = (req, res, next)=>{
res.send({name:"Ping Service"});
}
/src/handler.js or see handler.js
// @ts-nocheck
import express from "express";
import { applyRouters } from "@api/routers"; // Notice '@api', this is the moduleId!
export const handler = express();
// Add JSON-Parsing
handler.use(express.json());
handler.use(express.urlencoded({ extended: true }));
applyRouters((props) => {
const { method, route, path, cb } = props;
if (handler[method]) {
if (Array.isArray(cb)) {
handler[method](route, ...cb);
} else {
handler[method](route, cb);
}
} else {
console.log("Not Support", method, "for", route, "in", handler);
}
});
/src/server.ts or see server.ts
// @ts-ignore
import { handler } from "@api/handler"; // Notice '@api', this is the moduleId!
// @ts-ignore
import { endpoints } from "@api/routers"; // Notice '@api', this is the moduleId!
import express from "express";
const { PORT = 3000, PUBLIC_DIR = "import.meta.env.PUBLIC_DIR" } = process.env;
const server = express();
server.use(express.json());
server.use("import.meta.env.BASE", express.static(PUBLIC_DIR));
server.use("import.meta.env.BASE_API", handler);
server.listen(PORT, () => {
console.log(`Ready at http://localhost:${PORT}`);
console.log(endpoints);
});
mapper
attribute to support custom HTTP methods using a header attribute.FAQs
A Vite.js plugin that creates API routes by mapping the directory structure, similar to Next.js API Routes. This plugin enhances the functionality for backend development using Vite.
The npm package vite-plugin-api receives a total of 0 weekly downloads. As such, vite-plugin-api popularity was classified as not popular.
We found that vite-plugin-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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.