@grom.js/bot-api-spec 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,21 +1,93 @@
1
+ [![bot-api](https://img.shields.io/badge/v9.3-000?style=flat&logo=telegram&logoColor=%2325A3E1&label=Bot%20API&labelColor=%23000&color=%2325A3E1)][bot-api]
2
+ [![npm](https://img.shields.io/npm/v/%40grom.js%2Fbot-api-spec?style=flat&logo=npm&logoColor=%23BB443E&logoSize=auto&label=%C2%A0&labelColor=%23fff&color=%23BB443E)](https://www.npmjs.com/package/@grom.js/tgx)
3
+ [![jsr](https://img.shields.io/jsr/v/%40grom/bot-api-spec?style=flat&logo=jsr&logoColor=%231B3646&logoSize=auto&label=%C2%A0&labelColor=%23F3E051&color=%231B3646)](https://jsr.io/@grom/tgx)
4
+
1
5
  [Telegram Bot API](https://core.telegram.org/bots/api) specification as a collection of JavaScript objects in a [custom format](#format).
2
6
 
3
7
  ## Motivation
4
8
 
5
9
  Automatically generate tools, libraries, MCP servers, custom documentations, etc.
6
10
 
11
+ ## Installation
12
+
13
+ ```sh
14
+ # Using npm
15
+ npm install @grom.js/bot-api-spec
16
+
17
+ # Using JSR
18
+ deno add jsr:@grom/bot-api-spec
19
+ ```
20
+
7
21
  ## Usage
8
22
 
23
+ Root module exports two objects:
24
+
25
+ 1. `types` — definition of all Bot API types
26
+ 2. `methods` — definition of all Bot API methods
27
+
9
28
  ```ts
10
- import { types, methods } from '@grom.js/bot-api-spec'
29
+ import { types, methods } from '@grom.js/bot-api-spec' // '@grom/bot-api-spec' for JSR
30
+
31
+ console.log(types)
32
+ // {
33
+ // Update: {
34
+ // name: 'Update',
35
+ // description: { markdown: '...' },
36
+ // fields: [
37
+ // {
38
+ // name: 'update_id',
39
+ // type: { type: 'int32' },
40
+ // description: { markdown: '...' },
41
+ // required: true,
42
+ // },
43
+ // ...
44
+ // ],
45
+ // },
46
+ // ...
47
+ // }
11
48
 
12
- console.log(types) // { Update: <definition of Update>, ... }
13
- console.log(methods) // { getUpdates: <definition of getUpdates>, ... }
49
+ console.log(methods)
50
+ // {
51
+ // getUpdates: {
52
+ // name: 'getUpdates',
53
+ // description: { markdown: '...' },
54
+ // parameters: [
55
+ // {
56
+ // name: 'offset',
57
+ // type: { type: 'int32' },
58
+ // description: { markdown: '...' },
59
+ // required: false,
60
+ // },
61
+ // ...
62
+ // ],
63
+ // returnType: {
64
+ // type: 'array',
65
+ // of: {
66
+ // type: 'api-type',
67
+ // name: 'Update',
68
+ // },
69
+ // },
70
+ // },
71
+ // ...
72
+ // }
14
73
  ```
15
74
 
16
75
  ## Format
17
76
 
18
- See [./src/types.ts](./src/types.ts)
77
+ Refer to the [./src/format.ts](./src/format.ts) module for reference.
78
+
79
+ You can also import types in your code:
80
+
81
+ ```ts
82
+ import type { ValueType } from '@grom.js/bot-api-spec/format' // '@grom/bot-api-spec/format' for JSR
83
+
84
+ function generateCode(valueType: ValueType): string {
85
+ if (valueType.type === 'str') {
86
+ return 'string'
87
+ }
88
+ // ...
89
+ }
90
+ ```
19
91
 
20
92
  ### Value Types
21
93
 
@@ -36,9 +108,13 @@ Below are the rules how we map type of a field/parameter to the `ValueType`:
36
108
 
37
109
  ### Descriptions
38
110
 
39
- Objects also include descriptions of the API types, methods, fields, and parameters, with the following remarks:
111
+ All definitions of types, methods, fields, and parameters include their descriptions.
112
+
113
+ Descriptions are copied verbatim from the official [Bot API documentation][bot-api], with the following modifications:
114
+
115
+ - Description HTML is parsed and converted to Markdown.
116
+ - The "_Optional._" prefix is omitted from field descriptions. Instead, the `required` property is set to `false` for such fields.
117
+ - "JSON-serialized..." portions are omitted from field/parameter descriptions.
118
+ - "...may have more than 32 significant bits...but it has at most 52 significant bits..." portions are omitted from _Integer_ field/parameter descriptions. Instead, the `type` is set to `int53` for such fields/parameters (as per [TDLib](https://core.telegram.org/tdlib/docs/td__api_8h.html#a6f57ab89c6371535f0fb7fec2d770126)).
40
119
 
41
- - Description is an object with a single `markdown` property, a string containing the description in Markdown format with formatting (**bold**, _italic_, etc.) and links preserved.
42
- - "_Optional._" prefix in field descriptions is omitted; instead, the `required` property is set to `false` for such fields.
43
- - "JSON-serialized..." in field/parameter descriptions is omitted; instead, the `jsonSerialized` property is set to `true` for such fields/parameters.
44
- - "...may have more than 32 significant bits...but it has at most 52 significant bits..." in _Integer_ field/parameter descriptions is omitted; instead, `type` is set to `int53` for such fields/parameters (as per [TDLib](https://core.telegram.org/tdlib/docs/td__api_8h.html#a6f57ab89c6371535f0fb7fec2d770126)).
120
+ [bot-api]: https://core.telegram.org/bots/api
@@ -0,0 +1,149 @@
1
+ import type { types } from './types.gen.ts';
2
+ /**
3
+ * Type defined in the API. It can either be an {@link ApiTypeObject object}
4
+ * or a {@link ApiTypeOneOf union}.
5
+ */
6
+ export type ApiType = ApiTypeObject | ApiTypeOneOf;
7
+ export interface ApiTypeObject {
8
+ /**
9
+ * Name of the type.
10
+ */
11
+ name: string;
12
+ /**
13
+ * Description of the type.
14
+ */
15
+ description: Description;
16
+ /**
17
+ * Fields of the object representing this type.
18
+ */
19
+ fields: Array<FieldOrParam>;
20
+ oneOf?: never;
21
+ }
22
+ export interface ApiTypeOneOf {
23
+ /**
24
+ * Name of the type.
25
+ */
26
+ name: string;
27
+ /**
28
+ * Description of the type.
29
+ */
30
+ description: Description;
31
+ /**
32
+ * Array of possible types this type can be.
33
+ */
34
+ oneOf: Array<ValueTypeApiType>;
35
+ fields?: never;
36
+ }
37
+ export interface ApiMethod {
38
+ /**
39
+ * Name of the method.
40
+ */
41
+ name: string;
42
+ /**
43
+ * Description of the method.
44
+ */
45
+ description: Description;
46
+ /**
47
+ * Parameters this method takes.
48
+ */
49
+ parameters: Array<FieldOrParam>;
50
+ /**
51
+ * Type of the value this method returns.
52
+ */
53
+ returnType: ValueType;
54
+ }
55
+ export interface FieldOrParam {
56
+ /**
57
+ * Name of the field/parameter.
58
+ */
59
+ name: string;
60
+ /**
61
+ * Type of the value this field/parameter can be assigned.
62
+ */
63
+ type: ValueType;
64
+ /**
65
+ * Whether this is a required field/parameter.
66
+ */
67
+ required: boolean;
68
+ /**
69
+ * Description of the field/parameter.
70
+ */
71
+ description: Description;
72
+ /**
73
+ * Whether this field/parameter should be JSON-serialized.
74
+ */
75
+ jsonSerialized: boolean;
76
+ }
77
+ /**
78
+ * Description of a type/method/field/parameter.
79
+ */
80
+ export interface Description {
81
+ markdown: string;
82
+ }
83
+ /**
84
+ * Type of a value.
85
+ */
86
+ export type ValueType = ValueTypeString | ValueTypeBoolean | ValueTypeInteger32 | ValueTypeInteger53 | ValueTypeFloat | ValueTypeInputFile | ValueTypeApiType | ValueTypeArray | ValueTypeUnion;
87
+ /**
88
+ * `String` value type.
89
+ */
90
+ export interface ValueTypeString {
91
+ type: 'str';
92
+ literal?: string;
93
+ }
94
+ /**
95
+ * `Boolean` value type.
96
+ */
97
+ export interface ValueTypeBoolean {
98
+ type: 'bool';
99
+ literal?: boolean;
100
+ }
101
+ /**
102
+ * `Integer` value type, which fits in a 32-bit integer.
103
+ */
104
+ export interface ValueTypeInteger32 {
105
+ type: 'int32';
106
+ literal?: number;
107
+ }
108
+ /**
109
+ * `Integer` value type, which may have more than 32 significant bits, but has
110
+ * at most 52 significant bits, so a 64-bit integer or double-precision float
111
+ * type are safe for storing values of this type.
112
+ */
113
+ export interface ValueTypeInteger53 {
114
+ type: 'int53';
115
+ }
116
+ /**
117
+ * `Float` value type.
118
+ */
119
+ export interface ValueTypeFloat {
120
+ type: 'float';
121
+ }
122
+ /**
123
+ * [`InputFile`](https://core.telegram.org/bots/api#inputfile) value type.
124
+ */
125
+ export interface ValueTypeInputFile {
126
+ type: 'input-file';
127
+ }
128
+ /**
129
+ * Any {@link ApiType} value type.
130
+ */
131
+ export interface ValueTypeApiType {
132
+ type: 'api-type';
133
+ name: keyof typeof types;
134
+ }
135
+ /**
136
+ * Array of any value type.
137
+ */
138
+ export interface ValueTypeArray {
139
+ type: 'array';
140
+ of: ValueType;
141
+ }
142
+ /**
143
+ * Union of any value types.
144
+ */
145
+ export interface ValueTypeUnion {
146
+ type: 'union';
147
+ types: Array<ValueType>;
148
+ }
149
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAE3C;;;GAGG;AACH,MAAM,MAAM,OAAO,GACb,aAAa,GACb,YAAY,CAAA;AAElB,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,WAAW,EAAE,WAAW,CAAA;IAExB;;OAEG;IACH,MAAM,EAAE,KAAK,CAAC,YAAY,CAAC,CAAA;IAE3B,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,WAAW,EAAE,WAAW,CAAA;IAExB;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAA;IAE9B,MAAM,CAAC,EAAE,KAAK,CAAA;CACf;AAED,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,WAAW,EAAE,WAAW,CAAA;IAExB;;OAEG;IACH,UAAU,EAAE,KAAK,CAAC,YAAY,CAAC,CAAA;IAE/B;;OAEG;IACH,UAAU,EAAE,SAAS,CAAA;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,IAAI,EAAE,SAAS,CAAA;IAEf;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAA;IAEjB;;OAEG;IACH,WAAW,EAAE,WAAW,CAAA;IAExB;;OAEG;IACH,cAAc,EAAE,OAAO,CAAA;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,SAAS,GACf,eAAe,GACf,gBAAgB,GAChB,kBAAkB,GAClB,kBAAkB,GAClB,cAAc,GACd,kBAAkB,GAClB,gBAAgB,GAChB,cAAc,GACd,cAAc,CAAA;AAEpB;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,KAAK,CAAA;IACX,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,OAAO,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,OAAO,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,OAAO,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,YAAY,CAAA;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,UAAU,CAAA;IAChB,IAAI,EAAE,MAAM,OAAO,KAAK,CAAA;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,OAAO,CAAA;IACb,EAAE,EAAE,SAAS,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,OAAO,CAAA;IACb,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC,CAAA;CACxB"}
package/dist/format.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":""}