mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-20 07:41:22 +02:00
More template functions
This commit is contained in:
32
package-lock.json
generated
32
package-lock.json
generated
@@ -9,7 +9,7 @@
|
|||||||
"plugins/*"
|
"plugins/*"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@yaakapp/api": "^0.5.3"
|
"@yaakapp/api": "^0.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.7.4",
|
"@types/node": "^22.7.4",
|
||||||
@@ -999,9 +999,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@yaakapp/api": {
|
"node_modules/@yaakapp/api": {
|
||||||
"version": "0.5.3",
|
"version": "0.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.5.3.tgz",
|
"resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.6.0.tgz",
|
||||||
"integrity": "sha512-JKO0t5H+wM2KWpNgiNW5UVmk66c7p2WFCHa8TnLwnkFpub/3ktZfMY1Y+c21N2gsurqUe3wmcNRM0J1nQrR9rA==",
|
"integrity": "sha512-7DELi/E5Ayx+lmWSYVhjgE4Ll+hf7IWPnbFMkYhO7G14nk2WIjTeDdoAA3/7XMi5M4cL8+GGsx9mYXmts9YP7g==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node": "^22.5.4"
|
"@types/node": "^22.5.4"
|
||||||
}
|
}
|
||||||
@@ -1054,6 +1054,14 @@
|
|||||||
"resolved": "plugins/importer-yaak",
|
"resolved": "plugins/importer-yaak",
|
||||||
"link": true
|
"link": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@yaakapp/template-function-cookie": {
|
||||||
|
"resolved": "plugins/template-function-cookie",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
|
"node_modules/@yaakapp/template-function-encode": {
|
||||||
|
"resolved": "plugins/template-function-encode",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
"node_modules/@yaakapp/template-function-fs": {
|
"node_modules/@yaakapp/template-function-fs": {
|
||||||
"resolved": "plugins/template-function-fs",
|
"resolved": "plugins/template-function-fs",
|
||||||
"link": true
|
"link": true
|
||||||
@@ -1066,6 +1074,10 @@
|
|||||||
"resolved": "plugins/template-function-prompt",
|
"resolved": "plugins/template-function-prompt",
|
||||||
"link": true
|
"link": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@yaakapp/template-function-regex": {
|
||||||
|
"resolved": "plugins/template-function-regex",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
"node_modules/@yaakapp/template-function-request": {
|
"node_modules/@yaakapp/template-function-request": {
|
||||||
"resolved": "plugins/template-function-request",
|
"resolved": "plugins/template-function-request",
|
||||||
"link": true
|
"link": true
|
||||||
@@ -7190,6 +7202,14 @@
|
|||||||
"name": "@yaakapp/importer-yaak",
|
"name": "@yaakapp/importer-yaak",
|
||||||
"version": "0.0.1"
|
"version": "0.0.1"
|
||||||
},
|
},
|
||||||
|
"plugins/template-function-cookie": {
|
||||||
|
"name": "@yaakapp/template-function-cookie",
|
||||||
|
"version": "0.0.1"
|
||||||
|
},
|
||||||
|
"plugins/template-function-encode": {
|
||||||
|
"name": "@yaakapp/template-function-encode",
|
||||||
|
"version": "0.0.1"
|
||||||
|
},
|
||||||
"plugins/template-function-file": {
|
"plugins/template-function-file": {
|
||||||
"name": "@yaakapp/template-function-file",
|
"name": "@yaakapp/template-function-file",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
@@ -7207,6 +7227,10 @@
|
|||||||
"name": "@yaakapp/template-function-prompt",
|
"name": "@yaakapp/template-function-prompt",
|
||||||
"version": "0.0.1"
|
"version": "0.0.1"
|
||||||
},
|
},
|
||||||
|
"plugins/template-function-regex": {
|
||||||
|
"name": "@yaakapp/template-function-regex",
|
||||||
|
"version": "0.0.1"
|
||||||
|
},
|
||||||
"plugins/template-function-request": {
|
"plugins/template-function-request": {
|
||||||
"name": "@yaakapp/template-function-request",
|
"name": "@yaakapp/template-function-request",
|
||||||
"version": "0.0.1"
|
"version": "0.0.1"
|
||||||
|
|||||||
@@ -19,6 +19,6 @@
|
|||||||
"workspaces-run": "^1.0.2"
|
"workspaces-run": "^1.0.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@yaakapp/api": "^0.5.3"
|
"@yaakapp/api": "^0.6.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,7 +115,6 @@ export const plugin: PluginDefinition = {
|
|||||||
label: 'Client ID',
|
label: 'Client ID',
|
||||||
optional: true,
|
optional: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
name: 'clientSecret',
|
name: 'clientSecret',
|
||||||
|
|||||||
9
plugins/template-function-cookie/package.json
Normal file
9
plugins/template-function-cookie/package.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "@yaakapp/template-function-cookie",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": {
|
||||||
|
"build": "yaakcli build ./src/index.ts",
|
||||||
|
"dev": "yaakcli dev ./src/index.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
20
plugins/template-function-cookie/src/index.ts
Normal file
20
plugins/template-function-cookie/src/index.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||||
|
|
||||||
|
export const plugin: PluginDefinition = {
|
||||||
|
templateFunctions: [
|
||||||
|
{
|
||||||
|
name: 'cookie.value',
|
||||||
|
description: 'Read the value of a cookie in the jar, by name',
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'cookie_name',
|
||||||
|
label: 'Cookie Name',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
return ctx.cookies.getValue({ name: String(args.values.cookie_name) });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
9
plugins/template-function-encode/package.json
Normal file
9
plugins/template-function-encode/package.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "@yaakapp/template-function-encode",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": {
|
||||||
|
"build": "yaakcli build ./src/index.ts",
|
||||||
|
"dev": "yaakcli dev ./src/index.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
plugins/template-function-encode/src/index.ts
Normal file
22
plugins/template-function-encode/src/index.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||||
|
|
||||||
|
export const plugin: PluginDefinition = {
|
||||||
|
templateFunctions: [
|
||||||
|
{
|
||||||
|
name: 'base64.encode',
|
||||||
|
description: 'Encode a value to base64',
|
||||||
|
args: [{ label: 'Plain Text', type: 'text', name: 'value', multiLine: true }],
|
||||||
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
return Buffer.from(args.values.value ?? '').toString('base64');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'base64.decode',
|
||||||
|
description: 'Decode a value from base64',
|
||||||
|
args: [{ label: 'Encoded Value', type: 'text', name: 'value', multiLine: true }],
|
||||||
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
return Buffer.from(args.values.value ?? '', 'base64').toString('utf-8');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -1,25 +1,86 @@
|
|||||||
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||||
import { createHash } from 'node:crypto';
|
import { createHash, createHmac } from 'node:crypto';
|
||||||
|
|
||||||
const algorithms = ['md5', 'sha1', 'sha256', 'sha512'];
|
const algorithms = ['md5', 'sha1', 'sha256', 'sha512'] as const;
|
||||||
|
const encodings = ['base64', 'hex'] as const;
|
||||||
|
|
||||||
|
type TemplateFunctionPlugin = NonNullable<PluginDefinition['templateFunctions']>[number];
|
||||||
|
|
||||||
|
const hashFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({
|
||||||
|
name: `hash.${algorithm}`,
|
||||||
|
description: 'Hash a value to its hexidecimal representation',
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'input',
|
||||||
|
label: 'Input',
|
||||||
|
placeholder: 'input text',
|
||||||
|
multiLine: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'select',
|
||||||
|
name: 'encoding',
|
||||||
|
label: 'Encoding',
|
||||||
|
defaultValue: 'base64',
|
||||||
|
options: encodings.map(encoding => ({
|
||||||
|
label: capitalize(encoding),
|
||||||
|
value: encoding,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
const input = String(args.values.input);
|
||||||
|
const encoding = String(args.values.encoding) as typeof encodings[number];
|
||||||
|
|
||||||
|
return createHash(algorithm)
|
||||||
|
.update(input, 'utf-8')
|
||||||
|
.digest(encoding);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
const hmacFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({
|
||||||
|
name: `hmac.${algorithm}`,
|
||||||
|
description: 'Compute the HMAC of a value',
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'input',
|
||||||
|
label: 'Input',
|
||||||
|
placeholder: 'input text',
|
||||||
|
multiLine: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'key',
|
||||||
|
label: 'Key',
|
||||||
|
password: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'select',
|
||||||
|
name: 'encoding',
|
||||||
|
label: 'Encoding',
|
||||||
|
defaultValue: 'base64',
|
||||||
|
options: encodings.map(encoding => ({
|
||||||
|
value: encoding,
|
||||||
|
label: capitalize(encoding),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
const input = String(args.values.input);
|
||||||
|
const key = String(args.values.key);
|
||||||
|
const encoding = String(args.values.encoding) as typeof encodings[number];
|
||||||
|
|
||||||
|
return createHmac(algorithm, key, {})
|
||||||
|
.update(input)
|
||||||
|
.digest(encoding);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
export const plugin: PluginDefinition = {
|
export const plugin: PluginDefinition = {
|
||||||
templateFunctions: algorithms.map(algorithm => ({
|
templateFunctions: [...hashFunctions, ...hmacFunctions],
|
||||||
name: `hash.${algorithm}`,
|
|
||||||
description: 'Hash a value to its hexidecimal representation',
|
|
||||||
args: [
|
|
||||||
{
|
|
||||||
name: 'input',
|
|
||||||
label: 'Input',
|
|
||||||
placeholder: 'input text',
|
|
||||||
type: 'text',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
|
||||||
if (!args.values.input) return '';
|
|
||||||
return createHash(algorithm)
|
|
||||||
.update(args.values.input, 'utf-8')
|
|
||||||
.digest('hex');
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function capitalize(str: string): string {
|
||||||
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||||
|
}
|
||||||
|
|||||||
9
plugins/template-function-regex/package.json
Normal file
9
plugins/template-function-regex/package.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "@yaakapp/template-function-regex",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": {
|
||||||
|
"build": "yaakcli build ./src/index.ts",
|
||||||
|
"dev": "yaakcli dev ./src/index.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
28
plugins/template-function-regex/src/index.ts
Normal file
28
plugins/template-function-regex/src/index.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||||
|
|
||||||
|
export const plugin: PluginDefinition = {
|
||||||
|
templateFunctions: [{
|
||||||
|
name: 'regex.match',
|
||||||
|
description: 'Extract',
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'regex',
|
||||||
|
label: 'Regular Expression',
|
||||||
|
placeholder: '^\w+=(?<value>\w*)$',
|
||||||
|
defaultValue: '^(.*)$',
|
||||||
|
description: 'A JavaScript regular expression, evaluated using the Node.js RegExp engine. Capture groups or named groups can be used to extract values.',
|
||||||
|
},
|
||||||
|
{ type: 'text', name: 'input', label: 'Input Text', multiLine: true },
|
||||||
|
],
|
||||||
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
|
if (!args.values.regex) return '';
|
||||||
|
|
||||||
|
const regex = new RegExp(String(args.values.regex));
|
||||||
|
const match = args.values.input?.match(regex);
|
||||||
|
return match?.groups
|
||||||
|
? Object.values(match.groups)[0] ?? ''
|
||||||
|
: match?.[1] ?? match?.[0] ?? '';
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user