Gathering detailed insights and metrics for mihawk
Gathering detailed insights and metrics for mihawk
Gathering detailed insights and metrics for mihawk
Gathering detailed insights and metrics for mihawk
Dracula-Mihawk : A tiny async api mock server tool, without render template
npm install mihawk
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (94.53%)
JavaScript (3.01%)
HTML (1.88%)
Shell (0.57%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2 Stars
307 Commits
1 Watchers
2 Branches
1 Contributors
Updated on May 10, 2025
Latest Version
0.1.0-j
Package Id
mihawk@0.1.0-j
Unpacked Size
397.95 kB
Size
99.88 kB
File Count
135
NPM Version
9.5.1
Node Version
18.16.0
Published on
May 10, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
31
23
🇨🇳 中文版说明 → README.zh-CN.md
Recommend: use version@
v1.0.0
+
Make a easy mock-server to mock api, with GET /a/b/c
→ ./mocks/data/GET/a/b/c.json
mapping
GET
, POST
, PUT
, DELETE
etc.json
| json5
middleware.{js|cjs|ts}
, write as koa2 middleware
(or express-middleware
both ok with func.isExpress=true
)routes.json
, mapping multiple request to same resolve file。 routes key align glob
expressionjs
| cjs
| ts
socket
simulationsmihawk/tools
, eg: createRandPhone
、createRandEmail
1npm i -g mihawk
1mihawk --port=8888 2# mihawk -p 8888
then open browser and visit
http://localhost:8888
mock data directory:
./mocks/data
1./mocks 2 │ 3 ├── /data 4 │ │ 5 │ ├── DELETE 6 │ │ ├──/*.js DELETE request resolve logic 7 │ │ └──/*.json DELETE request resolve data 8 │ │ 9 │ ├── GET 10 │ │ ├──/*.js GET request resolve logic 11 │ │ └──/*.json GET request resolve data 12 │ │ 13 │ ├── POST 14 │ │ ├──/*.js POST request resolve logic 15 │ │ └──/*.json POST request resolve data 16 │ │ 17 │ └── PUT 18 │ ├──/*.js PUT request resolve logic 19 │ └──/*.json PUt request resolve data 20 │ 21 ├── middleware.js [optional] resolve middleware 22 │ 23 └── routes.json [optional] common routes
mapping:
1request : GET http://localhost:8888/a/b/c/d 2JSON-file : data/get/a/b/c/d.json 3mock-file : data/get/a/b/c/d.js
request
: mock request urlJSON-file
: mock origin datamock-file
: resolve mock logic, base on origin dataFinally, the return data will be the data after processing mock-file (the mock-file
) with origin data (the JSON-file
)
A more recommended way to use it is to write all config props into the
.mihawkrc.json
in the root directoryAnd then run
mihawk
in you shell
.mihawkrc.json
1mihawk init
then edit the
.mihawkrc.json
to customize your config
1{ 2 "host": "0.0.0.0", 3 "port": 8888, 4 "https": false, 5 "cors": true, 6 "cache": true, 7 "watch": true, 8 "mockDir": "mocks", 9 "mockDataFileType": "json", 10 "mockLogicFileType": "none" 11}
About root config props:
host
: string, default 0.0.0.0
, server listen on this hostport
: number, default 8888
, server listen on this porthttps
: boolean, default false
, if true
, will use https protocolcors
: boolean, default true
, if true
, will add Access-Control-Allow-Origin: *
(and other necessary cors props in headers ) to the response headerscache
: boolean, default true
, if true
, will cache the mock data and return the cached data when the request is the samewatch
: boolean, default true
, if true
, will watch the mock data directory and reload when changedmockDir
: string, default mocks
, the directory of mock datamockDataFileType
: string json
| json5
, default json
, the file type of mock datamockLogicFileType
: string js
| cjs
| ts
| none
, default none
, the file type of mock logicsetJsonByRemote
: { enable: boolean; target: string; timeout?: number; rewrite?: (path: string) => string } | null
undefined
{enable:true, target:'xxx' }
to fetch from remote proxynull
/undefined
to disabletarget
(required): remote server URL, requiredrewrite
: optional path rewrite functiontimeout
: request timeout in millisecondsMore detail → src/com-types.ts, interface MihawkRC define the config props
1graph LR 2 A[Dev Mode: Request] --> B(devServer) 3 B --> D1[Mode 1: mockServer → Local launched mockServer] 4 B --> D2[Mode 2: Proxy to backend → Change proxy to backend's address] 5 D1 --> C(Mihawk) 6 D2 --> E(BackendServer) 7 8 F[Production Mode: Request] --> G(BackendServer) 9 10 style A fill:#2c2c2c,stroke:#ccc,fill-opacity:1,color:#eee 11 style B fill:#5e6472,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 12 style C fill:#09c,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 13 style D1 fill:#5e6472,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 14 style D2 fill:#5e6472,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 15 style E fill:#7a6da2,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 16 style F fill:#2c2c2c,stroke:#ccc,fill-opacity:1,color:#eee 17 style G fill:#7a6da2,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0 18 19 classDef devStyle fill:#2c2c2c,stroke:#ccc,fill-opacity:1,color:#eee; 20 classDef serviceStyle fill:#5e6472,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0; 21 classDef backendStyle fill:#7a6da2,stroke:#f0f0f0,fill-opacity:1,color:#f0f0f0; 22 23 class A,F devStyle 24 class B,D1,D2 serviceStyle 25 class C,E,G backendStyle
In the above diagram,
devServer
is typically provided by bundling tools during local development, such as Vite or Webpack, which have corresponding configuration options.
Essentially, it is based on the proxy functionality of
devServer
, forwarding requests to the mihawk server.
in vite.config.js
file:
1import { defineConfig } from 'vite';
2export default defineConfig({
3 server: {
4 proxy: {
5 '/api': {
6 target: 'http://localhost:8888', // mihawk server address
7 changeOrigin: true,
8 rewrite: path => path.replace(/^\/api/, ''),
9 },
10 },
11 },
12});
in webpack.config.js
file:
1// webpack.config.js 2module.exports = { 3 devServer: { 4 proxy: { 5 '/api': { 6 target: 'http://localhost:8888', // mihawk server address 7 changeOrigin: true, 8 pathRewrite: { '^/api': '' }, 9 }, 10 }, 11 }, 12};
For request GET /api/fetch_a_random_number
,it return response with random number data
mocks/data/GET/api/fetch_a_random_number.json
file, content as below1{ 2 "code": 200, 3 "data": 123456, 4 "msg": "success" 5}
You cal aslo dont do this step, coz the mock data file is auto create when request a not exists file
Now, if request GET /api/fetch_a_random_number
,return data is 123456
, it is fixed data
mocks/data/GET/api/fetch_a_random_number.js
file, content as below1module.exports = async function (oldJson) { 2 oldJson.data = Math.floor(Math.random() * 1000000); // generate random number 3 return oldJson; // return data, it is required 4};
Start mihawk
server now, if request GET /api/fetch_a_random_number
,return data is random number, each request return a different data
About MockLogic File:
- Both support
js
|cjs
|ts
, the process is same。Attention toexport default
is necessary ints
file!- Recommend to set
autoCreateMockLogicFile
totrue
in.mihawkrc.json
, then, if request a not exists mock data file, it will auto create a mock logic file for you- Of course, it is worth mentioning that MockLogic files aren't necessary files. If there is no logical demand for data processing, using only JSON files can also simulate the request
routes
file demo in ts1/** 2 * mihawk's routes file: 3 */ 4const routes: Record<string, string> = { 5 'GET /test': './GET/test', 6 'GET /test-*': './GET/test', // key: routePath,support glob expression; value: mock data file path (no ext) 7}; 8// 9export default routes;
middleware
file demo in ts1/** 2 * mihawk's middleware file: 3 * - just a Koa2 Middleware 4 */ 5import type { Context: KoaContext, Next: KoaNext } from 'koa'; // need koa@v2.0.0+ (eg: koa@^2.15.3) 6// import type { KoaContext, KoaNext } from 'mihawk/con-types'; 7 8/** 9 * Middleware functions, to implement some special data deal logic, 10 * - This function exec before the default-mock-logic. Simply return or don`t call "await next()" could skip default-mock-logic 11 * - This function is a standard KOA middleware that follows the KOA onion ring model 12 * - see more:https://koajs.com/#middleware 13 * @param {Context} ctx 14 * @param {Next} next 15 * @returns {Promise<void>} 16 */ 17export default async function middleware(ctx: KoaContext, next: KoaNext) { 18 // do something here 19 console.log(ctx.url); 20 if (ctx.peth === '/diy') { 21 ctx.body = 'it is my diy logic'; 22 } else { 23 await next(); // default logic (such like mock json logic) 24 } 25}
Set
middleware.isExpress=true
to explicit definition a express middleware function before export, if you write in express-stype Other complex diy middleware demo, base on koa-router & koa-compose, middleware.md
mock-logic
file demo in ts1'use strict;'; 2/** 3 * GET /xxx 4 * This file isn‘t mandatory. If it is not needed (such as when there is no need to modify response data), it can be deleted directly 5 */ 6 7/** 8 * Mock data resolve function, the original data source is the JSON file with the same name as this file 9 * @param {object} originData (mocks/data/GET/xxx.json) 10 * @param {MhkCvtrExtra} extra { url,method,path,query,body } 11 * @returns {object} newData 12 */ 13export default async function convertData(originData: Record<string, any>, extra: Record<string, any>) { 14 // write your logic here... 15 originData.newProp = 'newPropXxx'; 16 return originData; // return data, it is required 17}
mhiawk/tools
: Built-in utility functions like creatRandXxx
for generating fake data; this functionality is not as powerful as Mockjs
data generate
and mihawk's server mock
together; they are not mutually exclusiveyour_project/mocks/middleware.ts
: Simulate backend services, such as httpServeryour_project/mocks/socket.ts
: Simulate backend services, such as socketServerNo vulnerabilities found.
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool is run on all commits
Details
Reason
4 existing vulnerabilities detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More