Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 6d6012f

Browse files
committed
More template functions
1 parent d028832 commit 6d6012f

10 files changed

Lines changed: 207 additions & 26 deletions

File tree

package-lock.json

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"workspaces-run": "^1.0.2"
2020
},
2121
"dependencies": {
22-
"@yaakapp/api": "^0.5.3"
22+
"@yaakapp/api": "^0.6.0"
2323
}
2424
}

plugins/auth-oauth2/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export const plugin: PluginDefinition = {
115115
label: 'Client ID',
116116
optional: true,
117117
},
118-
119118
{
120119
type: 'text',
121120
name: 'clientSecret',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@yaakapp/template-function-cookie",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"build": "yaakcli build ./src/index.ts",
7+
"dev": "yaakcli dev ./src/index.js"
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
2+
3+
export const plugin: PluginDefinition = {
4+
templateFunctions: [
5+
{
6+
name: 'cookie.value',
7+
description: 'Read the value of a cookie in the jar, by name',
8+
args: [
9+
{
10+
type: 'text',
11+
name: 'cookie_name',
12+
label: 'Cookie Name',
13+
},
14+
],
15+
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
16+
return ctx.cookies.getValue({ name: String(args.values.cookie_name) });
17+
},
18+
},
19+
],
20+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@yaakapp/template-function-encode",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"build": "yaakcli build ./src/index.ts",
7+
"dev": "yaakcli dev ./src/index.js"
8+
}
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
2+
3+
export const plugin: PluginDefinition = {
4+
templateFunctions: [
5+
{
6+
name: 'base64.encode',
7+
description: 'Encode a value to base64',
8+
args: [{ label: 'Plain Text', type: 'text', name: 'value', multiLine: true }],
9+
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
10+
return Buffer.from(args.values.value ?? '').toString('base64');
11+
},
12+
},
13+
{
14+
name: 'base64.decode',
15+
description: 'Decode a value from base64',
16+
args: [{ label: 'Encoded Value', type: 'text', name: 'value', multiLine: true }],
17+
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
18+
return Buffer.from(args.values.value ?? '', 'base64').toString('utf-8');
19+
},
20+
},
21+
],
22+
};
Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,86 @@
11
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
2-
import { createHash } from 'node:crypto';
2+
import { createHash, createHmac } from 'node:crypto';
33

4-
const algorithms = ['md5', 'sha1', 'sha256', 'sha512'];
4+
const algorithms = ['md5', 'sha1', 'sha256', 'sha512'] as const;
5+
const encodings = ['base64', 'hex'] as const;
56

6-
export const plugin: PluginDefinition = {
7-
templateFunctions: algorithms.map(algorithm => ({
8-
name: `hash.${algorithm}`,
9-
description: 'Hash a value to its hexidecimal representation',
10-
args: [
11-
{
12-
name: 'input',
13-
label: 'Input',
14-
placeholder: 'input text',
15-
type: 'text',
16-
},
17-
],
18-
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
19-
if (!args.values.input) return '';
20-
return createHash(algorithm)
21-
.update(args.values.input, 'utf-8')
22-
.digest('hex');
7+
type TemplateFunctionPlugin = NonNullable<PluginDefinition['templateFunctions']>[number];
8+
9+
const hashFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({
10+
name: `hash.${algorithm}`,
11+
description: 'Hash a value to its hexidecimal representation',
12+
args: [
13+
{
14+
type: 'text',
15+
name: 'input',
16+
label: 'Input',
17+
placeholder: 'input text',
18+
multiLine: true,
19+
},
20+
{
21+
type: 'select',
22+
name: 'encoding',
23+
label: 'Encoding',
24+
defaultValue: 'base64',
25+
options: encodings.map(encoding => ({
26+
label: capitalize(encoding),
27+
value: encoding,
28+
})),
29+
},
30+
],
31+
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
32+
const input = String(args.values.input);
33+
const encoding = String(args.values.encoding) as typeof encodings[number];
34+
35+
return createHash(algorithm)
36+
.update(input, 'utf-8')
37+
.digest(encoding);
38+
},
39+
}));
40+
41+
const hmacFunctions: TemplateFunctionPlugin[] = algorithms.map(algorithm => ({
42+
name: `hmac.${algorithm}`,
43+
description: 'Compute the HMAC of a value',
44+
args: [
45+
{
46+
type: 'text',
47+
name: 'input',
48+
label: 'Input',
49+
placeholder: 'input text',
50+
multiLine: true,
2351
},
24-
})),
52+
{
53+
type: 'text',
54+
name: 'key',
55+
label: 'Key',
56+
password: true,
57+
},
58+
{
59+
type: 'select',
60+
name: 'encoding',
61+
label: 'Encoding',
62+
defaultValue: 'base64',
63+
options: encodings.map(encoding => ({
64+
value: encoding,
65+
label: capitalize(encoding),
66+
})),
67+
},
68+
],
69+
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
70+
const input = String(args.values.input);
71+
const key = String(args.values.key);
72+
const encoding = String(args.values.encoding) as typeof encodings[number];
73+
74+
return createHmac(algorithm, key, {})
75+
.update(input)
76+
.digest(encoding);
77+
},
78+
}));
79+
80+
export const plugin: PluginDefinition = {
81+
templateFunctions: [...hashFunctions, ...hmacFunctions],
2582
};
83+
84+
function capitalize(str: string): string {
85+
return str.charAt(0).toUpperCase() + str.slice(1);
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@yaakapp/template-function-regex",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"build": "yaakcli build ./src/index.ts",
7+
"dev": "yaakcli dev ./src/index.js"
8+
}
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
2+
3+
export const plugin: PluginDefinition = {
4+
templateFunctions: [{
5+
name: 'regex.match',
6+
description: 'Extract',
7+
args: [
8+
{
9+
type: 'text',
10+
name: 'regex',
11+
label: 'Regular Expression',
12+
placeholder: '^\w+=(?<value>\w*)$',
13+
defaultValue: '^(.*)$',
14+
description: 'A JavaScript regular expression, evaluated using the Node.js RegExp engine. Capture groups or named groups can be used to extract values.',
15+
},
16+
{ type: 'text', name: 'input', label: 'Input Text', multiLine: true },
17+
],
18+
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
19+
if (!args.values.regex) return '';
20+
21+
const regex = new RegExp(String(args.values.regex));
22+
const match = args.values.input?.match(regex);
23+
return match?.groups
24+
? Object.values(match.groups)[0] ?? ''
25+
: match?.[1] ?? match?.[0] ?? '';
26+
},
27+
}],
28+
};

0 commit comments

Comments
 (0)