mtcute/packages/tl/README.md

103 lines
3.2 KiB
Markdown
Raw Normal View History

2021-08-05 20:38:24 +03:00
# @mtcute/tl
2021-04-08 12:19:38 +03:00
2023-11-01 14:05:45 +03:00
TL schema and related utils used for mtcute.
2021-04-08 12:19:38 +03:00
2024-08-07 17:20:42 +03:00
Generated from TL layer **185** (last updated on 07.08.2024).
2021-04-08 12:19:38 +03:00
## About
This package contains JSON schema, type declarations, binary (de-)serialization, errors, RSA keys and helper functions.
Package's major version is always TL schema layer number,
so version `42.0.0` means that this version was generated from TL layer 42.
2021-04-08 12:19:38 +03:00
- JSON schema, types, binary (de-)serialization and helper functions are generated directly from `.tl` files that are
2023-11-01 14:05:45 +03:00
automatically fetched from multiple sources and are merged together.
- Errors are generated from
[`errors.csv`](https://github.com/LonamiWebs/Telethon/blob/master/telethon_generator/data/errors.csv)
and official Telegram errors JSON file.
2021-04-08 12:19:38 +03:00
- RSA keys info is generated based on manually extracted PEMs from Telegram for Android source code.
2023-11-01 14:05:45 +03:00
## Exports
2021-04-08 12:19:38 +03:00
2023-11-01 14:05:45 +03:00
### Root
2021-04-08 12:19:38 +03:00
TypeScript typings and type helpers generated from the schema.
By default, all types are immutable (have their fields marked as `readonly`). That is because most of the time you don't
really need to modify the objects, and modifying them will only lead to confusion. However, there are still valid
use-cases for mutable TL objects, so you can use exported
2023-11-01 14:05:45 +03:00
`tl.Mutable` helper type to make a given object type mutable.
2021-04-08 12:19:38 +03:00
2023-11-01 14:05:45 +03:00
`tl` is exported as a namespace to allow better code insights,
as well as to avoid cluttering global namespace and very long import statements.
2021-04-08 12:19:38 +03:00
MTProto schema is available in namespace `mtp`, also exported by this package.
2021-04-08 12:19:38 +03:00
```typescript
2021-08-05 20:38:24 +03:00
import { tl } from '@mtcute/tl'
2021-04-08 12:19:38 +03:00
const obj: tl.RawInputPeerChat = { _: 'inputPeerChat', chatId: 42 }
console.log(tl.isAnyInputPeer(obj)) // true
```
RPC errors are also exposed in this package in `tl.errors` namespace:
```typescript
import { tl } from '@mtcute/tl'
try {
await client.call(...)
} catch (e) {
if (e instanceof tl.errors.ChatInvalidError) {
console.log('invalid chat')
} else throw e
}
```
2023-11-01 14:05:45 +03:00
### `/api-schema.json`
[Documentation](./modules/api_schema.html)
2021-04-08 12:19:38 +03:00
JSON file describing all available TL classes, methods and unions. Can be used to write custom code generators
2021-08-05 20:38:24 +03:00
> This very file is used to generate binary serialization and TypeScript typings for `@mtcute/tl`.
2021-04-08 12:19:38 +03:00
```typescript
2023-11-01 14:05:45 +03:00
import * as tlSchema from '@mtcute/tl/raw-schema.json'
2021-04-08 12:19:38 +03:00
console.log(`Current layer: ${tlSchema.apiLayer}`)
// Current layer: 124
```
2023-11-01 14:05:45 +03:00
### `/binary/reader.js`
2021-04-08 12:19:38 +03:00
Contains mapping used to read TL objects from binary streams.
```typescript
2023-11-01 14:05:45 +03:00
import { __tlReaderMap } from '@mtcute/tl/binary/reader.js'
import { TlBinaryReader } from '@mtcute/tl-runtime'
2021-04-08 12:19:38 +03:00
2023-11-01 14:05:45 +03:00
const reader = TlBinaryReader.manual(new Uint8Array([...]))
console.log(readerMap[0x5bb8e511 /* mt_message */](reader))
2021-04-08 12:19:38 +03:00
// { _: 'mt_message', ... }
```
2023-11-01 14:05:45 +03:00
### `/binary/writer.js`
2021-04-08 12:19:38 +03:00
Contains mapping used to write TL objects to binary streams.
```typescript
2023-11-01 14:05:45 +03:00
import { __tlWriterMap } from '@mtcute/tl/binary/writer'
import { TlBinaryWriter } from '@mtcute/tl-runtime'
2021-04-08 12:19:38 +03:00
2023-11-01 14:05:45 +03:00
const writer = TlBinaryWriter.manual(100)
writerMap[0x5bb8e511 /* mt_message */](writer, { ... })
2021-04-08 12:19:38 +03:00
console.log(writer.result())
2023-11-01 14:05:45 +03:00
// Uint8Array <11 e5 b8 5b ...>
2021-04-08 12:19:38 +03:00
```
2023-11-01 14:05:45 +03:00
### `/binary/rsa-keys.js`
Contains RSA keys used when authorizing with Telegram.
`old` flag also determines if the client should use the old
RSA padding scheme.