snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
11
plugins/template-function-hash/package.json
Executable file
11
plugins/template-function-hash/package.json
Executable file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "@yaak/template-function-hash",
|
||||
"displayName": "Hash Template Functions",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Template functions for generating hash values",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev"
|
||||
}
|
||||
}
|
||||
82
plugins/template-function-hash/src/index.ts
Executable file
82
plugins/template-function-hash/src/index.ts
Executable file
@@ -0,0 +1,82 @@
|
||||
import { createHash, createHmac } from "node:crypto";
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from "@yaakapp/api";
|
||||
|
||||
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 hexadecimal 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: [...hashFunctions, ...hmacFunctions],
|
||||
};
|
||||
|
||||
function capitalize(str: string): string {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||
}
|
||||
3
plugins/template-function-hash/tsconfig.json
Normal file
3
plugins/template-function-hash/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user