From e213c76870a77e3cf2d5caf6132b7d95677013be Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 14 Jan 2025 10:52:32 -0800 Subject: [PATCH] More plugins (#4) --- package-lock.json | 23 ++++++---- plugins/template-function-file/package.json | 9 ++++ plugins/template-function-file/src/index.ts | 18 ++++++++ plugins/template-function-prompt/src/index.ts | 1 + .../template-function-request/package.json | 9 ++++ .../template-function-request/src/index.ts | 45 +++++++++++++++++++ .../template-function-response/package.json | 2 +- 7 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 plugins/template-function-file/package.json create mode 100644 plugins/template-function-file/src/index.ts create mode 100755 plugins/template-function-request/package.json create mode 100755 plugins/template-function-request/src/index.ts diff --git a/package-lock.json b/package-lock.json index b3b9a7c2..4260ddd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1042,6 +1042,10 @@ "resolved": "plugins/importer-yaak", "link": true }, + "node_modules/@yaakapp/template-function-file": { + "resolved": "plugins/template-function-file", + "link": true + }, "node_modules/@yaakapp/template-function-fs": { "resolved": "plugins/template-function-fs", "link": true @@ -1054,6 +1058,14 @@ "resolved": "plugins/template-function-prompt", "link": true }, + "node_modules/@yaakapp/template-function-request": { + "resolved": "plugins/template-function-request", + "link": true + }, + "node_modules/@yaakapp/template-function-response": { + "resolved": "plugins/template-function-response", + "link": true + }, "node_modules/aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -5810,10 +5822,6 @@ "array-includes": "^3.0.3" } }, - "node_modules/template-function-response": { - "resolved": "plugins/template-function-response", - "link": true - }, "node_modules/through2": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/through2/-/through2-0.5.1.tgz", @@ -6698,8 +6706,7 @@ }, "plugins/template-function-file": { "name": "@yaakapp/template-function-file", - "version": "0.0.1", - "extraneous": true + "version": "0.0.1" }, "plugins/template-function-fs": { "name": "@yaakapp/template-function-fs", @@ -6715,10 +6722,10 @@ }, "plugins/template-function-request": { "name": "@yaakapp/template-function-request", - "version": "0.0.1", - "extraneous": true + "version": "0.0.1" }, "plugins/template-function-response": { + "name": "@yaakapp/template-function-response", "version": "0.0.1", "dependencies": { "@xmldom/xmldom": "^0.8.10", diff --git a/plugins/template-function-file/package.json b/plugins/template-function-file/package.json new file mode 100644 index 00000000..59aea7ab --- /dev/null +++ b/plugins/template-function-file/package.json @@ -0,0 +1,9 @@ +{ + "name": "@yaakapp/template-function-file", + "private": true, + "version": "0.0.1", + "scripts": { + "build": "yaakcli build ./src/index.ts", + "dev": "yaakcli dev ./src/index.js" + } +} diff --git a/plugins/template-function-file/src/index.ts b/plugins/template-function-file/src/index.ts new file mode 100644 index 00000000..472c1a0d --- /dev/null +++ b/plugins/template-function-file/src/index.ts @@ -0,0 +1,18 @@ +import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api'; +import fs from 'node:fs'; + +export const plugin: PluginDefinition = { + templateFunctions: [{ + name: 'fs.readFile', + args: [{ title: 'Select File', type: 'file', name: 'path', label: 'File' }], + async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise { + if (!args.values.path) return null; + + try { + return fs.promises.readFile(args.values.path, 'utf-8'); + } catch (err) { + return null; + } + }, + }], +}; diff --git a/plugins/template-function-prompt/src/index.ts b/plugins/template-function-prompt/src/index.ts index 957bc8ff..a40ce13e 100644 --- a/plugins/template-function-prompt/src/index.ts +++ b/plugins/template-function-prompt/src/index.ts @@ -6,6 +6,7 @@ export const plugin: PluginDefinition = { 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 }, ], diff --git a/plugins/template-function-request/package.json b/plugins/template-function-request/package.json new file mode 100755 index 00000000..a3ebb038 --- /dev/null +++ b/plugins/template-function-request/package.json @@ -0,0 +1,9 @@ +{ + "name": "@yaakapp/template-function-request", + "private": true, + "version": "0.0.1", + "scripts": { + "build": "yaakcli build ./src/index.ts", + "dev": "yaakcli dev ./src/index.js" + } +} diff --git a/plugins/template-function-request/src/index.ts b/plugins/template-function-request/src/index.ts new file mode 100755 index 00000000..0567d587 --- /dev/null +++ b/plugins/template-function-request/src/index.ts @@ -0,0 +1,45 @@ +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 { + 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 { + 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, + })); + }, + }, + ], +}; diff --git a/plugins/template-function-response/package.json b/plugins/template-function-response/package.json index dfb064ac..7cb3a387 100644 --- a/plugins/template-function-response/package.json +++ b/plugins/template-function-response/package.json @@ -1,5 +1,5 @@ { - "name": "template-function-response", + "name": "@yaakapp/template-function-response", "private": true, "version": "0.0.1", "scripts": {