npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kibcode/types-generator

v1.1.0-alpha.2

Published

This repository contains utilities which help developers to generate TypeScript types based on TON-specific files.

Downloads

1

Readme

TON TypeScript Generator

This repository contains utilities which help developers to generate TypeScript types based on TON-specific files.

Features

  • Types constructors and functions parameters types generation based on .tlo file.
  • Node client generation based on .tlo file.

Installation

yarn

yarn add --dev @kibcode/types-generator

npm

npm i -D @kibcode/types-generator

BinaryReader

BinaryReader is class which derives TypeLanguage file configuration from .tlo binary file. You can find an example of such file here .

Creating binary reader from different types of sources will append specific to this source meta information to config.

Usage

Create from file path

import {BinaryReader} from '@kibcode/types-generator';

const config = BinaryReader.fromFilePath('...').readConfig();

console.log(config);

Create from file URL

import {BinaryReader} from '@kibcode/types-generator';

(async () => {
  // To test fetch functionality, you could try this URL:
  // https://github.com/newton-blockchain/ton/blob/master/tl/generate/scheme/ton_api.tlo?raw=true
  const reader = await BinaryReader.fromFileURL('...');

  console.log(reader.readConfig());
})();

Types generator

To make communication with tonlib easier, this library generates types which are based on passed .tlo file.

As a result, it creates .ts file with namespace, which contains another two. The first one is responsible for carrying type names, the second one contains combinators which are presented as interfaces.

Usage

Common usage

import {writeTypes, BinaryReader} from '@kibcode/types-generator';

// Here we should define path to .tlo file.
const tloFilePath = '...';

// Read config from .tlo file.
const config = BinaryReader.fromFile(tloFilePath).readConfig();

// Write types to file.
writeTypes('types.ts', config);

Changing generated namespace names

// This will generate root namespace with name "MyRoot" and namespaces 
// "MyTypes" and "MyCombinators" for types and combinators respectively.
writeTypes('types.ts', config, {
  namespaces: {
    types: 'MyTypes',
    root: 'MyRoot',
    combinators: 'MyCombinators',
  }
});

Formatting options

To format TypeScript code, ESLint used. You can set your own linter configuration by passing linterConfig option:

writeTypes('types.ts', config, {
  linterConfig: {
    rules: {
      'max-len': ['error', 80],
    }
  }
});

While formatting TypeScript code, don't forget to pass required components which are mentioned here.

Types generator uses configuration mentioned above by default. So, you could reuse it too:

import {defaultLinterConfig} from '@kibcode/types-generator';

writeTypes('types.ts', config, {
  linterConfig: {
    ...defaultLinterConfig,
    rules: {
      ...defaultLinterConfig.rules,
      'max-len': ['error', 80]
    },
  }
});

Client generator

Generated client represents abstract class which required such methods as _request and _send to be implemented. Usually, client generation is connected with types generation. So, it is important to see [Generating types](#Generating types) section first.

Probably, to implement these methods, you could use Node tonlib implementation.

Usage

Common usage

import {writeClient, BinaryReader} from '@kibcode/types-generator';

// Here we should define path to .tlo file.
const tloFilePath = '...';

// Read config from .tlo file.
const config = BinaryReader.fromFile(tloFilePath).readConfig();

writeClient('client.ts', config, {
  // Set import source from which generator should get tonlib root namespace
  // declaration.
  // As a result, generated class file will contain such import:
  // import {Tonlib} from './types';
  typesSource: './types',
});

Changing source namespace and client names

import {writeTypes, writeClient, BinaryReader} from '@kibcode/types-generator';

// Here we should define path to .tlo file.
const tloFilePath = '...';

// Read config from .tlo file.
const config = BinaryReader.fromFile(tloFilePath).readConfig();

const namespaces = {
  root: 'MyRoot',
  types: 'MyTypes',
  combinators: 'MyCombinators',
};

// Write types to file.
writeTypes('types.ts', config, {namespaces});

// This will generate class "MyClass" with functions that will refer to
// types which are placed in MyRoot.MyTypes and MyRoot.MyCombinators 
// namespaces.
//
// Additionally, import declaration will have this form:
// import {MyRoot} from './types';
writeClient('client.ts', config, {
  typesSource: './types',
  names: {
    namespaces,
    class: 'MyClass',
  }
});

Formatting options

Client generator uses the same formatting flow as Types generator does. So, to learn more, follow this section.

Example output

You can find examples of generated output in output folder.