修复设置自定义通知

This commit is contained in:
whyour 2022-09-20 18:00:36 +08:00
parent 1ebcfd8001
commit e08f0cd308
6 changed files with 68 additions and 68 deletions

View File

@ -3,6 +3,7 @@ import * as path from 'path';
import got from 'got'; import got from 'got';
import iconv from 'iconv-lite'; import iconv from 'iconv-lite';
import { exec } from 'child_process'; import { exec } from 'child_process';
import FormData from 'form-data';
export function getFileContentByName(fileName: string) { export function getFileContentByName(fileName: string) {
if (fs.existsSync(fileName)) { if (fs.existsSync(fileName)) {
@ -355,3 +356,61 @@ export function promiseExec(command: string): Promise<string> {
); );
}); });
} }
export function parseHeaders(headers: string) {
if (!headers) return {};
const parsed: any = {};
let key;
let val;
let i;
headers && headers.split('\n').forEach(function parser(line) {
i = line.indexOf(':');
key = line.substring(0, i).trim().toLowerCase();
val = line.substring(i + 1).trim();
if (!key) {
return;
}
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
});
return parsed;
};
export function parseBody(body: string, contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded') {
if (!body) return '';
const parsed: any = {};
let key;
let val;
let i;
body && body.split('\n').forEach(function parser(line) {
i = line.indexOf(':');
key = line.substring(0, i).trim().toLowerCase();
val = line.substring(i + 1).trim();
if (!key || parsed[key]) {
return;
}
parsed[key] = val;
});
switch (contentType) {
case 'multipart/form-data':
return Object.keys(parsed).reduce((p, c) => {
p.append(c, parsed[c])
return p;
}, new FormData());
case 'application/x-www-form-urlencoded':
return Object.keys(parsed).reduce((p, c) => {
return p ? `${p}&${c}=${parsed[c]}` : `${c}=${parsed[c]}`;
});
}
return parsed;
};

View File

@ -91,8 +91,8 @@ export class EmailNotification extends NotificationBaseInfo {
} }
export class WebhookNotification extends NotificationBaseInfo { export class WebhookNotification extends NotificationBaseInfo {
public webhookHeaders: IncomingHttpHeaders = {}; public webhookHeaders: string = '';
public webhookBody: any = {}; public webhookBody: string = '';
public webhookUrl: string = ''; public webhookUrl: string = '';
public webhookMethod: 'GET' | 'POST' | 'PUT' = 'GET'; public webhookMethod: 'GET' | 'POST' | 'PUT' = 'GET';
public webhookContentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded' = 'application/json'; public webhookContentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded' = 'application/json';

View File

@ -6,6 +6,7 @@ import got from 'got';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import crypto from 'crypto'; import crypto from 'crypto';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'; import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import { parseBody, parseHeaders } from '../config/util';
@Service() @Service()
export default class NotificationService { export default class NotificationService {
@ -388,10 +389,12 @@ export default class NotificationService {
const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType } = const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType } =
this.params; this.params;
const bodyParam = this.formatBody(webhookContentType, webhookBody); const headers = parseHeaders(webhookHeaders);
const body = parseBody(webhookBody, webhookContentType);
const bodyParam = this.formatBody(webhookContentType, body);
const options = { const options = {
method: webhookMethod, method: webhookMethod,
headers: webhookHeaders, headers,
timeout: this.timeout, timeout: this.timeout,
retry: 0, retry: 0,
allowGetBody: true, allowGetBody: true,

View File

@ -65,6 +65,7 @@
"express": "^4.17.3", "express": "^4.17.3",
"express-jwt": "^6.1.1", "express-jwt": "^6.1.1",
"express-urlrewrite": "^1.4.0", "express-urlrewrite": "^1.4.0",
"form-data": "^4.0.0",
"got": "^11.8.2", "got": "^11.8.2",
"hpagent": "^0.1.2", "hpagent": "^0.1.2",
"iconv-lite": "^0.6.3", "iconv-lite": "^0.6.3",

View File

@ -13,15 +13,11 @@ const NotificationSetting = ({ data }: any) => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const handleOk = (values: any) => { const handleOk = (values: any) => {
const { type, webhookBody, webhookContentType } = values; const { type } = values;
if (type == 'closed') { if (type == 'closed') {
values.type = ''; values.type = '';
} }
if (type === 'webhook') {
values.webhookHeaders = { ...parseHeaders(values.webhookHeaders) };
values.webhookBody = parseBody(webhookBody, webhookContentType);
}
request request
.put(`${config.apiPrefix}user/notification`, { .put(`${config.apiPrefix}user/notification`, {
data: { data: {

View File

@ -266,64 +266,5 @@ export function depthFirstSearch<
} }
})(c); })(c);
console.log(keys);
return c; return c;
} }
export function parseHeaders(headers: string) {
if (!headers) return {};
const parsed: any = {};
let key;
let val;
let i;
headers && headers.split('\n').forEach(function parser(line) {
i = line.indexOf(':');
key = line.substring(0, i).trim().toLowerCase();
val = line.substring(i + 1).trim();
if (!key) {
return;
}
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
});
return parsed;
};
export function parseBody(body: string, contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded') {
if (!body) return '';
const parsed: any = {};
let key;
let val;
let i;
body && body.split('\n').forEach(function parser(line) {
i = line.indexOf(':');
key = line.substring(0, i).trim().toLowerCase();
val = line.substring(i + 1).trim();
if (!key || parsed[key]) {
return;
}
parsed[key] = val;
});
switch (contentType) {
case 'multipart/form-data':
return Object.keys(parsed).reduce((p, c) => {
p.append(c, parsed[c])
return p;
}, new FormData());
case 'application/x-www-form-urlencoded':
return Object.keys(parsed).reduce((p, c) => {
return p ? `${p}&${c}=${parsed[c]}` : `${c}=${parsed[c]}`;
});
}
return parsed;
};