mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-07 00:13:53 +00:00
split url and searchParams allow for post option
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Control, useFieldArray, UseFormRegister } from 'react-hook-form';
|
||||
import { Button, IconButton, Input, Switch } from '@chakra-ui/react';
|
||||
import { Button, IconButton, Input, Select, Switch } from '@chakra-ui/react';
|
||||
import { FiChevronUp } from '@react-icons/all-files/fi/FiChevronUp';
|
||||
import { IoRemove } from '@react-icons/all-files/io5/IoRemove';
|
||||
import { HttpSettings, TimerLifeCycle } from 'ontime-types';
|
||||
@@ -40,8 +40,10 @@ export default function SubscriptionRow(props: SubscriptionRowProps) {
|
||||
return;
|
||||
}
|
||||
append({
|
||||
message: '',
|
||||
url: '',
|
||||
options: '',
|
||||
enabled: false,
|
||||
method: 'GET',
|
||||
});
|
||||
};
|
||||
|
||||
@@ -70,10 +72,26 @@ export default function SubscriptionRow(props: SubscriptionRowProps) {
|
||||
size='xs'
|
||||
variant='ontime-filled-on-light'
|
||||
autoComplete='off'
|
||||
{...register(`subscriptions.${cycle}.${index}.message`, {
|
||||
{...register(`subscriptions.${cycle}.${index}.url`, {
|
||||
pattern: { value: startsWithHttpOrS, message: 'Request address must start with http://' },
|
||||
})}
|
||||
/>
|
||||
<Input
|
||||
placeholder='test=testtext'
|
||||
size='xs'
|
||||
variant='ontime-filled-on-light'
|
||||
autoComplete='off'
|
||||
{...register(`subscriptions.${cycle}.${index}.options`)}
|
||||
/>
|
||||
<Select
|
||||
variant='ontime-on-light'
|
||||
size='xs'
|
||||
width='6em'
|
||||
{...register(`subscriptions.${cycle}.${index}.method`)}
|
||||
>
|
||||
<option>GET</option>
|
||||
<option>POST</option>
|
||||
</Select>
|
||||
<Switch variant='ontime-on-light' {...register(`subscriptions.${cycle}.${index}.enabled`)} />
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -77,14 +77,15 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
|
||||
}
|
||||
|
||||
// check subscriptions for action
|
||||
const eventSubscriptions = this.subscriptions?.[action] || [];
|
||||
const eventSubscriptions = (this.subscriptions?.[action] as HttpSubscriptionOptions[]) || [];
|
||||
|
||||
eventSubscriptions.forEach((sub) => {
|
||||
const { enabled, message } = sub;
|
||||
if (enabled && message) {
|
||||
const parsedMessage = parseTemplateNested(message, state || {});
|
||||
const { enabled, url, options, method } = sub;
|
||||
if (enabled && url) {
|
||||
const templateUrl = parseTemplateNested(url, state || {});
|
||||
const templateOptions = parseTemplateNested(options, state || {});
|
||||
try {
|
||||
const parsedUrl = new globalThis.URL(parsedMessage);
|
||||
const parsedUrl = new globalThis.URL(templateUrl);
|
||||
// if (parsedUrl.protocol != 'http:') {
|
||||
// logger.error(LogOrigin.Tx, `HTTP Integration: Only HTTP allowed, got ${parsedUrl.protocol}`);
|
||||
// return {
|
||||
@@ -92,7 +93,7 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
|
||||
// message: `Only HTTP allowed, got ${parsedUrl.protocol}`,
|
||||
// };
|
||||
// }
|
||||
this.emit(parsedUrl);
|
||||
this.emit(templateUrl, templateOptions, method);
|
||||
} catch (err) {
|
||||
logger.error(LogOrigin.Tx, `HTTP Integration: ${err}`);
|
||||
return {
|
||||
@@ -104,11 +105,18 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
|
||||
});
|
||||
}
|
||||
|
||||
async emit(path: globalThis.URL) {
|
||||
async emit(path: string, options: string, method: 'GET' | 'POST') {
|
||||
try {
|
||||
await got.get(path, {
|
||||
retry: { limit: 0 },
|
||||
});
|
||||
if (method === 'GET') {
|
||||
await got.get(path, {
|
||||
searchParams: options,
|
||||
retry: { limit: 0 },
|
||||
});
|
||||
} else if (method === 'POST') {
|
||||
await got.post(path, {
|
||||
retry: { limit: 0 },
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(LogOrigin.Tx, `HTTP integration: ${err}`);
|
||||
}
|
||||
|
||||
@@ -227,9 +227,15 @@ export const parseOsc = (data: { osc?: Partial<OSCSettings> }): OSCSettings => {
|
||||
*/
|
||||
export const validateHttpSubscriptionCycle = (data: HttpSubscriptionOptions[]): boolean => {
|
||||
for (const subscriptionOption of data) {
|
||||
const isHttp =
|
||||
subscriptionOption.message?.startsWith('http://') || subscriptionOption.message?.startsWith('https://');
|
||||
if (typeof subscriptionOption.message !== 'string' || !isHttp || typeof subscriptionOption.enabled !== 'boolean') {
|
||||
const isHttp = subscriptionOption.url?.startsWith('http://') || subscriptionOption.url?.startsWith('https://');
|
||||
const ishttpMethod = subscriptionOption.method == 'GET' || subscriptionOption.method == 'POST';
|
||||
if (
|
||||
typeof subscriptionOption.url !== 'string' ||
|
||||
typeof subscriptionOption.options !== 'string' ||
|
||||
!isHttp ||
|
||||
!ishttpMethod ||
|
||||
typeof subscriptionOption.enabled !== 'boolean'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Subscription } from './Subscription.type.js';
|
||||
|
||||
export type HttpSubscriptionOptions = { message: string; enabled: boolean };
|
||||
type httpMethod = 'GET' | 'POST';
|
||||
export type HttpSubscriptionOptions = { url: string; options: string; enabled: boolean; method: httpMethod };
|
||||
export type HttpSubscription = Subscription<HttpSubscriptionOptions>;
|
||||
|
||||
export interface HttpSettings {
|
||||
|
||||
Reference in New Issue
Block a user