UUID, json/x path

This commit is contained in:
Gregory Schier
2025-05-28 13:07:29 -07:00
parent 21b9e5a02b
commit 053cbe49f9
8 changed files with 298 additions and 2 deletions

View File

@@ -7,7 +7,7 @@
"dev": "yaakcli dev ./src/index.js"
},
"dependencies": {
"jsonpath-plus": "^9.0.0"
"jsonpath-plus": "^10.3.0"
},
"devDependencies": {
"@types/jsonpath": "^0.2.4"

View File

@@ -0,0 +1,15 @@
{
"name": "@yaakapp/template-function-json",
"private": true,
"version": "0.0.1",
"scripts": {
"build": "yaakcli build ./src/index.ts",
"dev": "yaakcli dev ./src/index.js"
},
"dependencies": {
"jsonpath-plus": "^10.3.0"
},
"devDependencies": {
"@types/jsonpath": "^0.2.4"
}
}

View File

@@ -0,0 +1,45 @@
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
import { JSONPath } from 'jsonpath-plus';
export const plugin: PluginDefinition = {
templateFunctions: [
{
name: 'json.jsonpath',
description: 'Filter JSON-formatted text using JSONPath syntax',
args: [
{ type: 'text', name: 'input', label: 'Input', multiLine: true, placeholder: '{ "foo": "bar" }' },
{ type: 'text', name: 'query', label: 'Query', placeholder: '$..foo' },
{ type: 'checkbox', name: 'formatted', label: 'Format Output' },
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
try {
const parsed = JSON.parse(String(args.values.input));
const query = String(args.values.query ?? '$').trim();
let filtered = JSONPath({ path: query, json: parsed });
if (Array.isArray(filtered)) {
filtered = filtered[0];
}
if (args.values.formatted) {
return JSON.stringify(filtered, null, 2);
} else {
return JSON.stringify(filtered);
}
} catch (e) {
return null;
}
},
},
{
name: 'json.escape',
description: 'Escape a JSON string, useful when using the output in JSON values',
args: [
{ type: 'text', name: 'input', label: 'Input', multiLine: true, placeholder: 'Hello "World"' },
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
const input = String(args.values.input ?? '');
return input.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
},
},
],
};

View File

@@ -0,0 +1,12 @@
{
"name": "@yaakapp/template-function-uuid",
"private": true,
"version": "0.0.1",
"scripts": {
"build": "yaakcli build ./src/index.ts",
"dev": "yaakcli dev ./src/index.js"
},
"dependencies": {
"uuid": "^11.1.0"
}
}

View File

@@ -0,0 +1,76 @@
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
import { v1, v3, v4, v5, v6, v7 } from 'uuid';
export const plugin: PluginDefinition = {
templateFunctions: [
{
name: 'uuid.v1',
description: 'Generate a UUID V1',
args: [],
async onRender(_ctx: Context, _args: CallTemplateFunctionArgs): Promise<string | null> {
return v1();
},
},
{
name: 'uuid.v3',
description: 'Generate a UUID V3',
args: [
{ type: 'text', name: 'name', label: 'Name' },
{
type: 'text',
name: 'namespace',
label: 'Namespace UUID',
description: 'A valid UUID to use as the namespace',
placeholder: '24ced880-3bf4-11f0-8329-cd053d577f0e',
},
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
return v3(String(args.values.name), String(args.values.namespace));
},
},
{
name: 'uuid.v4',
description: 'Generate a UUID V4',
args: [],
async onRender(_ctx: Context, _args: CallTemplateFunctionArgs): Promise<string | null> {
return v4();
},
},
{
name: 'uuid.v5',
description: 'Generate a UUID V5',
args: [
{ type: 'text', name: 'name', label: 'Name' },
{ type: 'text', name: 'namespace', label: 'Namespace' },
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
return v5(String(args.values.name), String(args.values.namespace));
},
},
{
name: 'uuid.v6',
description: 'Generate a UUID V6',
args: [
{
type: 'text',
name: 'timestamp',
label: 'Timestamp',
optional: true,
description: 'Can be any format that can be parsed by JavaScript new Date(...)',
placeholder: '2025-05-28T11:15:00Z',
},
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
return v6({ msecs: new Date(String(args.values.timestamp)).getTime() });
},
},
{
name: 'uuid.v7',
description: 'Generate a UUID V7',
args: [],
async onRender(_ctx: Context, _args: CallTemplateFunctionArgs): Promise<string | null> {
return v7();
},
},
],
};

View File

@@ -0,0 +1,13 @@
{
"name": "@yaakapp/template-function-xml",
"private": true,
"version": "0.0.1",
"scripts": {
"build": "yaakcli build ./src/index.ts",
"dev": "yaakcli dev ./src/index.js"
},
"dependencies": {
"@xmldom/xmldom": "^0.8.10",
"xpath": "^0.0.34"
}
}

View File

@@ -0,0 +1,29 @@
import { DOMParser } from '@xmldom/xmldom';
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
import xpath from 'xpath';
export const plugin: PluginDefinition = {
templateFunctions: [{
name: 'xml.xpath',
description: 'Filter XML-formatted text using XPath syntax',
args: [
{ type: 'text', name: 'input', label: 'Input', multiLine: true, placeholder: '<foo></foo>' },
{ type: 'text', name: 'query', label: 'Query', placeholder: '//foo' },
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
try {
const doc = new DOMParser().parseFromString(String(args.values.input), 'text/xml');
let result = xpath.select(String(args.values.query), doc, false);
if (Array.isArray(result)) {
return String(result.map(c => String(c.firstChild))[0]);
} else if (result instanceof Node) {
return String(result.firstChild);
} else {
return String(result);
}
} catch (e) {
return null;
}
},
}],
};