Skip to content

Configure custom api(actions and retruns) for the vite dev server

License

Notifications You must be signed in to change notification settings

azhi09/vite-plugin-dev-server-api

Repository files navigation

vite-plugin-dev-server-api

Configure custom api(action and retrun) for vite dev server

Install

pnpm add vite-plugin-dev-server-api -D

Use

// vite.config.ts
import devServerApi from "vite-plugin-dev-server-api";

export default {
  // ...

  plugins: [
    // ...,
    devServerApi(options?: {}),
  ],

  // ...
};
// api/index.ts
import fs from "fs/promise";

export default {
  "/dev/server/api/json/data": async function () {
    // You can call all the node.js capabilities here
    let ctn = await fs.readFile("../json/data.json", { encoding: "utf-8" });
    return ctn;
  },
};
axios.get("/dev/server/api/json/data").then((res) => {
  console.log(res); // json/data.json content
});

Features

  • Auto import api form options.dir(default: api).
  • Support .js|.cjs|.mjs|.ts|json file in options.dir. (with default export, eg: export default/module.exports)
  • Full TypeScript support.
  • duplicate addresses prompt

About

Configure custom api(actions and retruns) for the vite dev server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published