mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-26 03:11:28 +01:00
Merge main into proxy branch (formatting and docs)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -50,7 +50,7 @@ This will generate a random JSON body on every request:
|
||||
The plugin provides access to all FakerJS modules and their methods:
|
||||
|
||||
| Category | Description | Example Methods |
|
||||
|------------|---------------------------|--------------------------------------------|
|
||||
| ---------- | ------------------------- | ------------------------------------------ |
|
||||
| `airline` | Airline-related data | `aircraftType`, `airline`, `airplane` |
|
||||
| `animal` | Animal names and types | `bear`, `bird`, `cat`, `dog`, `fish` |
|
||||
| `color` | Colors in various formats | `human`, `rgb`, `hex`, `hsl` |
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@yaak/faker",
|
||||
"private": true,
|
||||
"version": "1.1.1",
|
||||
"displayName": "Faker",
|
||||
"version": "1.1.1",
|
||||
"private": true,
|
||||
"description": "Template functions for generating fake data using FakerJS",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -12,7 +12,7 @@
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev",
|
||||
"test": "vitest --run tests"
|
||||
"test": "vp test --run tests"
|
||||
},
|
||||
"dependencies": {
|
||||
"@faker-js/faker": "^10.1.0"
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
import { faker } from '@faker-js/faker';
|
||||
import type { DynamicTemplateFunctionArg, PluginDefinition } from '@yaakapp/api';
|
||||
import { faker } from "@faker-js/faker";
|
||||
import type { DynamicTemplateFunctionArg, PluginDefinition } from "@yaakapp/api";
|
||||
|
||||
const modules = [
|
||||
'airline',
|
||||
'animal',
|
||||
'color',
|
||||
'commerce',
|
||||
'company',
|
||||
'database',
|
||||
'date',
|
||||
'finance',
|
||||
'git',
|
||||
'hacker',
|
||||
'image',
|
||||
'internet',
|
||||
'location',
|
||||
'lorem',
|
||||
'person',
|
||||
'music',
|
||||
'number',
|
||||
'phone',
|
||||
'science',
|
||||
'string',
|
||||
'system',
|
||||
'vehicle',
|
||||
'word',
|
||||
"airline",
|
||||
"animal",
|
||||
"color",
|
||||
"commerce",
|
||||
"company",
|
||||
"database",
|
||||
"date",
|
||||
"finance",
|
||||
"git",
|
||||
"hacker",
|
||||
"image",
|
||||
"internet",
|
||||
"location",
|
||||
"lorem",
|
||||
"person",
|
||||
"music",
|
||||
"number",
|
||||
"phone",
|
||||
"science",
|
||||
"string",
|
||||
"system",
|
||||
"vehicle",
|
||||
"word",
|
||||
];
|
||||
|
||||
function normalizeResult(result: unknown): string {
|
||||
if (typeof result === 'string') return result;
|
||||
if (typeof result === "string") return result;
|
||||
if (result instanceof Date) return result.toISOString();
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
@@ -37,20 +37,20 @@ function normalizeResult(result: unknown): string {
|
||||
function args(modName: string, fnName: string): DynamicTemplateFunctionArg[] {
|
||||
return [
|
||||
{
|
||||
type: 'banner',
|
||||
color: 'info',
|
||||
type: "banner",
|
||||
color: "info",
|
||||
inputs: [
|
||||
{
|
||||
type: 'markdown',
|
||||
type: "markdown",
|
||||
content: `Need help? View documentation for [\`${modName}.${fnName}(…)\`](https://fakerjs.dev/api/${encodeURIComponent(modName)}.html#${encodeURIComponent(fnName)})`,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'options',
|
||||
label: 'Arguments',
|
||||
type: 'editor',
|
||||
language: 'json',
|
||||
name: "options",
|
||||
label: "Arguments",
|
||||
type: "editor",
|
||||
language: "json",
|
||||
optional: true,
|
||||
placeholder: 'e.g. { "min": 1, "max": 10 } or 10 or ["en","US"]',
|
||||
},
|
||||
@@ -61,22 +61,22 @@ export const plugin: PluginDefinition = {
|
||||
templateFunctions: modules.flatMap((modName) => {
|
||||
const mod = faker[modName as keyof typeof faker];
|
||||
return Object.keys(mod)
|
||||
.filter((n) => n !== 'faker')
|
||||
.filter((n) => n !== "faker")
|
||||
.map((fnName) => ({
|
||||
name: ['faker', modName, fnName].join('.'),
|
||||
name: ["faker", modName, fnName].join("."),
|
||||
args: args(modName, fnName),
|
||||
async onRender(_ctx, args) {
|
||||
const fn = mod[fnName as keyof typeof mod] as (...a: unknown[]) => unknown;
|
||||
const options = args.values.options;
|
||||
|
||||
// No options supplied
|
||||
if (options == null || options === '') {
|
||||
if (options == null || options === "") {
|
||||
return normalizeResult(fn());
|
||||
}
|
||||
|
||||
// Try JSON first
|
||||
let parsed: unknown = options;
|
||||
if (typeof options === 'string') {
|
||||
if (typeof options === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(options);
|
||||
} catch {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
describe('template-function-faker', () => {
|
||||
it('exports all expected template functions', async () => {
|
||||
const { plugin } = await import('../src/index');
|
||||
describe("template-function-faker", () => {
|
||||
it("exports all expected template functions", async () => {
|
||||
const { plugin } = await import("../src/index");
|
||||
const names = plugin.templateFunctions?.map((fn) => fn.name).sort() ?? [];
|
||||
|
||||
// Snapshot the full list of exported function names so we catch any
|
||||
@@ -10,12 +10,13 @@ describe('template-function-faker', () => {
|
||||
expect(names).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('renders date results as unquoted ISO strings', async () => {
|
||||
const { plugin } = await import('../src/index');
|
||||
const fn = plugin.templateFunctions?.find((fn) => fn.name === 'faker.date.future');
|
||||
it("renders date results as unquoted ISO strings", async () => {
|
||||
const { plugin } = await import("../src/index");
|
||||
const fn = plugin.templateFunctions?.find((fn) => fn.name === "faker.date.future");
|
||||
// oxlint-disable-next-line unbound-method
|
||||
const onRender = fn?.onRender;
|
||||
|
||||
expect(onRender).toBeTypeOf('function');
|
||||
expect(onRender).toBeTypeOf("function");
|
||||
if (onRender == null) {
|
||||
throw new Error("Expected template function 'faker.date.future' to define onRender");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user