30.24.0 • Published 8 months ago

@divkitframework/jsonbuilder v30.24.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
8 months ago

DivKit TypeScript JSON Buidler

DivKit Documentation

What is this and what for

@divkitframework/jsonbuilder library provides type safe tools to generate DivKit JSON

Example

import { divCard, DivContainer, DivText, reference, rewritRefs, template, templateHelper } from '@divkitframework/jsonbuilder';

const templates = {
  sampleBlock: new DivContainer({
    items: [
      template('header', {
        text: reference('title')
      }),
      template('header', {
        text: reference('subtitle')
      }),
    ],
  }),
  header: new DivText({
    font_size: 24,
  }),
};

const tHelper = templateHelper(templates);

console.log(JSON.stringify(
  divCard(rewriteRefs(templates), {
    log_id: 'sample_card',
    states: [
      {
        state_id: 0,
        div: tHelper.sampleBlock({
          title: "Some Title",
          subtitle: "Some Subtitle",
        }),
      },
    ],
  }
)));

In the result JSON.stringify(divCard(...)) will return JSON below:

{
  "templates": {
    "sampleBlock": {
      "type": "container",
      "items": [
        {
          "type": "header",
          "$text": "title"
        },
        {
          "type": "header",
          "$text": "subtitle"
        },
      ]
    },
    "header": {
      "type": "text",
      "font_size": 24
    }
  },
  "card": {
    "log_id": "sample_card",
    "states": [
      {
        "state_id": 0,
        "div": {
          "type": "sampleBlock",
          "title": "Some Title",
          "subtitle": "Some Subtitle",
        }
      }
    ]
  }
}

Typesafe templates with compile-time validation

You can use templateHelper helper function to achieve compile time template parameters validation. Type safety works only when you enable strictNullChecks in tsconfig.json

const block = template('header', {
    title: 'Some Title'
});

// using templateHelper for checking template parameters at compile-time
const safeBlock = tHelper.header({
    title: 'Some Title'
});

Validity guarantees

While developing cards you need to make sure that:

  • Textual string are not empty;
  • Urls of images, actions, etc are valid;
  • Arrays are non-empty.

Documentation. Medium tutorial. Habr tutorial.

Telegram: News | English-speaking chat | Чат на русском.

30.24.0

8 months ago

30.23.0

8 months ago

30.16.0

10 months ago

30.12.0

11 months ago

30.7.0

1 year ago

30.3.0

1 year ago

30.19.0

9 months ago

30.11.0

11 months ago

30.15.0

10 months ago

30.6.0

1 year ago

30.2.0

1 year ago

30.22.0

9 months ago

30.18.0

10 months ago

30.10.0

12 months ago

30.14.0

11 months ago

30.9.0

12 months ago

30.5.0

1 year ago

30.21.0

9 months ago

30.17.0

10 months ago

30.13.0

11 months ago

30.8.0

12 months ago

30.4.0

1 year ago

30.20.0

9 months ago

30.1.0

1 year ago

30.0.0

1 year ago

29.13.1

1 year ago

29.15.0

1 year ago

29.14.0

1 year ago

29.13.0

1 year ago

29.12.0

1 year ago

29.11.0

1 year ago

29.10.0

1 year ago

29.9.0

1 year ago

29.8.0

1 year ago

29.7.0

1 year ago

29.6.0

1 year ago

29.5.0

1 year ago

29.4.0

1 year ago

29.3.0

1 year ago

29.2.0

1 year ago

29.1.0

2 years ago

29.0.0

2 years ago

28.13.0

2 years ago

28.1.0

2 years ago

28.5.0

2 years ago

28.3.0

2 years ago

28.10.0

2 years ago

28.9.0

2 years ago

28.12.0

2 years ago

28.5.1

2 years ago

28.7.0

2 years ago

26.1.0

2 years ago

27.1.0

2 years ago

27.3.0

2 years ago

25.7.0

2 years ago

28.0.0

2 years ago

28.4.0

2 years ago

28.2.0

2 years ago

28.8.0

2 years ago

28.6.0

2 years ago

28.11.0

2 years ago

26.2.1

2 years ago

26.2.0

2 years ago

26.0.0

2 years ago

27.0.0

2 years ago

27.2.0

2 years ago

27.4.0

2 years ago

25.6.0

2 years ago

25.5.0

2 years ago

25.4.1

2 years ago

25.2.0

2 years ago

25.1.0

2 years ago

25.4.0

2 years ago

25.3.0

2 years ago

25.0.0

2 years ago

24.3.0

2 years ago

24.2.0

2 years ago

24.1.0

2 years ago

24.0.0

2 years ago

23.4.0

2 years ago

23.3.1

2 years ago

23.6.0

2 years ago

23.5.0

2 years ago

23.7.0

2 years ago

23.3.0

2 years ago

23.0.0

2 years ago

23.2.0

2 years ago

23.1.0

2 years ago

22.0.0

2 years ago

21.1.0

2 years ago

19.0.0

3 years ago

21.0.0

3 years ago

17.0.0

3 years ago

20.0.0

3 years ago

18.0.0

3 years ago

16.0.0

3 years ago

14.0.0

3 years ago

15.0.0

3 years ago

1.5.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.0.0

3 years ago