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 () => {
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';
const tloFilePath = '...';
const config = BinaryReader.fromFile(tloFilePath).readConfig();
writeTypes('types.ts', config);
Changing generated namespace names
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';
const tloFilePath = '...';
const config = BinaryReader.fromFile(tloFilePath).readConfig();
writeClient('client.ts', config, {
typesSource: './types',
});
Changing source namespace and client names
import {writeTypes, writeClient, BinaryReader} from '@kibcode/types-generator';
const tloFilePath = '...';
const config = BinaryReader.fromFile(tloFilePath).readConfig();
const namespaces = {
root: 'MyRoot',
types: 'MyTypes',
combinators: 'MyCombinators',
};
writeTypes('types.ts', config, {namespaces});
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.