More template functions

This commit is contained in:
Gregory Schier
2025-05-25 20:27:04 -07:00
parent d02883282f
commit 6d6012fe67
10 changed files with 207 additions and 26 deletions

View File

@@ -115,7 +115,6 @@ export const plugin: PluginDefinition = {
label: 'Client ID',
optional: true,
},
{
type: 'text',
name: 'clientSecret',

View 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"
}
}

View 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) });
},
},
],
};

View 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"
}
}

View 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');
},
},
],
};

View File

@@ -1,25 +1,86 @@
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 = {
templateFunctions: algorithms.map(algorithm => ({
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');
},
})),
templateFunctions: [...hashFunctions, ...hmacFunctions],
};
function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}

View 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"
}
}

View 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] ?? '';
},
}],
};