Skip to content

Commit

Permalink
feat & refactor: migrate to cordis version and add code of request ca…
Browse files Browse the repository at this point in the history
…pture
  • Loading branch information
TimeBather authored and ilharp committed Oct 30, 2024
1 parent fe1c446 commit a0aebde
Show file tree
Hide file tree
Showing 17 changed files with 442 additions and 40 deletions.
26 changes: 26 additions & 0 deletions client/components/CaptureButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import {inject} from "vue"
import Button from "primevue/button";
import {send, useRpc} from "@cordisjs/client";
import type {HttpSummary} from "../../src/data";
const model = useRpc<HttpSummary>();
const toast : (severity:string, summary:string, detail:string)=>void = inject("toast");
async function startCapture(){
if(!await send('http/capture.start'))
return;
toast("success","操作成功","HTTP 请求捕获已开始");
}
async function stopCapture(){
if(!await send('http/capture.stop'))
return;
toast("success","操作成功","HTTP 请求捕获已停止");
}
</script>
<template>
<Button icon="pi pi-camera" class="mr-2" severity="secondary" size="small" text v-tooltip.bottom="'捕获 HTTP 请求'"
v-if="!model.captureEnabled" @click="startCapture()"/>
<Button icon="pi pi-stop" class="mr-2" severity="secondary" size="small" text v-tooltip.bottom="'捕获 HTTP 请求'"
v-else @click="stopCapture()"/>
</template>
15 changes: 15 additions & 0 deletions client/components/CaptureListMenu.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script setup lang="ts">
import Button from "primevue/button";
import CaptureButton from "./CaptureButton.vue";
import {send} from "@cordisjs/client";
async function clearCapture(){
if(!await send('http/capture.clear'))
return;
}
</script>

<template>
<CaptureButton/>
<Button icon="pi pi-trash" class="mr-2" severity="secondary" size="small" text v-tooltip.bottom="'清空捕获的请求'" @click="clearCapture"/>
</template>

24 changes: 21 additions & 3 deletions client/components/HttpTools.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@ import RequestList from "./RequestList.vue";
import Splitter from "primevue/splitter";
import SplitterPanel from "primevue/splitterpanel";
import HttpEditor from "./editors/HttpEditor.vue";
import {ref} from "vue";
import {useRpc} from '@cordisjs/client'
const model = useRpc();
import Toast from "primevue/toast";
import {provide} from 'vue'
import {useToast} from "primevue/usetoast";
import HttpEditorProxy from "./editors/HttpEditorProxy.vue";
const toast = useToast()
provide('toast',(severity:"success" | "info" | "warn" | "error" | "secondary" | "contrast", summary:string, detail:string)=>{
toast.add({
severity: severity,
summary: summary,
detail: detail,
life: 5000
})
})
const model = defineModel()
const current = ref();
const currentModel = ref();
</script>

<template>
<div style="display: flex;flex-direction: column;height:100%">
<Toast />
<MainMenu/>
<div style="display: flex;flex-direction: row;height:100%">
<Splitter style="height: 100%;width: 100%">
<SplitterPanel :size="20" style="min-width: 300px">
<RequestList :requests="model"/>
<RequestList :requests="model" v-model="current"/>
</SplitterPanel>
<SplitterPanel :size="80">
<HttpEditor/>
<HttpEditorProxy v-if="current" :type="current['type']" :id="current['id']"/>
</SplitterPanel>
</Splitter>
</div>
Expand Down
19 changes: 12 additions & 7 deletions client/components/MainMenu.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<script setup lang="ts">
import {useRpc} from "@cordisjs/client";
import Toolbar from "primevue/toolbar";
import Button from "primevue/button";
import SplitButton from "primevue/splitbutton";
import type {HttpSummary} from "../../src/data";
import CaptureButton from "./CaptureButton.vue";
const model = useRpc<HttpSummary>();
</script>
<template>

<Toolbar>
<template #start>
<Button icon="pi pi-plus" class="mr-2" severity="secondary" text v-tooltip.bottom="'创建 HTTP 请求'"/>
<Button icon="pi pi-camera" class="mr-2" severity="secondary" text v-tooltip.bottom="'捕获 HTTP 请求'"/>
<CaptureButton/>
<Button icon="pi pi-upload" severity="secondary" text v-tooltip.bottom="'上传已有 HTTP 请求'"/>
</template>

Expand All @@ -15,9 +26,3 @@
</template>
</Toolbar>
</template>

