Why actions and handlers defined separately?

Open
#30 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
typescript

Research direction

The issue mentions createCatalog, actions, handlers, and a proposed execute field, but names no repository files or tests. Start by tracing those API entry points and comparing how action definitions and handlers are used; done should clearly document the rationale for keeping them separate or specify the supported alternative.

Written by the indexing model from the issue text.

Description

This is one of the most interesting projects I've seen! Just want to make sure if I understand the logic behind json-render library (would be great if this repository would have Github Discussions).

My question is, why actions and handlers need to be defined separately?

const catalog = createCatalog({
  components: { /* ... */ },
  actions: {
    submit_form: {
      params: z.object({
        formId: z.string(),
      }),
      description: 'Submit a form',
    },
    export_data: {
      params: z.object({
        format: z.enum(['csv', 'pdf', 'json']),
        filters: z.object({
          dateRange: z.string().optional(),
        }).optional(),
      }),
    },
    navigate: {
      params: z.object({
        url: z.string(),
      }),
    },
  },
});

// ...
  const handlers = {
    submit_form: async (params) => {
      const response = await fetch('/api/submit', {
        method: 'POST',
        body: JSON.stringify({ formId: params.formId }),
      });
      return response.json();
    },
    
    export_data: async (params) => {
      const blob = await generateExport(params.format, params.filters);
      downloadBlob(blob, `export.${params.format}`);
    },
    
    navigate: (params) => {
      window.location.href = params.url;
    },
  };
// ...

Why not to use execute instead within actions object?

const catalog = createCatalog({
  components: { /* ... */ },
  actions: {
    submit_form: {
      params: z.object({
        formId: z.string(),
      }),
      description: 'Submit a form',
      execute: async (params) => {
        const response = await fetch('/api/submit', {
          method: 'POST',
          body: JSON.stringify({ formId: params.formId }),
        });
        return response.json();
      },
    },
Dominant language
TypeScript
Stars
20k
Forks
1.1k
Avg merge
1d 18h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel-labs/json-render

All issues in vercel-labs/json-render

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.