Add an option to allow jsonpath/xpath to return as array (#297)

Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
Gregor Majcen
2025-11-13 14:57:11 +01:00
committed by GitHub
parent a4c4663011
commit 593a7ab7e5
34 changed files with 800 additions and 338 deletions

View File

@@ -4,6 +4,7 @@
"description": "Template functions for working with XML data",
"private": true,
"version": "0.1.0",
"main": "src/index.ts",
"scripts": {
"build": "yaakcli build",
"dev": "yaakcli dev",

View File

@@ -2,6 +2,10 @@ import { DOMParser } from '@xmldom/xmldom';
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
import xpath from 'xpath';
const RETURN_FIRST = 'first';
const RETURN_ALL = 'all';
const RETURN_JOIN = 'join';
export const plugin: PluginDefinition = {
templateFunctions: [
{
@@ -15,20 +19,39 @@ export const plugin: PluginDefinition = {
multiLine: true,
placeholder: '<foo></foo>',
},
{
type: 'h_stack',
inputs: [
{
type: 'select',
name: 'result',
label: 'Return Format',
defaultValue: RETURN_FIRST,
options: [
{ label: 'First result', value: RETURN_FIRST },
{ label: 'All results', value: RETURN_ALL },
{ label: 'Join with separator', value: RETURN_JOIN },
],
},
{
name: 'join',
type: 'text',
label: 'Separator',
optional: true,
defaultValue: ', ',
dynamic(_ctx, args) {
return { hidden: args.values.result !== RETURN_JOIN };
},
},
],
},
{ type: 'text', name: 'query', label: 'Query', placeholder: '//foo' },
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const doc: any = new DOMParser().parseFromString(String(args.values.input), 'text/xml');
const 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);
}
const result = (args.values.result || RETURN_FIRST) as XPathResult;
const join = args.values.join == null ? null : String(args.values.join);
return filterXPath(String(args.values.input), String(args.values.query), result, join);
} catch {
return null;
}
@@ -36,3 +59,26 @@ export const plugin: PluginDefinition = {
},
],
};
export type XPathResult = 'first' | 'join' | 'all';
export function filterXPath(
body: string,
path: string,
result: XPathResult,
join: string | null,
): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const doc: any = new DOMParser().parseFromString(body, 'text/xml');
const items = xpath.select(path, doc, false);
if (!Array.isArray(items)) {
return String(items);
} else if (!Array.isArray(items) || result === 'first') {
return items[0] != null ? String(items[0].firstChild ?? '') : '';
} else if (result === 'join') {
return items.map((item) => String(item.firstChild ?? '')).join(join ?? '');
} else {
// Not sure what cases this happens in (?)
return String(items);
}
}