Async template functions working

This commit is contained in:
Gregory Schier
2024-08-19 06:21:03 -07:00
parent ec22191409
commit 1fbcfeaa30
32 changed files with 618 additions and 393 deletions

View File

@@ -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;
}

View File

@@ -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'