whistle.interceptors 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ declare module 'koa' {
2
+ interface DefaultContext {
3
+ storage: Whistle.Storage
4
+ }
5
+ }
6
+
7
+ import Koa from 'koa';
8
+ import bodyParser from 'koa-bodyparser';
9
+ // import onerror from 'koa-onerror';
10
+ // @ts-ignore
11
+ import cors from '@koa/cors';
12
+ import serve from 'koa-static';
13
+ import path from 'path';
14
+ import Router from 'koa-router';
15
+ import setupRouter from './router';
16
+
17
+ const MAX_AGE = 1000 * 60 * 5;
18
+
19
+
20
+ export default (server: Whistle.PluginServer, options: Whistle.PluginOptions) => {
21
+
22
+ const app = new Koa();
23
+
24
+ app.context.storage = options.storage;
25
+
26
+ app.proxy = true;
27
+ app.silent = true;
28
+ app.use(
29
+ cors({
30
+ allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
31
+ }),
32
+ );
33
+ // onerror(app);
34
+ const router = new Router();
35
+ setupRouter(router);
36
+ app.use(bodyParser());
37
+ app.use(router.routes());
38
+ app.use(router.allowedMethods());
39
+ app.use(serve(path.join(__dirname, '../../public'), { maxage: MAX_AGE }));
40
+ server.on('request', app.callback());
41
+ };
@@ -0,0 +1,68 @@
1
+ import Router from "koa-router";
2
+ import { apis, LOCAL_PREFIX } from "./constant";
3
+
4
+ type RouterContext = {
5
+ storage: Whistle.Storage;
6
+ } & Router.IRouterContext;
7
+
8
+ export default (router: Router) => {
9
+ router.get(apis.get, (ctx: RouterContext) => {
10
+ try {
11
+ const data = ctx.storage.getProperty(LOCAL_PREFIX);
12
+ ctx.body = {
13
+ result: "ok",
14
+ data: data ? JSON.parse(data) : [],
15
+ };
16
+ } catch (error) {
17
+ ctx.body = {
18
+ result: "error",
19
+ data: "get rules error " + error,
20
+ };
21
+ }
22
+ });
23
+
24
+ router.post(apis.add, (ctx: RouterContext) => {
25
+ console.log("ssss", ctx.request.body, typeof ctx.request.body);
26
+ ctx.storage.setProperty(LOCAL_PREFIX, JSON.stringify(ctx.request.body));
27
+ ctx.body = {
28
+ result: "ok",
29
+ data: null,
30
+ };
31
+ });
32
+
33
+ router.delete(apis.delete, (ctx) => {
34
+ console.log("ssss", ctx);
35
+ ctx.body = "ok";
36
+ });
37
+
38
+ router.get(apis.sse, (ctx: RouterContext) => {
39
+
40
+ const storage_prefix = ctx.query.storage_prefix as string
41
+
42
+ ctx.set("Content-Type", "text/event-stream");
43
+ ctx.set("Cache-Control", "no-cache");
44
+ ctx.set("Connection", "keep-alive");
45
+
46
+ ctx.status = 200;
47
+ ctx.respond = false; // 对于 Koa 框架
48
+ ctx.res.flushHeaders();
49
+
50
+ // 发送数据
51
+ const sendEvent = (data: any) => {
52
+ // console.log('[info: 49]:', '向客户端发送消息', data)
53
+ ctx.res.write(`data: ${data}\n\n`);
54
+ };
55
+
56
+ // 模拟实时数据发送
57
+ const interval = setInterval(() => {
58
+ // console.log('[info: 55]:', '模拟实时数据发送')
59
+ sendEvent(ctx.storage.getProperty(storage_prefix));
60
+ }, 1000);
61
+
62
+ // 当客户端关闭连接时清除定时器
63
+ ctx.req.on("close", () => {
64
+ clearInterval(interval);
65
+ ctx.storage.removeProperty(storage_prefix);
66
+ });
67
+ });
68
+ };
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: ['src/server.ts', 'src/uiServer/index.ts'],
5
+ format: ['cjs'],
6
+ clean: true,
7
+ minify: true,
8
+ splitting: false,
9
+ sourcemap: false,
10
+ outDir: 'dist'
11
+ })
package/README.md DELETED
@@ -1,29 +0,0 @@
1
- # whistle.interceptors
2
-
3
- 一个用于灵活拦截和模拟HTTP请求的whistle插件。
4
-
5
- ## 功能特点
6
-
7
- - 基于请求体参数的动态Mock能力
8
- - 简单易用的配置规则
9
-
10
- ## 安装
11
-
12
- ```bash
13
- npm install -g whistle.interceptors
14
- ```
15
-
16
-
17
- ## 配置示例
18
-
19
- ```
20
- test.example.cn/api interceptors:// @userid=001&file=res.json
21
- ```
22
-
23
- 当匹配到test.example.cn/api请求时,如果请求体中参数userid=001,则返回res.json文件中的内容。
24
-
25
- ```
26
- test.example.cn/api interceptors:// @userid=001&file=res.json|userid=002&file=res2.json
27
- ```
28
-
29
- 当匹配到test.example.cn/api请求时,如果请求体中参数userid=001,则返回res.json文件中的内容;如果请求体中参数userid=002,则返回res2.json文件中的内容。