29.15.0 • Published 19 days ago

@divkitframework/jsonbuilder v29.15.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
19 days 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 | Чат на русском.

29.13.1

19 days ago

29.15.0

19 days ago

29.14.0

26 days ago

29.13.0

1 month ago

29.12.0

1 month ago

29.11.0

2 months ago

29.10.0

2 months ago

29.9.0

2 months ago

29.8.0

2 months ago

29.7.0

3 months ago

29.6.0

3 months ago

29.5.0

3 months ago

29.4.0

3 months ago

29.3.0

4 months ago

29.2.0

4 months ago

29.1.0

5 months ago

29.0.0

5 months ago

28.13.0

5 months ago

28.1.0

8 months ago

28.5.0

7 months ago

28.3.0

8 months ago

28.10.0

6 months ago

28.9.0

6 months ago

28.12.0

6 months ago

28.5.1

7 months ago

28.7.0

7 months ago

26.1.0

10 months ago

27.1.0

9 months ago

27.3.0

9 months ago

25.7.0

10 months ago

28.0.0

8 months ago

28.4.0

7 months ago

28.2.0

8 months ago

28.8.0

6 months ago

28.6.0

7 months ago

28.11.0

6 months ago

26.2.1

10 months ago

26.2.0

10 months ago

26.0.0

10 months ago

27.0.0

9 months ago

27.2.0

9 months ago

27.4.0

9 months ago

25.6.0

11 months ago

25.5.0

11 months ago

25.4.1

11 months ago

25.2.0

12 months ago

25.1.0

12 months ago

25.4.0

11 months ago

25.3.0

11 months ago

25.0.0

12 months ago

24.3.0

1 year ago

24.2.0

1 year ago

24.1.0

1 year ago

24.0.0

1 year ago

23.4.0

1 year ago

23.3.1

1 year ago

23.6.0

1 year ago

23.5.0

1 year ago

23.7.0

1 year ago

23.3.0

1 year ago

23.0.0

1 year ago

23.2.0

1 year ago

23.1.0

1 year ago

22.0.0

1 year ago

21.1.0

1 year ago

19.0.0

1 year ago

21.0.0

1 year ago

17.0.0

1 year ago

20.0.0

1 year ago

18.0.0

1 year ago

16.0.0

1 year ago

14.0.0

2 years ago

15.0.0

1 year ago

1.5.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.0.0

2 years ago