Tweak plugins

This commit is contained in:
Gregory Schier
2024-10-15 07:45:45 -07:00
parent 61543fb10f
commit 17a429525f
8 changed files with 50 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
{
"name": "@yaakapp/template-function-file",
"name": "@yaakapp/template-function-fs",
"private": true,
"version": "0.0.1",
"scripts": {

View File

@@ -3,9 +3,9 @@ import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/ap
export const plugin: PluginDefinition = {
templateFunctions: [{
name: 'prompt.text',
description: 'Prompt the user for input when sending a request',
args: [
{ type: 'text', name: 'title', label: 'Title' },
{ type: 'text', name: 'label', label: 'Label', optional: true },
{ type: 'text', name: 'defaultValue', label: 'Default Value', optional: true },
{ type: 'text', name: 'placeholder', label: 'Placeholder', optional: true },
],
@@ -14,7 +14,7 @@ export const plugin: PluginDefinition = {
return await ctx.prompt.text({
id: `prompt-${args.values.label}`,
label: args.values.label ?? '',
label: args.values.title ?? '',
title: args.values.title ?? '',
defaultValue: args.values.defaultValue,
placeholder: args.values.placeholder,

View File

@@ -1,9 +0,0 @@
{
"name": "@yaakapp/template-function-request",
"private": true,
"version": "0.0.1",
"scripts": {
"build": "yaakcli build ./src/index.ts",
"dev": "yaakcli dev ./src/index.js"
}
}

View File

@@ -1,45 +0,0 @@
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
export const plugin: PluginDefinition = {
templateFunctions: [
{
name: 'request.body',
args: [{
name: 'requestId',
label: 'Http Request',
type: 'http_request',
}],
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
const httpRequest = await ctx.httpRequest.getById({ id: args.values.requestId ?? 'n/a' });
if (httpRequest == null) return null;
return String(await ctx.templates.render({
data: httpRequest.body?.text ?? '',
purpose: args.purpose,
}));
},
},
{
name: 'request.header',
args: [
{
name: 'requestId',
label: 'Http Request',
type: 'http_request',
},
{
name: 'header',
label: 'Header Name',
type: 'text',
}],
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
const httpRequest = await ctx.httpRequest.getById({ id: args.values.requestId ?? 'n/a' });
if (httpRequest == null) return null;
const header = httpRequest.headers.find(h => h.name.toLowerCase() === args.values.header?.toLowerCase());
return String(await ctx.templates.render({
data: header?.value ?? '',
purpose: args.purpose,
}));
},
},
],
};

View File

@@ -61,6 +61,7 @@ export const plugin: PluginDefinition = {
},
{
name: 'response.body.path',
description: 'Access a field of the response body using JsonPath or XPath',
aliases: ['response'],
args: [
requestArg,
@@ -108,6 +109,38 @@ export const plugin: PluginDefinition = {
return null; // Bail out
},
},
{
name: 'response.body.raw',
description: 'Access the entire response body, as text',
aliases: ['response'],
args: [
requestArg,
behaviorArg,
],
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
if (!args.values.request) return null;
const response = await getResponse(ctx, {
requestId: args.values.request,
purpose: args.purpose,
behavior: args.values.behavior ?? null,
});
if (response == null) return null;
if (response.bodyPath == null) {
return null;
}
let body;
try {
body = readFileSync(response.bodyPath, 'utf-8');
} catch (_) {
return null;
}
return body;
},
},
],
};