<script setup>
import Toolbar from "primevue/toolbar";
import Button from "primevue/button";
import SplitButton from "primevue/splitbutton";
</script>
29 changes: 23 additions & 6 deletions client/components/RequestList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import ContextMenu from "primevue/contextmenu";
import {computed,ref} from 'vue'
import {getHttpMethodColor} from "./editors/http/colors";
import CaptureListMenu from "./CaptureListMenu.vue";
const model = defineModel<Record<string,any>>();
const model = defineModel<any>();
const props = defineProps<{
requests?: {
capture?:any[],
Expand All @@ -28,11 +29,12 @@ const menuModel = ([
const listBoxOptions = computed(()=>([
{
label: '用户',
items: props.requests?.user ?? []
items: props.requests?.user.map(t=>({...t, type:'user'})) ?? []
},
{
label: '捕获',
items: props.requests?.capture ?? []
items: props.requests?.capture.map(t=>({...t, type:'capture'})) ?? [],
menu: CaptureListMenu
}
]))
</script>
Expand All @@ -49,6 +51,7 @@ const listBoxOptions = computed(()=>([
</InputGroup>
</div>
<ListBox
v-model="model"
:options="listBoxOptions"
optionGroupLabel="label"
optionGroupChildren="items"
Expand All @@ -57,7 +60,13 @@ const listBoxOptions = computed(()=>([
listStyle="height:calc(100% - 20px)"
>
<template #optiongroup="slotProps">
<div>{{ slotProps.option.label }} ({{slotProps.option?.items?.length ?? 0}})</div>
<div style="display: flex; flex-direction: row">
<div>{{ slotProps.option.label }} ({{slotProps.option?.items?.length ?? 0}})</div>
<div style="flex:1"></div>
<div v-if="slotProps.option.menu">
<component :is="slotProps.option.menu"></component>
</div>
</div>
</template>
<template #option="slotProps">
<div @contextmenu="(e)=>menu.show(e)" style="width: 100%">
Expand All @@ -66,9 +75,17 @@ const listBoxOptions = computed(()=>([
:value="slotProps.option.method?.toUpperCase()"
:severity="getHttpMethodColor(slotProps.option.method)"
style="font-size: 12px;padding:0.2rem 0.4rem"
></Tag><div style="margin-left: 10px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis">/api/login</div>
></Tag>
<div style="margin-left: 10px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis">
{{
(slotProps.option.path ?
(slotProps.option.path.startsWith('/') ?
slotProps.option.path : '/'+slotProps.option.path)
: '[NO PATH]'
)
}}</div>
</div>
<span style="font-size:12px">api.github.com | 200ms | 200 OK</span>
<span style="font-size:12px">{{ slotProps.option?.host ?? 'localhost' }}</span>
</div>
</template>
</ListBox>
Expand Down
35 changes: 32 additions & 3 deletions client/components/editors/HttpEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,44 @@ import InputNumber from "primevue/inputnumber";
import InputGroupAddon from "primevue/inputgroupaddon";
import HttpRequestEditor from "./http/HttpRequestEditor.vue";
import HttpResponsePreviewer from "./http/HttpResponsePreviewer.vue";
import InputText from "primevue/inputtext";
import {computed, defineModel} from 'vue'
import {getHttpMethodColor} from "./http/colors";
import Tag from "primevue/tag";
const model = defineModel<any>();
const method = computed({
get(){
return model.value.method?.toUpperCase()
},
set(value){
console.info("UPD",model.value.method, value.toLowerCase())
model.value.method = value.toLowerCase();
},
})
</script>

<template>
<div style="display: flex;flex-direction: column;gap: 5px">
<div style="padding: 8px 20px;">
<InputGroup>
<Select style="width: 200px!important;flex:unset" placeholder="请求方法" size="large"/>
<InputGroupAddon>https://BASESERVER:PORT/</InputGroupAddon>
<InputNumber placeholder="URL" size="large"/>
<Select
style="width: 200px!important;flex:unset"
placeholder="请求方法"
size="large"
v-model="method"
:options="['GET','POST','PUT','PATCH','UPDATE','DELETE','OPTIONS']"
>
<template #value="slotProps">
<Tag
:value="slotProps.value?.toUpperCase()"
:severity="getHttpMethodColor(slotProps.value)"
style="font-size: 12px;padding:0.2rem 0.4rem"
></Tag>
</template>
</Select>
<InputText placeholder="URL" size="large" v-model="model.url"/>
</InputGroup>
</div>
<div style="padding-left: 20px">
Expand Down
74 changes: 74 additions & 0 deletions client/components/editors/HttpEditorProxy.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<script setup lang="ts">
import {ref,watch,nextTick} from 'vue'
import ProgressSpinner from "primevue/progressspinner";
import Skeleton from "primevue/skeleton";
import HttpEditor from "./HttpEditor.vue";
import {send} from '@cordisjs/client'
const props = defineProps<{
id: number,
type: string
}>()
const int = ref(false);
setInterval(()=>int.value = !int.value,1000);
const data = ref();
const changed = ref(false);
async function fetchData(type:string, id:number){
const response = await send('http/request',{type, id});
if(response){
data.value = response;
nextTick(()=>{
changed.value = false;
});
}
}
watch(props,(val)=>{
data.value = null;
fetchData(val.type, val.id);
},{immediate:true})
watch(data,()=>{
console.info("Updated")
changed.value = true;
},{deep:true})
let savingTimeout : NodeJS.Timeout | number | false = false;
watch(changed,(value)=>{
if(value && savingTimeout === false){
savingTimeout = setTimeout(()=>{
console.info("Save");
savingTimeout = false;
changed.value = false;
}, 3000);
}else if(!value && savingTimeout !== false){
clearTimeout(savingTimeout);
savingTimeout = false;
}
})
</script>

<template>
<HttpEditor v-if="data" v-model="data"/>
<div style="height:100%;width: 100%;display: flex;flex-direction: column" v-else>
<div style="display: flex;flex-direction: column;gap: 5px">
<div style="padding: 8px 20px;">
<Skeleton height="2.5rem" width="100%"></Skeleton>
</div>
</div>
<div style="padding-left: 20px;padding-top:0.4rem">
<Skeleton height="1rem" width="24rem" borderRadius="16px"></Skeleton>
</div>
<Skeleton height="2.5rem" width="16rem" style="margin-top:1rem;margin-left:1.4rem"></Skeleton>
<div style="margin: 1.4rem;flex:1">
<Skeleton height="100%" width="100%"/>
</div>
</div>
</template>

<style scoped>
</style>
8 changes: 5 additions & 3 deletions client/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Context } from '@koishijs/client'
import { Context } from '@cordisjs/client'
import Page from './index.vue'
import PrimeVue from 'primevue/config'
import Tooltip from 'primevue/tooltip'

// @ts-ignore
import Aura from '@primevue/themes/aura'
import 'primeicons/primeicons.css'

export default (ctx: Context) => {
import ToastService from 'primevue/toastservice'
export default (ctx: Context, config: any) => {
// 此 Context 非彼 Context
// 我们只是在前端同样实现了一套插件逻辑
ctx.page({
Expand All @@ -20,6 +21,7 @@ export default (ctx: Context) => {
preset: Aura,
},
})
ctx.app.use(ToastService)

ctx.app.directive('tooltip', Tooltip)
}
11 changes: 8 additions & 3 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
"compilerOptions": {
"rootDir": ".",
"module": "esnext",
"moduleResolution": "node",
"moduleResolution": "Bundler",
"types": [
"@koishijs/client/global",
"@koishijs/client/global"
],
},
"include": ["."],
"include": [".","../src"],
"references": [
{
"path": "../tsconfig.json",
},
]
}
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "koishi-plugin-http-tools",
"name": "cordis-plugin-http-tools",
"version": "0.0.1",
"description": "HTTP Tools for Koishi",
"main": "lib/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"network",
"api"
],
"koishi": {
"cordis": {
"service": {
"required": [
"http"
Expand All @@ -45,19 +45,24 @@
"lint": "eslint --ext=ts --cache"
},
"devDependencies": {
"@cordisjs/client": "^0.1.12",
"@cordisjs/eslint-config": "^1.1.1",
"@koishijs/client": "^5.11.0",
"@cordisjs/plugin-http": "^0.6.1",
"@primevue/themes": "^4.0.4",
"@types/node": "^20.11.30",
"atsc": "^2.0.1",
"cordis": "^3.17.9",
"esbuild": "^0.18.20",
"esbuild-register": "^3.5.0",
"eslint": "^8.57.0",
"koishi": "^4.17.5",
"primeicons": "^7.0.0",
"primevue": "^4.0.4",
"typescript": "^5.4.3",
"yml-register": "^1.2.5"
},
"peerDependencies": {
"@koishijs/plugin-console": "^5.11.0",
"koishi": "^4.17.5"
"@cordisjs/plugin-http": "^0.6.1",
"@cordisjs/plugin-webui": "^0.1.12",
"cordis": "^3.17.9"
}
}
Loading

0 comments on commit a0aebde

Please sign in to comment.