mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-17 23:13:51 +01:00
Optimize request function (#242)
Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
@@ -17,8 +17,8 @@ import type {
|
||||
SendHttpRequestResponse,
|
||||
ShowToastRequest,
|
||||
TemplateRenderRequest,
|
||||
TemplateRenderResponse,
|
||||
} from '../bindings/gen_events.ts';
|
||||
import { JsonValue } from '../bindings/serde_json/JsonValue';
|
||||
|
||||
export interface Context {
|
||||
clipboard: {
|
||||
@@ -59,6 +59,6 @@ export interface Context {
|
||||
find(args: FindHttpResponsesRequest): Promise<FindHttpResponsesResponse['httpResponses']>;
|
||||
};
|
||||
templates: {
|
||||
render(args: TemplateRenderRequest): Promise<TemplateRenderResponse['data']>;
|
||||
render<T extends JsonValue>(args: TemplateRenderRequest & { data: T }): Promise<T>;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { HttpUrlParameter } from '@yaakapp-internal/models';
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
@@ -53,5 +54,47 @@ export const plugin: PluginDefinition = {
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'request.param',
|
||||
args: [
|
||||
{
|
||||
name: 'requestId',
|
||||
label: 'Http Request',
|
||||
type: 'http_request',
|
||||
},
|
||||
{
|
||||
name: 'param',
|
||||
label: 'Param Name',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const paramName = String(args.values.param ?? '');
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
|
||||
const renderedUrl = await ctx.templates.render({
|
||||
data: httpRequest.url,
|
||||
purpose: args.purpose,
|
||||
});
|
||||
|
||||
const querystring = renderedUrl.split('?')[1] ?? '';
|
||||
const paramsFromUrl: HttpUrlParameter[] = new URLSearchParams(querystring)
|
||||
.entries()
|
||||
.map(([name, value]): HttpUrlParameter => ({ name, value }))
|
||||
.toArray();
|
||||
|
||||
const allParams = [...paramsFromUrl, ...httpRequest.urlParameters];
|
||||
const allEnabledParams = allParams.filter((p) => p.enabled !== false);
|
||||
const foundParam = allEnabledParams.find((p) => p.name === paramName);
|
||||
|
||||
const renderedValue = await ctx.templates.render({
|
||||
data: foundParam?.value ?? '',
|
||||
purpose: args.purpose,
|
||||
});
|
||||
return renderedValue;
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ import type {
|
||||
} from '@yaakapp-internal/plugins';
|
||||
import classNames from 'classnames';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useCallback } from 'react';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useActiveRequest } from '../hooks/useActiveRequest';
|
||||
import { useRandomKey } from '../hooks/useRandomKey';
|
||||
import { capitalize } from '../lib/capitalize';
|
||||
@@ -415,7 +415,7 @@ function FileArg({
|
||||
disabled={arg.disabled}
|
||||
help={arg.description}
|
||||
onChange={({ filePath }) => onChange(filePath)}
|
||||
filePath={filePath === '__NULL__' ? null : filePath}
|
||||
filePath={filePath === DYNAMIC_FORM_NULL_ARG ? null : filePath}
|
||||
directory={!!arg.directory}
|
||||
/>
|
||||
);
|
||||
@@ -432,7 +432,14 @@ function HttpRequestArg({
|
||||
}) {
|
||||
const folders = useAtomValue(foldersAtom);
|
||||
const httpRequests = useAtomValue(httpRequestsAtom);
|
||||
const activeRequest = useActiveRequest();
|
||||
const activeHttpRequest = useActiveRequest('http_request');
|
||||
|
||||
useEffect(() => {
|
||||
if (value === DYNAMIC_FORM_NULL_ARG && activeHttpRequest) {
|
||||
onChange(activeHttpRequest.id);
|
||||
}
|
||||
}, [activeHttpRequest, onChange, value]);
|
||||
|
||||
return (
|
||||
<Select
|
||||
label={arg.label ?? arg.name}
|
||||
@@ -446,7 +453,7 @@ function HttpRequestArg({
|
||||
return {
|
||||
label:
|
||||
buildRequestBreadcrumbs(r, folders).join(' / ') +
|
||||
(r.id == activeRequest?.id ? ' (current)' : ''),
|
||||
(r.id == activeHttpRequest?.id ? ' (current)' : ''),
|
||||
value: r.id,
|
||||
};
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user