Dictionaries

Dictionaries

A list your API serves, like provinces or categories, defined once and used by any choice.

Define one

A dictionary says where a list lives. It is a value: define it once, export it, and import it where a form needs it. There is nothing to register.

import { defineDictionary } from "#form-engine";

export const provinces = defineDictionary({
  path: () => "/dictionaries/provinces",
});

The engine asks for it through your app's http.get, once per visit, however many fields use it.

Use it

Any radio, select or checkboxes takes it as its options:

{ key: "province", type: "select", label: "Province", options: { dictionary: provinces } }

The answer is the picked entry, and the payload gets its code.

The reply

By default a reply is a list, or { items: [...] }, of entries with a code and a name in each language:

[
  { "code": "12", "name_kh": "ភ្នំពេញ", "name_en": "Phnom Penh" },
  { "code": "17", "name_kh": "សៀមរាប", "name_en": "Siem Reap" }
]

When your API answers in another shape, entries turns the reply into entries:

export const categories = defineDictionary({
  path: () => "/categories",
  entries: reply => (reply as { data: { id: string; title: string }[] }).data
    .map(item => ({ code: item.id, name_kh: item.title, name_en: item.title })),
});

Params

params names the values the path needs. A field fills them with fixed params, or with other answers in the same section through from, and waits until they are given.

export const districts = defineDictionary({
  params: ["province"],
  path: ({ province }) => `/dictionaries/provinces/${province}/districts`,
});
{ key: "district", type: "select", label: "District", options: { dictionary: districts, from: { province: "province" } } }

Here from reads "fill the province param from the province field". The district list loads once a province is picked.

Serving it from Nuxt

Your app's http.get sends the dictionary's path to your API. In a Nuxt app, a file in server/api can answer it. Its name is the path: kinds.get.ts answers GET /api/kinds.

app/forms/cafe/dictionaries.ts
import { defineDictionary } from "#form-engine";

export const kinds = defineDictionary({
  path: () => "/api/kinds",
});

export const specialities = defineDictionary({
  params: ["kind"],
  path: ({ kind }) => `/api/kinds/${kind}/specialities`,
});
server/api/kinds.get.ts
export default defineEventHandler(() => [
  { code: "COFFEE", name_kh: "កាហ្វេ", name_en: "Coffee" },
  { code: "BAKERY", name_kh: "នំដុត", name_en: "Bakery" },
  { code: "TEA", name_kh: "តែ", name_en: "Tea" },
]);

A folder named in brackets is a param. kinds/[kind]/specialities.get.ts answers /api/kinds/COFFEE/specialities, and reads the code with getRouterParam:

server/api/kinds/[kind]/specialities.get.ts
export default defineEventHandler((event) => {
  const kind = getRouterParam(event, "kind") ?? "";
  return SPECIALITIES[kind] ?? [];
});

The playground runs server/api files the same way. Change a list there and watch the choices change.

Open in the playgroundserver/api/kinds.get.ts

More from each entry

OptionWhat it does
method"POST" for an API that lists by post. "GET" by default.
choicekeeps more of each entry in the answer, like a group or a fee
optionadds details to show under a picked option, or values to fill other fields
detailfetches one record when it is picked, and shows what it returns
iconan icon for each code, like a flag

A kept property can be tested by a condition or a check; see property in Conditions and Checks.