mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 15:34:09 +01:00
Run oxfmt across repo, add format script and docs
Add .oxfmtignore to skip generated bindings and wasm-pack output. Add npm format script, update DEVELOPMENT.md for Vite+ toolchain, and format all non-generated files with oxfmt. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "@yaak/template-function-request",
|
||||
"displayName": "Request Template Functions",
|
||||
"description": "Template functions for extracting value from requests",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Template functions for extracting value from requests",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev"
|
||||
|
||||
@@ -1,64 +1,64 @@
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||
import type { AnyModel, HttpUrlParameter } from '@yaakapp-internal/models';
|
||||
import type { GenericCompletionOption } from '@yaakapp-internal/plugins';
|
||||
import type { JSONPathResult } from '../../template-function-json';
|
||||
import { filterJSONPath } from '../../template-function-json';
|
||||
import type { XPathResult } from '../../template-function-xml';
|
||||
import { filterXPath } from '../../template-function-xml';
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from "@yaakapp/api";
|
||||
import type { AnyModel, HttpUrlParameter } from "@yaakapp-internal/models";
|
||||
import type { GenericCompletionOption } from "@yaakapp-internal/plugins";
|
||||
import type { JSONPathResult } from "../../template-function-json";
|
||||
import { filterJSONPath } from "../../template-function-json";
|
||||
import type { XPathResult } from "../../template-function-xml";
|
||||
import { filterXPath } from "../../template-function-xml";
|
||||
|
||||
const RETURN_FIRST = 'first';
|
||||
const RETURN_ALL = 'all';
|
||||
const RETURN_JOIN = 'join';
|
||||
const RETURN_FIRST = "first";
|
||||
const RETURN_ALL = "all";
|
||||
const RETURN_JOIN = "join";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
templateFunctions: [
|
||||
{
|
||||
name: 'request.body.raw',
|
||||
aliases: ['request.body'],
|
||||
name: "request.body.raw",
|
||||
aliases: ["request.body"],
|
||||
args: [
|
||||
{
|
||||
name: 'requestId',
|
||||
label: 'Http Request',
|
||||
type: 'http_request',
|
||||
name: "requestId",
|
||||
label: "Http Request",
|
||||
type: "http_request",
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
return String(
|
||||
await ctx.templates.render({
|
||||
data: httpRequest.body?.text ?? '',
|
||||
data: httpRequest.body?.text ?? "",
|
||||
purpose: args.purpose,
|
||||
}),
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'request.body.path',
|
||||
previewArgs: ['path'],
|
||||
name: "request.body.path",
|
||||
previewArgs: ["path"],
|
||||
args: [
|
||||
{ name: 'requestId', label: 'Http Request', type: 'http_request' },
|
||||
{ name: "requestId", label: "Http Request", type: "http_request" },
|
||||
{
|
||||
type: 'h_stack',
|
||||
type: "h_stack",
|
||||
inputs: [
|
||||
{
|
||||
type: 'select',
|
||||
name: 'result',
|
||||
label: 'Return Format',
|
||||
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 },
|
||||
{ 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',
|
||||
name: "join",
|
||||
type: "text",
|
||||
label: "Separator",
|
||||
optional: true,
|
||||
defaultValue: ', ',
|
||||
defaultValue: ", ",
|
||||
dynamic(_ctx, args) {
|
||||
return { hidden: args.values.result !== RETURN_JOIN };
|
||||
},
|
||||
@@ -66,52 +66,52 @@ export const plugin: PluginDefinition = {
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
name: 'path',
|
||||
label: 'JSONPath or XPath',
|
||||
placeholder: '$.books[0].id or /books[0]/id',
|
||||
type: "text",
|
||||
name: "path",
|
||||
label: "JSONPath or XPath",
|
||||
placeholder: "$.books[0].id or /books[0]/id",
|
||||
dynamic: async (ctx, args) => {
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
|
||||
const contentType =
|
||||
httpRequest.headers
|
||||
.find((h) => h.name.toLowerCase() === 'content-type')
|
||||
?.value.toLowerCase() ?? '';
|
||||
if (contentType.includes('xml') || contentType?.includes('html')) {
|
||||
.find((h) => h.name.toLowerCase() === "content-type")
|
||||
?.value.toLowerCase() ?? "";
|
||||
if (contentType.includes("xml") || contentType?.includes("html")) {
|
||||
return {
|
||||
label: 'XPath',
|
||||
placeholder: '/books[0]/id',
|
||||
description: 'Enter an XPath expression used to filter the results',
|
||||
label: "XPath",
|
||||
placeholder: "/books[0]/id",
|
||||
description: "Enter an XPath expression used to filter the results",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
label: 'JSONPath',
|
||||
placeholder: '$.books[0].id',
|
||||
description: 'Enter a JSONPath expression used to filter the results',
|
||||
label: "JSONPath",
|
||||
placeholder: "$.books[0].id",
|
||||
description: "Enter a JSONPath expression used to filter the results",
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
|
||||
const body = httpRequest.body?.text ?? '';
|
||||
const body = httpRequest.body?.text ?? "";
|
||||
|
||||
try {
|
||||
const result: JSONPathResult =
|
||||
args.values.result === RETURN_ALL
|
||||
? 'all'
|
||||
? "all"
|
||||
: args.values.result === RETURN_JOIN
|
||||
? 'join'
|
||||
: 'first';
|
||||
? "join"
|
||||
: "first";
|
||||
return filterJSONPath(
|
||||
body,
|
||||
String(args.values.path || ''),
|
||||
String(args.values.path || ""),
|
||||
result,
|
||||
args.values.join == null ? null : String(args.values.join),
|
||||
);
|
||||
@@ -122,13 +122,13 @@ export const plugin: PluginDefinition = {
|
||||
try {
|
||||
const result: XPathResult =
|
||||
args.values.result === RETURN_ALL
|
||||
? 'all'
|
||||
? "all"
|
||||
: args.values.result === RETURN_JOIN
|
||||
? 'join'
|
||||
: 'first';
|
||||
? "join"
|
||||
: "first";
|
||||
return filterXPath(
|
||||
body,
|
||||
String(args.values.path || ''),
|
||||
String(args.values.path || ""),
|
||||
result,
|
||||
args.values.join == null ? null : String(args.values.join),
|
||||
);
|
||||
@@ -140,21 +140,21 @@ export const plugin: PluginDefinition = {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'request.header',
|
||||
description: 'Read the value of a request header, by name',
|
||||
previewArgs: ['header'],
|
||||
name: "request.header",
|
||||
description: "Read the value of a request header, by name",
|
||||
previewArgs: ["header"],
|
||||
args: [
|
||||
{
|
||||
name: 'requestId',
|
||||
label: 'Http Request',
|
||||
type: 'http_request',
|
||||
name: "requestId",
|
||||
label: "Http Request",
|
||||
type: "http_request",
|
||||
},
|
||||
{
|
||||
name: 'header',
|
||||
label: 'Header Name',
|
||||
type: 'text',
|
||||
name: "header",
|
||||
label: "Header Name",
|
||||
type: "text",
|
||||
async dynamic(ctx, args) {
|
||||
if (typeof args.values.requestId !== 'string') return null;
|
||||
if (typeof args.values.requestId !== "string") return null;
|
||||
|
||||
const request = await ctx.httpRequest.getById({ id: args.values.requestId });
|
||||
if (request == null) return null;
|
||||
@@ -164,15 +164,15 @@ export const plugin: PluginDefinition = {
|
||||
placeholder: validHeaders[0]?.name,
|
||||
completionOptions: validHeaders.map<GenericCompletionOption>((h) => ({
|
||||
label: h.name,
|
||||
type: 'constant',
|
||||
type: "constant",
|
||||
})),
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const headerName = String(args.values.header ?? '');
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const headerName = String(args.values.header ?? "");
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
const header = httpRequest.headers.find(
|
||||
@@ -180,29 +180,29 @@ export const plugin: PluginDefinition = {
|
||||
);
|
||||
return String(
|
||||
await ctx.templates.render({
|
||||
data: header?.value ?? '',
|
||||
data: header?.value ?? "",
|
||||
purpose: args.purpose,
|
||||
}),
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'request.param',
|
||||
name: "request.param",
|
||||
args: [
|
||||
{
|
||||
name: 'requestId',
|
||||
label: 'Http Request',
|
||||
type: 'http_request',
|
||||
name: "requestId",
|
||||
label: "Http Request",
|
||||
type: "http_request",
|
||||
},
|
||||
{
|
||||
name: 'param',
|
||||
label: 'Param Name',
|
||||
type: 'text',
|
||||
name: "param",
|
||||
label: "Param Name",
|
||||
type: "text",
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const paramName = String(args.values.param ?? '');
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const paramName = String(args.values.param ?? "");
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
|
||||
@@ -211,7 +211,7 @@ export const plugin: PluginDefinition = {
|
||||
purpose: args.purpose,
|
||||
});
|
||||
|
||||
const querystring = renderedUrl.split('?')[1] ?? '';
|
||||
const querystring = renderedUrl.split("?")[1] ?? "";
|
||||
const paramsFromUrl: HttpUrlParameter[] = new URLSearchParams(querystring)
|
||||
.entries()
|
||||
.map(([name, value]): HttpUrlParameter => ({ name, value }))
|
||||
@@ -222,23 +222,23 @@ export const plugin: PluginDefinition = {
|
||||
const foundParam = allEnabledParams.find((p) => p.name === paramName);
|
||||
|
||||
const renderedValue = await ctx.templates.render({
|
||||
data: foundParam?.value ?? '',
|
||||
data: foundParam?.value ?? "",
|
||||
purpose: args.purpose,
|
||||
});
|
||||
return renderedValue;
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'request.name',
|
||||
name: "request.name",
|
||||
args: [
|
||||
{
|
||||
name: 'requestId',
|
||||
label: 'Http Request',
|
||||
type: 'http_request',
|
||||
name: "requestId",
|
||||
label: "Http Request",
|
||||
type: "http_request",
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const requestId = String(args.values.requestId ?? 'n/a');
|
||||
const requestId = String(args.values.requestId ?? "n/a");
|
||||
const httpRequest = await ctx.httpRequest.getById({ id: requestId });
|
||||
if (httpRequest == null) return null;
|
||||
|
||||
@@ -250,37 +250,37 @@ export const plugin: PluginDefinition = {
|
||||
|
||||
// TODO: Use a common function for this, but it fails to build on windows during CI if I try importing it here
|
||||
export function resolvedModelName(r: AnyModel | null): string {
|
||||
if (r == null) return '';
|
||||
if (r == null) return "";
|
||||
|
||||
if (!('url' in r) || r.model === 'plugin') {
|
||||
return 'name' in r ? r.name : '';
|
||||
if (!("url" in r) || r.model === "plugin") {
|
||||
return "name" in r ? r.name : "";
|
||||
}
|
||||
|
||||
// Return name if it has one
|
||||
if ('name' in r && r.name) {
|
||||
if ("name" in r && r.name) {
|
||||
return r.name;
|
||||
}
|
||||
|
||||
// Replace variable syntax with variable name
|
||||
const withoutVariables = r.url.replace(/\$\{\[\s*([^\]\s]+)\s*]}/g, '$1');
|
||||
if (withoutVariables.trim() === '') {
|
||||
return r.model === 'http_request'
|
||||
? r.bodyType && r.bodyType === 'graphql'
|
||||
? 'GraphQL Request'
|
||||
: 'HTTP Request'
|
||||
: r.model === 'websocket_request'
|
||||
? 'WebSocket Request'
|
||||
: 'gRPC Request';
|
||||
const withoutVariables = r.url.replace(/\$\{\[\s*([^\]\s]+)\s*]}/g, "$1");
|
||||
if (withoutVariables.trim() === "") {
|
||||
return r.model === "http_request"
|
||||
? r.bodyType && r.bodyType === "graphql"
|
||||
? "GraphQL Request"
|
||||
: "HTTP Request"
|
||||
: r.model === "websocket_request"
|
||||
? "WebSocket Request"
|
||||
: "gRPC Request";
|
||||
}
|
||||
|
||||
// GRPC gets nice short names
|
||||
if (r.model === 'grpc_request' && r.service != null && r.method != null) {
|
||||
const shortService = r.service.split('.').pop();
|
||||
if (r.model === "grpc_request" && r.service != null && r.method != null) {
|
||||
const shortService = r.service.split(".").pop();
|
||||
return `${shortService}/${r.method}`;
|
||||
}
|
||||
|
||||
// Strip unnecessary protocol
|
||||
const withoutProto = withoutVariables.replace(/^(http|https|ws|wss):\/\//, '');
|
||||
const withoutProto = withoutVariables.replace(/^(http|https|ws|wss):\/\//, "");
|
||||
|
||||
return withoutProto;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user