mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-19 15:37:08 +01:00
Async template functions working
This commit is contained in:
@@ -1,88 +1,19 @@
|
||||
import type { HttpRequest } from '@yaakapp/api';
|
||||
|
||||
export interface TemplateFunctionArgBase {
|
||||
name: string;
|
||||
optional?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export interface TemplateFunctionSelectArg extends TemplateFunctionArgBase {
|
||||
type: 'select';
|
||||
defaultValue?: string;
|
||||
options: readonly { name: string; value: string }[];
|
||||
}
|
||||
|
||||
export interface TemplateFunctionTextArg extends TemplateFunctionArgBase {
|
||||
type: 'text';
|
||||
defaultValue?: string;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export interface TemplateFunctionHttpRequestArg extends TemplateFunctionArgBase {
|
||||
type: HttpRequest['model'];
|
||||
}
|
||||
|
||||
export type TemplateFunctionArg =
|
||||
| TemplateFunctionSelectArg
|
||||
| TemplateFunctionTextArg
|
||||
| TemplateFunctionHttpRequestArg;
|
||||
|
||||
export interface TemplateFunction {
|
||||
name: string;
|
||||
args: TemplateFunctionArg[];
|
||||
}
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { GetTemplateFunctionsResponse } from '@yaakapp/api';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
|
||||
export function useTemplateFunctions() {
|
||||
const fns: TemplateFunction[] = [
|
||||
{
|
||||
name: 'timestamp',
|
||||
args: [
|
||||
{
|
||||
type: 'text',
|
||||
name: 'from',
|
||||
label: 'From',
|
||||
placeholder: '2023-23-12T04:03:03',
|
||||
optional: true,
|
||||
},
|
||||
{
|
||||
type: 'select',
|
||||
label: 'Format',
|
||||
name: 'format',
|
||||
options: [
|
||||
{ name: 'RFC3339', value: 'rfc3339' },
|
||||
{ name: 'Unix', value: 'unix' },
|
||||
{ name: 'Unix (ms)', value: 'unix_millis' },
|
||||
],
|
||||
optional: true,
|
||||
defaultValue: 'rfc3339',
|
||||
},
|
||||
],
|
||||
const result = useQuery({
|
||||
queryKey: ['template_functions'],
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async () => {
|
||||
const responses = (await invokeCmd(
|
||||
'cmd_template_functions',
|
||||
)) as GetTemplateFunctionsResponse[];
|
||||
return responses;
|
||||
},
|
||||
{
|
||||
name: 'response',
|
||||
args: [
|
||||
{
|
||||
type: 'http_request',
|
||||
name: 'request',
|
||||
label: 'Request',
|
||||
},
|
||||
{
|
||||
type: 'select',
|
||||
name: 'attribute',
|
||||
label: 'Attribute',
|
||||
options: [
|
||||
{ name: 'Body', value: 'body' },
|
||||
{ name: 'Header', value: 'header' },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
name: 'filter',
|
||||
label: 'Filter',
|
||||
placeholder: 'JSONPath or XPath expression',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
const fns = result.data?.flatMap((r) => r.functions) ?? [];
|
||||
return fns;
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ type TauriCmd =
|
||||
| 'cmd_send_http_request'
|
||||
| 'cmd_set_key_value'
|
||||
| 'cmd_set_update_mode'
|
||||
| 'cmd_template_functions'
|
||||
| 'cmd_track_event'
|
||||
| 'cmd_update_cookie_jar'
|
||||
| 'cmd_update_environment'
|
||||
|
||||
Reference in New Issue
Block a user