mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-11 22:40:26 +01:00
Official 1Password Template Function (#305)
This commit is contained in:
@@ -25,7 +25,11 @@ import { Context, PluginDefinition } from '@yaakapp/api';
|
||||
import console from 'node:console';
|
||||
import { type Stats, statSync, watch } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { applyDynamicFormInput, applyFormInputDefaults } from './common';
|
||||
import {
|
||||
applyDynamicFormInput,
|
||||
applyFormInputDefaults,
|
||||
validateTemplateFunctionArgs,
|
||||
} from './common';
|
||||
import { EventChannel } from './EventChannel';
|
||||
import { migrateTemplateFunctionSelectOptions } from './migrations';
|
||||
|
||||
@@ -334,15 +338,22 @@ export class PluginInstance {
|
||||
);
|
||||
} else if (typeof fn?.onRender === 'function') {
|
||||
const resolvedArgs = await applyDynamicFormInput(ctx, fn.args, payload.args);
|
||||
payload.args.values = applyFormInputDefaults(resolvedArgs, payload.args.values);
|
||||
try {
|
||||
const result = await fn.onRender(ctx, payload.args);
|
||||
const values = applyFormInputDefaults(resolvedArgs, payload.args.values);
|
||||
const error = validateTemplateFunctionArgs(fn.name, resolvedArgs, values);
|
||||
if (error && payload.args.purpose !== 'preview') {
|
||||
this.#sendPayload(
|
||||
context,
|
||||
{
|
||||
type: 'call_template_function_response',
|
||||
value: result ?? null,
|
||||
},
|
||||
{ type: 'call_template_function_response', value: null, error },
|
||||
replyId,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await fn.onRender(ctx, { ...payload.args, values });
|
||||
this.#sendPayload(
|
||||
context,
|
||||
{ type: 'call_template_function_response', value: result ?? null },
|
||||
replyId,
|
||||
);
|
||||
} catch (err) {
|
||||
|
||||
@@ -48,9 +48,36 @@ export async function applyDynamicFormInput(
|
||||
...(typeof dynamic === 'function' ? await dynamic(ctx, callArgs as any) : undefined),
|
||||
};
|
||||
if ('inputs' in newArg && Array.isArray(newArg.inputs)) {
|
||||
newArg.inputs = await applyDynamicFormInput(ctx, newArg.inputs, callArgs as any);
|
||||
try {
|
||||
newArg.inputs = await applyDynamicFormInput(ctx, newArg.inputs, callArgs as any);
|
||||
} catch (e) {
|
||||
console.error('Failed to apply dynamic form input', e);
|
||||
}
|
||||
}
|
||||
resolvedArgs.push(newArg);
|
||||
}
|
||||
return resolvedArgs;
|
||||
}
|
||||
|
||||
export function validateTemplateFunctionArgs(
|
||||
fnName: string,
|
||||
args: TemplateFunctionArg[],
|
||||
values: CallTemplateFunctionArgs['values'],
|
||||
): string | null {
|
||||
for (const arg of args) {
|
||||
if ('inputs' in arg && arg.inputs) {
|
||||
// Recurse down
|
||||
const err = validateTemplateFunctionArgs(fnName, arg.inputs, values);
|
||||
if (err) return err;
|
||||
}
|
||||
if (!('name' in arg)) continue;
|
||||
if (arg.optional) continue;
|
||||
if (arg.defaultValue != null) continue;
|
||||
if (arg.hidden) continue;
|
||||
if (values[arg.name] != null) continue;
|
||||
|
||||
return `Missing required argument "${arg.label || arg.name}" for template function ${fnName}()`;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user