mirror of
https://github.com/YMFE/yapi.git
synced 2025-04-12 15:10:23 +08:00
Merge pull request #1543 from AlexStacker/add-hook-request
feat: add hook request
This commit is contained in:
commit
607925cb57
@ -35,6 +35,8 @@ const {
|
||||
checkNameIsExistInArray
|
||||
} = require('common/postmanLib.js');
|
||||
|
||||
const plugin = require('client/plugin.js');
|
||||
|
||||
const createContext = require('common/createContext')
|
||||
|
||||
const HTTP_METHOD = constants.HTTP_METHOD;
|
||||
@ -332,13 +334,29 @@ export default class Run extends Component {
|
||||
let options = handleParams(this.state, this.handleValue),
|
||||
result;
|
||||
|
||||
|
||||
await plugin.emitHook('before_request', options, {
|
||||
type: this.props.type,
|
||||
caseId: options.caseId,
|
||||
projectId: this.props.projectId,
|
||||
interfaceId: this.props.interfaceId
|
||||
});
|
||||
|
||||
try {
|
||||
options.taskId = this.props.curUid;
|
||||
result = await crossRequest(options, this.state.pre_script, this.state.after_script, createContext(
|
||||
result = await crossRequest(options, options.pre_script || this.state.pre_script, options.after_script || this.state.after_script, createContext(
|
||||
this.props.curUid,
|
||||
this.props.projectId,
|
||||
this.props.interfaceId
|
||||
));
|
||||
|
||||
await plugin.emitHook('after_request', result, {
|
||||
type: this.props.type,
|
||||
caseId: options.caseId,
|
||||
projectId: this.props.projectId,
|
||||
interfaceId: this.props.interfaceId
|
||||
});
|
||||
|
||||
result = {
|
||||
header: result.res.header,
|
||||
body: result.res.body,
|
||||
@ -346,6 +364,7 @@ export default class Run extends Component {
|
||||
statusText: result.res.statusText,
|
||||
runTime: result.runTime
|
||||
};
|
||||
|
||||
} catch (data) {
|
||||
result = {
|
||||
header: data.header,
|
||||
|
@ -25,6 +25,7 @@ import { initCrossRequest } from 'client/components/Postman/CheckCrossInstall.js
|
||||
import produce from 'immer';
|
||||
import {InsertCodeMap} from 'client/components/Postman/Postman.js'
|
||||
|
||||
const plugin = require('client/plugin.js');
|
||||
const {
|
||||
handleParams,
|
||||
crossRequest,
|
||||
@ -311,6 +312,13 @@ class InterfaceColContent extends Component {
|
||||
validRes: []
|
||||
};
|
||||
|
||||
await plugin.emitHook('before_col_request', Object.assign({}, options, {
|
||||
type: 'col',
|
||||
caseId: options.caseId,
|
||||
projectId: interfaceData.project_id,
|
||||
interfaceId: interfaceData.interface_id
|
||||
}));
|
||||
|
||||
try {
|
||||
let data = await crossRequest(options, interfaceData.pre_script, interfaceData.after_script, createContext(
|
||||
this.props.curUid,
|
||||
@ -328,6 +336,13 @@ class InterfaceColContent extends Component {
|
||||
statusText: data.res.statusText
|
||||
};
|
||||
|
||||
await plugin.emitHook('after_col_request', result, {
|
||||
type: 'col',
|
||||
caseId: options.caseId,
|
||||
projectId: interfaceData.project_id,
|
||||
interfaceId: interfaceData.interface_id
|
||||
});
|
||||
|
||||
if (options.data && typeof options.data === 'object') {
|
||||
requestParams = {
|
||||
...requestParams,
|
||||
|
@ -71,6 +71,52 @@ hooks = {
|
||||
mulit: true,
|
||||
listener: []
|
||||
},
|
||||
/**
|
||||
* 在运行页面或单个测试也里每次发送请求前调用
|
||||
* 可以用插件针对某个接口的请求头或者数据进行修改或者记录
|
||||
*/
|
||||
before_request: {
|
||||
type: 'listener',
|
||||
mulit: true,
|
||||
listener: []
|
||||
},
|
||||
/**
|
||||
* 在运行页面或单个测试也里每次发送完成后调用
|
||||
* 返回值为响应原始值 +
|
||||
* {
|
||||
* type: 'inter' | 'case',
|
||||
* projectId: string,
|
||||
* interfaceId: string
|
||||
* }
|
||||
*/
|
||||
after_request: {
|
||||
type: 'listener',
|
||||
mulit: true,
|
||||
listener: []
|
||||
},
|
||||
/**
|
||||
* 在测试集里运行每次发送请求前调用
|
||||
*/
|
||||
before_col_request: {
|
||||
type: 'listener',
|
||||
mulit: true,
|
||||
listener: []
|
||||
},
|
||||
/**
|
||||
* 在测试集里运行每次发送请求后调用
|
||||
* 返回值为响应原始值 +
|
||||
* {
|
||||
* type: 'col',
|
||||
* caseId: string,
|
||||
* projectId: string,
|
||||
* interfaceId: string
|
||||
* }
|
||||
*/
|
||||
after_col_request: {
|
||||
type: 'listener',
|
||||
mulit: true,
|
||||
listener: []
|
||||
},
|
||||
/**
|
||||
* header下拉菜单 menu 钩子
|
||||
* @param HeaderMenu
|
||||
|
Loading…
x
Reference in New Issue
Block a user