whistle.interceptors 0.0.2 → 0.0.3

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,40 @@
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
+
9
+ export default (router: Router) => {
10
+
11
+ router.get(apis.get, (ctx: RouterContext) => {
12
+ try {
13
+ const data = ctx.storage.getProperty(LOCAL_PREFIX);
14
+ console.log('get data', data)
15
+ ctx.body = {
16
+ result: 'ok',
17
+ data: JSON.parse(data)
18
+ }
19
+ } catch (error) {
20
+ ctx.body = {
21
+ result: 'error',
22
+ data: 'get rules error ' + error
23
+ }
24
+ }
25
+ });
26
+
27
+ router.post(apis.add, (ctx : RouterContext) => {
28
+ console.log('ssss', ctx.request.body, typeof ctx.request.body)
29
+ ctx.storage.setProperty(LOCAL_PREFIX, JSON.stringify(ctx.request.body))
30
+ ctx.body = {
31
+ result: 'ok',
32
+ data: null
33
+ }
34
+ });
35
+
36
+ router.delete(apis.delete, (ctx) => {
37
+ console.log('ssss', ctx)
38
+ ctx.body = 'ok'
39
+ });
40
+ };
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文件中的内容。