Official 1Password Template Function (#305)

This commit is contained in:
Gregory Schier
2025-11-22 06:08:13 -08:00
committed by GitHub
parent 43a7132014
commit 2bac610efe
20 changed files with 1440 additions and 142 deletions

View File

@@ -6,7 +6,7 @@ import type {
WebsocketRequest,
Workspace,
} from '@yaakapp-internal/models';
import { httpResponsesAtom } from '@yaakapp-internal/models';
import { environmentsAtom, httpResponsesAtom } from '@yaakapp-internal/models';
import type { GetTemplateFunctionConfigResponse, JsonPrimitive } from '@yaakapp-internal/plugins';
import { useAtomValue } from 'jotai';
import { md5 } from 'js-md5';
@@ -22,6 +22,8 @@ export function useTemplateFunctionConfig(
const workspaceId = useAtomValue(activeWorkspaceIdAtom);
const environmentId = useAtomValue(activeEnvironmentIdAtom);
const responses = useAtomValue(httpResponsesAtom);
const environments = useAtomValue(environmentsAtom);
const environmentsKey = environments.map((e) => e.id + e.updatedAt).join(':');
// Some auth handlers like OAuth 2.0 show the current token after a successful request. To
// handle that, we'll force the auth to re-fetch after each new response closes
@@ -41,6 +43,7 @@ export function useTemplateFunctionConfig(
responseKey,
workspaceId,
environmentId,
environmentsKey,
],
placeholderData: (prev) => prev, // Keep previous data on refetch
queryFn: async () => {

View File

@@ -20,27 +20,18 @@ export function useTemplateFunctionCompletionOptions(
if (!enabled) {
return [];
}
return (
templateFunctions.map((fn) => {
const NUM_ARGS = 2;
const argsWithName = fn.args.filter((a) => 'name' in a);
const shortArgs =
argsWithName
.slice(0, NUM_ARGS)
.map((a) => a.name)
.join(', ') + (fn.args.length > NUM_ARGS ? ', …' : '');
return {
name: fn.name,
aliases: fn.aliases,
type: 'function',
description: fn.description,
args: argsWithName.map((a) => ({ name: a.name })),
value: null,
label: `${fn.name}(${shortArgs})`,
onClick: (rawTag: string, startPos: number) => onClick(fn, rawTag, startPos),
};
}) ?? []
);
return templateFunctions.map((fn) => {
const argsLabel = fn.args.length > 0 ? '…' : '';
const fn2: TwigCompletionOption = {
type: 'function',
onClick: (rawTag: string, startPos: number) => onClick(fn, rawTag, startPos),
label: `${fn.name}(${argsLabel})`,
invalid: false,
value: null,
...fn,
};
return fn2;
});
}, [enabled, onClick, templateFunctions]);
}