0.0.2 • Published 2 years ago

vite-plugin-dev-endpoint v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

npm version license npm.io


Integrate Mock API Capacity with Vite

Install

# npm
npm i -D vite-plugin-dev-endpoint

# yarn
yarn add -D vite-plugin-dev-endpoint

# pnpm
pnpm i -D vite-plugin-dev-endpoint

Usage

import { defineConfig } from "vite";
import devEndpoint from "vite-plugin-dev-endpoint";
import fakeResponse from "./assets/fake-response.json";

export default defineConfig({
  server: {
    port: 10880,
  },
  plugins: [
    devEndpoint({
      "/jsonAPI": {
        type: "json",
        data: fakeResponse,
      },
      "/textAPI": {
        type: "text",
        data: '123'
      },
      "mp3": {
        type: "file",
        path: "/home/user/assets/listen.mp3"
      }
    }),
  ],
});

The All Config Like below:

export type Config =(
  | {
    type: 'text';
    data: string;
  }
  | {
      type: "json"
      data: object;
    }
  | {
      type: "file";
      path: string;
    }
  | {
      type: "error";
      statusCode: number;
    }
  | {
    type: 'custom';
    handler: NextHandleFunction | HandleFunction;
  }) & {
    // By default, vite-plugin-dev-endpoint will provide a suitable content-type
    // in response header according to `type` or file extension name.
    // If you want to appoint a special content-type, you can set it.
    contentType?: string; 
  }

export type Endpoint = {
  [path: string]: Config;
};