Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huibizhang/vmoth
https://github.com/huibizhang/vmoth
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/huibizhang/vmoth
- Owner: huibizhang
- Created: 2024-05-12T23:54:20.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-06-26T15:06:48.000Z (7 months ago)
- Last Synced: 2024-10-01T19:40:02.827Z (3 months ago)
- Language: TypeScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# vMoth
**vMoth** 是一個基於 `axios` 和 `axios-mock-adapter` 的輕量級 HTTP 請求模擬工具,它可以幫助您在測試中模擬 HTTP 請求的行為。
## 安裝
您可以通過 npm 或 yarn 安裝 `vmoth`:
```bash
npm install vmoth
# 或者
yarn add vmoth
```## 使用方法
```javascript
import axios from "axios";
import { vMoth } from "vmoth";const mock = vMoth(axios);
mock.on("/users", {
username: "github",
user_id: 123,
});try {
const res = await axios.get("/users");
console.log("test", res.data);
} catch (e) {
console.error("myError", e);
}
```## Pattern
使用預定義的樣板作為**回傳值**
#### 範例
```javascript
import axios from "axios";
import { vMoth, pattern } from "vmoth";const mock = vMoth(axios);
// 返回狀態碼 401
mock.on("/admin", pattern._401);// 返回 json-string
mock.on(
"/users",
pattern.json({
username: "github",
user_id: 123,
})
);try {
const res = await axios.get("/users");
console.log("test", res.data);
} catch (e) {
console.error("myError", e);
}
```預設樣板列表:
- `_400`
- `_401`
- `_404`
- `_500`
- `json(data)`