Skip to content

Commit

Permalink
feat: filters to status code
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ngjie committed Oct 5, 2022
1 parent d94a7b4 commit 14611c3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "colibri",
"private": true,
"description": "Modify your Ajax response to test",
"version": "1.0.4",
"version": "1.0.5",
"scripts": {
"dev": "pnpm -C ./packages/shell-popup dev",
"watch": "run-p watch:lib watch:chrome",
Expand Down
11 changes: 8 additions & 3 deletions packages/colibri-lib.v2/src/createFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ function CustomFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Resp
// @ts-ignore
return OriginFetch(input, init).then((response: Response) => {
let txt: string | undefined;
let status = response.status
let statusText = response.statusText
globalState.value.matching_content.forEach(target => {
const { switch_on = true, match_url, override = "", filter_type, method } = target
const { switch_on = true, match_url, override = "", filter_type, method, statusCode = "200" } = target
// 是否需要匹配
if (switch_on && match_url) {
// 判断是否存在协议匹配
Expand All @@ -28,6 +30,9 @@ function CustomFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Resp
if (!matched) return // 退出当前循环
// 修改响应
txt = typeof override === "string" ? override : JSON.stringify(override);
// 修改状态码
status = +statusCode
statusText = statusCode
// 通知
notice(response.url, match_url, fetchMethod || "")
}
Expand All @@ -44,8 +49,8 @@ function CustomFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Resp
});
const newResponse = new Response(stream, {
headers: response.headers,
status: response.status,
statusText: response.statusText,
status: status,
statusText: statusText,
});
const proxy = new Proxy(newResponse, {
get: function (target, prop) {
Expand Down
9 changes: 7 additions & 2 deletions packages/colibri-lib.v2/src/createXHR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CustomXHR extends XMLHttpRequest {
responseText!: string;
// XHR 响应
response: any;
status!: number;
statusText: string = "";
// 请求协议
method = 'GET'
// 消息锁
Expand Down Expand Up @@ -48,7 +50,7 @@ class CustomXHR extends XMLHttpRequest {
// 规则匹配,修改响应内容
private maybeNeedModifyRes() {
globalState.value.matching_content.forEach(target => {
const { switch_on = true, match_url, override = "", filter_type, method } = target
const { switch_on = true, match_url, override = "", filter_type, method, statusCode = "200" } = target
// 是否需要匹配
if (switch_on && match_url) {
// 判断是否存在协议匹配
Expand All @@ -59,6 +61,9 @@ class CustomXHR extends XMLHttpRequest {
// 修改响应
this.responseText = override;
this.response = override;
// 修改状态码
this.status = +statusCode
this.statusText = statusCode
// 通知
if (!this.message_once_lock) {
notice(this.responseURL, match_url, this.method);
Expand All @@ -73,7 +78,7 @@ class CustomXHR extends XMLHttpRequest {
// 重写属性
// @ts-ignore
if (typeof xhr[attr] === "function") this[attr] = xhr[attr].bind(xhr);
else if (['responseText', 'response'].includes(attr))
else if (['responseText', 'response', 'status', 'statusText'].includes(attr))
// responseText和response 属性只读
// 缓存在对应 自定义 _[attr] 上
Object.defineProperty(this, attr, {
Expand Down
2 changes: 2 additions & 0 deletions packages/colibri-lib.v2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface IMatchContent {
hit?: number;
/**标题 */
label?: string;
/**状态码 */
statusCode?: string
}

export interface IGlobalState {
Expand Down
1 change: 1 addition & 0 deletions packages/colibri-lib.v2/test/base.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}, {
switch_on: true,
match_url: 'https://api.github.com/feeds',
statusCode: "200",
override: JSON.stringify({ ok: true, text: "success" }),
}
]
Expand Down
2 changes: 2 additions & 0 deletions packages/colibri-lib.v2/types/createXHR.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export declare const initXHRState: (state: Ref<IGlobalState>) => Ref<IGlobalStat
declare class CustomXHR extends XMLHttpRequest {
responseText: string;
response: any;
status: number;
statusText: string;
method: string;
private message_once_lock;
constructor();
Expand Down
2 changes: 2 additions & 0 deletions packages/colibri-lib.v2/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface IMatchContent {
hit?: number;
/**标题 */
label?: string;
/**状态码 */
statusCode?: string;
}
export interface IGlobalState {
/**全局开关 */
Expand Down
2 changes: 1 addition & 1 deletion packages/shell-chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Colibri: modify Ajax response",
"version": "1.0.4",
"version": "1.0.5",
"description": "Modify your Ajax response to test",
"author": "Gj",
"content_scripts": [
Expand Down
37 changes: 37 additions & 0 deletions packages/shell-popup/src/components/table/tools/setupRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineComponent, ref, onMounted, computed } from "vue";
import { NForm, NFormItem, NInput, NIcon } from "naive-ui";
import { ITableRowData } from "@/interfaces";
import { isNumber } from "@alrale/common-lib";

// 展开面板设置项
export default defineComponent({
Expand All @@ -14,11 +15,35 @@ export default defineComponent({
const refOverride = ref()

const feedbackErr = ref("")
// status code error message
const statusErr = ref("")

onMounted(() => {
refOverride.value?.focus()
})

const createStatus = (value?: string) => {
if (!value) {
statusErr.value = ""
return undefined
}
// 100 - 999
// status code 200、301、404、500...
const bool = isNumber(value)
if (bool) {

if (value.length !== 3) {
statusErr.value = "Please enter the correct status code"
return "warning"
} else {
statusErr.value = ""
return undefined
}
}
statusErr.value = "Status code is not legal"
return 'error'
}

const handleFormat = () => {
try {
props.data.override = JSON.stringify(JSON.parse(props.data.override || ""), null, 2)
Expand All @@ -37,6 +62,18 @@ export default defineComponent({
placeholder="please input label"
/>
</NFormItem>
<NFormItem
label="Status"
validationStatus={computed(() => createStatus(props.data.statusCode)).value}
feedback={statusErr.value}
>
{/* 设置Status */}
<NInput
maxlength={3}
v-model:value={props.data.statusCode}
placeholder="200"
/>
</NFormItem>
<NFormItem
size="small"
label="Override"
Expand Down

0 comments on commit 14611c3

Please sign in to comment.